gpt4 book ai didi

regex - 通过混合使用语法和正则表达式模式搜索字符串

转载 作者:行者123 更新时间:2023-12-02 01:39:26 24 4
gpt4 key购买 nike

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

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

我知道像 openNLPqdap 这样的包提供了方便的词性标注。有没有人使用它的输出来进行这种模式处理?

最佳答案

作为初学者,使用 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."

关于regex - 通过混合使用语法和正则表达式模式搜索字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29341376/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com