从tm对象移动到koRpus对象,反之亦然

时间:2017-02-16 10:07:19

标签: r text-mining tm korpus

我在从tm对象移动到koRpus对象时遇到问题。 我必须使用tm工具对语料库进行标准化,使用koRpus将结果变为lemmatize并返回到tm以对结果进行分类。 为了做到这一点,我必须将tm对象转换为R数据帧,然后我将其转换为excel文件,然后转换为txt文件,最后转换为koRpus对象。 这是代码:

#from VCORPUS to DATAFRAME 
dataframeD610P<-data.frame(text=unlist(sapply(Corpus.TotPOS, `[`, "content")), stringsAsFactors=F)

#from DATAFRAME to XLSX 
#library(xlsx)
write.xlsx(dataframeD610P$text, ".\\mycorpus.xlsx")

#open with excel 
#save in csv (UTF-8)

#import in KORPUS and lemmatization with KORPUS/TREETAGGER 

tagged.results <- treetag(".\\mycorpus.csv", treetagger="manual", lang="it", sentc.end = c(".", "!", "?", ";", ":"),
                          TT.options=list(path="C:/TreeTagger", preset="it-utf8", no.unknown=T)) 

然后我需要全部倒退才能回到tm。 这是代码:

#from KORPUS to TXT 
write.table(tagged.results@TT.res$lemma, ".\\mycorpusLEMMATIZED.txt")

#open with a text editor and formatting of the text

#from TXT to R
Lemma1.POS<- readLines(".\\mycorpusLEMMATIZEDfrasi.txt", encoding = "UTF-8")

#from R object to DATAFRAME
Lemma2.POS<-as.data.frame(Lemma1.POS, encoding = "UTF-8")

#from DATAFRAME to CORPUS
CorpusPOSlemmaFINAL = Corpus(VectorSource(Lemma2.POS$Lemma1.POS))

在不离开R的情况下,是否有更优雅的解决方案? 我真的很感激任何帮助或反馈。

BTW,有没有人知道如何询问VCorpus中哪个文件包含特定令牌? 我通常将语料库转换为数据帧以识别文档。有没有办法在tm中做到这一点?

1 个答案:

答案 0 :(得分:0)

感谢unDocUMeantIt,可以在这里找到一些答案https://github.com/unDocUMeantIt/koRpus/issues/6