gpt4 book ai didi

python - 我如何制作 NLTK pos_tag 单词而不是字符?

转载 作者:太空宇宙 更新时间:2023-11-03 12:48:58 29 4
gpt4 key购买 nike

我有这段代码可以在句子中查找名词和动词。

 # -*- coding: utf-8 -*-
from nltk.corpus import wordnet as wn
from nltk import pos_tag
import nltk
syno =[]


sentence = '''His father suggested he study to become a parson instead, but Darwin was far more inclined to study natural history.DarwinDar·win (där'wĭn),Charles Robert.1809-1882.British naturalist who revolutionized the study of biology with his theory ofevolutionbased on natural selection
Like several scientists before him, Darwin believed all the life on earth evolved (developed gradually) over millions of years from a few common ancestors.'''

sent = pos_tag(word_tokenize(sentence))

返回

[('H', 'NNP'), ('e', 'VBP'), ('l', 'NN'), ('l', 'NN'), ('o', 'NN'), (' ', ':'), ('m', 'NN'), ('y', 'NN'), (' ', ':'), ('n', 'NN'), ('a', 'DT'), ('m', 'NN'), ('e', 'NN'), (' ', ':'), ('i', 'PRP'), ('s', 'VBZ'), (' ', ':'), ('A', 'DT'), ('b', 'NN'), ('h', 'NN'), ('i', 'PRP'), ('s', 'VBZ'), ('h', 'JJ'), ('e', 'NN'), ('k', 'NN'), (' ', ':'), ('M', 'NNP'), ('i', 'PRP'), ('t', 'VBP'), ('r', 'JJ'), ('a', 'DT')]

不过我希望它对单词进行操作,而不是对字符进行操作!我该怎么做?

最佳答案

您需要先标记化:

>>> from nltk import pos_tag, word_tokenize
>>> sentence = "Hello my name is Derek. I live in Salt Lake city."
>>> pos_tag(word_tokenize(sentence))
[('Hello', 'NNP'), ('my', 'PRP$'), ('name', 'NN'), ('is', 'VBZ'), ('Derek.', 'NNP'), ('I', 'PRP'), ('live', 'VBP'), ('in', 'IN'), ('Salt', 'NNP'), ('Lake', 'NNP'), ('city', 'NN'), ('.', '.')]

关于python - 我如何制作 NLTK pos_tag 单词而不是字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18249538/

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