【问题标题】:Deleting all the noun phrases from text using Textblob使用 Textblob 从文本中删除所有名词短语
【发布时间】:2020-04-06 07:48:03
【问题描述】:

我需要从文本中删除所有专有名词。 结果是数据框。 我正在使用文本块。下面是代码。

from textblob import TextBlob

          strings = []
            for col in result:
                for i in range(result.shape[0]):
                    text = result[col][i]
                    Txtblob = TextBlob(text)

                    for word, pos in Txtblob.noun_phrases:
                        print (word, pos)
                        if tag != 'NNP'
                           print(' '.join(edited_sentence))

它只识别一个 NNP

【问题讨论】:

  • 请检查您的代码 sn-p 的格式。您需要提供minimal reproducible example
  • 你能分享代码吗,带有示例输入和输出。这将使复制更容易
  • @Philip 请不要鼓励人们分享他们的整个代码,只是重现问题所需的最低限度。
  • 这是关于文本 blob 的完整代码
  • 然后提供您的输入样本text 以及您设置tagas 和edited_sentence 的位置。我不确定您是否知道,但您在此部分 if tag != 'NNP' 之后缺少 :,它应该是 if tag != 'NNP':

标签: python textblob


【解决方案1】:

要从以下文本(来自documenation)中删除所有带有“NNP”标签的单词,您可以执行以下操作:

from textblob import TextBlob

# Sample text
text = '''
The titular threat of The Blob has always struck me as the ultimate movie
monster: an insatiably hungry, amoeba-like mass able to penetrate
virtually any safeguard, capable of--as a doomed doctor chillingly
describes it--"assimilating flesh on contact.'''

text = TextBlob(text)

# Create a list of words that are tagged with 'NNP'
# In this case it will only be 'Blob'
words_to_remove = [word[0] for word in [tag for tag in text.tags if tag[1] == 'NNP']]

# Remove the Words from the sentence, using words_to_remove
edited_sentence = ' '.join([word for word in text.split(' ') if word not in words_to_remove])

# Show the result
print(edited_sentence)

出来

# Notice the lack of the word 'Blob'
'\nThe titular threat of The has always struck me as the ultimate
 movie\nmonster: an insatiably hungry, amoeba-like mass able to 
 penetrate\nvirtually any safeguard, capable of--as a doomed doctor 
 chillingly\ndescribes it--"assimilating flesh on contact.\nSnide 
 comparisons to gelatin be damned, it\'s a concept with the 
 most\ndevastating of potential consequences, not unlike the grey goo 
 scenario\nproposed by technological theorists fearful of\nartificial 
 intelligence run rampant.\n'

对您的示例的评论

from textblob import TextBlob

strings = [] # This variable is not used anywhere
for col in result:
    for i in range(result.shape[0]):
        text = result[col][i]
        txt_blob = TextBlob(text)

        # txt_blob.noun_phrases will return a list of noun_phrases,
        # To get the position of each list you need use the function 'enuermate', like this
        for word, pos in enumerate(txt_blob.noun_phrases):

            # Now you can print the word and position
            print (word, pos)
            # This will give you something like the following:
            # 0 titular threat
            # 1 blob
            # 2 ultimate movie monster

            # This following line does not make any sense, because tag has not yet been assigned
            # and you are not iterating over the words from the previous step
            if tag != 'NNP'
                # You are not assigning anything to edited_sentence, so this would not work either.
                print(' '.join(edited_sentence))

带有新代码的示例

from textblob import TextBlob

for col in result:
    for i in range(result.shape[0]):
        text = result[col][i]
        txt_blob = TextBlob(text)

        # Create a list of words that are tagged with 'NNP'
        # In this case it will only be 'Blob'
        words_to_remove = [word[0] for word in [tag for tag in txt_blob.tags if tag[1] == 'NNP']]

        # Remove the Words from the sentence, using words_to_remove
        edited_sentence = ' '.join([word for word in text.split(' ') if word not in words_to_remove])

        # Show the result
        print(edited_sentence)

【讨论】:

  • @AnnaSmith,我已经更新了答案。我还在提供的示例上测试了这是否有效,它删除了标记为NNPBlob)的唯一单词。如果您在使用它时遇到问题,请告诉我。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-02-19
  • 2011-05-09
  • 2019-01-19
  • 2015-05-04
  • 1970-01-01
  • 2017-05-08
  • 1970-01-01
相关资源
最近更新 更多