简体   繁体   English

使用以下命令提取R中的POS标签

[英]Extracting the POS tags in R using

In my dataset I am trying to create variables containing the number of nouns, verbs and adjectives, respectively for each observation. 在我的数据集中,我试图为每个观察分别创建包含名词,动词和形容词数量的变量。 Using the openNLP package I have managed to get this far: 使用openNLP包,我设法做到了这一点:

s <- paste(c("Pierre Vinken, 61 years old, will join the board as a ",
             "nonexecutive director Nov. 29.\n",
             "Mr. Vinken is chairman of Elsevier N.V., ",
             "the Dutch publishing group."),
           collapse = "")
s <- as.String(s)
s

sent_token_annotator <- Maxent_Sent_Token_Annotator()
word_token_annotator <- Maxent_Word_Token_Annotator()
a2 <- annotate(s, list(sent_token_annotator, word_token_annotator))
pos_tag_annotator <- Maxent_POS_Tag_Annotator()
pos_tag_annotator
a3 <- annotate(s, pos_tag_annotator, a2)
a3
a3w <- subset(a3, type == "word")
a3w

This gives me the output: 这给了我输出:

id type     start end features
1 sentence     1  84 constituents=<<integer,18>>
2 sentence    86 153 constituents=<<integer,13>>
3 word         1   6 POS=NNP
4 word         8  13 POS=NNP
5 word        14  14 POS=,

And so on. 等等。

My question is, how do I extract for example the number of nouns per observation so I can use this for further analysis. 我的问题是,例如,如何提取每个观测的名词数量,以便可以将其用于进一步分析。

Thanks! 谢谢!

I don't use openNLP , but use different packages for POS tagging. 我不使用openNLP ,而是使用不同的软件包进行POS标记。 If someone has an answer for openNLP that can help you that would be great. 如果有人对openNLP有答案,可以为您提供帮助,那就太好了。

But I will give you a solution using udpipe . 但是我会给你一个使用udpipe的解决方案。 You might find it useful. 您可能会发现它很有用。

s <- paste(c("Pierre Vinken, 61 years old, will join the board as a ",
             "nonexecutive director Nov. 29.\n",
             "Mr. Vinken is chairman of Elsevier N.V., ",
             "the Dutch publishing group."),
           collapse = "")

library(udpipe)

if (file.exists("english-ud-2.0-170801.udpipe")) 
  ud_model <- udpipe_load_model(file = "english-ud-2.0-170801.udpipe") else {
    ud_model <- udpipe_download_model(language = "english")
    ud_model <- udpipe_load_model(ud_model$file_model)
}

x <- udpipe_annotate(ud_model, s)
x <- as.data.frame(x)
table(x$upos)

  ADJ   ADP   AUX   DET  NOUN   NUM PROPN PUNCT  VERB 
    2     2     2     3     6     2     8     5     1 

edit: counts per sentence: 编辑:每句话计数:

table(x$sentence_id, x$upos)
    ADJ ADP AUX DET NOUN NUM PROPN PUNCT VERB
  1   2   1   1   2    3   2     3     3    1
  2   0   1   1   1    3   0     5     2    0

When you create a data.frame from x after the annotations, you have access to doc_id, paragraph_id, sentence_id, etc etc. You can create a whole range of statistics per document / sentence etc. The vignettes give a good overview of what is possible. 当您在注释后从x创建data.frame时,您可以访问doc_id,parad_id,s句子_id等。您可以为每个文档/句子等创建整个统计范围。小插图概述了可能出现的情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM