def test_suite(): #Few tests of "add_vectors" function test(wdt.cleanword("what?") == "what") test(wdt.cleanword("'now!'") == "now") test(wdt.cleanword("?+='w-o-r-d!,@$()'") == "word") test(wdt.has_dashdash("distance--but")) test(not wdt.has_dashdash("several")) test(wdt.has_dashdash("spoke--")) test(wdt.has_dashdash("distance--but")) test(not wdt.has_dashdash("-yo-yo-")) test(wdt.extract_words("Now is the time! 'Now', is the time? Yes, now.") == ['now','is','the','time','now','is','the','time','yes','now']) test(wdt.extract_words("she tried to curtsey as she spoke--fancy") == ['she','tried','to','curtsey','as','she','spoke','fancy']) test(wdt.wordcount("now", ["now","is","time","is","now","is","is"]) == 2) test(wdt.wordcount("is", ["now","is","time","is","now","the","is"]) == 3) test(wdt.wordcount("time", ["now","is","time","is","now","is","is"]) == 1) test(wdt.wordcount("frog", ["now","is","time","is","now","is","is"]) == 0) test(wdt.wordset(["now", "is", "time", "is", "now", "is", "is"]) == ["is", "now", "time"]) test(wdt.wordset(["I", "a", "a", "is", "a", "is", "I", "am"]) == ["I", "a", "am", "is"]) test(wdt.wordset(["or", "a", "am", "is", "are", "be", "but", "am"]) == ["a", "am", "are", "be", "but", "is", "or"]) test(wdt.longestword(["a", "apple", "pear", "grape"]) == 5) test(wdt.longestword(["a", "am", "I", "be"]) == 2) test(wdt.longestword(["this","supercalifragilisticexpialidocious"]) == 34) test(wdt.longestword([ ]) == 0)
#!/usr/bin/env python """Program to compute number of times each word occurs in Alice in Wonderland""" import wordtools infile = open('alice_in_wonderland.txt', 'r') outfile = open('alice_words.txt', 'w') word_list = {} text = infile.read().split() for word in text: word = wordtools.cleanword(word).lower() word_list[word] = word_list.get(word, 0) + 1 print("%-18s%s")%('Word', 'Count') print("=======================") most_frequent = 'a' for word in sorted(word_list.keys()): print("%-18s%i")%(word, word_list[word]) if word_list[word] > word_list[most_frequent]: most_frequent = word infile.close() outfile.close() print "Most frequent word is %s, it appeared %i times" % (most_frequent, word_list[most_frequent]) print "The word 'alice' appeared %i times" % word_list['alice']
import wordtools from wordtools import test from wordtools import cleanword from wordtools import has_dashdash from wordtools import extract_words from wordtools import wordcount from wordtools import wordset from wordtools import longestword test(cleanword("what?") == "what") test(cleanword("'now!'") == "now") test(cleanword("?+='w-o-r-d!,@$()'") == "word") test(has_dashdash("distance--but")) test(not has_dashdash("several")) test(has_dashdash("spoke--")) test(has_dashdash("distance--but")) test(not has_dashdash("-yo-yo-")) test( extract_words("Now is the time! 'Now', is the time? Yes, now.") == ['now', 'is', 'the', 'time', 'now', 'is', 'the', 'time', 'yes', 'now']) test( extract_words("she tried to curtsey as she spoke--fancy") == ['she', 'tried', 'to', 'curtsey', 'as', 'she', 'spoke', 'fancy']) test(wordcount("now", ["now", "is", "time", "is", "now", "is", "is"]) == 2) test(wordcount("is", ["now", "is", "time", "is", "now", "the", "is"]) == 3) test(wordcount("time", ["now", "is", "time", "is", "now", "is", "is"]) == 1) test(wordcount("frog", ["now", "is", "time", "is", "now", "is", "is"]) == 0) test( wordset(["now", "is", "time", "is", "now", "is", "is"]) == ["is", "now", "time"]) test(
import wordtools print("test(wordtools.cleanword(\"what?\") == \"what\"") wordtools.test(wordtools.cleanword("what?"), "what") print("test(wordtools.cleanword(\"'now!'\") == \"now\"") wordtools.test(wordtools.cleanword("'now!'"), "now") print("test(cleanword(\"?+='w-o-r-d!,@$()'\") == \"word\"") wordtools.test(wordtools.cleanword("?+='w-o-r-d!,@$()'"), "word") print("test(has_dashdash(\"distance--but\")") wordtools.test(wordtools.has_dashdash("distance--but"), 1) print("test(not has_dashdash(\"several\")") wordtools.test(wordtools.has_dashdash("spoke--"), 1) print("test(has_dashdash(\"distance--but\")") wordtools.test(wordtools.has_dashdash("distance--but"), 1) print("test(not has_dashdash(\"-yo-yo-\")") wordtools.test(wordtools.has_dashdash("-yo-yo-"), 0) print( "test(extract_words(\"Now is the time! 'Now', is the time? Yes, now.\") == \['now','is','the','time','now','is','the','time','yes','now'\]" ) wordtools.test( wordtools.extract_words("Now is the time! 'Now', is the time? Yes, now."), ['now', 'is', 'the', 'time', 'now', 'is', 'the', 'time', 'yes', 'now']) print(
def test_cleanword(): assert cleanword("what?") == "what" assert cleanword("'now!'") == "now" assert cleanword("?+='w-o-r-d!,@$()'") == "word"