from modules.test import * from modules.searches import * import time ''' friends = ["Joe", "Zoe", "Brad", "Angelina", "Zuki", "Thandi", "Paris"] test(search_linear(friends, "Zoe") == 1) test(search_linear(friends, "Joe") == 0) test(search_linear(friends, "Paris") == 6) test(search_linear(friends, "Bill") == -1) vocab = ["apple", "boy", "dog", "down", "fell", "girl", "grass", "the", "tree"] book_words = "the apple fell from the tree to the grass".split() test(find_unknown_words(vocab, book_words) == ["from", "to"]) test(find_unknown_words([], book_words) == book_words) test(find_unknown_words(vocab, ["the", "boy", "fell"]) == []) test(text_to_words("My name is Earl!") == ["my", "name", "is", "earl"]) test(text_to_words('"Well, I never!", said Alice.') == ["well", "i", "never", "said", "alice"]) ''' bigger_vocab = load_words_from_file("vocab.txt") print("There are {0} words in the vocab, starting with\n {1} ".format( len(bigger_vocab), bigger_vocab[:6])) book_words = get_words_in_book("alice_in_wonderland.txt") print("There are {0} words in the book, the first 100 are \n{1}".format( len(book_words), book_words[:100])) t0 = time.clock()
# model = modules.attn_model() # model = modules.lstm_model() # 3. TRAINING THE MODEL # Shuffling the training dataset feat_arr_train, labels_train = shuffle(feat_arr_train, labels_train, random_state=0) es = EarlyStopping(monitor='val_loss', mode='min', verbose=1, patience=15) mc = ModelCheckpoint('best_model_wts.h5', monitor='val_loss', mode='min', save_best_only=True, save_weights=True, verbose=1) history = model.fit(np.array(feat_arr_train), labels_train.astype(float), validation_data=(np.array(feat_arr_val), labels_val.astype(float)), epochs=200, batch_size=32, callbacks=[es, mc]) model.load_weights('best_model_wts.h5') modules.test(model, feat_arr_test_male, feat_arr_test_female, labels_test_male[:, 1], labels_test_male[:, 0], labels_test_female[:, 1], labels_test_female[:, 0])
import sys sys.path.insert(0, './') # 当前文件直接用import import modules modules.test() # 引入其他文件夹,需要在文件夹中创建一个空的 __init__.py 文件 from subModules import useFaterModule useFaterModule.test3Func() # 引入其他文件夹所有文件,在__init__.py中这么编写 # from . import modules2 # from . import test3 import subModules subModules.modules2.test2() # 引入上级, 需要先用 sys.path.insert(0, './') # 设置根目录为项目索引第一项,然后就可正常引入了 # 设置了sys.path.insert(0, './')之后就可以很方便的引入各级目录 # 每个文件夹内都要有__init__.py 并且都 from . import xxx 了文件 import modules_fater modules_fater.modules_fater_file.testFaterModules() from subModules import subsub subsub.subsub_file.testSubSubFunc() import secondModule secondModule.secondModule_file.testSecondModuleFunc()
return ix ix += 1 return -1 def count_letter(text, ch): count = 0 for i in text: if i == ch: count += 1 return(count) def remove_punctuation(s): s_sans_punct = "" for letter in s: if letter not in string.punctuation: s_sans_punct += letter return s_sans_punct test(remove_vowels("compsci") == "cmpsc") test(remove_vowels("aAbEefIijOopUus") == "bfjps") test(count_letter("banana", "a") == 3) ss = "Python strings have some interesting methods." test(find(ss, "s") == 7) test(find(ss, "s", 7) == 7) test(find(ss, "s", 8) == 13) test(find(ss, "s", 8, 13) == -1) test(find(ss, ".") == len(ss)-1) test(remove_punctuation('"Well, I never did!", said Alice.') == "Well I never did said Alice") test(remove_punctuation("Are you very, very, sure?") == "Are you very very sure")
return total def recur_max(nested_list): """ Find the maximum in a recursive structure of lists within other lists. Precondition: No lists or sublists are empty. """ largest = None first_time = True for element in nested_list: if type(element) == type([]): value = recur_max(element) else: value = element if first_time or value > largest: largest = value first_time = False return largest total = recur_sum([1, 2, [11, 13], 8]) print("Total: {0}".format(total)) test(recur_max([2, 9, [1, 13], 8, 6]) == 13) test(recur_max([2, [[100, 7], 90], [1, 13], 8, 6]) == 100) test(recur_max([[[13, 7], 90], 2, [1, 100], 8, 6]) == 100) test(recur_max(["joe", ["sam", "ben"]]) == "sam")
from modules.test import * from modules.searches import * import time ''' xs = [2,3,5,7,11,13,17,23,29,31,37,43,47,53] test(search_binary(xs, 20) == -1) test(search_binary(xs, 99) == -1) test(search_binary(xs, 1) == -1) for (i, v) in enumerate(xs): test(search_binary(xs, v) == i) ''' bigger_vocab = load_words_from_file("vocab.txt") print("There are {0} words in the vocab, starting with\n {1} " .format(len(bigger_vocab), bigger_vocab[:6])) book_words = get_words_in_book("alice_in_wonderland.txt") print("There are {0} words in the book, the first 100 are \n{1}". format(len(book_words), book_words[:100])) t0 = time.clock() missing_words = find_unknown_words(bigger_vocab, book_words) t1 = time.clock() print("There are {0} unknown words.".format(len(missing_words))) print("That took {0:.4f} seconds.".format(t1-t0))
def count_letter(text, ch): count = 0 for i in text: if i == ch: count += 1 return (count) def remove_punctuation(s): s_sans_punct = "" for letter in s: if letter not in string.punctuation: s_sans_punct += letter return s_sans_punct test(remove_vowels("compsci") == "cmpsc") test(remove_vowels("aAbEefIijOopUus") == "bfjps") test(count_letter("banana", "a") == 3) ss = "Python strings have some interesting methods." test(find(ss, "s") == 7) test(find(ss, "s", 7) == 7) test(find(ss, "s", 8) == 13) test(find(ss, "s", 8, 13) == -1) test(find(ss, ".") == len(ss) - 1) test( remove_punctuation('"Well, I never did!", said Alice.') == "Well I never did said Alice") test( remove_punctuation("Are you very, very, sure?") == "Are you very very sure")
def recur_max(nested_list): """ Find the maximum in a recursive structure of lists within other lists. Precondition: No lists or sublists are empty. """ largest = None first_time = True for element in nested_list: if type(element) == type([]): value = recur_max(element) else: value = element if first_time or value > largest: largest = value first_time = False return largest total = recur_sum([1, 2, [11, 13], 8]) print("Total: {0}".format(total)) test(recur_max([2, 9, [1, 13], 8, 6]) == 13) test(recur_max([2, [[100, 7], 90], [1, 13], 8, 6]) == 100) test(recur_max([[[13, 7], 90], 2, [1, 100], 8, 6]) == 100) test(recur_max(["joe", ["sam", "ben"]]) == "sam")
from modules.test import * from modules.searches import * test(remove_adjacent_dups([1,2,3,3,3,3,5,6,9,9]) == [1,2,3,5,6,9]) test(remove_adjacent_dups([]) == []) test(remove_adjacent_dups(["a", "big", "big", "bite", "dog"]) == ["a", "big", "bite", "dog"]) all_words = get_words_in_book("alice_in_wonderland.txt") all_words.sort() book_words = remove_adjacent_dups(all_words) print("There are {0} words in the book. Only {1} are unique." .format(len(all_words), len(book_words))) print("The first 100 words are \n{0}".format(book_words[:100]))