def anonymous_letter(L, M): """Takes 2 strings as input, L and M. Returns True if L can be made from words in M, False if it cannot. Case insensitive and punctuation insensitive.""" L = make_all_lowercase(strip_punctuation(L)) # clean them up M = make_all_lowercase(strip_punctuation(M)) L_words = find_word_frequencies(L) M_words = find_word_frequencies(M) if set(L_words.keys()).issubset(set( M_words.keys())): # test if all words in L are in M pass else: return False print L_words.items() for word in L_words.keys( ): # test if all frequencies of words in L are <= frequencies of words in M print word, L_words[word], M_words[word] if L_words[word] <= M_words[word]: print "Yay!" else: print "No!" return False return True
def find_word_frequencies(string): # modified frequent_words """Takes input as a string, returns dict of all words, made lowercase, and their frequencies. Case insensitive. Input will be stripped of common punctuation.""" word_frequencies = {} string = make_all_lowercase(strip_punctuation(string)) # clean up string words = string.split() for word in words: if word_frequencies.get(word) == None: word_frequencies[word] = 1 else: word_frequencies[word] += 1 return word_frequencies
def anonymous_letter_files(L_file, M_file): """Takes 2 filenames as input, which contain text. Returns True if the text in L can be made from words in M, False if it cannot. Case insensitive and punctuation insensitive.""" L_words = find_word_frequencies( make_all_lowercase(strip_punctuation(open(L_file).read()))) M_words = find_word_frequencies( make_all_lowercase(strip_punctuation(open(M_file).read()))) # test if all words in L are in M if set(L_words.keys()).issubset(set(M_words.keys())): pass else: return False # test if all frequencies of words in L are <= frequencies of words in M for word in L_words.keys(): print word, L_words[word], M_words[word] if L_words[word] <= M_words[word]: pass else: return False return True