【问题标题】:Search a string by a mix of syntactical and regex patterns通过混合语法和正则表达式模式搜索字符串
【发布时间】:2015-03-30 08:17:33
【问题描述】:

我想使用 R 在文本中搜索通过 POS 和实际字符串混合表达的模式。 (我在这里的 python 库中看到了这个功能:http://www.clips.ua.ac.be/pages/pattern-search)。

例如,搜索模式可以是:'NOUNPHRASE be|is|was ADJECTIVE than NOUNPHRASE',并且应该返回包含以下结构的所有字符串:“猫比狗快”。

我知道像 openNLPqdap 这样的软件包提供方便的 POS 标记。有没有人将它的输出用于这种模式加工?

【问题讨论】:

    标签: regex r nlp tm part-of-speech


    【解决方案1】:

    作为初学者,使用koRpusTreeTagger

    library(koRpus) 
    library(tm)
    mytxt <- c("This is my house.", "A house is better than no house.", "A cat is faster than a dog.")
    pattern <- "Noun, singular or mass.*?Adjective, comparative.*?Noun, singular or mass"
    
    tagged.results <- treetag(file = mytxt, treetagger="C:/TreeTagger/bin/tag-english.bat", lang="en", format="obj", stopwords=stopwords("en")) 
    tagged.results <- kRp.filter.wclass(tagged.results, "stopword")
    taggedText(tagged.results)$id <- factor(head(cumsum(c(0, taggedText(tagged.results)$desc == "Sentence ending punctuation")) + 1, -1))
    
    setNames(mytxt, grepl(pattern, aggregate(desc~id, taggedText(tagged.results), FUN = paste0)$desc))
    #               FALSE                               TRUE                               TRUE 
    # "This is my house." "A house is better than no house."      "A cat is faster than a dog."
    

    【讨论】:

    • 谢谢,太好了。有没有办法混合 POS 标签和实际单词?例如,如果我只想将猫作为第一个名词:“cat*?形容词,比较。*?名词,单数或质量”?
    • 好吧,我想这也是可能的。一种策略是在应用正则表达式之前连接单词和单词类。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-28
    • 2012-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多