forked from AustinGhub/Search-Engine-text-files-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindexing.py
37 lines (26 loc) · 884 Bytes
/
indexing.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
import collections,re,array,struct,csv,math
import pathlib
hit = collections.namedtuple("Hit", "offsets")
def wordFind(texts):
return re.findall(r"\w+", texts.lower())
def indexer():
documents = []
index = collections.defaultdict(list)
terms = {}
words = []
dir = 'large-sample'
dir = pathlib.Path(dir)
tinydir = dir/".tiny"
tinydir.mkdir(exist_ok=True)
for path in dir.glob("**/*.txt"):
text = path.read_text(encoding = "utf-8", errors = "replace")
doc_words = wordFind(text) #holds the value of the words
#print(doc_words)
words.append(doc_words)
csv_writing(words)
def csv_writing(wordList: list):
with open("terms.csv", "w") as csvfile:
writer = csv.writer(csvfile)
for word in wordList:
writer.writerow(word)
indexer()