-
Notifications
You must be signed in to change notification settings - Fork 0
/
state_helper_functions.py
127 lines (85 loc) · 3.82 KB
/
state_helper_functions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
from get_preference_from_sentence import ONTOLOGY_PATH
from user_model import Requestables, ANY_PREFERENCE_CONSTANT
from Levenshtein.StringMatcher import StringMatcher
from get_preference_from_sentence import find_closest_match
import json
import csv
import re
def get_inform_requestable_dict():
file = open(ONTOLOGY_PATH)
json_file = json.load(file)
file.close()
informables = json_file['informable']
variable_dict = {}
for key, value in informables.items():
for variable_word in value:
variable_dict[variable_word] = Requestables(key)
variable_dict['any'] = Requestables.Any
variable_dict['anything'] = Requestables.Any
return variable_dict
# return word + requestable
def get_requestable_from_sentence(sentence: str, requestable_dict):
no_punctual_sentence = re.sub(r'[^\w\s]', '', sentence)
lowercase_sentence = no_punctual_sentence.lower()
split_sentence = re.split(r'\s+', lowercase_sentence)
results = []
for word, requestable in requestable_dict.items():
if word in sentence:
results.append((word, requestable))
if len(results) > 0:
return results
for word in split_sentence:
closest_match, closest_requestable = find_closest_match(requestable_dict, word)
word_distance = StringMatcher(seq1=word, seq2=closest_match).distance()
if word_distance <= 2:
results.append((closest_match, closest_requestable))
return results
def get_restaurant_list():
reader = csv.DictReader(open('restaurantinfo.csv'))
restaurant_dict_list = []
for restaurant_dict in reader:
restaurant_dict_list.append(restaurant_dict)
return restaurant_dict_list
class RestaurantInfo:
def __init__(self):
reader = csv.DictReader(open('restaurantinfo.csv'))
restaurant_dict_list = []
for restaurant_dict in reader:
restaurant_dict_list.append(restaurant_dict)
self.restaurant_list = restaurant_dict_list
self.preferred_restaurant_list = None
self.selected_restaurant = None
self.current_index = 0
def reset(self):
self.preferred_restaurant_list = None
self.selected_restaurant = None
self.current_index = 0
def get_next_suggestion(self):
self.current_index += 1
if self.current_index == len(self.preferred_restaurant_list):
return None
elif self.current_index >= len(self.preferred_restaurant_list):
self.current_index = 0
self.selected_restaurant = self.preferred_restaurant_list[self.current_index]
return self.selected_restaurant
def get_suggestions(self, food_preference=None, pricerange_preference=None, area_preference=None):
if self.preferred_restaurant_list is not None:
return self.get_next_suggestion()
preferred_restaurants = []
# if A restaurant has all our preferences or we don't care
for restuarant_dict in self.restaurant_list:
if food_preference is not None:
if restuarant_dict['food'] != food_preference and food_preference != ANY_PREFERENCE_CONSTANT:
continue
if pricerange_preference is not None:
if restuarant_dict['pricerange'] != pricerange_preference and pricerange_preference != ANY_PREFERENCE_CONSTANT:
continue
if area_preference is not None:
if restuarant_dict['area'] != area_preference and area_preference != ANY_PREFERENCE_CONSTANT:
continue
preferred_restaurants.append(restuarant_dict)
if len(preferred_restaurants) == 0:
return None
self.preferred_restaurant_list = preferred_restaurants
self.selected_restaurant = preferred_restaurants[0]
return self.selected_restaurant