简体   繁体   中英

Python: extracting a sentence with a particular word

I have a json file containing texts like:

dr. goldberg offers everything.parking is good.he's nice and easy to talk

How can I extract the sentence with the keyword "parking"? I don't need the other two sentences.

I tried this:

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

It prints all the text and not that particular sentence.

I even tried using regex:

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

Even this shows the same result.

you can use 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 ! 

and as a complete way you can use a function :

>>> 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.']

You can use the standard library re module:

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)

It will print parking is good .

Idea is simple - you search for sentence starting from optional dot character . , than consume all non-dots, parking word and the rest of non-dots.

Question mark handles the case where your sentence is in the start of the line.

How about parsing the string and looking at the values?

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))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM