【问题标题】:How to perform Lemmatization in R?如何在 R 中执行词形还原?
【发布时间】:2015-01-29 11:55:16
【问题描述】:

这个问题可能与 Lemmatizer in R or python (am, are, is -> be?) 重复,但我再次添加它,因为前一个问题已关闭,说它太宽泛,唯一的答案效率不高(因为它为此访问一个外部网站,这太慢了,因为我有非常大的语料库无法找到它的引理)。所以这个问题的一部分会和上面提到的问题类似。

根据维基百科,词形还原定义为:

语言学中的词形还原(或词形还原)是将单词的不同变形形式组合在一起的过程,以便可以将它们作为单个项目进行分析。

在 R 中对词形还原的简单 Google 搜索指向 R 的包 wordnet。当我尝试此包时,期望字符向量 c("run", "ran", "running") 输入到词形还原函数在c("run", "run", "run"),我看到这个包只通过各种过滤器名称和字典提供类似于grepl功能的功能。

wordnet 包中的示例代码,其中最多包含 5 个以“car”开头的单词,正如过滤器名称所解释的那样:

filter <- getTermFilter("StartsWithFilter", "car", TRUE)
terms <- getIndexTerms("NOUN", 5, filter)
sapply(terms, getLemma)

以上内容不是我正在寻找的词形还原。我正在寻找的是,使用R 我想找到单词的真正根源:(例如,从c("run", "ran", "running")c("run", "run", "run"))。

【问题讨论】:

  • 抱歉,但我认为这是“寻找包或工具”——不是试图解决特定的编程问题。也许您可以在计算语言学/文本挖掘论坛上提问?
  • 我认为这个问题与接近的典型包/工具搜索问题略有不同。它询问如何在 R 中执行词形还原,这是一个编程问题。 @StrikeR:我建议您更改最后一行“Is there ...”以避免这个问题被关闭。
  • @ChthonicProject 感谢您的建议。进行了相应的更改。
  • 这不是编程问题。编程部分有一个简单的答案 - 查找/创建字典并执行查找。
  • @eddi 我不同意您关于这不是编程问题的评论。在您的评论中,您假设只有一种使用字典查找的词形还原形式,但还有其他基于规则的形式。所以,我猜编程部分并不像你想象的那么简单。我对任何可以执行词形还原的答案都很好,特别是在 R 中,无论是基于字典还是基于规则。但唯一的限制是,处理一个巨大的文本语料库不应该很慢。

标签: r nlp lemmatization


【解决方案1】:

您好,您可以尝试使用koRpus 包,它允许使用Treetagger

tagged.results <- treetag(c("run", "ran", "running"), treetagger="manual", format="obj",
                      TT.tknz=FALSE , lang="en",
                      TT.options=list(path="./TreeTagger", preset="en"))
tagged.results@TT.res

##     token tag lemma lttr wclass                               desc stop stem
## 1     run  NN   run    3   noun             Noun, singular or mass   NA   NA
## 2     ran VVD   run    3   verb                   Verb, past tense   NA   NA
## 3 running VVG   run    7   verb Verb, gerund or present participle   NA   NA

查看lemma 列,了解您要求的结果。

【讨论】:

  • 谢谢维克多。这个答案对我有帮助。但我仍在努力。想再等 2 天寻找其他解决方案,如果没有更好的答案,请接受。
  • 没问题,我明白了,使用外部软件可能会很棘手。
【解决方案2】:

如前一篇文章所述,R 包 textstem 中的函数 lemmatize_words() 可以执行此操作,并为您提供我所理解的您想要的结果:

library(textstem)
vector <- c("run", "ran", "running")
lemmatize_words(vector)

## [1] "run" "run" "run"

【讨论】:

    【解决方案3】:

    @Andy 和 @Arunkumar 说 textstem 库可用于执行词干提取和/或词形还原是正确的。但是,lemmatize_words() 仅适用于单词向量。但是在语料库中,我们没有词向量;我们有字符串,每个字符串都是文档的内容。因此,要对语料库执行词形还原,您可以使用函数 lemmatize_strings() 作为 tm 包的 tm_map() 的参数。

    > corpus[[1]]
    [1] " earnest roughshod document serves workable primer regions recent history make 
    terrific th-grade learning tool samuel beckett applied iranian voting process bard 
    black comedy willie loved another trumpet blast may new mexican cinema -bornin "
    > corpus <- tm_map(corpus, lemmatize_strings)
    > corpus[[1]]
    [1] "earnest roughshod document serve workable primer region recent history make 
    terrific th - grade learn tool samuel beckett apply iranian vote process bard black 
    comedy willie love another trumpet blast may new mexican cinema - bornin"
    

    完成词形还原后不要忘记运行以下代码行:

    > corpus <- tm_map(corpus, PlainTextDocument)
    

    这是因为为了创建一个文档术语矩阵,你需要有 'PlainTextDocument' 类型的对象,在你使用 lemmatize_strings() 之后会发生变化(更具体地说,语料库对象不包含内容和元- 不再是每个文档的数据 - 它现在只是一个包含文档内容的结构;这不是 DocumentTermMatrix() 用作参数的对象类型。

    希望这会有所帮助!

    【讨论】:

      【解决方案4】:

      也许stemming 对你来说就足够了吗?典型的自然语言处理任务使用词干文本。你可以从 NLP 的 CRAN 任务视图中找到几个包:http://cran.r-project.org/web/views/NaturalLanguageProcessing.html

      如果您确实需要更复杂的东西,那么可以使用基于将句子映射到神经网络的专门解决方案。据我所知,这些都需要大量的训练数据。 Stanford NLP Group创建并提供了许多开放软件。

      如果你真的想深入研究这个话题,那么你可以浏览链接在同一个斯坦福 NLP 小组publications 部分的事件档案。还有一些关于这个主题的书籍。

      【讨论】:

      • Stemming 是我目前在我的语料库中使用的,但我真正在寻找的是词形还原,我想比较使用时结果将如何改善(持怀疑态度)词形还原代替词干。不过感谢您提供的信息。
      【解决方案5】:

      我认为这里的答案有点过时了。您现在应该使用 R 包 udpipe - 可在 https://CRAN.R-project.org/package=udpipe 获得 - 请参阅 https://github.com/bnosac/udpipehttps://bnosac.github.io/udpipe/en 的文档

      请注意以下示例中在进行词形还原和进行词干提取时单词meeting (NOUN) 和单词meet (VERB) 之间的区别,以及在进行词干提取时令人讨厌的单词“someone”与“someon”的混淆.

      library(udpipe)
      x <- c(doc_a = "In our last meeting, someone said that we are meeting again tomorrow",
             doc_b = "It's better to be good at being the best")
      anno <- udpipe(x, "english")
      anno[, c("doc_id", "sentence_id", "token", "lemma", "upos")]
      #>    doc_id sentence_id    token    lemma  upos
      #> 1   doc_a           1       In       in   ADP
      #> 2   doc_a           1      our       we  PRON
      #> 3   doc_a           1     last     last   ADJ
      #> 4   doc_a           1  meeting  meeting  NOUN
      #> 5   doc_a           1        ,        , PUNCT
      #> 6   doc_a           1  someone  someone  PRON
      #> 7   doc_a           1     said      say  VERB
      #> 8   doc_a           1     that     that SCONJ
      #> 9   doc_a           1       we       we  PRON
      #> 10  doc_a           1      are       be   AUX
      #> 11  doc_a           1  meeting     meet  VERB
      #> 12  doc_a           1    again    again   ADV
      #> 13  doc_a           1 tomorrow tomorrow  NOUN
      #> 14  doc_b           1       It       it  PRON
      #> 15  doc_b           1       's       be   AUX
      #> 16  doc_b           1   better   better   ADJ
      #> 17  doc_b           1       to       to  PART
      #> 18  doc_b           1       be       be   AUX
      #> 19  doc_b           1     good     good   ADJ
      #> 20  doc_b           1       at       at SCONJ
      #> 21  doc_b           1    being       be   AUX
      #> 22  doc_b           1      the      the   DET
      #> 23  doc_b           1     best     best   ADJ
      lemmatisation <- paste.data.frame(anno, term = "lemma", 
                                        group = c("doc_id", "sentence_id"))
      lemmatisation
      #>   doc_id sentence_id
      #> 1  doc_a           1
      #> 2  doc_b           1
      #>                                                             lemma
      #> 1 in we last meeting , someone say that we be meet again tomorrow
      #> 2                          it be better to be good at be the best
      
      library(SnowballC)
      tokens   <- strsplit(x, split = "[[:space:][:punct:]]+")
      stemming <- lapply(tokens, FUN = function(x) wordStem(x, language = "en"))
      stemming
      #> $doc_a
      #>  [1] "In"       "our"      "last"     "meet"     "someon"   "said"    
      #>  [7] "that"     "we"       "are"      "meet"     "again"    "tomorrow"
      #> 
      #> $doc_b
      #>  [1] "It"     "s"      "better" "to"     "be"     "good"   "at"     "be"    
      #>  [9] "the"    "best"
      

      【讨论】:

        【解决方案6】:

        可以使用 textStem 包在 R 中轻松完成词形还原。
        步骤是:
        1) 安装 textstem
        2)通过加载包 library(textstem)
        3) stem_word=lemmatize_words(word, dictionary = lexicon::hash_lemmas)
        其中 stem_word 是词形还原的结果,而 word 是输入词。

        【讨论】:

        • 这不是一个很好的代码示例。它甚至没有正确格式化。如何将其应用于 VCorpus 对象?
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-10-20
        • 1970-01-01
        • 1970-01-01
        • 2017-08-07
        • 2018-01-05
        • 1970-01-01
        相关资源
        最近更新 更多