【问题标题】:Extracting the POS tags in R using使用 R 提取 POS 标签
【发布时间】:2018-05-14 08:04:03
【问题描述】:

在我的数据集中,我试图为每个观察分别创建包含名词、动词和形容词数量的变量。使用 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

这给了我输出:

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=,

等等。

我的问题是,我如何提取例如每次观察的名词数量,以便我可以将其用于进一步分析。

谢谢!

【问题讨论】:

    标签: r text-mining pos-tagger


    【解决方案1】:

    我不使用openNLP,而是使用不同的包进行 POS 标记。如果有人回答 openNLP 可以帮助您,那就太好了。

    但我会使用udpipe 为您提供解决方案。您可能会发现它很有用。

    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 
    

    编辑:每句话的计数:

    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
    

    当您在注释之后从 x 创建一个 data.frame 时,您可以访问 doc_id、paragraph_id、sentence_id 等。您可以为每个文档/句子等创建一系列统计信息。小插图很好地概述了什么是可能的。

    【讨论】:

    • 谢谢!是否还有一种方法可以为每个单独的句子获取这些计数?例如句子 1 中的 4 个名词。
    猜你喜欢
    • 2013-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-03
    • 1970-01-01
    相关资源
    最近更新 更多