【问题标题】:Extracting words/phrase followed by a phrase提取单词/短语后跟一个短语
【发布时间】:2021-10-07 07:07:01
【问题描述】:

我有一个包含短语列表的文本文件。以下是文件的外观:

文件名:KP.txt

从下面的输入(段落)中,我想提取KP.txt 短语之后的下两个单词(这些短语可以是我上面的KP.txt 文件中显示的任何内容)。我只需要提取接下来的 2 个单词。

输入:

This is Lee. Thanks for contacting me. I wanted to know the exchange policy at Noriaqer hardware services.

在上面的示例中,我发现短语" I wanted to know"KP.txt 文件内容相匹配。因此,如果我想在此之后提取接下来的 2 个单词,我的输出将类似于 "exchange policy"

如何在 python 中提取它?

【问题讨论】:

  • 我建议您查看spaCy.io ...浏览他们的教程,您将轻松解决此问题。除了对你真正想要达到的目标有更多的想法。
  • “告诉我如何解决这个编码问题”是off-topic for Stack Overflow。我们希望您发送honest attempt at the solution,发布该尝试,然后询问有关它的具体问题(即解释它为什么不起作用或它有什么问题)。
  • 您是否正在尝试构建自动完成算法?
  • 欢迎来到 Stackoverflow。请不要将文字作为图像发布。这使得搜索引擎和许多残疾人无法访问您的帖子,仅举几个不采用这种方法的原因

标签: python extract phrase


【解决方案1】:

假设您已经知道如何将输入文件读入列表,则可以借助正则表达式来完成。

>>> wordlist = ['I would like to understand', 'I wanted to know', 'I wish to know', 'I am interested to know']
>>> input_text = 'This is Lee. Thanks for contacting me. I wanted to know exchange policy at Noriaqer hardware services.'
>>> def word_extraction (input_text, wordlist):
...     for word in wordlist:
...         if word in input_text:
...             output = re.search (r'(?<=%s)(.\w*){2}' % word, input_text)
...             print (output.group ().lstrip ())
>>> word_extraction(input_text, wordlist)
exchange policy
>>> input_text = 'This is Lee. Thanks for contacting me. I wish to know where is Noriaqer hardware.'
>>> word_extraction(input_text, wordlist)
where is
>>> input_text = 'This is Lee. Thanks for contacting me. I\'d like to know where is Noriaqer hardware.'
>>> word_extraction(input_text, wordlist)

>>>
  1. 首先我们需要检查我们想要的短语是否在句子中。如果您的列表很大,这不是最有效的方法,但现在可以。
  2. 接下来,如果它在我们的短语“字典”中,我们使用正则表达式来提取我们想要的关键字。
  3. 最后去掉目标词前面的前导空格。

正则表达式提示:

  • (?
  • (.\w*){2} 表示我们的短语后面的任何字符,后跟一个或多个在关键短语后两个单词处停止的单词。

【讨论】:

    【解决方案2】:

    我认为自然语言处理可能是一个更好的解决方案,但这段代码会有所帮助:)

    def search_in_text(kp,text):
        for line in kp:
            #if a search phrase found in kp lines
            if line in text:
                #the starting index of the two words
                i1=text.find(line)+len(line)
                #the end index of the following two words (first index+50 at maximum)
                i2=(i1+50) if len(text)>(i1+50) else len(text)
                #split the following text to words (next_words) and remove empty spaces
                next_words=[word for word in text[i1:i2].split(' ') if word!='']
                #return  only the next two words from (next_words)
                return next_words[0:2]        
        return [] # return empty list if no phrase matching
            
    
    #read your kp file as list of lines
    kp=open("kp.txt").read().split("\n")
    
    #input 1 
    text = 'This is Lee. Thanks for contacting me. I wanted to know exchange policy at Noriaqer hardware services.'
    print('input ->>',text)
    output = search_in_text(kp,text)
    print('output ->>',output)
    
    input ->> This is Lee. Thanks for contacting me. I wanted to know exchange policy at Noriaqer hardware services.
    output ->> ['exchange', 'policy']
    
    #input 2
    text = 'Boss was very angry and said: I wish to know why you are late?'
    print('input ->>',text)
    output = search_in_text(kp,text)
    print('output ->>',output)
    
    input ->> Boss was very angry and said: I wish to know why you are late?
    output ->> ['why', 'you']
    

    【讨论】:

      【解决方案3】:

      你可以用这个:

      with open("KP.txt") as fobj:
          phrases = list(map(lambda sentence : sentence.lower().strip(), fobj.readlines()))
      
      paragraph = input("Enter The Whole Paragraph in one line:\t").lower()
      
      for phrase in phrases:
          if phrase in paragraph:
              temp = paragraph.split(phrase)[1:]
              for clause in temp:
                  print(" ".join(clause.split()[:2]))
      
      

      【讨论】:

      • 请说明您的解决方案如何解决 OP 的问题。
      • Kunal 工作正常,谢谢。还有一个疑问,如果我想提取输入文本中存在的超过 1 个短语怎么办,例如:“我是 Lee。感谢您与我联系。我想知道 Noriaqer 硬件服务的交换政策。我很想知道其他政策也是如此”,我们要提取“交换政策”(因为“我想知道”)和“其他政策”(因为“我有兴趣知道”)
      • @Veera 它适用于 KP.txt 中存在的所有短语,您可以尝试一下,只需确认 KP.txt 中存在所需的短语,.
      • @Luke 根据我的说法,代码非常简单,任何对列表和字符串切片的基本操作不了解的人都可以很容易地理解它,这就是它在这里的原因,
      • 虽然此代码可能会解决问题,including an explanation 关于如何以及为什么解决问题将真正有助于提高您的帖子质量,并可能导致更多的赞成票。请记住,您正在为将来的读者回答问题,而不仅仅是现在提问的人。请edit您的回答添加解释并说明适用的限制和假设。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-07
      • 2017-11-23
      • 1970-01-01
      • 2011-08-30
      • 2012-07-08
      • 1970-01-01
      相关资源
      最近更新 更多