-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added modification to user.py also created
user_utility directory and enhanced utility/_utility replica of utility/utility
- Loading branch information
Showing
4 changed files
with
165 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,6 +25,9 @@ def __init__(self, name, email_Id, mobile): | |
# resource file path | ||
self.file_path = 'data/res.npy' | ||
|
||
# tokens | ||
tokens = ['state', 'city', 'resources'] | ||
|
||
# load resource list saved under data/ directory | ||
# self.load_res() | ||
# initialize resource dict | ||
|
@@ -36,17 +39,14 @@ def resource_provider(self): | |
def help_required(self): | ||
self.aid = True | ||
|
||
# def load_res(self): | ||
# self.resource = list(np.load('data/res.npy', allow_pickle=True)) | ||
|
||
def create_res_dict(self): | ||
# list of zeros of same lenght as of resources | ||
self.resource = user_utility.load_file(self.file_path) | ||
indicator = [0] * len(self.resource) | ||
self.res_dict = dict(zip(self.resource, indicator)) | ||
|
||
def get_details(self): | ||
details = [self.name, self.email_Id, self.mobile, self.helper, self.aid] | ||
details = [self.name, self.email_Id, self.mobile, self.helper, self.aid, self.state, self.city] | ||
return details,self.resource, self.res_dict | ||
|
||
def mapping(self): | ||
|
@@ -55,9 +55,20 @@ def mapping(self): | |
if value == 1: | ||
res_available.append(key) | ||
|
||
def update_attributes(self, token, update_key): | ||
if token == 'state': | ||
self.state = update_key | ||
elif token == 'city': | ||
self.city = update_key | ||
elif token == 'resources': | ||
self.res_dict = user_utility.update_dict(self.res_dict, update_key) | ||
|
||
if __name__ == "__main__": | ||
|
||
user1 = user('nidhir', '[email protected]', '123456789') | ||
user1.resource_provider() | ||
lst, res, x = user1.get_details() | ||
print(res) | ||
print(lst) | ||
user1.update_attributes('city', 'Vadodara') | ||
lst, res, x = user1.get_details() | ||
print(lst) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,19 @@ | ||
import numpy as np | ||
|
||
|
||
# load files from data directory | ||
def load_file(file_path): | ||
return np.load(file_path, allow_pickle=True) | ||
return np.load(file_path, allow_pickle=True) | ||
|
||
# update resource dict | ||
def update_dict(res_dict, update_key): | ||
for key, value in res_dict.items(): | ||
if key == update_key: | ||
res_dict[key] = 1 | ||
return res_dict | ||
|
||
def example(): | ||
list_ = list(load_file('data/res.npy')) | ||
dict_ = dict(zip(list_, [0]*len(list_))) | ||
return update_dict(dict_, 'Oximeter') | ||
|
||
# print(example()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import numpy as np | ||
import enchant | ||
|
||
|
||
def take_input(input_type): | ||
# temporary user_input | ||
user_input = 'Oximater' | ||
user_input = user_input.lower() | ||
# handle input_type = 'int' | ||
processed_input = check_validity(user_input, input_type) | ||
return processed_input | ||
|
||
def check_validity(user_input, input_type): | ||
|
||
sim_input, status = most_similar(user_input, input_type) | ||
if status == True: | ||
possible_inputs = handle_invalid_input(user_input, input_type) | ||
return [status, possible_inputs] | ||
else: | ||
return sim_input | ||
|
||
# universal invalid user input handler | ||
def handle_invalid_input(user_input, input_type): | ||
k = 5 | ||
if input_type == 'city': | ||
objective = load_cities() | ||
elif input_type == 'res': | ||
objective = load_resources() | ||
scores = [] | ||
# calculate distance for all cities | ||
for content in objective: | ||
distance = enchant.utils.levenshtein(user_input, content) | ||
scores.append(distance) | ||
# convert to numpy array | ||
scores = np.array(scores) | ||
possible_inputs = objective[np.argpartition(scores, k)[:k]] | ||
return list(possible_inputs) | ||
|
||
# universal similarity checker for both city and resource input type | ||
def most_similar(user_input, input_type): | ||
if input_type == 'city': | ||
objective = load_cities() | ||
elif input_type == 'res': | ||
objective = load_resources() | ||
status = False | ||
scores = [] | ||
# calculate score b/w contents in objective list and user_input | ||
for content in objective: | ||
distance = enchant.utils.levenshtein(user_input, content) | ||
scores.append(distance) | ||
# convert to numpy array | ||
scores = np.array(scores) | ||
min_idx = scores.argmin() | ||
if scores[min_idx] > 3: | ||
status = True | ||
sim_obj = objective[min_idx] | ||
return sim_obj, status | ||
|
||
|
||
def load_cities(): | ||
return np.load('../data/cities.npy', allow_pickle=True) | ||
|
||
def load_resources(): | ||
return np.load('../data/res.npy', allow_pickle=True) | ||
|
||
print(take_input('res')) |