-
Notifications
You must be signed in to change notification settings - Fork 0
/
Processor.py
78 lines (60 loc) · 2.62 KB
/
Processor.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
import re
class Processor:
def strip(self,term):
return term.strip()
def special_char_remove(self,term):
text = ""
for chr in str(term):
if chr not in "[!@#$%^&*()[];:,./<>?\|`~-=_+]":
text+=chr
return text
def tokenize(self,term):
return term.split()
def stop_word_remove(self,tokens):
with open("stop words.txt","r",encoding="UTF-8") as f:
stop_words = [i.strip() for i in f.readlines()]
tokens = [token for token in tokens if token not in stop_words]
return tokens
def get_boosters(self,tokens):
heuristics = {}
heuristics["artist"] = ['ගායකයා', 'ගයනවා', 'ගායනා', 'ගැයු', 'ගයන', 'ගයපු', 'කියපු', 'කියන']
heuristics["name"] = ["නම","ගීතය","නාමය"]
heuristics["source"] = ["මෙන්","සේ","වැනි","බඳු","වන්","අයුරු","අයුරින්","ලෙස","උපමේය"]
heuristics["targets"] = ["උපමා","උපමාවක්","රූපක","රූපකයක්"]
boosts={}
boosts["artist"]=1
boosts["name"]=1
boosts["source"]=1
boosts["targets"]=1
for token in tokens:
for field in heuristics.keys():
if token in heuristics[field]:
boosts[field] +=4
boosters=[
"name^"+str(boosts["name"]),
"singers^"+str(boosts["artist"]),
"metaphors.source^"+str(boosts["source"]),
"metaphors.targets^"+str(boosts["targets"])
]
return boosters
def get_sorter(self,tokens):
by_date=0
by_popularity=0
date =["නවතම", "අලුත්", "නව", "අලුත්ම" ]
popularity = ['ජනප්රිය', 'ප්රචලිත', 'ප්රසිද්ධ', 'ජනප්රියම', 'ප්රචලිතම' 'ප්රචලිතම']
for token in tokens:
if token in date:
by_date+=1
if token in popularity:
by_popularity+=1
if by_date==by_popularity:
return None
return {"view_count":"desc"} if by_popularity>by_date else {"published_on":"desc"}
def get_range(self,tokens):
range = 10
for token in tokens:
if str(token).isdigit():
range = int(token)
return range
def query_text(self,tokens):
return " ".join(tokens)