-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils_asgn.py
68 lines (58 loc) · 1.61 KB
/
utils_asgn.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
def search(sen=None,
con_list=None):
'''
wrapper for seacrhing a list of concepts in a sentence
Args:
sen: a Sentence object
con_list : a list of Concept objects
Returns:
list of concepts found in the Setence object.
Returns an empty list, if no concepts are found
'''
if con_list is not None:
con_in_sen = filter(lambda concept: search_(sen,concept),con_list)
return con_in_sen
else:
return []
def search_(sen=None,
concept=None):
'''
wrapper for search a concept-string in a sen-string
This is the place to incorporate new search algorithms
'''
sen = sen.get_str()
concept_pr = concept.get_str()
T = concept.BMHsearch_Table
return BMHSearch(text=sen,
pattern=concept_pr,
T=T)
def BruteForce(text=None,
pattern=None):
'''
Brute-force implemenation using python string comparison
'''
if pattern in text:
return True
else:
return False
def BMHSearch(text=None,
pattern=None,
T=None):
'''
The code has been developed based on the pseudocode on Wikipedia page for
Boyer Moore Horspool algorithm
'''
skip = 0
len_text = len(text)
len_pattern = len(pattern)
while len_text - skip >= len_pattern:
i = len_pattern - 1
while text[skip+i] == pattern[i]:
if i == 0:
return True
i = i - 1
skip += T[ord(text[skip + len_pattern - 1])]
return False
def BMSearch(sen=None,
con=None):
return NotImplementedError()