2013-08-26 142 views
0

Similar to this SO member,我一直在R中尋找一個簡單的包來過濾掉非英語單詞。例如,我可能有一個單詞列表,看起來像這樣:使用`textcat`過濾掉語料庫中的非英語單詞

Flexivel 
eficaz 
gut-wrenching 
satisfatorio 
apropiado 
Benutzerfreundlich 
interessante 
genial 
cool 
marketing 
clients 
internet 

我的最終目標是簡單地過濾掉從語料庫非英語的話讓我的列表只是:

gut-wrenching 
cool 
marketing 
clients 
internet 

我已將數據作爲data.frame讀入,但隨後會將其轉換爲語料庫,然後是TermDocumentMatrix,以便使用wordcloudtm創建wordcloud。

我正在使用packages textcat按語言篩選。 The documentation is a bit above my head,但似乎表明您可以在列表上運行命令textcat。例如,如果上面的數據是在一個稱爲data.frame與df稱爲「字」一列,我會運行命令:

library(textcat) 
textcat(c(df$word)) 

然而,這具有的讀取的整個列表的效果單詞作爲單個文檔,而不是查看每一行並確定其語言。請幫忙!

+1

我投票結束,因爲這個問題是要求包建議。也許可以調整一下問題,你將如何在R中做到這一點,並顯示你做了一些腿部的工作。同時消除獎金問題。 –

+0

定義「英文單詞」。 (提示:你不能。) –

+0

@TylerRinker - 感謝您的輸入。我發現'cldr'包使用Chrome來檢測語言,但似乎應用概率判斷來產生關於「前三種可能的語言」的猜測。這比我需要的更復雜一點,所以我一直在尋找一種更簡單的基於字典的方法。我會繼續探索,並且一旦找到選項就讓問題更具體。 (PS,我愛'qdap' :)) – roody

回答

0

對於字典搜索,你可以使用aspell

txt <- c("Flexivel", "eficaz", "gut-wrenching", "satisfatorio", "apropiado", 
    "Benutzerfreundlich", "interessante", "genial", "cool", "marketing", 
    "clients", "internet") 

fn <- tempfile() 
writeLines(txt, fn) 
result <- aspell(fn) 

results$Original給出了不匹配的話。從這些可以選擇匹配的話:

> result$Original 
[1] "Flexivel"   "eficaz"    "satisfatorio"  
[4] "apropiado"   "interessante"  "Benutzerfreundlich" 
> english <- txt[!(txt %in% result$Original)] 
> english 
[1] "gut-wrenching" "genial"  "cool"   "marketing"  
[5] "clients"  "internet" 

然而,正如卡爾Witthoft表示您不能肯定這些實際上是英文單詞。例如,'cool','marketing'和'internet'也是有效的荷蘭語單詞。