2017-05-17 5 views
0

私は、テキストData_clean $ Reviewからnグラム(ここでは1グラムと2グラム)を抽出するためにQuanteda Rパッケージを使用しますが、Rを使ってChi-squareをコンパイルする方法を探していますドキュメントと抽出されたngram:nグラムとドキュメントとの間のカイ二乗値を計算します。

ここでは、テキストを整理して(再表示)、nグラムを生成するために行ったRコードです。

どうぞよろしくお願いします。

あなたはこのためngramsを使用するのではなく、textstat_collocations()呼び出される関数ではないでしょうあなたに

#delete rows with empty value columns 
Data_clean <- Data[Data$Note!="" & Data$Review!="",] 


Data_clean$id <- seq.int(nrow(Data_clean)) 

train.index <- 1:50000 
test.index <- 50001:nrow(Data_clean) 


#clean up 
# remove grammar/punctuation 
Data_clean$Review.clean <- tolower(gsub('[[:punct:]0-9]', ' ', Data_clean$Review)) 


train <- Data_clean[train.index, ] 
test <- Data_clean[test.index, ] 

temp.tf <- Data_clean$Raison.Reco.clean %>% tokens(ngrams = 1:2) %>% # generate tokens 
     dfm # generate dfm 

答えて

1

に感謝します。

これらのオブジェクトは説明も提供もしていないので、正確な例に従うのは少し難しいですが、quantedaのビルトインデータで試してみましょう。私は最初のコーパスからテキストを入手し、上記のものと同様のフィルタを適用します。だから、カイ^ 2用のバイグラムを獲得するために

、あなたが使用します。

# create the corpus, subset on some conditions (could be Note != "" for instance) 
corp_example <- data_corpus_inaugural 
corp_example <- corpus_subset(corp_example, Year > 1960) 

# this will remove punctuation and numbers 
toks_example <- tokens(corp_example, remove_punct = TRUE, remove_numbers = TRUE) 

# find and score chi^2 bigrams 
coll2 <- textstat_collocations(toks_example, method = "chi2", max_size = 2) 
head(coll2, 10) 
#    collocation count  X2 
# 1  reverend clergy  2 28614.00 
# 2  Majority Leader  2 28614.00 
# 3  Information Age  2 28614.00 
# 4  Founding Fathers  3 28614.00 
# 5 distinguished guests  3 28614.00 
# 6  Social Security  3 28614.00 
# 7   Chief Justice  9 23409.82 
# 8   middle class  4 22890.40 
# 9  Abraham Lincoln  2 19075.33 
# 10  society's ills  2 19075.33 

追加

# needs to be a list of the collocations as separate character elements 
coll2a <- sapply(coll2$collocation, strsplit, " ", USE.NAMES = FALSE) 

# compound the tokens using top 100 collocations 
toks_example_comp <- tokens_compound(toks_example, coll2a[1:100]) 
toks_example_comp[[1]][1:20] 
# [1] "Vice_President" "Johnson"   "Mr_Speaker"  "Mr_Chief"  "Chief_Justice" 
# [6] "President"  "Eisenhower"  "Vice_President" "Nixon"   "President"  
# [11] "Truman"   "reverend_clergy" "fellow_citizens" "we"    "observe"   
# [16] "today"   "not"    "a"    "victory"   "of"    
+0

はどうもありがとうございましたが、私はあなたが提案するこの方法で考えて、それがdfmを作成するのは簡単ではないでしょう。ここで、フィーチャーは連鎖のリストであり、文書は 'Data_Clean $ review'です。 –

+1

これは簡単なことです...私はこれで今作業中です。 –

+0

ありがとうございました:) –

関連する問題