【问题标题】:Python: extracting a sentence with a particular wordPython:提取带有特定单词的句子
【发布时间】:2015-01-20 09:33:43
【问题描述】:

我有一个包含如下文本的 json 文件:

博士。戈德堡提供一切。停车很好。他很好,很容易 说说

如何提取关键字“停车”的句子? 我不需要另外两句话。

我试过了:

with open("test_data.json") as f:
    for line in f:
        if "parking" in line:
            print line

它打印所有的文本,而不是那个特定的句子。

我什至尝试过使用正则表达式:

f=open("test_data.json")
for line in f:
    line=line.rstrip()
    if re.search('parking',line):
        print line

即使这显示了相同的结果。

【问题讨论】:

  • 当你在文件指针中使用 readline 时,它​​不会只读取一行。它会一直读到看到“\n”。
  • 使用简单的正则表达式。使用 dmitry_romanov 提到的模式,甚至可以尝试模式 re.search(".*\.(.*parking.*\.)",a).group(1)

标签: python regex nltk


【解决方案1】:

你可以使用nltk.tokenize

from nltk.tokenize import sent_tokenize
from nltk.tokenize import word_tokenize
f=open("test_data.json").read()
sentences=sent_tokenize(f)
my_sentence=[sent for sent in sentences if 'parking' in word_tokenize(sent)] #this gave you the all sentences that your special word is in it ! 

作为一种完整的方式,您可以使用函数:

>>> def sentence_finder(text,word):
...    sentences=sent_tokenize(text)
...    return [sent for sent in sentences if word in word_tokenize(sent)]

>>> s="dr. goldberg offers everything. parking is good. he's nice and easy to talk"
>>> sentence_finder(s,'parking')
['parking is good.']

【讨论】:

    【解决方案2】:

    解析字符串并查看值怎么样?

    import json
    
    def sen_or_none(string):
      return "parking" in string.lower() and string or None
    
    def walk(node):
      if isinstance(node, list):
        for item in node:
          v = walk(item)
          if v:
            return v
      elif isinstance(node, dict):
        for key, item in node.items():
          v = walk(item)
          if v:
            return v
      elif isinstance(node, basestring):
        for item in node.split("."):
          v = sen_or_none(item)
          if v:
            return v
      return None
    
    with open('data.json') as data_file:    
      print walk(json.load(data_file))
    

    【讨论】:

      【解决方案3】:

      可以使用标准库re模块:

      import re
      line = "dr. goldberg offers everything.parking is good.he's nice and easy to talk"
      res = re.search("\.?([^\.]*parking[^\.]*)", line)
      if res is not None:
          print res.group(1)
      

      它将打印parking is good

      想法很简单 - 你从可选的点字符 . 开始搜索句子,而不是使用所有非点、parking 单词和其余非点。

      问号处理句子在行首的情况。

      【讨论】:

      • 但这对于任何带有标点缩写的句子都会失败,例如输入中的前一个句子。
      • @tripleee,恐怕没有语法。 dr. 中的点 . 与任何句子的末尾相同。如果有人需要可以像人类一样阅读的解决方案,他/她要么编写脆弱的正则表达式,要么训练神经网络。恕我直言,这两种情况都过大了。可能是 dr 被替换为 delta r 就像在物理教科书中一样,谁知道呢?我的解决方案将处理逗号等。以 !, ?易于添加等。
      • 对于标记为nltk 的问题,我希望并期待一个至少能够处理实际人类语言基础知识的解决方案。是的,它依赖于上下文,因此像正则表达式这样的上下文无关工具本身就不够用。
      • @tripleee 我完全同意你的看法(现在我在玩 nltk,谢谢你的链接:-))。关于“不足”,我们不能从这里说 OP 是否对语言感知解决方案感兴趣,我们也不能说他的项目中是否允许额外的依赖项(通常我在工作中没有这样的奢侈)。那是他/她的设计决定,而不是我们的。因此,我在他的正则表达式解决方案中修复了模式,因此它可以处理提供的数据,给出 OP 要求的准确结果。就是这样。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-04-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多