-
Notifications
You must be signed in to change notification settings - Fork 0
/
text_search.py
54 lines (46 loc) · 1.49 KB
/
text_search.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
import re
def read_text_file(text_file):
lines = file(text_file).readlines()
return [line[:-1] for line in lines]
def index_text(text_lines):
index = {}
i = 0
for line in text_lines:
words = re.split('[ .:)(?!-\'\"]+',line)
for word in words:
if word != '':
index[word] = index.get(word, []) + [i]
i += 1
return index
def print_rows(word,text_index,text_lines):
print ''
i = 0
last_line_id = -1
for line_id in text_index.get(word, []):
line = text_lines[line_id]
if not line_id == last_line_id:
print ' [Line:' + (5 - len(str(line_id + 1))) * ' ' + str(line_id + 1) + '] ' + line
print 16 * ' ' + build_pointers(word,line)
i += 1
last_line_id = line_id
if i>0:
print ' "' + word + '" was found ' + str(i) + ' times.\n'
else:
print ' Nothing found for "' + word + '".\n'
def build_pointers(word,str):
pointers = ''
while len(str) > 0:
offset = str.find(word)
if not offset == -1:
pointers = pointers + offset * ' ' + '^'
str = str[offset + 1:]
else:
return pointers
return pointers
def find_word(text_index,text_lines):
print_rows(raw_input('\nSearch for: '),text_index,text_lines)
find_word(text_index,text_lines)
f = str(raw_input('\nFilename: '))
text_lines = read_text_file(f)
text_index = index_text(text_lines)
find_word(text_index,text_lines)