Пример #1
0
class Model(object):
    def __init__(self):
        self.root = TrieNode()
        self.PrefixOnly = 1
        
    def Construct(self,filename):
        buf = LoadFile(filename)
        for line in buf:
            line = line.strip()
            self.root.add(line)
        buf.close()
 
    def List(self,word):
        container = []
        if self.PrefixOnly == 1:
            self.root.find(word,container)
        else:
            self.root.findInfix(word,container)
        return container 
    
    def Contains(self,word):
        return self.root.contains(word.strip())
        
    def SwitchCommand(self):
        self.PrefixOnly = 1 ^ self.PrefixOnly
Пример #2
0
def trie_search_surname(surname):
    book_list = load_books(file)
    book_list = book_list[1:]
    surnames = []
    for book in book_list:
        author = book[3]
        author_last_name = author.split(" ")[-1]
        surnames.append(author_last_name)
    root = TrieNode()
    root.insert_many(surnames)
    return root.find(req_title)
Пример #3
0
def trie_search_id(req_title):
    book_list = load_books(file)
    titles = []
    for i in range(len(book_list)):
        if i == 0:
            continue
        else:
            book = book_list[i]
            title = "".join(book[2:len(book) - 2])
            titles.append(title)
    root = TrieNode()
    root.insert_many(titles)
    return root.find(req_title)