【问题标题】:How extract the current sentence and surrounding sentences around a particular word with Python?如何使用 Python 提取特定单词周围的当前句子和周围句子?
【发布时间】:2021-07-16 01:05:21
【问题描述】:

有没有办法让句子中任何选定单词周围的句子。假设我们的目标是获取下面示例中包含单词“Champion”的当前句子以及围绕它的前后句子,无论它们的位置、标签或单词 Champion 重复了多少次。

text = "This is sentence 1. We are the champions. This is sentence 3. This is sentence 4. This is sentence 5. You are champions too."

在上面的例子中,单词 Champion 在句子 2 和 6 中重复出现。所以我们想要发送 1、2、3、5、6 并排除发送 4。

我们如何使用 Spacy 或其他工具来实现这一目标?

【问题讨论】:

    标签: python-3.x nlp spacy


    【解决方案1】:

    您可以只使用re.split 分割标点符号,确定哪些句子包含该单词,然后抓取任何与该句子匹配或与该句子相邻的索引。

    >>> import re    
    >>> text = "This is sentence 1. We are the champions. This is sentence 3. This is sentence 4. This is sentence 5. You are champions too."
    >>> sentences = sentences = re.split('[\.\!\?] *',text)[:-1]
    >>> sentences
    ['This is sentence 1', 'We are the champions', 'This is sentence 3', 'This is sentence 4', 'This is sentence 5', 'You are champions too']
    >>> champion_indices = set(
        [
          i for i in range(len(sentences))
          if 'champions' in sentences[i].casefold()]
        )
    >>> champion_indices
    {1, 5}
    >>> champion_adjacent_sentences = [
          sentences[i] for i in range(len(sentences))
          if (i - 1 in champion_indices
             or i in champion_indices
             or i+1 in champion_indices)]
    >>> champion_adjacent_sentences
    ['This is sentence 1', 'We are the champions', 'This is sentence 3', 'This is sentence 5', 'You are champions too']
    

    这里唯一可能不熟悉的是casefold 的使用,这是一种巧妙的方法,可以将两个字符串小写以便对它们进行不区分大小写的比较。

    【讨论】:

      【解决方案2】:

      使用这个函数会给出周围的句子。

      from nltk.tokenize import sent_tokenize
      from nltk.tokenize import word_tokenize
      
      def surrounding_sentences(text, word):
      
          sentences=sent_tokenize(text)
          
          my_sents=[]
          for i in range(len(sentences)):
              if word in word_tokenize(sentences[i].lower()): 
                  if i-1>0 : 
                      previous_sent = sentences[i-1]
                      my_sents.append(previous_sent)
                  else: pass
                  sent= sentences[i]
                  my_sents.append(sent)
                  if i+1 < len(sentences):
                      nextsent = sentences[i+1]
                      my_sents.append(nextsent)
                  else: pass
          my_sents = list(set(my_sents))
          return my_sents
      

      【讨论】:

        猜你喜欢
        • 2014-07-11
        • 1970-01-01
        • 1970-01-01
        • 2013-03-09
        • 1970-01-01
        • 2015-01-20
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多