def handle_request_for_synonym(): raw_return = request.GET.get('word') send_pack = wh_lib.find_syns(raw_return) raw_send = json.dumps(send_pack) return raw_send
def handle_request_for_synonym(): raw_return = bottle.request.body.readline() return_pack = json.loads(raw_return.decode('utf-8')) send_pack = wh_lib.find_syns(return_pack['word']) raw_send = json.dumps(send_pack).encode('utf-8') return raw_send
def get_best_match(self, w): # find synonyms # iterate synonyms and match articles. # articles that appear the most are chosen as best match w = w.lower() splitword = w.split(' ') synlist = [] for word in splitword: synlist.extend(find_syns(word)) relate_dict = {} artcol = self.client['mh']['articles'] for word in splitword: queryresult = artcol.find({'keywords': {'$in': [word]}}) for art in queryresult: if str(art['_id']) in relate_dict: relate_dict[str(art['_id'])] += 3 else: relate_dict[str(art['_id'])] = 3 for syn in synlist: synqueryresult = artcol.find({'keywords': {'$in': [syn]}}) for art in synqueryresult: if str(art['_id']) in relate_dict: relate_dict[str(art['_id'])] += 1 else: relate_dict[str(art['_id'])] = 1 keys = relate_dict.keys() values = relate_dict.values() max_index = array_max_index(values) if max_index < 0: return False best_id = keys[max_index] best_article = artcol.find({'_id': ObjectId(best_id)})[0] best_article.pop('text') best_article.pop('keywords') best_article['_id'] = str(best_article['_id']) best_article['site'] = get_site(best_article['url']) best_article['title'] = strip_title(best_article['title']) return best_article