python - NLTK正则表达式,具体语法怎么写?

标签 python regex nltk chunking

我正在使用 Python 和 RegexpParser,我想编写这样的语法:

<JJ><NN><anything>
<RB><JJ><not NN nor NNT>
  • 第一个意思是:第一个单词应该是 JJ,第二个单词是 NN,第三个单词应该是任何内容
  • 第二个意思是:RB 后跟 JJ,第三个单词不能是 NN 或 NNT

我很难用正则表达式表达(也不是......)

...

最佳答案

例如下面的例子,语法是:

grammar = """ P: {<NN><VBD><JJ><CC><JJ>}
                    {<NN><VBD><JJ>} 
             """

分块器:

PChunker = RegexpParser(grammar)

这句话是:

sentence = ['The', 'pizza', 'was', 'good', 'but', 'pasta', 'was', 'bad']

运行以下代码后:

print("sentence : ",PChunker.parse(pos_tag(sentence)))

结果将是:

sentence :  (S
              The/DT
              (P pizza/NN was/VBD good/JJ)
              but/CC
              (P pasta/NN was/VBD bad/JJ))

就我而言,我想编写如下所示的语法: enter image description here

关于python - NLTK正则表达式,具体语法怎么写?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55655114/

相关文章:

python - 生成 python 多处理池时意外的内存占用差异

regex - pyspark 不支持正则表达式

javascript - 在值中插入特殊字符

php - 使用正则表达式从 po 文件获取翻译计数和模糊计数...

python - 无法导入normalize_corpus python 3

python - 使用 Oauth 从非 App Engine 服务器访问 App Engine 拉取队列

python - 仅生成具有特定数字的随机数

python - 使用 NLTK 在不完整的句子中查找主题

使用 NLTK 的 Python 在 sent_tokenize 和 word_tokenize 处显示错误

python - Python 2.7 中的 Google Cloud 客户端库