-
Notifications
You must be signed in to change notification settings - Fork 0
/
test2.py
190 lines (132 loc) · 4.93 KB
/
test2.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
"""
A test for ragdoll.
"""
from ragdoll import *
mongo = MongoDB(host='47.93.246.201', port=27017, database='eatech', user='harry', password='password')
#------------------------------------------------#
def viaName(str, databaseName, dispName=True):
'''
Fuzzy search for name string matching (case insensitive).
For English fuzzy search, insert string seperated with space. eg: 'Butter salt'
Generated query: {'name.long': {'$regex': '^(?=.*Butter)(?=.*salt).+', '$options': 'i'}}
Return: any object whose name contains all keywords in any order.
Chinese fuzzy search not defined yet.
TODO:
Search suggestions: split sentence into words and compare words with high frequency dictionary,
give close words search when original word is not available.
'''
if databaseName == 'USDA':
path = 'name.long'
regexStr = '^'+''.join(['(?=.*'+word+')' for word in str.split(' ')])+'.+'
elif databaseName == 'Foodmate' or databaseName == 'DIY':
path = 'name'
regexStr = '.*' + str + '.*'
else:
raise Exception('Insert a vaild database.')
def queryGen(str):
query = {}
query[path] = {}
query[path]['$regex'] = str
query[path]['$options'] = 'i'
return query
# further output format to be set
display = {'_id':1}
if dispName: display['name'] = 1
query = queryGen(regexStr)
if databaseName == 'USDA':
cursor = mongo.database.USDA.find(query, display)
elif databaseName == 'Foodmate':
cursor = mongo.database.Foodmate.find(query, display)
elif databaseName == 'DIY':
cursor = mongo.database.DIY.find(query, display)
else:
raise Exception('Insert a vaild database.')
return cursor
#------------------------------------------------#
def viaRange(nutrients, databaseName='USDA', dispNutrients=False):
# DIY contains materials not nutrients, not available right now
'''
Query example:
cur = db.USDA.find({'nutrients': { '$all': [{ '$elemMatch' : {'abbr' : tag1, 'value' : val1}},
{ '$elemMatch' : {'abbr' : tag2, 'value' : val2}}
]}},
{'_id' : 1, 'nutrients' : 1})
'''
def queryGen(nuts, fuzzySearch=True, fltPct=0.1):
'''
Fuzzy search enabled by default for accurate values.
fltPct: floating value (percentage).
'''
query = {}
query['nutrients'] = {}
query['nutrients']['$all'] = []
for nutInfo in nuts:
new_nutInfo = nutInfo.copy()
val = new_nutInfo['value']
# range (only support a valid range eg [10, 20] yet)
if type(val) == list:
new_nutInfo['value'] = {'$gt':val[0], '$lt':val[1]}
# single value
elif type(val) == int or type(val) == float:
if fuzzySearch:
new_nutInfo['value'] = {'$gt':(1-fltPct)*val, '$lt':(1+fltPct)*val}
else:
raise Exception('Insert a value or a range to search.')
subdict = {}
subdict['$elemMatch'] = new_nutInfo
query['nutrients']['$all'].append(subdict.copy())
return query
# further output format to be set
display = {'_id' : 1}
if dispNutrients: display['nutrients'] = 1
query = queryGen(nutrients)
if databaseName == 'USDA':
cursor = mongo.database.USDA.find(query, display)
elif databaseName == 'Foodmate':
cursor = mongo.database.Foodmate.find(query, display)
elif databaseName == 'DIY':
raise Exception('DIY not supported yet, please choose a vaild database.')
else:
raise Exception('Insert a vaild database.')
return cursor
#------------------------------------------------#
def viaTag(reqDict, databaseName):
'''
eg: {'type':'乳类', 'name':'奶油'}
'''
query = reqDict
display = {}
for name, val in reqDict.items():
display[name] = 1
if databaseName == 'USDA':
cursor = mongo.database.USDA.find(query, display)
elif databaseName == 'Foodmate':
cursor = mongo.database.Foodmate.find(query, display)
elif databaseName == 'DIY':
cursor = mongo.database.DIY.find(query, display)
else:
raise Exception('Insert a vaild database.')
return cursor
#------------------------------------------------#
# test for viaName
'''
str = 'Butter salt'
cur = viaName(str,databaseName='USDA')
for doc in cur:
print(doc)
'''
# test for viaRange
'''
nutrients = [{'abbr' : 'PROCNT', 'value' : 0.49},
{'abbr' : 'CHOCDF', 'value' : 2.87}]
cur = viaRange(nutrients)
for doc in cur:
print(doc)
'''
# test for viaTag
'''
dictionary = {'type':'乳类', 'name': '奶油''}
cur = viaTag(dictionary, databaseName='Foodmate')
for doc in cur:
print(doc)
'''