我有这个闭包,它的目的是在传输和扩充元数据时将一个解析PlainTextDocument
为几个 child :PlainTextDocument
segment_doc <- function(doc) {
txt = paste0(doc, collapse=' ')
au <- meta(doc, tag='Author');
desc <- meta(doc, tag='description');
ori <- meta(doc, tag='origin');
locmeta <- attr(doc,'LocalMetaData');
function(df){
dfrows <- nrow(df);
v<-rep(NA,dfrows);
for(i in 1:dfrows) {
a <- df[i,'after'];
b <- df[i, 'before'];
m <- df[i, 'meta'];
sec <-PlainTextDocument(mkmeta(b, a, txt), author= au, description=desc, origin=ori, heading = m, localmetadata= locmeta)
#verified using debug that sec is a 'PlainTextDocument' with the expected text and metadata
v[i]=sec;
}
v #should be a vector of PlainTextDocuments, BUT it is vector of character vectors. WHY??
}
}
我可以使用如下:
# mycorpus is a Corpus object containing PlainTextDocuments
# sections is a data.frame with 3 columns of type character named 'before', 'after' and 'meta' and 6 rows
sectioner <- segment_doc(mycorpus[[1]]);
ptv <- sectioner(sections); #expect a vector of 6 PlainTextDocuments
class(ptv);
[1] "character"
length(ptv);
[1] 6
问题
- 为什么放在向量中时会
sec
从PlainTextDocument
对象转换为字符向量? - 我怎样才能
sectioner
返回一个Corpus
对象?(PlainTextDocument
s 的向量也可以)。
我已经阅读了关于tm
. 是的,全部。这不应该那么难。我应该使用其他方法吗?