-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget_book_content.py
More file actions
67 lines (51 loc) · 2.12 KB
/
get_book_content.py
File metadata and controls
67 lines (51 loc) · 2.12 KB
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
from rapidfuzz import fuzz
import re
with open("book.txt", "r", encoding="utf-8") as file:
book_text = file.read()
book_in_words = re.findall(r'\S+', book_text)
book_text_words = " ".join(book_in_words).lower()
def get_index(snippet, hint_position=None):
match_window_index = None
match_index = None
top_score = 0
print("finding location")
snippet = snippet.lower()
snippet_words = re.findall(r'\S+', snippet)
snippet_text = " ".join(snippet_words)
snippet_len = len(snippet_words)
step = max(1, snippet_len)
text_window = max(3, snippet_len * 3)
for i in range(0, len(book_in_words) - text_window + 1, step):
window = book_in_words[i:i + text_window]
book_text_chunk = " ".join(window).lower()
score = fuzz.partial_ratio(snippet_text, book_text_chunk)
if hint_position is not None:
distance_penalty = abs(i - hint_position) / len(book_in_words)
score *= (1 - 0.5 * distance_penalty)
if score > top_score:
top_score = score
match_window_index = i
if match_window_index is not None:
top_score = 0
best_chunk = book_in_words[match_window_index:match_window_index + text_window]
fine_step = 1
fine_window = len(snippet_words)
for i in range(0, len(best_chunk) - fine_window, fine_step):
best_chunk_chunk = " ".join(best_chunk[i:i + fine_window])
score = fuzz.partial_ratio(snippet_text, best_chunk_chunk)
if score > top_score:
top_score = score
match_index = i
if match_index is not None:
match_index = match_window_index + match_index
else:
match_index = match_window_index
return match_index if match_index is not None else 0
def get_context(word_index, context_size):
print("getting context")
print(len(book_in_words))
context_start = max(word_index, 0)
context_end = min(word_index + context_size, len(book_in_words))
context_split_words = book_in_words[context_start:context_end]
context = ' '.join(context_split_words)
return context