-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathhelpers.py
More file actions
74 lines (51 loc) · 1.91 KB
/
Copy pathhelpers.py
File metadata and controls
74 lines (51 loc) · 1.91 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
68
69
70
71
72
73
74
import nltk
import re
import os
import pickle
import sys
from nltk.corpus import stopwords
def assert_dir(path):
if not os.path.exists(path):
print('ERROR: {} does not exists'.format(path))
sys.exit(1)
if not os.path.isdir(path):
print('ERROR: {} is not a directory'.format(path))
sys.exit(1)
def preprocess_text(text):
processed_text = text.lower()
processed_text = processed_text.replace("’", "'")
processed_text = processed_text.replace("“", '"')
processed_text = processed_text.replace("”", '"')
non_words = re.compile(r"[^A-Za-z']+")
processed_text = re.sub(non_words, ' ', processed_text)
return processed_text
def get_text_from_file(filename):
with open(filename, encoding='cp1252', mode='r') as f:
text = f.read()
return text
def get_words_from_text(text):
stop_words = set(stopwords.words('english'))
processed_text = preprocess_text(text)
words = {w for w in processed_text.split() if w not in stop_words}
return words
def build_inverted_index(docs_path):
inverted_index = {}
for doc_file in os.listdir(docs_path):
filename = os.path.join(docs_path, doc_file)
text = get_text_from_file(filename)
words = get_words_from_text(text)
for word in words:
if inverted_index.get(word, None) is None:
inverted_index[word] = {filename}
else:
inverted_index[word].add(filename)
return inverted_index
def index(docs_path, data_path):
inverted_index = build_inverted_index(docs_path)
dic_file = os.path.join(data_path, 'dictionary.txt')
inverted_index_file = os.path.join(data_path, 'inverted_index.pickle')
with open(dic_file, mode='w') as f:
for word in inverted_index.keys():
f.write(word + '\n')
with open(inverted_index_file, mode='wb') as f:
pickle.dump(inverted_index, f)