Skip to content

Instantly share code, notes, and snippets.

@andy-williams
Created August 19, 2016 16:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andy-williams/809a79e85f6d1a35e904d0a18ce1bed0 to your computer and use it in GitHub Desktop.
Save andy-williams/809a79e85f6d1a35e904d0a18ce1bed0 to your computer and use it in GitHub Desktop.
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