Advertisement
Guest User

Untitled

a guest
Apr 5th, 2020
416
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.43 KB | None | 0 0
  1. from collections import defaultdict
  2.  
  3. import nltk
  4.  
  5.  
  6. class Place:
  7.     title = None
  8.     words = None
  9.  
  10.     def __init__(self, title: str):
  11.         title = title.lower()
  12.         self.title = title
  13.         self.words = nltk.word_tokenize(title)
  14.  
  15.     def __repr__(self) -> str:
  16.         return 'Place(%s)' % self.title
  17.  
  18.     def contains_word(self, word: str):
  19.         return word in self.words
  20.  
  21.  
  22. place_titles = [
  23.     'Moscow', 'New York', 'New Castle', 'Atlanta'
  24. ]
  25.  
  26. all_places = []
  27.  
  28. places_by_word = defaultdict(list)
  29.  
  30. text1 = '''
  31. The streets of New York City are so desolate now that you half expect tumbleweed to blow along the pavement where cars and cabs once clustered. There is barely a plane in the sky. You hear the wheeze of an empty bus rounding a corner, the flutter of pigeons on a fire escape, the wail of an ambulance. The sirens are unnervingly frequent. But even on these sunny, early-spring days there are few people in sight. For weeks, as the distancing rules of the pandemic took hold, a
  32. '''
  33.  
  34. text2 = '''
  35. Lines of cars stretched for blocks around five Atlanta Public Schools facilities on Saturday as families sheltering in place from the coronavirus pandemic scrambled for food before the system’s spring break holidays.
  36. Atlanta Public Schools’ emergency meal distribution sites were swamped Saturday, with all five locations running out of food after less than two hours.The school system, which is closed through April 10, will have no food distribution over the break, offered families a week’s worth of food at Saturday’s pickup sites. APS Superintendent Meria Carstarphen said long lines were waiting when they opened at 9 a.m.“It’s been a massive demand,” she said. “We knew people would come and we’d distribute all the food. We didn’t know how fast it would happen.”
  37. '''
  38.  
  39. def init_system():
  40.     for title in place_titles:
  41.         place = Place(title)
  42.         all_places.append(place)
  43.         for word in place.words:
  44.             places_by_word[word].append(place)
  45.  
  46.  
  47. def break_into_words(text: str):
  48.     words = nltk.word_tokenize(text)
  49.     return words
  50.  
  51.  
  52. class Searcher:
  53.     def __init__(self, places: set, words):
  54.         self.places = places
  55.         self.words = words
  56.         self.i = 0
  57.  
  58.     def search(self):
  59.         self.i = 0
  60.         while self.i < len(self.words):
  61.             word = self.words[self.i]
  62.             places1 = places_by_word.get(word)
  63.             if places1:
  64.                 places2 = list(places1)
  65.                 while self.i < len(self.words):
  66.                     self.i += 1
  67.                     word_next = self.words[self.i]
  68.                     places3 = list(filter(lambda x: x.contains_word(word_next), places2))
  69.                     if places3:
  70.                         places2 = places3
  71.                     else:
  72.                         self.places.update(places2)
  73.                         break
  74.                 # self.places.update(places1)
  75.             self.i += 1
  76.  
  77.  
  78. def find_place(text: str) -> list:
  79.     text = text.lower()
  80.     places_result = set()
  81.     words = break_into_words(text)
  82.     searcher = Searcher(places_result, words)
  83.     searcher.search()
  84.     return list(places_result)
  85.  
  86.  
  87. def main():
  88.     global all_places, places_by_word
  89.     # nltk.download()
  90.     init_system()
  91.     for text in [text1, text2]:
  92.         pp = find_place(text)
  93.         print('text: %s' % text)
  94.         print('places: %s' % pp)
  95.  
  96.  
  97. if __name__ == '__main__':
  98.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement