2015-05-25 20 views
8

Tôi hoàn toàn mới trong gói R và tm, vì vậy hãy giải thích câu hỏi ngu ngốc của tôi ;-) Làm cách nào để hiển thị văn bản của một văn bản thuần trong gói R tm?Cách hiển thị văn bản văn bản trong gói R tm?

Tôi đã tải một corpus với 323 tập tin văn bản đơn giản trong một corpus:

src <- DirSource("Korpora/technologie") 
corpus <- Corpus(src) 

Nhưng khi tôi gọi là corpus với:

corpus[[1]] 

tôi luôn luôn nhận được một số kết quả như thế này thay vì bản thân văn bản chính:

<<PlainTextDocument>> 
Metadata: 7 
Content: chars: 144 
Content: chars: 141 
Content: chars: 224 
Content: chars: 75 
Content: chars: 105 

Làm cách nào để hiển thị văn bản của kho văn bản?

Cảm ơn!

CẬP NHẬT mẫu tái sản xuất: Tôi đã thử nó với built-in văn bản mẫu:

> data("crude") 
> crude 
<<VCorpus>> 
Metadata: corpus specific: 0, document level (indexed): 0 
Content: documents: 20 
> crude[1] 
<<VCorpus>> 
Metadata: corpus specific: 0, document level (indexed): 0 
Content: documents: 1 
> crude[[1]] 
<<PlainTextDocument>> 
Metadata: 15 
Content: chars: 527 

Làm thế nào tôi có thể in nội dung của văn bản?

UPDATE 2: phiên Info:

> sessionInfo() 
R version 3.1.3 (2015-03-09) 
Platform: x86_64-w64-mingw32/x64 (64-bit) 
Running under: Windows 7 x64 (build 7601) Service Pack 1 

locale: 
[1] LC_COLLATE=German_Germany.1252 LC_CTYPE=German_Germany.1252 
[3] LC_MONETARY=German_Germany.1252 LC_NUMERIC=C     
[5] LC_TIME=German_Germany.1252  

attached base packages: 
[1] stats  graphics grDevices utils  datasets methods base  

other attached packages: 
[1] tm_0.6-1 NLP_0.1-7 

loaded via a namespace (and not attached): 
[1] parallel_3.1.3 slam_0.1-32 tools_3.1.3 
+0

Chào mừng bạn đến với SO. Vui lòng cung cấp ví dụ có thể tái sản xuất tối thiểu: http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – lukeA

Trả lời

9

Bạn có thể thử chuyển đổi văn bản corpus của bạn thành một dataframe, và truy cập vào văn bản yêu cầu từ các dataframe riêng của mình. Tôi đã sử dụng dữ liệu mẫu được xây dựng sẵn "thô" (từ gói tm) làm ví dụ.

data("crude") 
dataframe<-data.frame(text=unlist(sapply(crude, `[`, "content")), stringsAsFactors=F) 

dataframe[1,] 
[1] "Diamond Shamrock Corp said that\neffective today it had cut its contract prices for crude oil by\n1.50 dlrs a barrel.\n The reduction brings its posted price for West Texas\nIntermediate to 16.00 dlrs a barrel, the copany said.\n \"The price reduction today was made in the light of falling\noil product prices and a weak crude oil market,\" a company\nspokeswoman said.\n Diamond is the latest in a line of U.S. oil companies that\nhave cut its contract, or posted, prices over the last two days\nciting weak oil markets.\n Reuter" 
+0

OK, công trình này, cảm ơn. Có lẽ sẽ có một bugfix cho tm trong tương lai;) – Azrael

0
> inspect(crude[1]) 
<<VCorpus (documents: 1, metadata (corpus/indexed): 0/0)>> 

$`reut-00001.xml` 
<<PlainTextDocument (metadata: 15)>> 
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 
+0

Xin lỗi, không hoạt động:
> kiểm tra (thô [1]) <> Metadata: corpus cụ thể: 0, mức tài liệu (lập chỉ mục): 0 Nội dung: văn bản: 1' $ 'reut-00001.xml' << >> PlainTextDocument Metadata: 15 Nội dung: chars: 527 > – Azrael

+1

Điều đó thật thú vị, nó hoạt động tốt trên mỏ của tôi. Bạn có thể thử 'thô [1] $ nội dung' không? – Ricky

+0

Giống nhau. Tôi sử dụng RStudio, có lẽ đó là vấn đề hoặc tôi đã bỏ lỡ một số thiết lập trong RStudio?
CẬP NHẬT: Tương tự trong R console – Azrael

2

Tôi có thể xác nhận rằng tm 0.6-1 kiểm tra không in đẹp. Bạn có thể ghép nó với qdap gói mà tôi duy trì để chuyển đổi một cách dễ dàng để một data.frame như folows:

library(qdap) 
as.data.frame(crude) 

Để làm cho nó nhiều hơn ike cũ kiểm tra hành vi của bạn có thể sử dụng:

as.data.frame(crude) %>% 
    with(., invisible(sapply(text, function(x) {strWrap(x); cat("\n\n")}))) 

này trông giống như:

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 


OPEC may be forced to meet before a scheduled June session to 
readdress its production cutting agreement if the organization wants 
to halt the current slide in oil prices, oil industry analysts said. 
"The movement to higher oil prices was never to be as easy as OPEC 
thought. They may need an emergency meeting to sort out the 
problems," said Daniel Yergin, director of Cambridge Energy Research 
Associates, CERA. Analysts and oil industry sources said the problem 
OPEC faces is excess oil supply in world oil markets. "OPEC's problem 
is not a price problem but a production issue and must be addressed 
in that way," said Paul Mlotok, oil analyst with Salomon Brothers 
Inc. He said the market's earlier optimism about OPE 
. 
. 
. 
32

này hoạt động trong tôi, để in các văn bản nội dung, với phiên bản mới nhất của tm,

corpus[[1]]$content 

Lưu ý: Nhiều hơn hoặc ít hơn theo đề xuất của Ricky trong nhận xét trước. Xin lỗi, tôi muốn viết bình luận, chỉ có đại diện của tôi mới có 25 (cần tối thiểu 50 đại diện để bình luận).

+0

Tác phẩm này. Có ai biết tại sao điều này cần phải được thêm vào? Các dấu ngoặc đơn được sử dụng để làm việc một mình mà không cần thêm nội dung $ –

5

Đây là một cách đơn giản và trực tiếp để hiển thị nội dung của một corpus:

strwrap(corpus[[1]]) 

Đối với các dữ liệu thô này sẽ ra

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

Từ Vignette tm, công trình này:

writeLines(as.character(doc.corpus[[8]]))

Nơi '8' là bất kỳ số nguyên tố nào bạn h

1

Chúng tôi có thể nhận được content của mọi mục trong kho.

data("crude") 
out <- sapply(crude, function(x){x$content}) 
out 

# optionally export 
writeCorpus(out, "outputdir/", filenames = "corpus.txt") 
Các vấn đề liên quan