Created
August 19, 2016 16:22
-
-
Save andy-williams/809a79e85f6d1a35e904d0a18ce1bed0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class TermFinder: | |
@staticmethod | |
def find_terms(to_search, terms): | |
resultDict = {} | |
for term in terms: | |
resultDict[term] = TermFinder._find_term(to_search, term) | |
return resultDict | |
@staticmethod | |
def _find_term(to_search, term): | |
initial = 0 | |
result = [] | |
while True: | |
initial = to_search.lower().find(term.lower(), initial) | |
if initial == -1: | |
break | |
result.append(initial) | |
initial += len(term) | |
return result | |
def execute(*args): | |
print args | |
if __name__ == "__main__": | |
toSearch = """Hi, C#, c#, sql, SQL, Database, C# etc | |
Wait, there's more, C#, SQL, C# and maybe some Python too. | |
""" | |
result = TermFinder.find_terms(toSearch, ['C#', 'SQL', 'Python']) | |
for key in result.iterkeys(): | |
arr = result.get(key) | |
print 'term: {0}'.format(key) | |
print 'number of occurances: {0}'.format(len(arr)) | |
for startIndex in arr: | |
print 'start: {0}, end: {1}'.format(startIndex, startIndex + len(key)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment