This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| model = Sequential() | |
| model.add(LSTM(units=32, recurrent_dropout=0.5, input_shape=(window, len(int_to_char)))) | |
| model.add(Dense(len(int_to_char), activation='softmax')) | |
| model.compile(loss='categorical_crossentropy',optimizer='adam') | |
| model.fit(X, Y, batch_size=1024, epochs=10 ,steps_per_epoch=3000) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def generate(base_name): | |
| if len(base_name): | |
| base_name = base_name[:window] | |
| x = np.zeros((1,window,len(int_to_char))) | |
| seq_word = [] | |
| ind_list = [char_to_int[i] for i in base_name] | |
| for i,ind in enumerate(ind_list): | |
| x[0 ,i ,ind] = 1 | |
| seq_word.append(ind) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| text_count = 0 | |
| input_text = 'PRIYA' | |
| while text_count < 50: | |
| gen_text = generate(input_text) | |
| gen_text = gen_text.strip('.') | |
| if len(gen_text) > 6 and gen_text not in names: | |
| text_count += 1 | |
| input_text = gen_text | |
| print(gen_text) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import requests | |
| from bs4 import BeautifulSoup | |
| import re | |
| import os | |
| import time | |
| home_url = 'http://www.wiseoldsayings.com' | |
| r = requests.get(home_url) | |
| soup = BeautifulSoup(r.content, 'html5lib') | |
| quote_classes = [] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def get_quotes(url): | |
| i = 1 | |
| quotes = [] | |
| while True: | |
| curr_quotes = [] | |
| quote_url = url + 'page-' + str(i) + '/' | |
| i += 1 | |
| quote_r = requests.get(quote_url) | |
| quote_soup = BeautifulSoup(quote_r.content, 'html5lib') | |
| quote_list = quote_soup.find('div', attrs = {'class':'quote_list'}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| quotes = [] | |
| max_len = 0 | |
| min_len = 5 | |
| sent_len_dic = defaultdict(int) | |
| with open('quotes.txt', 'r') as f: | |
| while True: | |
| quote = f.readline() | |
| if not quote: | |
| break | |
| words = quote.split(' ') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| words_dict = dict() | |
| def unique_words(data_list): | |
| for x in data_list: | |
| for word in x.split(' '): | |
| words_dict[word] = 1 | |
| return words_dict.keys() | |
| words = list(unique_words(quotes)) | |
| words.sort() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| co_occ_matrix = defaultdict(int) | |
| window = 5 # not greater than 5 | |
| for sent in quotes: | |
| words = sent.split(' ') | |
| # first window | |
| for i in range(0, window): | |
| for j in range(i+1, window): | |
| weight = 1/(j-i) | |
| ind_1 = word_to_int[words[i]] | |
| ind_2 = word_to_int[words[j]] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| tot_pairs = len(co_occ_matrix) | |
| p1, p2,occs = list(), list(), list() | |
| for i in co_occ_matrix.keys(): | |
| p1.append(i[0]) | |
| p2.append(i[1]) | |
| occs.append(co_occ_matrix[i]) | |
| p1 = np.array(p1) | |
| p2 = np.array(p2) | |
| occs = np.array(occs) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def get_batch(batch_size): | |
| ind = np.random.permutation(occs.size).tolist() | |
| i = 0 | |
| for i in range(0, tot_pairs, batch_size): | |
| batch_ids = ind[i:i+batch_size] | |
| yield p1[batch_ids], p2[batch_ids], occs[batch_ids] | |
| device = None | |
| if torch.cuda.is_available(): | |
| device = torch.device("cuda:0") |