Public feed
#893967
#!/usr/bin/python # -*- coding: UTF-8 -*- import redef NumberOfOccurencesOfWordInText(word,text): """Returns the nb. of occurences of whole word(s) (case insensitive) in a text""" #Basic attempt, does not pass all the tests: you have to improve it. #return text.lower().count(word.lower()) unformated = re.compile('\s+', re.I | re.M) text_unformated = unformated.sub(' ', text.lower()) regex_to_match = re.compile('\W'+word.lower()+'\W', re.I | re.M | re.U) nb = len( regex_to_match.findall(text_unformated) ) regex_to_match = re.compile('\W'+word.lower()+'\Z', re.I | re.M | re.U) nb += len( regex_to_match.findall(text_unformated) ) regex_to_match = re.compile('\A'+word.lower()+'\W', re.I | re.M | re.U) nb += len( regex_to_match.findall(text_unformated) ) print text_unformated print nb return nb