我使用corenlp包对R中的一个用例使用依赖关系解析。但是,我需要针对特定的用例调整数据帧。
我需要一个数据帧,其中我有三列。我使用了下面的代码来到达依赖关系树。
devtools::install_github("statsmaths/coreNLP")
coreNLP::downloadCoreNLP()
initCoreNLP()
inp_cl = "generate odd numbers from column one and print."
output = annotateString(inp_cl)
dc = getDependency(output)
sentence governor dependent type governorIdx dependentIdx govIndex depIndex
1 1 ROOT generate root 0 1 NA 1
2 1 numbers odd amod 3 2 3 2
3 1 generate numbers dobj 1 3 1 3
4 1 column from case 5 4 5 4
5 1 generate column nmod:from 1 5 1 5
6 1 column one nummod 5 6 5 6
7 1 column and cc 5 7 5 7
8 1 generate print nmod:from 1 8 1 8
9 1 column print conj:and 5 8 5 8
10 1 generate . punct 1 7 1 10使用带有以下代码的POS标记,我最终得到了以下数据帧。
ps = getToken(output)
ps = ps[,c(1,2,7,3)]
colnames(dc)[8] = "id"
dp = merge(dc, ps[,c("sentence","id","POS")],
by.x=c("sentence","governorIdx"),by.y = c("sentence","id"),all.x = T)
dp = merge(dp, ps[,c("sentence","id","POS")],
by.x=c("sentence","dependentIdx"),by.y = c("sentence","id"),all.x = T)
colnames(dp)[9:10] = c("POS_gov","POS_dep")
sentence dependentIdx governorIdx governor dependent type govIndex id POS_gov POS_dep
1 1 1 0 ROOT generate root NA 1 <NA> VB
2 1 2 3 numbers odd amod 3 2 NNS JJ
3 1 3 1 generate numbers dobj 1 3 VB NNS
4 1 4 5 column from case 5 4 NN IN
5 1 5 1 generate column nmod:from 1 5 VB NN
6 1 6 5 column one nummod 5 6 NN CD
7 1 7 5 column and cc 5 7 NN CC
8 1 8 1 generate print nmod:from 1 8 VB NN
9 1 8 5 column print conj:and 5 8 NN NN
10 1 9 1 generate . punct 1 9 VB .如果动词(动作词)附加到非动词(非动作词),但非动词(非动作词)连接到其他非动词(非动作词),则一行应指示整个连接。generate是与numbers相关的动词,numbers是与odd相关的非动词。
因此,预期的数据帧需要
Topic1 Topic2 Action
numbers odd generate
column from generate
column one generate
column and generate
column from print
column one print
column and print
. generate发布于 2018-02-16 13:45:52
首先,您需要将依赖关系树标记打印为动词,而不是名词。
试着用一个有两个独立从句的句子,看看第二个独立从句的词根是否有这样的标记。
如果是这样的话,您只需简单地浏览一下治理署的专栏。如果没有,您将需要解决依赖关系树生成器的机制。
https://stackoverflow.com/questions/48807333
复制相似问题