def search(self, query, hits=10): urls = self._search_google(query) text_parser = TextParser.create() if 'who' in query.lower(): names_aggregation = {} for url in urls: names = text_parser.find_names(self._get_page(url)) for name in names: if names_aggregation.get(name, None): names_aggregation[name] += 1 else: names_aggregation[name] = 1 data = sorted(names_aggregation, key=names_aggregation.get)[::-1][:10] return { 'keyword': 'who', 'data': data } elif 'where' in query.lower(): places_aggregation = {} for url in urls: places = text_parser.find_names(self._get_page(url)) for place in places: if places_aggregation.get(place, None): places_aggregation[place] += 1 else: places_aggregation[place] = 1 data = sorted(places_aggregation, key=places_aggregation.get)[::-1][:10] return { 'keyword': 'where', 'data': data } else: hitword_aggregation = {} for url in urls: words = text_parser.find_hitwords(self._get_page(url, souped=True)) for word in words: if hitword_aggregation.get(word, None): hitword_aggregation[word] += 1 else: hitword_aggregation[word] = 1 data = sorted(hitword_aggregation, key=hitword_aggregation.get)[::-1][:10] return { 'keyword': None, 'data': data }