class Suggester(object): def __init__(self): self.trie = None def update_trie(self, word_list): self.trie = StringTrie() for word in word_list: word = word.lower() self.trie[word] = word def search_prefix(self, prefix): return self.trie.values(prefix=prefix)
def is_valid(valid_list, letters): # create empty trie trie = StringTrie() # traverse through list of string to insert it in trie. # Here value of key is itself key because at last we need to retun for key in valid_list: trie[key] = key # values(search) method returns list of values of keys # which contains search pattern as prefix return trie.values(letters)
def prefixSearch(arr, prefix): trie = StringTrie() # traverse through list of strings # to insert it in trie. Here value of # key is itself key because at last # we need to return for key in arr: trie[key] = key length = len(prefix) + 1 for x in range(2, length): answer = trie.values(prefix[:x]) answer.sort() print(answer)
def build_trie_and_execute(map_to_search, prefix_term): trie = StringTrie() for key in map_to_search: trie[key] = key prefix_matches = trie.values(url_id) if len(prefix_matches) > 0: first_match = prefix_matches[0] index = map_to_search[first_match][0] open_url_in_browser(links[index], get_params()) return True return False
def prefix_search(arr, prefix): # create empty trie trie = StringTrie() # traverse through list of strings # to insert it in trie. Here value of # key is itself key because at last # we need to return for key in arr: trie[key] = key # values(search) method returns list # of values of keys which contains # search pattern as prefix return trie.values(prefix)
class GeohashContainer(object): """ A class which can be used as a container of geohashes and corresponding items. The underlying data structure is a trie (StringTrie).""" def __init__(self, itemCollection): self.container = StringTrie() for item in itemCollection: self.container[Geohash.encode(item.Location.Latitude, item.Location.Longitude)] = item def Retrieve(self, keyPrefix): """ Returns all items which match the specified keyPrefix """ results = self.container.values(keyPrefix) return results def Add(self, key, value): """ Adds a new item to its underlying container """ self.container[key] = value
class Suggester(object): def __init__(self): self.trie = None self.trie = StringTrie() def update_trie(self, word_list): for word in word_list: # word = word.lower() # 拼音提取,首字母,全拼都改成小写,去空格 word_pinyin1 = get_every_word_first(word) word_pinyin2 = get_all_pinying(word) # 拼音建立字典树 self.trie[word] = word self.trie[word_pinyin1] = word_pinyin1 self.trie[word_pinyin2] = word_pinyin2 def search_prefix(self, prefix): return self.trie.values(prefix=prefix)
print("%-18s %9s" % ("County tax: ", locale.currency(countytax))) print("%-18s %9s" % ("City tax: ", locale.currency(citytax))) # print the total amount due, the amount paid by the customer, and the amount of change print("%-18s %9s" % ("Total due: ", locale.currency(round(tax + total, 2)))) print("%-18s %9s" % ("Amount paid: ", locale.currency(customerAmount))) change = customerAmount - tax - total print("%-18s %9s" % ("Change: ", locale.currency(round(change, 2)))) print() # print a goodbye message print("Thank you! Have a nice day!") # break to ensure that we exit the while loop and the program completes and exits break # prefix matching values from the trie - trie provides efficient prefix matching against strings matches = trie.values(userInput) # case where nothing matched if len(matches) == 0: print("No matches") # case where exactly one item matched elif len(matches) == 1: # get the item and the relevant fields item = matches[0] id = item[0] name = item[1] price = item[2] # if an item has already been rung up then print a message and continue if id in rungup: print("That item has already been rung up") continue