-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsoundex.py
More file actions
130 lines (105 loc) · 3.17 KB
/
soundex.py
File metadata and controls
130 lines (105 loc) · 3.17 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
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
import os
import re
from collections import defaultdict
dir = r"C:\Users\VEDANK\Desktop\Files"
count = 0
files = []
allwords = []
for r, d, f in os.walk(dir):
for file in f:
files.append(os.path.join(r, file))
count += 1
print(count)
index = dict()
for f in files:
file = open(f, 'r')
text = file.read().lower()
words = text.split()
for x in words:
allwords.append(x)
allwords = list(dict.fromkeys(allwords))
for key in allwords:
postList = []
for f in files:
file = open(f, 'r')
text = file.read().lower()
# found = re.search(' ' + key + ' ', ' ' + text + ' ')
if(f' {key} ' in f' {text} '):
postList.append(os.path.basename(f))
index[key] = postList
for x, y in index.items():
print(x, ' ', y)
with open(r'C:\Users\VEDANK\Desktop\Output\Index1.txt', 'w') as f:
print(index, file=f)
soundex = defaultdict(list)
letters = 'abcdefghijklmnopqrstuvwxyz.'
codes = '012301200224550126230102020'
codeDict = dict(zip(letters, codes))
def soundexCode(word):
result = word[0]
word = word[1:]
word = re.sub(r'[0-9]+', '', word)
prev_code=""
for char in word:
code = codeDict[char]
if code != '0' and code != prev_code:
result += code
prev_code = code
return (result + "0000")[:4]
for term in allwords:
word = soundexCode(term)
soundex[word].append(term)
for x, y in soundex.items():
print(x, ' ', y)
with open(r'C:\Users\VEDANK\Desktop\Output\Soundex.txt', 'w') as f:
print(index, file=f)
query = input("Enter query: ")
query = query.split()
docs=[]
for term in query:
word = soundexCode(term)
print(word)
if word in soundex:
wordList = soundex[word]
print(wordList)
for i in wordList:
if i in index:
tempList = index[i]
for j in tempList:
docs.append(j)
docs=list(dict.fromkeys(docs))
print("---------------")
print(docs)
'''
for term in allwords:
replaceWord = term[0]
for i in range(1,len(term)):
if term[i] in ['a','e','i','o','u']:
replaceWord = replaceWord + '0'
if term[i] in ['b','f','p','v']:
replaceWord = replaceWord + '1'
if term[i] in ['c','g','j','k','q','s','x','z']:
replaceWord = replaceWord + '2'
if term[i] in ['d','t']:
replaceWord = replaceWord + '3'
if term[i] in ['l']:
replaceWord = replaceWord + '4'
if term[i] in ['m', 'n']:
replaceWord = replaceWord + '5'
if term[i] in ['r']:
replaceWord = replaceWord + '6'
stripSpace=replaceWord[0]
for i in range(1,len(replaceWord)):
try:
if replaceWord[i]==replaceWord[i-1]:
pass
else:
stripSpace = stripSpace + replaceWord[i]
except:
pass
stripZeros=stripSpace[0]
for i in range(1,len(stripSpace)):
if stripSpace[i]!='0':
stripZeros = stripZeros + stripSpace[i]
print(stripZeros)
'''