-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHash.py
More file actions
137 lines (115 loc) · 2.87 KB
/
Hash.py
File metadata and controls
137 lines (115 loc) · 2.87 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
131
132
133
134
135
136
137
import string
class HashTable:
def __init__(self):
self.hash_array = [HashNode(i) for i in range(10000)]
def get_hash(self, word):
result = 0
for c in word:
result = (result * 131 + ord(c)) % 10000
return result
def find(self, key):
key = key.translate(None, string.punctuation).lower()
key_node = self.search(key)
if key_node:
return key_node.count
else:
return -1
def search(self, key):
# key = key.translate(None, string.punctuation).lower()
key_hash = self.get_hash(key)
hash_node = self.hash_array[key_hash]
key_node = None
# search if exists
while hash_node:
if hash_node.word == key:
key_node = hash_node
break
hash_node = hash_node.next
return key_node
def insert(self, key, value = -1, pos = -1):
key = key.translate(None, string.punctuation).lower()
key_node = self.search(key)
key_hash = self.get_hash(key)
if key_node:
if value == -1:
key_node.increase_count(pos)
else:
key_node.count = value
else:
temp_next = self.hash_array[key_hash].next
a = HashNode(key, pos)
self.hash_array[key_hash].next = a
if value != -1:
a.count = value
self.hash_array[key_hash].next.next = temp_next
def delete(self,key):
key = key.translate(None, string.punctuation).lower()
key_hash = self.get_hash(key)
hash_node = self.hash_array[key_hash]
prev_node = hash_node
main_node = hash_node.next
# search if exists
while main_node:
if main_node.word == key:
break
prev_node = main_node
main_node = main_node.next
if main_node:
prev_node.next = main_node.next
else:
print "Key Error"
def increase(self, key):
key = key.translate(None, string.punctuation).lower()
key_node = self.search(key)
if key_node:
key_node.increase_count()
else:
print "Key Error"
def list_keys(self):
key_list = []
for i in range(10000):
temp = self.hash_array[i].next
if temp != None:
while temp:
key_list.append(temp.word)
temp = temp.next
return key_list
def print_hash(self):
for i in range(10000):
temp = self.hash_array[i].next
if temp != None:
while temp:
print "||", temp.word, ">" , str(temp.count), ">", str(temp.positions), "||\t\t",
temp = temp.next
print
class HashNode:
def __init__(self, word, pos = -1):
self.word = word
self.count = 1
self.next = None
self.positions = []
if pos != -1:
self.positions.append(pos)
def increase_count(self, pos = -1):
self.count += 1
if pos != -1:
self.positions.append(pos)
H = HashTable()
if __name__ == '__main__':
wc = 0
with open('testFile.txt', 'r') as f:
for line in f:
B = line.split()
for i in B:
wc += 1
H.insert(i, -1, wc)
# print H.find('soon')
H.insert('random', 10)
H.increase('random')
print H.find('random')
H.delete('random')
print H.find('random')
print H.find('three')
H.increase('three')
print H.find('three')
# H.print_hash()