2016-04-18 12 views
1

Rを使用してテキストマイニングを行う場合、テキストデータを再処理した後、さらに調査するために文書用語マトリックスを作成する必要があります。しかし、中国語に似て、英語には「意味論的距離」、「機械学習」などのいくつかの段階がありますが、それらを単語に分割すると全く異なる意味があります。単語(term)。Rテキストマイニング文書を語句ではなくフレーズに分割する方法

+0

はあなたが事前に定義されたフレーズに分割するようにしたいかを、またはすべての長さの隣接する組み合わせ(すべてのバイグラムなど) –

+0

私は定義済みの辞書である定義済みのフレーズに文書を分割したいと思っています。定義された辞書には、 "意味距離"、 "機械学習"などが含まれています。 –

+0

quantumaパッケージ内の辞書関数がリストです。意味的距離を 'semantic distance'に 'semantic_distance'に変更する必要があります' –

答えて

0

あなたはおそらく英語で参照しているマルチワード表現になり、統計collocatesとしてマルチワード表現を検出することができquantedaパッケージを、使用してRでこれを行うことができます。ストップワードを含むコロケーションを削除するには、最初にテキストをトークン化し、ストップワードを削除して結果内の偽の隣接関係(実際には隣接していない2つのワード)。この「配合」トークンセットを使用して

require(quanteda) 

pres_tokens <- 
    tokens(data_corpus_inaugural) %>% 
    tokens_remove("\\p{P}", padding = TRUE, valuetype = "regex") %>% 
    tokens_remove(stopwords("english"), padding = TRUE) 

pres_collocations <- textstat_collocations(pres_tokens, size = 2) 

head(pres_collocations) 
#   collocation count count_nested length lambda  z 
# 1  united states 157   0  2 7.893307 41.19459 
# 2    let us 97   0  2 6.291128 36.15520 
# 3 fellow citizens 78   0  2 7.963336 32.93813 
# 4 american people 40   0  2 4.426552 23.45052 
# 5   years ago 26   0  2 7.896626 23.26935 
# 6 federal government 32   0  2 5.312702 21.80328 

# convert the corpus collocations into single tokens, for top 1,500 collocations 
pres_compounded_tokens <- tokens_compound(pres_tokens, pres_collocations[1:1500]) 

tokens_select(pres_compounded_tokens[2], "*_*") 
# tokens from 1 document. 
# 1793-Washington : 
# [1] "called_upon" "shall_endeavor" "high_sense"  "official_act" 

、我々は今の機能が当初の条件(これらのコロケーションでは見られない)とコロケーションの混合物からなる文書-機能行列にこれを変えることができます。以下に見られるように、「統一された」は単独で、またコロケーション「united_states」の一部として発生します。

pres_dfm <- dfm(pres_compounded_tokens) 
head(pres_dfm[1:5, grep("united|states", featnames(pres_dfm))]) 
# Document-feature matrix of: 5 documents, 10 features (86% sparse). 
# 5 x 10 sparse Matrix of class "dfm" 
#     features 
# docs    united states statesmen statesmanship reunited unitedly devastates statesman confederated_states united_action 
# 1789-Washington  4  2   0    0  0  0   0   0     0    0 
# 1793-Washington  1  0   0    0  0  0   0   0     0    0 
# 1797-Adams   3  9   0    0  0  0   0   0     0    0 
# 1801-Jefferson  0  0   0    0  0  0   0   0     0    0 
# 1805-Jefferson  1  4   0    0  0  0   0   0     0    0 

あなたはより多くのブルートフォースアプローチをしたい場合、それは単にドキュメント・バイ・バイグラム行列をこのように作成することが可能です:

# just form all bigrams 
head(dfm(data_inaugural_corpus, ngrams = 2)) 
## Document-feature matrix of: 57 documents, 63,866 features. 
## (showing first 6 documents and first 6 features) 
##     features 
## docs    fellow-citizens_of of_the the_senate senate_and and_of the_house 
## 1789-Washington     1  20   1   1  2   2 
## 1797-Adams      0  29   0   0  2   0 
## 1793-Washington     0  4   0   0  1   0 
## 1801-Jefferson     0  28   0   0  3   0 
## 1805-Jefferson     0  17   0   0  1   0 
## 1809-Madison      0  20   0   0  2   0 
+0

'dfm'関数はフィーチャを辞書オブジェクトのフレーズで定義された等価クラスに変換できませんでした。 –

+0

現在、 'dfm(x、dictionary = yourDictionary) 'によっても呼び出される' applyDictionary() 'のような辞書関数は、値が空白で区切られたトークンで構成される辞書では機能しません。 –

関連する問題