Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
class TermFinder:
def find_terms(to_search, terms):
resultDict = {}
for term in terms:
resultDict[term] = TermFinder._find_term(to_search, term)
return resultDict
def _find_term(to_search, term):
initial = 0
result = []
while True:
initial = to_search.lower().find(term.lower(), initial)
if initial == -1:
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