想要为语料库中的文档编写标签。标签存储在语料库之外的具有特定唯一文档 ID 的数据框中。
挑战:(1)从数据帧中获取每个 ID,(2)在语料库中找到对应的文档,(3)将数据帧中的标签设置为具有特定 ID 的语料库文档。
library("tm")
someID <- paste(letters[1:15], 16:30, sep="")
someTag <- sample(c("a","x","g","h","e"), 15, replace=TRUE)
data(crude) # a corpus with 20 docs
meta(crude, type="local", tag="someID") <- someID # adding some additional IDs to the corpus
mydf <- data.frame(cbind(someTag, someID)) # Creating a dataframe with similar IDs
mydf <- mydf[sample(nrow(mydf)),] # permutation of elements (rows)
rownames(mydf) <- 1:15 # overwriting the rownames
############################################
# doesn't work - my try - pseudocode
for (i in 1:nrow(mydf)){
meta(crude[which(crude$someID==mydf$someID[i])], tag="someTag", type="local") <- mydf$someTag[i]
}
############################################
# How the data looks like:
mydf
# R output:
> mydf
someTag someID
1 h l27
2 x g22
3 h d19
4 a e20
5 h i24
6 x j25
7 h o30
8 x n29
9 e h23
10 x m28
11 h k26
12 e c18
13 a a16
14 e b17
15 x f21
meta(crude[1], type="local")
# R output:
> meta(crude[1], type="local")
Available meta data pairs are:
Author :
DateTimeStamp: 1987-02-26 17:00:56
Description :
Heading : DIAMOND SHAMROCK (DIA) CUTS CRUDE PRICES
ID : 127
Language : en
Origin : Reuters-21578 XML
User-defined local meta data pairs are:
$TOPICS
[1] "YES"
$LEWISSPLIT
[1] "TRAIN"
$CGISPLIT
[1] "TRAINING-SET"
$OLDID
[1] "5670"
$Topics
[1] "crude"
$Places
[1] "usa"
$People
character(0)
$Orgs
character(0)
$Exchanges
character(0)
$someID
[1] "a16"
感谢您的任何帮助 (;