Public feed
#894009
#!/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()) W = '[^\'a-zA-Z0-9]' unformated = re.compile('\s+|(\'\'+)', re.I | re.M) text_unformated = unformated.sub(' ', text.lower()) #t = text.lower() #reg = '('+W+t+W+')|('+W+t+'\Z)|(\A'+t+W+')' #regex = re.compile(reg, re.I | re.M | re.U) #nb = len( regex.findall(text_unformated)) 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) ) return nb