Ejemplo n.º 1
0
def tests():
    #test trie
    trie = Trie.Trie()
    assert trie.root == {}

    #test add: "ate"

    assert trie.add("ate") == {
        'a': [False, {
            't': [False, {
                'e': [True, {None}]
            }]
        }]
    }
    assert trie.add("atom") == {
        'a': [
            False, {
                't': [
                    False, {
                        'e': [True, {None}],
                        'o': [False, {
                            'm': [True, {None}]
                        }]
                    }
                ]
            }
        ]
    }
    print("tests passed")
Ejemplo n.º 2
0
    def __init__(self, parent, *args, **kwargs):
        ttk.Frame.__init__(self, parent, *args, **kwargs)
        self.root = parent

        #Initialize the Trie (dictionary)
        self.dictionary = Trie()

        #initialize text widget
        self.text = Text(self.root, width=100, height=40)
        self.text.grid(column=0, row=2)

        #Initialize the menu
        self.init_gui()

        #Bind space and right-click events on the text widget
        self.text.bind("<space>", self.spell_check)
        self.text.bind('<Button-3>', self.autocorrect)
        self.text.bind('<Key>', self.typing_suggestions)

        #Initize the filename to empty string
        self.filename = ''

        #Initialize the underline tag for mispelt words
        self.text.tag_configure('underline', foreground='red', underline=True)

        #Initialize the highligh tag for found words
        self.text.tag_configure('highlight', background='red')
        self.text.tag_configure('unhighlight', background='white')

        #Initialize pop-up menu for right click
        self.pop_menu = tkinter.Menu(parent, tearoff=0)

        #Initialize pop-up menu for text statistics
        self.pop_stats = tkinter.Menu(parent, tearoff=0)
Ejemplo n.º 3
0
def creattree():
    diclist = opendict()
    blendssearch = Trie.Trie()
    lendict = len(diclist)
    for i in range(lendict):
        blendssearch.insert(diclist[i])
    return blendssearch
Ejemplo n.º 4
0
 def testAutocomplete_AlmostPrefix_Present_SuggestPartial_2(self):
     trie = Trie.Trie()
     for word in prefix2:
         trie.insert(word, trie.root)
     self.assertCountEqual(
         Counter(trie.autocomplete("fores")),
         Counter(["foreshadow", "foresight", "foreseeable"]))
Ejemplo n.º 5
0
 def visit(self,p,alpha,newEventSet,i,k,Q):
     for e in newEventSet:
         comparison=False
         for child in p.children:
             if child.event==e:
                 comparison=True
         if comparison:
             if self.debug:
                 print('Already have')
         else:
             q=Trie(e,k+1)
             p.children.append(q)
             if self.debug:
                 print("New Trie:")
                 print(p)
                 print("alpha.events:"+str(alpha.events))
             qAlphaPath=alpha.events.copy()
             if self.debug:
                 print("qAlphaPath:"+str(qAlphaPath))
             qAlphaPath.append(e)
             qAlpha=Episode(qAlphaPath,Window(i,k+1))
             Q.add(qAlpha)
             if self.debug:
                 print("New Q:")
                 for ep in Q:
                     print(Q)
     contained=False
     for ep in Q:
         if str(ep.events)==str(alpha.events):
             contained=True
     if contained:
         p.isLO=False
     if self.debug:
         print("P:")
         print(p)
Ejemplo n.º 6
0
 def test_complexity(self):
     empty_tree = Trie.Trie()
     empty_list = []
     test_set = create_test_set(100)
     tree, list = build(empty_tree, empty_list, 100000)
     tree_time = tree_complexity(tree, test_set)
     list_time = list_complexity(list, test_set)
     self.assertTrue(list_time > tree_time)
def compressao(entrada, saida):
    with open(entrada, 'r') as file:
        texto = file.read()
    trie = Trie.Trie()
    arq_saida = open(saida, 'wb')
    indice = 0
    indice = trie.insert(texto, indice, arq_saida)
    file.close()
    arq_saida.close()
Ejemplo n.º 8
0
def main():

    keys = parse_file('dictionary.txt')

    t = Trie.Trie()
    for key in keys:
        t.insert(key)

    solution(t)
Ejemplo n.º 9
0
    def __init__(self):
        self.positives = ["good", "gud", "well", "great", "decent", "amazing", "excellent", "sexy", "superb", "suburb",
                          "awesome", "awsm", "nice", "happy", "high", "average",
                          "fast", "quick", "immersing", "immersive", "premium",
                          "best", "better",
                          "perfect", "perfection", "beast", "great", "fantastic", "faster", " fabulous", "blazing",
                          "loved", "love",
                          "marvellous", "comfortable", "unbeatable"
                          "charge", "smooth", "beautifully", "beautiful", "superb"]
        self.negatives = ["very bad", "bad", "disappointment", "wrong", "never", "slow", "no", "not good", "not work",
                          "not", "terrible", "heavy"
                          "issue", "defect", "slowest", "lags", "waste", "doesn't work",
                          "doesnt work", "doesnt", "doesn't",
                          "problem", "sucks", "worst",
                          "pathetic", "not good", "not very good", "ineffective", "poor", "not success"]
        self.features = ["phone", "phones", "device", "product", "mobile", "look",
                         "front camera", "back camera", "rear camera", "camera", "selfie", "front",
                         "photos", "pictures", "video", "images", "lowlight pictures", "lowlight",
                         "fingerprint", "fingerprints", "finger print", "finger lock", "fingerlock", "finger", "touch",
                         "display", "hd", "design", "build", "performance", "gorilla glass", "screen", "super amoled",
                         "amoled", "notch", "gorrilla glass",
                         "battery", "backup", "charging", "charge", "charger",
                         "connectivity", "network",
                         "face unlock", "face", "face recognition",
                         "sound", "headset", "headphones", "audio", "speakers", "speaker",
                         "water resistance", "water",
                         "notifications light", "notification light",
                         "adaptive brightness sensor", "sensor", "call quality", "call", "nfc", "wifi", "bluetooth",
                         "other devices",
                         "one ui", "one-ui", "ui", "os", "color os", "coloros", "miui",
                         "pubg", "gaming", "games", "color", "ram", "memory",
                         "heating", "price", "cost", "value for money", "budget", "affordable",
                         "processor", "chipset", "cpu", "speed", "bloatware",
                         "microsd", "micro sd", "storage",
                         "weight"]

        # STOPWORDS
        self.stopwords = nltk.corpus.stopwords.words('english')
        for pos in self.positives:
            if pos in self.stopwords:
                self.stopwords.remove(pos)
        for neg in self.negatives:
            if neg in self.stopwords:
                self.stopwords.remove(neg)
        for ftr in self.features:
            if ftr in self.stopwords:
                self.stopwords.remove(ftr)

        self.trie = Trie()
        for pos in self.positives:
            self.trie.insert(pos, 'Positive')
        for neg in self.negatives:
            self.trie.insert(neg, 'Negative')
        for ftr in self.features:
            self.trie.insert(ftr, 'Features')
Ejemplo n.º 10
0
 def __init__(self, documentPath, dictionaryFilePath):
     # Create Trie to store dictionary words
     self.dictionaryTrie = Trie.Trie()
     # Create rope structure for document
     self.document = None
     self.populateDocumentWithInputText(documentPath)
     # Insert dictionary words in Trie
     for word in self.yieldWords(dictionaryFilePath):
         self.dictionaryTrie.insert(word, self.dictionaryTrie.root)
     # Create an empty rope object to later store pasted text
     self.paste_text = ropes.Rope("")
Ejemplo n.º 11
0
 def testAutocomplete_LongWord_NotPresent_1(self):
     trie = Trie.Trie()
     for word in prefix1:
         trie.insert(word, trie.root)
     longEnd = ""
     for _ in range(20):
         longEnd += chr(
             choice([
                 i for i in range(65, 123) if i not in list(range(91, 97))
             ]))
     self.assertEqual(trie.autocomplete("inter" + longEnd), None)
Ejemplo n.º 12
0
 def testAutocomplete_NotPrefix_Not_Present_5(self):
     trie = Trie.Trie()
     for word in prefix5:
         trie.insert(word, trie.root)
     # generates a random letter (uppercase or lower) that is not that of the first letter in the prefix
     random_letter = choice([
         i for i in range(65, 123) if i not in list(range(91, 97)) +
         [ord(prefixes[4][0].upper()),
          ord(prefixes[4][0].lower())]
     ])
     self.assertEqual(trie.autocomplete(chr(random_letter)), None)
Ejemplo n.º 13
0
def test_insertion():
    """Test behavior of trie."""
    tree = Trie.Trie()
    words = [
        "monkey", "monkeybusiness", "banana", "monkey", "bananas", "bananas",
        "m"
    ]

    for word in words:
        tree.insert(word)
    return tree
Ejemplo n.º 14
0
    def test_total_count(self):
        t = Trie.Trie()
        self.assertEqual(t.total_count(), 0)

        t.add_word('wonder')
        t.add_word('happy')
        t.add_word('beautiful')

        self.assertEqual(t.total_count(), 3)

        t.add_word('')
        self.assertEqual(t.total_count(), 3)
Ejemplo n.º 15
0
 def __init__(self, path=None):
     self.path = path
     self.lock = Lock()
     if path is None:
         self.db = sqlite3.connect(p.curdir + "/BlockChain.sqlite3")
         self.path = p.curdir + "/BlockChain.sqlite3"
     else:
         self.db = sqlite3.connect(path)
     if os.path.exists("./trie"):
         file = io.open("./trie", 'rb')
         self.trie = Trie.Trie('$')
         id = file.read(11)
         while len(id) == 11:
             self.trie.insert(id.decode())
             id = file.read(11)
         file.close()
     else:
         self.db.cursor().execute("drop table if exists Blocks")
         io.open("./trie", 'xb')
         self.trie = Trie.Trie('$')
     self.tip2 = []
     self.init_db()
     self.db.close()
Ejemplo n.º 16
0
def creatreversetree():
    dictlist = []
    f1 = open("dict.txt", "r")
    for words in f1:
        if duplicate(words) == 1:
            dictwords = words.strip()
            dicwords = dictwords[::1]
            dictlist.append(dicwords)
    f1.close()
    research = Trie.Trie()
    lendict = len(dictlist)
    for i in range(lendict):
        research.insert(dictlist[i])
    return research
Ejemplo n.º 17
0
def lexicon_from_file(lexicon_filename):
    '''takes a file of words and builds Trie from it.
    Note: each word in the list should end with a newline character.
    Args: filename (str)
    Returns: lexicon (Trie)'''

    lexicon = Trie.Trie()
    f = open(lexicon_filename, 'r')
    p = re.compile('qu')

    for line in f:
        if re.match('q[^u]', line): continue
        lexicon.add_word(p.sub('q', line[:-1]))
    return lexicon
Ejemplo n.º 18
0
def main():
    word_lists = [
        'words_44k.txt', 'words_109k.txt', 'words_178k.txt', 'words_263k.list',
        'words_370k.txt'
    ]

    # insert
    print("Insertion:\n")
    insert_times = []
    counter = 1
    for file in word_lists:
        print("Processing file " + str(counter))
        f_read = open('word_list/' + file)
        T = Trie()
        start = time.time()
        for word in f_read.readlines():
            T.insert(word.rstrip())
        endtime = time.time() - start
        print(endtime)
        insert_times.append(endtime)
        f_read.close()
        counter += 1
    f_write_insertradix = open('data/trie_insert.txt', 'w')
    for timetaken in insert_times:
        f_write_insertradix.write(str(timetaken) + '\n')
    f_write_insertradix.close()

    # search
    print("\nSearch:\n")
    word_searches = [
        'to', 'unappeasableness', 'pseudopseudohypoparathyroidism',
        'hippopotomonstrosesquippedaliophobia',
        'pneumonoultramicroscopicsilicovolcanoconiosis'
    ]
    counter = 1
    search_times = []
    for word in word_searches:
        print("Processing word " + str(counter))
        start = time.time()
        T.search(word)
        endtime = time.time() - start
        print(endtime)
        search_times.append(endtime)
        counter += 1
    f_write_searchradix = open('data/trie_search.txt', 'w')
    for timetaken in search_times:
        f_write_searchradix.write(str(timetaken) + '\n')
    f_write_searchradix.close()
Ejemplo n.º 19
0
 def __init__(self, document):
     self.textLength = len(document)
     self.original = document
     self.buffer = ""
     self.pieces = [Piece(False, 0, len(document))]
     self.dictionary = Trie.Trie()
     # On windows, the dictionary can often be found at:
     # C:/Users/{username}/AppData/Roaming/Microsoft/Spelling/en-US/default.dic
     with open(
             "C:/Users/kirti/AppData/Roaming/Microsoft/Spelling/en-US/default.dic"
     ) as input_dictionary:
         for line in input_dictionary:
             words = line.strip().split(" ")
             for word in words:
                 self.dictionary.insert(word.lower())
     self.pasteText = ""
Ejemplo n.º 20
0
def main():
    my_trie = Trie()
    file = open('text.txt', 'r')
    word = ''
    line_c = 1
    for line in file:
        col_c = 1
        for ch in line:
            if ch.isalpha():
                word += ch
            elif len(word) > 0:
                my_trie.insert(word, (line_c, col_c))
                word = ''
            col_c += 1
        line_c += 1
    file.close()
    print(my_trie.search('the'))
    print(my_trie.match_prefix('se'))
Ejemplo n.º 21
0
def suggestions():

    # Receiveing the parameters from the GET request
    queryParams = request.args.get('q')
    if queryParams == "":
        return 'null'
    # Creating a Trie object
    trie = Trie()

    # Creating a Full Trie from the corpus received
    sentences = service_data
    for sentence in sentences:
        trie.addSentence(sentence)

    # Getting the list of suggestions by supplying query
    result = trie.generate_completions(queryParams)

    return jsonify(Completions=result)
Ejemplo n.º 22
0
 def _generateTriesByNeedOneDirection(self, clueSpots, ds, tries={}):
     for direction, row, col in clueSpots:
         pair = (row, col)
         wordCoordinates, length = self.coordWord(pair, direction)
         infoInTable = self.wordInfo(wordCoordinates)
         orderOfTrie, allPermanent = self.orderOfTrie(wordCoordinates)
         if (length, orderOfTrie) in tries:
             continue
         else:
             dictionaryOfWordOfRightLength = ds[length - 3]
             trieMadeTheRightWay = Trie.Trie(None)
             # TAKES A LOT OF TIME!
             for word in dictionaryOfWordOfRightLength:
                 wordAltered = Board_helper.putWordIntoCustomizedOrder(
                     word, orderOfTrie)
                 trieMadeTheRightWay = trieMadeTheRightWay.addWord(
                     wordAltered)
             tries[(length, orderOfTrie)] = trieMadeTheRightWay
     return tries
Ejemplo n.º 23
0
def add_from_text(text, word_length, trie=None):
    """Adds all substrings of text with specified length to a trie.

    If no trie is specified, an empty one is first created.

    Args:
        text: A string from which the words to add to the trie are formed.
        word_length: Integer specifying length of the substrings of text.
        trie: A trie (Trie.Trie). Optional. If not specified an empty one is created.

    Returns:
        A trie (Trie.Trie).
    """
    if trie is None:
        trie = Trie.Trie()

    trie.feed_text(text, word_length)

    return trie
Ejemplo n.º 24
0
    def test_random_word(self):
        t = Trie.Trie()
        t.add_word('wonky')

        random_word = t.random_word(3)
        self.assertIsInstance(random_word, str)
        self.assertEqual(len(random_word), 3)

        self.assertIsNone(t.random_word(100))

        t.add_word('toaster')
        t.add_word('care bear')
        t.add_word('croakamancer')

        prefix = 'toa'
        for counter in range(10):
            prefixed_word = t.random_word(5, prefix=prefix)
            self.assertSequenceEqual(prefixed_word[:len(prefix)],
                                     prefix,
                                     seq_type=str)
Ejemplo n.º 25
0
def Implement_Trie_Loop(ope, params):
    if len(ope) <= 0 or len(params) <= 0:
        return

    if len(ope) != len(params):
        return

    for i in range(len(ope)):
        if ope[i] == "Trie":
            trie = Trie.Trie()
            print("Trie()")

        elif ope[i] == "insert":
            trie.insert(params[i])
            print("Trie.insert({0})".format(params[i]))

        elif ope[i] == "search":
            res = trie.search(params[i])
            print("Trie.search({0}) ... {1}".format(params[i], res))

        elif ope[i] == "startsWith":
            res = trie.startsWith(params[i])
            print("Trie.startsWith({0}) ... {1}".format(params[i], res))
Ejemplo n.º 26
0
def add_from_gutenberg(urls, word_length, trie=None):
    """Adds substrings from Project Gutenberg text to a trie.

    Takes URLs for a Project Gutenberg book and adds all substrings of length
    word_length from those texts to a trie. If no trie is supplied, a new one is created.

    Args:
        urls: A list of URLs to Project Gutenberg text files.
        word_length: Integer specifying the length of the substrings to add to the trie.
        trie: Optional. A trie (Trie.Trie). If not supplied, an empty trie will be created.

    Returns:
        A trie (Trie.Trie).
    """
    if trie is None:
        trie = Trie.Trie()

    for url in urls:
        response = requests.get(url)
        text = clean_gutenberg_txt(response.text)
        trie.feed_text(text, word_length)

    return trie
Ejemplo n.º 27
0
 def buildTrie(self,newEventSet,k):
     root=Trie('0',k+1)
     for c in newEventSet:
         node=Trie(c,k+1)
         root.children.append(node)
     return root
Ejemplo n.º 28
0
# -*- coding: utf-8 -*-
"""
Created on Sun Feb 19 20:09:25 2017

@author: user
"""
import Trie
from words_tagging import all_noun
from Declension import Declension_noun
from Stemming import stem, initialize
mytrie = Trie.Trie()
mytrie = initialize()

if __name__ == '__main__':
    correct = 0
    incorrect = 0
    total = 0
    incorrect_words = []
    for s_type in all_noun:
        for noun in s_type:
            dec = Declension_noun(noun)
            print(noun)
            #print(dec)
            for row in dec:
                for col in row:
                    total = total + 1
                    if stem(col) == noun:
                        correct = correct + 1
                    else:
                        incorrect = incorrect + 1
                        incorrect_words.append(col)
Ejemplo n.º 29
0
 def __init__(self):
     self._trie = my_trie.Trie()
     pass
Ejemplo n.º 30
0
            Dictionary._instance = Dictionary()

        return Dictionary._instance

    def load_dic(self, _file_path):
        #todo check the _file_path
        with open(_file_path) as _dic_file:
            for line in _dic_file:
                _word_list = (_word, _nota, _id, _cost,
                              _tag) = line.rstrip().split("\t")
                self._trie.add_item(_nota, (_word_list))
        sys.stderr.write("==> load dic finished \n")

    def search_exact(self, _key):
        return self._trie.search_item(_key)

    def search_prefix(self, _key):
        return self._trie.search_prefix(_key)

    def destory_dict(self):
        del self._trie


if __name__ == "__main__":
    tri = my_trie.Trie()
    diction = Dictionary.get_instance()
    diction.load_dic("./dictionary/basic.dic")
    print "\t".join(diction.search_exact("ひと"))
    for item in diction.search_prefix("わたし"):
        print "\t".join(item)