def main(): """ Find pairs of SGB word vectors that differ by +/-d in each component. To do this, iterate through each word, generate the possible candidate matchings, and if they exist, add the pair to a set. """ words = get_words() #words = words[:1000] words = set(get_words()) for d in [1, 2, 3]: tic = timeit.default_timer() # List of string tuples off_by_n = set() # Iterate over every word for iw, word in enumerate(words): # Generate all possible candidate matches # distance +/-d from this word at each # position all_vars = get_all_variations(word, d) for word_var in all_vars: if word_var in words: # Found a new (unordered) pair if word < word_var: left = word right = word_var else: left = word_var right = word off_by_n.add((left, right)) off_by_n = list(off_by_n) off_by_n.sort() toc = timeit.default_timer() for o in off_by_n[:10]: print("{:s} {:s}".format(o[0], o[1])) print( "Found {0:d} pairs of words that differ by +/-{1:d} in each component." .format(len(off_by_n), d)) print("Time: %0.4f" % (toc - tic))
def getTranspose(self): g = GraphDirected() words = get_words() for word in words: g.addVertex(word) for i in self.adjacentList: for j in self.adjacentList[i]: g.addEdge(j, i) return g
def main(): """ Find pairs of SGB word vectors that differ by +/-1 in each component. To do this, iterate through each word, generate the 32 possible candidate matchings, and if they exist, add the pair to a set. """ # words is a hash table (unsorted) words = get_words() #words = words[:1000] words = set(get_words()) # List of string tuples off_by_one = set() # Iterate over every word for iw, word in enumerate(words): # Generate all 2^5 = 32 possible candidate # matches for this word (total 185k strings) all_vars = get_all_variations(word) for word_var in all_vars: # O(1) check if this variation exists # in the five letter words set if word_var in words: # Found a new (unordered) pair if word < word_var: left = word right = word_var else: left = word_var right = word off_by_one.add((left, right)) off_by_one = list(off_by_one) off_by_one.sort() for o in off_by_one: print("{:s} {:s}".format(o[0], o[1])) print("Found {0:d} pairs of words that differ by +/-1 in each component.". format(len(off_by_one)))
def get_pure_user_words(num=9): """ Main process of the game run. This function gets words from user, checks them, counts wrong and right inputs (in points). (int) -> None :param num: amount of the letters to construct words from. (Default is 9). :returns None. """ while True: random_letters = generate_grid(num) # letters for user to create words words_from_dict = get_words(random_letters, num) # all words from a dictionary that consist generated letters if len(words_from_dict) > num: break print('Set up the words using these letters: ', random_letters, '\nYour word must contain', random_letters[round(num / 2)], 'letters.\nWords should not be repeated. Good luck! :)') counter = 0 well = 0 wrong = 0 # The main process of the game while counter < num: try: user_word = input('Enter a word, please: ') if all(i not in ascii_lowercase for i in user_word): raise ValueError if user_word in words_from_dict: words_from_dict.remove(user_word) print('You are right! :)') well += 1 else: print('You are wrong! :(') except ValueError: print('Only letters!') wrong += 1 finally: counter += 1 print('Your result:', well, 'from', num)
def trie_search(n, verbose=False): words = get_words() words = words[:n] perfect_count = 0 imperfect_count = 0 for letter in ALPHABET: tree = TryTrieTree(words) tree.set_root(letter) tree.assemble() tree.bubble_up() #print(tree) if tree.root.count >= 2: if verbose: print( "The letter {0:s} has a perfect binary trie in WORDS({1:d})." .format(letter, n)) perfect_count += 1 else: if verbose: print( "The letter {0:s} has no perfect binary trie in WORDS({1:d})." .format(letter, n)) imperfect_count += 1 if verbose: print("") print("Perfect count: {:d}".format(perfect_count)) print("Imperfect count: {:d}".format(imperfect_count)) return perfect_count, imperfect_count
from get_words import get_words import sys sys.setrecursionlimit(1000000) from GraphDirected import GraphDirected words = get_words() graph_directed = GraphDirected() for word in words: graph_directed.addVertex(word) allVertex = graph_directed.get_all_nodes() for idx1 in range(len(allVertex)): for idx2 in range(len(allVertex)): if graph_directed.is_four_character(allVertex[idx1], allVertex[idx2]): graph_directed.addEdge(allVertex[idx1],allVertex[idx2]) print("Strongly Component Numbers: ",end="") graph_directed.printSCCs() for i in range(2): vertex_start = input('Nhập đỉnh bắt đầu từ: ') vertext_end = input('Nhập đỉnh kết thúc bằng từ: ') print(graph_directed.elements_between_two_vertexes(vertex_start, vertext_end))
def get_corpus(file): corpus = get_words(file) return corpus
def histogram(words): dict = {} # creates empty dict for word in words: #check if word is in dict if word in dict: #increase count by 1 dict[word] += 1 else: #set count to 1 dict[word] = 1 return dict def unique_words(histogram): #returns number of unique words in histogram total_count = sum(histogram.values()) def frequency(word, histogram): if word in histogram: #checks if word is in histogram return histogram[word] #returns key value pairs else: return "That word is not in the histogram" #returns error msg if __name__ == '__main__': histo_text = get_words('Alice_Wonder.txt') histo = histogram(histo_text) print(histo)
from get_words import get_words if __name__ == "__main__": words = get_words("filtered_words.txt") while True: sentence = input() for word in words: if str(word) in sentence: sentence = sentence.replace(word, len(str(word)) * '*') print(sentence)