如何将语料库转换为包含元数据的R中的数据框?我已经尝试了convert corpus into data.frame in R的建议,但结果数据框只包含语料库中所有文档的文本行。
我还需要文档ID,也可能是两列中文本行的行号。
那么,我该如何扩展此命令:dataframe <- data.frame(text=unlist(sapply(mycorpus,
[, "content")), stringsAsFactors=FALSE)
以获取数据?
我已经尝试了
dataframe <-
data.frame(id=sapply(corpus, meta(corpus, "id")),
text=unlist(sapply(corpus, `[`, "content")),
stringsAsFactors=F)
但它没有帮助;我只收到一条错误消息&#34; match.fun(FUN)出错: meta(语料库,&#34; id&#34;)&#39; ist nicht Funktion,Zeichen oder Symbol&#34;
语料库是从纯文本文件中提取的;这是一个例子:
> str(corpus)
[...]
$ 1178531510 :List of 2
..$ content: chr [1:67] " uberrasch sagt [...] gemacht echt schad verursacht" ...
..$ meta :List of 7
.. ..$ author : chr(0)
.. ..$ datetimestamp: POSIXlt[1:1], format: "2015-08-16 14:44:11"
.. ..$ description : chr(0)
.. ..$ heading : chr(0)
.. ..$ id : chr "1178531510" # <--- This is the ID i want in the data.frame
.. ..$ language : chr "de"
.. ..$ origin : chr(0)
.. ..- attr(*, "class")= chr "TextDocumentMeta"
..- attr(*, "class")= chr [1:2] "PlainTextDocument" "TextDocument"
[...]
非常感谢提前:)
答案 0 :(得分:0)
有两个问题:你不应该在sapply
中重复论证语料库,而且多段文本转向长度为&gt;的字符向量。 1,你应该在不列出之前粘贴在一起。
dataframe <-
data.frame(id=sapply(corpus, meta, "id"),
text=unlist(lapply(sapply(corpus, '[', "content"),paste,collapse="\n")),
stringsAsFactors=FALSE)