web-dev-qa-db-ja.com

Rのユーザー定義のコーパスからストップワードを削除する

一連のドキュメントがあります。

documents = c("She had toast for breakfast",
 "The coffee this morning was excellent", 
 "For lunch let's all have pancakes", 
 "Later in the day, there will be more talks", 
 "The talks on the first day were great", 
 "The second day should have good presentations too")

この一連のドキュメントでは、ストップワードを削除したいと思います。私はすでに句読点を削除し、小文字に変換しました:

documents = tolower(documents) #make it lower case
documents = gsub('[[:punct:]]', '', documents) #remove punctuation

まず、Corpusオブジェクトに変換します。

documents <- Corpus(VectorSource(documents))

次に、ストップワードを削除してみます。

documents = tm_map(documents, removeWords, stopwords('english')) #remove stopwords

しかし、この最後の行により、次のエラーが発生します。

THE_PROCESS_HAS_FORKED_AND_YOU_CANNOT_USE_THIS_COREFOUNDATION_FUNCTIONALITY ___ YOU_MUST_EXEC()をデバッグします。

これはすでに尋ねられました here が答えはありませんでした。このエラーはどういう意味ですか?

[〜#〜]編集[〜#〜]

はい、tmパッケージを使用しています。

以下は、sessionInfo()の出力です。

Rバージョン3.0.2(2013-09-25)プラットフォーム:x86_64-Apple-darwin10.8.0(64ビット)

4
StatsSorceress

tmの問題が発生すると、元のテキストを編集するだけになってしまうことがよくあります。

単語を削除するのは少し厄介ですが、tmのストップワードリストから正規表現を一緒に貼り付けることができます。

stopwords_regex = paste(stopwords('en'), collapse = '\\b|\\b')
stopwords_regex = paste0('\\b', stopwords_regex, '\\b')
documents = stringr::str_replace_all(documents, stopwords_regex, '')

> documents
[1] "     toast  breakfast"             " coffee  morning  excellent"      
[3] " lunch lets   pancakes"            "later   day  will   talks"        
[5] " talks   first day  great"         " second day   good presentations "
10
Mhairi McNeill

quantedaパッケージを使用してストップワードを削除できますが、最初にワードがトークンであることを確認してから、以下を使用します。

library(quanteda)
x<- tokens_select(x,stopwords(), selection=)
0
Aakash

たぶんtm_map関数を使用してドキュメントを変換します。私の場合はうまくいくようです。

> documents = c("She had toast for breakfast",
+  "The coffee this morning was excellent", 
+  "For lunch let's all have pancakes", 
+  "Later in the day, there will be more talks", 
+  "The talks on the first day were great", 
+  "The second day should have good presentations too")
> library(tm)
Loading required package: NLP
> documents <- Corpus(VectorSource(documents))
> documents = tm_map(documents, content_transformer(tolower))
> documents = tm_map(documents, removePunctuation)
> documents = tm_map(documents, removeWords, stopwords("english"))
> documents
<<VCorpus>>
Metadata:  corpus specific: 0, document level (indexed): 0
Content:  documents: 6

これは

> documents[[1]]$content
[1] "  toast  breakfast"
> documents[[2]]$content
[1] " coffee  morning  excellent"
> documents[[3]]$content
[1] " lunch lets   pancakes"
> documents[[4]]$content
[1] "later   day  will   talks"
> documents[[5]]$content
[1] " talks   first day  great"
> documents[[6]]$content
[1] " second day   good presentations "
0
Ely