forked from bphall/semantic_search
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sem_functions.py
163 lines (141 loc) · 7.2 KB
/
sem_functions.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
from nltk import RegexpTokenizer
from nltk.corpus import stopwords
import nltk
import pickle
import streamlit as st
import requests
import os
nltk.download('stopwords')
stop_set = set(stopwords.words('english'))
if not os.path.exists('models/fourth_model.pkl'):
response = requests.get('https://braytonhall-public.s3.amazonaws.com/semanticsearchmodels/fourth_model.pkl', stream=True)
with open('models/fourth_model.pkl', 'wb') as f:
f.write(response.content)
if not os.path.exists('models/paragraphs_dataframe'):
response = requests.get('https://braytonhall-public.s3.amazonaws.com/semanticsearchmodels/paragraphs_dataframe2', stream=True)
with open('models/paragraphs_dataframe', 'wb') as f:
f.write(response.content)
paragraphs = pickle.load(open("models/paragraphs_dataframe", "rb"))
third_model = pickle.load(open('models/fourth_model.pkl', 'rb'))
def model_tokenizer(input_data):
tokenizer = RegexpTokenizer(r'\w+')
lowered = input_data.lower()
tokens = tokenizer.tokenize(lowered)
return tokens
def para_from_id(tag, rank, cosine):
start = tag - 1
end = tag + 2
name = paragraphs.loc[tag].title
book_range = paragraphs[paragraphs.title == f'{name}'].index
location = tag - book_range[0]
length = book_range[-1] - book_range[0]
percent = round((location / length) * 100, 2)
st.write('NOVEL: ' + name, '\n')
st.write('LOCATION IN NOVEL: AT PARAGRAPH {0} out of {1}, {2}% into the book'.format(location, length, percent), '\n')
for i in range(start, end):
if i < tag:
try:
st.write('...' + paragraphs.chunks[i][-200:])
except:
st.write('Beginning of Novel')
if i > tag:
try:
st.write(paragraphs.chunks[i][:200] + '...')
except:
st.write('End of Novel')
if i == tag:
st.write('**************************************************')
st.write(' {0} MOST SIMILAR PARAGRAPH, COSINE_SIMILARITY: {1} '.format(
rank, cosine), '\n')
st.write(paragraphs.chunks[i])
st.write('**************************************************')
def semantic_search(type_text_here, include_quoted_novel=True):
# Tokenizes the input text, vectorizes it, and finds the 300 most similar tagged paragraphs.
if len(type_text_here) < 3:
return None
tokens = model_tokenizer(type_text_here)
vector = third_model.infer_vector(tokens)
top300 = third_model.docvecs.most_similar([vector], topn=300)
final_tags = []
final_tags_other_novels = []
quoted_novel = paragraphs.loc[int(top300[0][0])].title
# These need to be removed later from the whole DataFrame, but in the meantime this solution works
rid = "La Navigation Aérienne L'aviation Et La Direction Des Aérostats Dans Les Temps Anciens Et Modernes"
rid2 = "The Decameron of Giovanni Boccaccio"
rid3 = "The MemoirsCorrespondenceAnd MiscellaniesFrom The Papers Of Thomas Jefferson"
rid4 = 'The Kama Sutra of Vatsyayana'
rid5 = 'Leviathan'
rid6 = "Divine ComedyLongfellow's TranslationHell"
rid7 = "Also sprach ZarathustraEnglish"
rid8 = "Geschlecht und CharakterEnglish"
rid9 = 'Index of Project Gutenberg Works on Black History'
# The following two for-loops create final_tags and final_tags_other novels, which are used to
# filter out results based on whether the user wants to include the quoted novel.
#
# The numbers (> 20 here) filters out search results with 20 or fewer words, since the model
# is biased to think that short input strings are similar to short paragraphs, which only exist
# at the beginning and end of novels, based on how they were broken up originally.
#
# The rid statements are a short term solution for removing two Italian and French novels,
# and the 'letter' and 'chapter' statements are used to remove most books' appendices.
for i in top300:
if ((len(model_tokenizer(paragraphs.chunks[int(i[0])])) > 60) &
(paragraphs.loc[int(i[0])].title != rid) &
(paragraphs.loc[int(i[0])].title != rid2) &
(paragraphs.loc[int(i[0])].title != rid3) &
(paragraphs.loc[int(i[0])].title != rid4) &
(paragraphs.loc[int(i[0])].title != rid5) &
(paragraphs.loc[int(i[0])].title != rid6) &
(paragraphs.loc[int(i[0])].title != rid7) &
(paragraphs.loc[int(i[0])].title != rid8) &
(paragraphs.loc[int(i[0])].title != rid9) &
(model_tokenizer(paragraphs.chunks[int(i[0])]).count('letter') < 10) &
(model_tokenizer(paragraphs.chunks[int(i[0])]).count('chapter') < 10) &
('Gutenberg' not in paragraphs.chunks[int(i[0])])):
final_tags.append(i)
for i in top300:
if ((len(model_tokenizer(paragraphs.chunks[int(i[0])])) > 60) &
(paragraphs.loc[int(i[0])].title != rid) &
(paragraphs.loc[int(i[0])].title != rid2) &
(paragraphs.loc[int(i[0])].title != rid3) &
(paragraphs.loc[int(i[0])].title != rid4) &
(paragraphs.loc[int(i[0])].title != rid5) &
(paragraphs.loc[int(i[0])].title != rid6) &
(paragraphs.loc[int(i[0])].title != rid7) &
(paragraphs.loc[int(i[0])].title != rid8) &
(paragraphs.loc[int(i[0])].title != rid9) &
(paragraphs.loc[int(i[0])].title != quoted_novel) &
(model_tokenizer(paragraphs.chunks[int(i[0])]).count('letter') < 10) &
(model_tokenizer(paragraphs.chunks[int(i[0])]).count('chapter') < 10) &
('Gutenberg' not in paragraphs.chunks[int(i[0])])):
final_tags_other_novels.append(i)
rank = ['FIRST', 'SECOND', 'THIRD', 'FOURTH', 'FIFTH']
index = 0
# The following two for-loops simply run the semantic search with or without the possibly-quoted novel,
# since otherwise the results will be dominated by paragraphs within the same novel.
if include_quoted_novel == True:
st.write(
' SEARCH RESULTS (INCLUDING THE QUOTED NOVEL) ',
'\n')
for i in final_tags[:5]:
st.write('______________________________________________________________________________________')
st.write(index + 1)
rank_i = rank[index]
cosine_i = round(i[1], 4)
tag_i = int(i[0])
para_from_id(tag_i, rank_i, cosine_i)
index += 1
st.write('\n')
if include_quoted_novel == False:
st.write(
' SEARCH RESULTS (*NOT* INCLUDING THE QUOTED NOVEL) ',
'\n')
for i in final_tags_other_novels[:5]:
st.write('______________________________________________________________________________________')
st.write(index + 1)
rank_i = rank[index]
cosine_i = round(i[1], 4)
tag_i = int(i[0])
para_from_id(tag_i, rank_i, cosine_i)
index += 1
st.write('\n')