Skip to content

Commit

Permalink
Added modification to user.py also created
Browse files Browse the repository at this point in the history
user_utility directory and enhanced utility/_utility
replica of utility/utility
  • Loading branch information
Nid989 committed Apr 26, 2021
1 parent d16ee63 commit fa5a699
Show file tree
Hide file tree
Showing 4 changed files with 165 additions and 7 deletions.
68 changes: 68 additions & 0 deletions Notebooks Checkpoint/cities-data.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,74 @@
"dictn"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Original arrays:\n[ 1. 7. 8. 2. 0.1 3. 15. 2.5]\n"
]
}
],
"source": [
"array1 = np.array([1, 7, 8, 2, 0.1, 3, 15, 2.5])\n",
"print(\"Original arrays:\")\n",
"print(array1)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"\nk smallest values:\n[0.1 1. 2. 2.5]\n"
]
}
],
"source": [
"k = 4\n",
"result = np.argpartition(array1, k)\n",
"print(\"\\nk smallest values:\")\n",
"print(array1[result[:k]])"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"array([0.1, 1. , 2. , 2.5])"
]
},
"metadata": {},
"execution_count": 6
}
],
"source": [
"array1[result[:k]]"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand Down
21 changes: 16 additions & 5 deletions user.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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):
Expand All @@ -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)
17 changes: 15 additions & 2 deletions user_utility/user_utility.py
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())
66 changes: 66 additions & 0 deletions utility/_utility.py
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'))

0 comments on commit fa5a699

Please sign in to comment.