在nltk neu chun中显示像PERSON和GPE这样的实体

2024-09-28 23:33:34 发布

您现在位置:Python中文网/ 问答频道 /正文

def test():
    sample = "hello my name is Shefali and I live in Nebraska."
    print sample
    sentences = nltk.sent_tokenize(sample)
    tokenized_sentences = [nltk.word_tokenize(sentence) for sentence in sentences]
    tagged_sentences = [nltk.pos_tag(sentence) for sentence in tokenized_sentences]
    chunked_sentences = nltk.ne_chunk_sents(tagged_sentences)

    print(list(chunked_sentences))  

输出为:

hello my name is Shefali and I live in Nebraska.
[Tree('S', [('hello', 'NN'), ('my', 'PRP$'), ('name', 'NN'), ('is', 'VBZ'), Tree('PERSON', [('Shefali', 'NNP')]), ('and', 'CC'), ('I', 'PRP'), ('live', 'VBP'), ('in', 'IN'), Tree('GPE', [('Nebraska', 'NNP')]), ('.', '.')])]

{cd2>我只想把输出写出来。我该怎么做?发生器是什么?在


Tags: andsamplenameinlivetreehellois