微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

python – 匹配POS标签和单词序列

我有以下两个带有POS标签的字符串:

发送1:“像作家专业或短语如何工作的东西真的很酷.”

[(‘something’, ‘NN’), (‘like’, ‘IN’), (‘how’, ‘WRB’), (‘writer’,
‘NN’), (‘pro’, ‘NN’), (‘or’, ‘CC’), (‘phraSEOlogy’, ‘NN’), (‘works’,
‘NNS’), (‘would’, ‘MD’), (‘be’, ‘VB’), (‘really’, ‘RB’), (‘cool’,
‘JJ’), (‘.’, ‘.’)]

Sent2:“更多选项,如语法编辑器会很好”

[(‘more’, ‘JJR’), (‘options’, ‘NNS’), (‘like’, ‘IN’), (‘the’, ‘DT’),
(‘Syntax’, ‘NN’), (‘editor’, ‘NN’), (‘would’, ‘MD’), (‘be’, ‘VB’),
(‘nice’, ‘JJ’)]

我正在寻找一种方法来检测(返回True),如果有序列:“will”是“形容词(无论形容词的位置,只要它在”will“”be“之后)在这些字符串中.形容词的第二个字符串,“nice”紧跟在“将”之后,但在第一个字符串中不是这种情况.

在我之前的一个问题中解决了这个简单的案例(在形容词之前没有其他词语;“会很好”):detecting POS tag pattern along with specified words

我现在正在寻找一个更通用的解决方案,其中可选词可能出现在形容词之前.我是NLTK和Python的新手.

解决方法:

首先按照说明安装nltk_cli:https://github.com/alvations/nltk_cli

然后,这是nltk_cli中的一个秘密函数,也许你会发现它很有用:

alvas@ubi:~/git/nltk_cli$cat infile.txt 
something like how writer pro or phraSEOlogy works would be really cool .
more options like the Syntax editor would be nice
alvas@ubi:~/git/nltk_cli$python senna.py --chunk2 VP+ADJP infile.txt 
would be    really cool
would be    nice

为了说明其他可能的用法

alvas@ubi:~/git/nltk_cli$python senna.py --chunk2 VP+VP infile.txt 
!!! NO CHUNK of VP+VP in this sentence !!!
!!! NO CHUNK of VP+VP in this sentence !!!
alvas@ubi:~/git/nltk_cli$python senna.py --chunk2 NP+VP infile.txt 
how writer pro or phraSEOlogy works would be
the Syntax editor   would be
alvas@ubi:~/git/nltk_cli$python senna.py --chunk2 VP+NP infile.txt 
!!! NO CHUNK of VP+NP in this sentence !!!
!!! NO CHUNK of VP+NP in this sentence !!!

然后,如果你想检查句子中的短语并输出True / False,只需读取并遍历nltk_cli的输出并检查if-else条件.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐