【问题标题】:Select only 'NN' and 'VB' words from NTLK pos_tag从 NTLK pos_tag 中仅选择 'NN' 和 'VB' 词
【发布时间】:2019-10-06 18:42:52
【问题描述】:

我只需要从输入的句子中打印 'NN' 和 'VB' 单词。

import nltk
import re
import time

var = raw_input("Please enter something: ")


exampleArray = [var]


def processLanguage():
    try:
        for item in exampleArray:
            tokenized = nltk.word_tokenize(item)
            tagged = nltk.pos_tag(tokenized)
            print tagged

            time.sleep(555)


    except Exception, e:
        print str(e)

processLanguage()

【问题讨论】:

    标签: python string nlp nltk part-of-speech


    【解决方案1】:

    换个方式

        print tagged
    

        print [(word, tag) for word, tag in tagged if tag in ('NN', 'VB')]
    

    【讨论】:

      【解决方案2】:

      您可能需要使用 POS 标签的前 2 个字符,请参阅 NLTK - Get and Simplify List of Tags

      nn_vb_tagged = [(word,tag) for word, tag in tagged 
                      if tag.startswith('NN') or tag.startswith('VB')]
      

      【讨论】:

      • str.startswith 也接受一个元组:tag.startswith(('NN', 'VB'))
      【解决方案3】:

      你可以试试这个:

      example = "This is a sample sentence, showing off the stop words filtration.!"
      word_tokens = word_tokenize(example)
      pos = nltk.pos_tag(word_tokens)
      selective_pos = ['NN','VB']
      selective_pos_words = []
      for word,tag in pos:
           if tag in selective_pos:
               selective_pos_words.append((word,tag))
      print(selective_pos_words)
      

      通过在列表“selective_pos”中添加您的选择性词性,您可以选择任何您喜欢的词。

      【讨论】:

        猜你喜欢
        • 2019-05-14
        • 1970-01-01
        • 2016-06-22
        • 2016-08-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多