더북(TheBook)

코퍼스와 문서

tm에서 문서의 집합은 Corpus로, 각 문서는 TextDocument로 표현된다. 이들 문서는 다음 표에 보인 함수들로 내용을 살펴볼 수 있다.

표 10-12 코퍼스와 문서 내용

tm::summary : 코퍼스의 요약 정보를 보여준다.

tm::summary(
  corpus  # 정보를 살펴볼 corpus
)

tm::inspect : 문서 정보를 보여준다.

tm::inspect(
  x  # 코퍼스 또는 단어-문서(term-document) 행렬
)

로이터Reuter 뉴스 기사 중 원유와 관련된 기사 20개가 저장된 crude 데이터를 살펴보자.

> install.packages("tm")
> library(tm)
> data(crude)
> summary(crude)
    Length Class Mode
127 2       PlainTextDocument list
144 2       PlainTextDocument list
191 2       PlainTextDocument list
194 2       PlainTextDocument list
211 2       PlainTextDocument list
...

문서의 본문은 inspect( ) 함수로 볼 수 있다. inspect(crude)를 호출하면 모든 문서에 대한 내용을 보여주며, 특정 문서를 지정해서 보려면 crude[start:end] 형태로 범위를 지정하거나 crude[index] 형태로 색인을 지정한다. 다음은 crude의 첫 번째 문서를 살펴보는 예다.

> inspect(crude[1])
A corpus with 1 text document

The metadata consists of 2 tag-value pairs and a data frame
Available tags are:
  create_date creator
Available variables in the data frame are:
  MetaID

$`reut-00001.xml`
Diamond Shamrock Corp said that
effective today it had cut its contract prices for crude oil by
1.50 dlrs a barrel.
    The reduction brings its posted price for West Texas
Intermediate to 16.00 dlrs a barrel, the copany said.
    "The price reduction today was made in the light of falling
oil product prices and a weak crude oil market," a company
spokeswoman said.
    Diamond is the latest in a line of U.S. oil companies that
have cut its contract, or posted, prices over the last two days
citing weak oil markets.
 Reuter