Remove meaningless words from corpus in R

你说的曾经没有我的故事 提交于 2019-12-05 02:38:53

问题


I am using tm and wordcloud for performing some basic text mining in R. The text being processed contains many words which are meaningless like asfdg,aawptkr and i need to filter such words. The closest solution i have found is using library(qdapDictionaries) and building a custom function to check validity of words.

library(qdapDictionaries)
is.word  <- function(x) x %in% GradyAugmented

# example
> is.word("aapg")
[1] FALSE

The rest of text mining used is :

curDir <- "E:/folder1/"  # folder1 contains a.txt, b.txt
myCorpus <- VCorpus(DirSource(curDir))
myCorpus <- tm_map(myCorpus, removePunctuation)
myCorpus <- tm_map(myCorpus, removeNumbers)

myCorpus <- tm_map(myCorpus,foo) # foo clears meaningless words from corpus

The issue is is.word() works fine for handling dataframes but how to use it for corpus handling ?

Thanks


回答1:


Not sure if it will be the most resource efficient method (I don't know the package very well) but it should work:

tdm <- TermDocumentMatrix(myCorpus )
all_tokens       <- findFreqTerms(tdm, 1)
tokens_to_remove <- setdiff(all_tokens,GradyAugmented)
corpus <- tm_map(corpus, content_transformer(removeWords), 
                 tokens_to_remove)



回答2:


If you are willing to try a different text mining package, then this will work:

library(readtext)
library(quanteda)
myCorpus <- corpus(readtext("E:/folder1/*.txt"))

# tokenize the corpus
myTokens <- tokens(myCorpus, remove_punct = TRUE, remove_numbers = TRUE)
# keep only the tokens found in an English dictionary
myTokens <- tokens_select(myTokens, names(data_int_syllables))

From there you can form at document-term matrix (called a "dfm" in quanteda) for analysis, and it will only contain the features found as English-language terms as matched in the dictionary (which contains about 130,000 words).



来源:https://stackoverflow.com/questions/44307896/remove-meaningless-words-from-corpus-in-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!