-
Notifications
You must be signed in to change notification settings - Fork 2
/
ticker_finder.py
46 lines (40 loc) · 1.44 KB
/
ticker_finder.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
import difflib
def similarity_score(s1, s2):
# Function to calculate the similarity between strings s1 and s2
s1_lower = s1.lower()
s2_lower = s2.lower()
matcher = difflib.SequenceMatcher(None, s1_lower, s2_lower)
return matcher.ratio()
def find_similar_ticker(commodity_selected, data_tickers):
commodity_selected = (
commodity_selected.split(" - ")[0]
.replace("index", "")
.replace("futures", "")
.replace("-", " ")
.replace("ultra","")
)
commodity_selected = " ".join(commodity_selected.split()[:3])
best_match = None
best_score = 0.0
for value in data_tickers.values():
score = similarity_score(commodity_selected, value)
if score > best_score:
best_match = value
best_score = score
if best_match is not None and best_score >= 0.6:
return data_tickers.inverse[best_match]
else:
# Try to reverse the word order in the user's query and compare again
reversed_commodity_selected = " ".join(reversed(commodity_selected.split()))
reversed_score = (
similarity_score(reversed_commodity_selected, best_match)
if best_match
else 0.0
)
if reversed_score >= 0.6:
tk = data_tickers.inverse[best_match]
print("Ticker found ", tk)
return tk
else:
print("No matching ticker found.")
return None