#-------------------------------------------------------------------------------
# Name: wordtools.py
# Purpose:
#
# Author: penglaixy
#
# Created: 17/08/2013
# Copyright: (c) penglaixy 2013
# Licence: <your licence>
#-------------------------------------------------------------------------------
import string
def cleanword(str):
new_str = ''
for chr in str:
if chr not in string.punctuation:
new_str += chr
return new_str
def has_dashdash(str):
return "--" in str
def extract_words(str):
str = str.lower()
new_str = ''
before = 'a'
for ch in str:
if ch in string.punctuation or ch == " ":
if before == " ":
continue
else:
new_str += " "
before = " "
else:
new_str += ch
before = ch
return new_str.split(" ")
pass
def wordcount(word, list_in):
count = 0
for i in list_in:
if word == i:
count += 1
return count
def wordset(list_in):
tmp_list = []
for i in list_in:
if i not in tmp_list:
tmp_list.append(i)
tmp_list.sort()
return tmp_list
def longestword(list_in):
max_len = 0
for i in list_in:
if max_len < len(i):
max_len = len(i)
return max_len
def main():
pass
if __name__ == '__main__':
main()
#-------------------------------------------------------------------------------
# Name: test.py
# Purpose:
#
# Author: penglaixy
#
# Created: 11/08/2013
# Copyright: (c) penglaixy 2013
# Licence: <your licence>
#-------------------------------------------------------------------------------
import sys
from wordtools import *
def test(did_pass):
'''''
print the result of a test
'''
linenum = sys._getframe(1).f_lineno
if did_pass:
msg = 'Test at line {0} ok'.format(linenum)
else:
msg = 'Test at line {0} failed'.format(linenum)
print msg
def myreplace(old, new, s):
'''
replace all occurrences of old with new in s.
'''
tmp_list = s.split(old)
new_list = []
for i in tmp_list:
if i == '':
continue
new_list.append(i)
return new.join(new_list)
def test_suite():
'''''
Run the suite of tests for code in this module
'''
test(myreplace(",", ";", "this, that, and some other thing") ==
"this; that; and some other thing")
test(myreplace(" ","**", "Words will now be separated by stars.")==
"Words**will**now**be**separated**by**stars.")
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(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(wordset(["I","a",'a','is','a','is','I','am']) ==
['I','a','am','is'])
test(wordset(["or",'a','am','is','are','be','but','am']) ==
['a','am','are','be','but','is','or'])
test(longestword(['a','apple','pear','grape']) == 5)
test(longestword(['a','am','I','be']) == 2)
test(longestword(['this','supercalifragilisticexpialidocious']) == 34)
test(longestword([ ]) == 0)
#print extract_words("Now is the time! 'now', is the time? Yes, now")
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"])
pass
def main():
test_suite()
if __name__ == '__main__':
main()