fork download
  1. import re
  2.  
  3. sentence = "Machine learning and data mining often employ the same methods and overlap significantly."
  4.  
  5. dic = {'MLDM': ['machine learning and data mining'], 'ML': ['machine learning'],
  6. 'DM': ['data mining']}
  7.  
  8. def get_key(val):
  9. for k,v in dic.items():
  10. if m.group().lower() in map(str.lower, v):
  11. return k
  12. return ''
  13.  
  14. # Flatten the lists in values and sort the list by length in descending order
  15. l=sorted([v for x in dic.values() for v in x], key=len, reverse=True)
  16. # Build the alternation based regex with \b to match each item as a whole word
  17. rx=r'\b(?:{})\b'.format("|".join(l))
  18. for m in re.finditer(rx, sentence, re.I): # Search case insensitively
  19. key = get_key(m.group())
  20. if key:
  21. print("{} {}".format(key, m.start()))
  22.  
Success #stdin #stdout 0.01s 27744KB
stdin
Standard input is empty
stdout
MLDM 0