【问题标题】:Python extracting sentence containing 2 wordsPython提取包含2个单词的句子
【发布时间】:2013-09-02 23:03:42
【问题描述】:

我有同样的问题在这个链接Python extract sentence containing word 中讨论过,但不同的是我想在同一个句子中找到 2 个单词。我需要从包含 2 个特定单词的语料库中提取句子。请问有谁可以帮帮我吗?

【问题讨论】:

    标签: python regex nltk sentence text-segmentation


    【解决方案1】:

    TextBlob 包与Python 的内置sets 一起使用会很简单。

    基本上,遍历文本中的句子,并检查句子中的单词集与搜索词之间是否存在交集。

    from text.blob import TextBlob
    
    search_words = set(["buy", "apples"])
    blob = TextBlob("I like to eat apple. Me too. Let's go buy some apples.")
    matches = []
    for sentence in blob.sentences:
        words = set(sentence.words)
        if search_words & words:  # intersection
            matches.append(str(sentence))
    print(matches)
    # ["Let's go buy some apples."]
    

    更新: 或者,更 Python 化地,

    from text.blob import TextBlob
    
    search_words = set(["buy", "apples"])
    blob = TextBlob("I like to eat apple. Me too. Let's go buy some apples.")
    matches = [str(s) for s in blob.sentences if search_words & set(s.words)]
    print(matches)
    # ["Let's go buy some apples."]
    

    【讨论】:

      【解决方案2】:

      如果这是你的意思:

      import re
      txt="I like to eat apple. Me too. Let's go buy some apples."
      define_words = 'some apple'
      print re.findall(r"([^.]*?%s[^.]*\.)" % define_words,txt)  
      
      Output: [" Let's go buy some apples."]
      

      你也可以试试:

      define_words = raw_input("Enter string: ")
      

      检查句子是否包含定义的单词:

      import re
      txt="I like to eat apple. Me too. Let's go buy some apples."
      words = 'go apples'.split(' ')
      
      sentences = re.findall(r"([^.]*\.)" ,txt)  
      for sentence in sentences:
          if all(word in sentence for word in words):
              print sentence
      

      【讨论】:

      • 感谢 badc0re,但我忘了说这两个词不需要连续。它是一种使用正则表达式并通过 moliware 获得与以下答案相同的结果的方法吗?
      • 我使用正则表达式添加了另一个类似于@moliware 的解决方案。
      【解决方案3】:

      我想你想用 nltk 来回答。而且我猜这两个词不需要是连续的吧?

      >>> from nltk.tokenize import sent_tokenize, word_tokenize
      >>> text = 'I like to eat apple. Me too. Let's go buy some apples.'
      >>> words = ['like', 'apple']
      >>> sentences = sent_tokenize(text)
      >>> for sentence in sentences:
      ...   if (all(map(lambda word: word in sentence, words))):
      ...      print sentence
      ...
      I like to eat apple.
      

      【讨论】:

        猜你喜欢
        • 2013-04-08
        • 2020-10-10
        • 1970-01-01
        • 2014-07-11
        • 1970-01-01
        • 1970-01-01
        • 2017-04-27
        相关资源
        最近更新 更多