【问题标题】:Search text from bag of words in python从python中的词袋中搜索文本
【发布时间】:2020-02-14 00:19:08
【问题描述】:

假设我有一袋关键字。 例如:

['profit low', 'loss increased', 'profit lowered']

我有一个 pdf 文档,我从中解析了整个文本, 现在我想得到与词袋匹配的句子。

让我们说一句话是:

'The profit in the month of November lowered from 5% to 3%.'

这应该匹配在 bag of words 'profit lowered' 匹配这个句子。

在 python 中解决这个问题的最佳方法是什么?

【问题讨论】:

    标签: python nlp text-processing text-parsing


    【解决方案1】:
    # input
    checking_words = ['profit low', 'loss increased', 'profit lowered']
    checking_string = 'The profit in the month of November lowered from 5% to 3%.'
    
    trans_check_words = checking_string.split()
    # output
    for word_bug in [st.split() for st in checking_words]:
        if word_bug[0] in trans_check_words and word_bug[1] in trans_check_words:
            print(word_bug)
    

    【讨论】:

      【解决方案2】:

      您想检查所有 Check Words 列表元素是否在长句中

      sentence = 'The profit in the month of November lowered from 5% to 3%.'
      
      words = ['profit','month','5%']
      
      for element in words:
          if element in sentence:
              #do something with it
              print(element)
      

      如果你想更简洁,可以使用这个单行循环将匹配的单词收集到一个列表中:

      sentence = 'The profit in the month of November lowered from 5% to 3%.'
      
      words = ['profit','month','5%']
      
      matched_words = [] # Will collect the matched words in the next life loop:
      
      [matched_words.append(word) for word in words if word in sentence]
      
      print(matched_words)
      

      如果您的列表中的每个元素上都有“间隔”单词,您想通过使用 split() 方法来处理它。

      sentence = 'The profit in the month of November lowered from 5% to 3%.'
      
      words = ['profit low','month high','5% 3%']
      
      single_words = []
      for w in words:
          for s in range(len(w.split(' '))):
              single_words.append(w.split(' ')[s])
      
      matched_words = [] # Will collect the matched words in the next life loop:
      [matched_words.append(word) for word in single_words if word in sentence]
      
      print(matched_words)
      

      【讨论】:

        【解决方案3】:

        您可以尝试以下方法:

        将词袋转化为句子:

        bag_of_words = ['profit low', 'loss increased', 'profit lowered']    
        bag_of_word_sent =  ' '.join(bag_of_words)
        

        然后是句子列表:

        list_sents = ['The profit in the month of November lowered from 5% to 3%.']
        

        使用 Levenshtein 距离:

        import distance
        for sent in list_sents:
            dist = distance.levenshtein(bag_of_word_sent, sent)
            if dist > len(bag_of_word_sent):
                # do something
                print(dist)
        

        【讨论】:

          猜你喜欢
          • 2019-02-28
          • 2017-08-28
          • 1970-01-01
          • 1970-01-01
          • 2019-03-15
          • 1970-01-01
          • 2013-03-08
          • 2015-01-31
          相关资源
          最近更新 更多