-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwordmap.py
73 lines (45 loc) · 1.22 KB
/
wordmap.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import sys
#delimiter list
delimiters = " ","\n",",",".","?","!"
# Sanitizing string
def split(delimiters, string, maxsplit=0):
import re
regex = '|'.join(map(re.escape, delimiters))
return re.split(regex, string, maxsplit)
class bloom:
def __init__(self, wordlist ):
self.wordlist = wordlist
self.dictionary = {}
self.sieve = [False]*26
# Creating the dictionary
for (i,letter) in enumerate( 'abcdefghigklmnopqrstuvwxyz'):
self.dictionary[letter] = i
self.createFilter()
def createFilter( self ):
dictionary = self.dictionary
words = self.wordlist
for word in words:
for letter in word:
index = dictionary[letter]
if self.sieve[index] == False:
self.sieve[index] = True
def validates( self, word ):
for letter in word:
index = self.dictionary[letter]
if self.sieve[index] == False:
return False
return True
def debugme( self ):
print self.wordlist, self.sieve
if __name__ == '__main__':
data = sys.stdin.readlines()
words = split(delimiters, data[0])
myfilter = bloom( words )
testwords = split(delimiters, data[1])
count = 0
for word in testwords:
nword = word.lower()
if myfilter.validates( nword ):
count +=1
print '\n'
print count