当前位置: 首页>>代码示例>>Python>>正文


Python Trie.insert方法代码示例

本文整理汇总了Python中trie.Trie.insert方法的典型用法代码示例。如果您正苦于以下问题:Python Trie.insert方法的具体用法?Python Trie.insert怎么用?Python Trie.insert使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在trie.Trie的用法示例。


在下文中一共展示了Trie.insert方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: find_compound_words

# 需要导入模块: from trie import Trie [as 别名]
# 或者: from trie.Trie import insert [as 别名]
def find_compound_words(words):
    """ trie + BFS + pruning
    Advantages of trie:
    1. Predictable O(k) lookup time where k is the size of the key.
    2. We can easily get all prefixes of a given word.
    Drawbacks of tries:
    1. Space-consuming, it is a trade-off between time-complexity and space\
    complexity. We can use radix-tree to get optimized space, but in \
    practice, it doesn't have a reasonable improvement and it takes more\
    time than trie.
    """
    compound_words = set([])
    trie = Trie()
    queue = collections.deque()
    prefixes_dict = {}
    for word in words:
        prefixes = trie.has_prefixes(word)
        for prefix in prefixes:
            queue.append((word, word[len(prefix) :]))
        trie.insert(word)
    while queue:
        word, suffix = queue.popleft()
        # pruning
        if word in compound_words:
            continue
        # find a compund word
        if suffix in trie:
            compound_words.add(word)
        else:
            prefixes = trie.has_prefixes(suffix)
            for prefix in prefixes:
                queue.append((word, suffix[len(prefix) :]))
    return compound_words
开发者ID:redswallow,项目名称:acm-icpc,代码行数:35,代码来源:solver.py

示例2: getCommonPrefix

# 需要导入模块: from trie import Trie [as 别名]
# 或者: from trie.Trie import insert [as 别名]
def getCommonPrefix(fileSet, trieNode, separator='-_'):
    debug = False
    # debug = ('killswitch engage - temple from the within.mp3' in fileSet)
    root = Trie('$$')
    for f in fileSet:
        root.insert(list(trieNode.children[f].key))
    prefixList = []
    def dfs(trieNode, curStr=''):
        if debug:
            print curStr, trieNode.num_successors, int(0.8*len(fileSet))
        if (trieNode.num_successors >= int(0.8*len(fileSet)) and trieNode.num_successors > 1) or trieNode.num_successors >= 3:
            res = False
            for k in trieNode.children:
                res = (dfs(trieNode.children[k], curStr+k) or res)
            if res:
                return True
            elif trieNode.key in separator:
                prefixList.append((curStr, trieNode.num_successors))
                return True
            else:
                return False
        else:
            return False
    dfs(root)
    # if len(prefixList) > 0:
    #     print prefixList, "->\n", '\n\t'.join(fileSet)
    return prefixList
开发者ID:nims11,项目名称:media-filename-analyser,代码行数:29,代码来源:guessMusicBatch.py

示例3: test_contains_on_shorter

# 需要导入模块: from trie import Trie [as 别名]
# 或者: from trie.Trie import insert [as 别名]
def test_contains_on_shorter():
    """Test contains returns false on non-inserted longer word."""
    from trie import Trie
    trie = Trie()
    token = 'pig'
    trie.insert(token)
    assert trie.contains('piglet') is False
开发者ID:jmcclena94,项目名称:data-structures,代码行数:9,代码来源:test_trie.py

示例4: find_top_k_with_trie

# 需要导入模块: from trie import Trie [as 别名]
# 或者: from trie.Trie import insert [as 别名]
def find_top_k_with_trie(k = 10):
    """
    Too slow and large memory consuming.
    
    time consuming:  147.656000137
    (164, 'mh')
    (164, 'sq')
    (165, 'bi')
    (165, 'mo')
    (167, 'im')
    (168, 'ux')
    (169, 'br')
    (169, 'gj')
    (170, 'ij')
    (171, 'qd')
    """
    result = []
    t = Trie()
    # trie
    with open(TDATA) as f:
        for line in f:
            t.insert(line.strip())
    
    # heapq
    for n in t.ipreorder(t.root):
        if len(result) < k:
            heapq.heappush(result, n)
        else:
            heapq.heappushpop(result, n)
            
    return result
开发者ID:yflau,项目名称:dsapp,代码行数:33,代码来源:bigdata.py

示例5: car_trie

# 需要导入模块: from trie import Trie [as 别名]
# 或者: from trie.Trie import insert [as 别名]
def car_trie():
    """Filled trie."""
    from trie import Trie
    t = Trie()
    for word in WORDS:
        t.insert(word)
    return t
开发者ID:flegald,项目名称:data_structures,代码行数:9,代码来源:test_trie.py

示例6: test_contains_on_partial

# 需要导入模块: from trie import Trie [as 别名]
# 或者: from trie.Trie import insert [as 别名]
def test_contains_on_partial():
    """Test contains returns false on partial match."""
    from trie import Trie
    trie = Trie()
    token = 'piglet'
    trie.insert(token)
    assert trie.contains('pig') is False
开发者ID:jmcclena94,项目名称:data-structures,代码行数:9,代码来源:test_trie.py

示例7: test_contains_given_string_one_string_

# 需要导入模块: from trie import Trie [as 别名]
# 或者: from trie.Trie import insert [as 别名]
def test_contains_given_string_one_string_():
    """Test that a given string is in the list."""
    from trie import Trie
    trie = Trie()
    token = 'pig'
    trie.insert(token)
    assert trie.contains(token)
开发者ID:jmcclena94,项目名称:data-structures,代码行数:9,代码来源:test_trie.py

示例8: test_insert_one_token

# 需要导入模块: from trie import Trie [as 别名]
# 或者: from trie.Trie import insert [as 别名]
def test_insert_one_token():
    """Test when token is inserted into the trie correctly."""
    from trie import Trie
    trie = Trie()
    token = 'pig'
    trie.insert(token)
    assert trie.container == {'p': {'i': {'g': {'$': '$'}}}}
开发者ID:jmcclena94,项目名称:data-structures,代码行数:9,代码来源:test_trie.py

示例9: test_insert_on_empty_long

# 需要导入模块: from trie import Trie [as 别名]
# 或者: from trie.Trie import insert [as 别名]
 def test_insert_on_empty_long(self):
     trie = Trie()
     data = 'ripper X!'
     trie.insert( 'ab', data )
     self.assertIsNone(trie.get('a'))     # a is not in the dict
     self.assertIsNotNone(trie.get('ab')) # ab is
     self.assertEqual(trie.get('ab'), data)
开发者ID:mpchoy,项目名称:python-trie,代码行数:9,代码来源:trie_test.py

示例10: test_contatins

# 需要导入模块: from trie import Trie [as 别名]
# 或者: from trie.Trie import insert [as 别名]
def test_contatins():
    """Test contains responds with true for a word that has been inserted."""
    from trie import Trie
    t = Trie()
    t.insert("cat")
    result = t.contains("cat")
    assert result is True
开发者ID:muniri92,项目名称:Data-Structures-2.0,代码行数:9,代码来源:test_trie.py

示例11: test_contatins_false

# 需要导入模块: from trie import Trie [as 别名]
# 或者: from trie.Trie import insert [as 别名]
def test_contatins_false():
    """Test contains responds with false for a word that is not inserted."""
    from trie import Trie
    t = Trie()
    t.insert("cat")
    result = t.contains("dog")
    assert result is False
开发者ID:muniri92,项目名称:Data-Structures-2.0,代码行数:9,代码来源:test_trie.py

示例12: test_overlapping_words

# 需要导入模块: from trie import Trie [as 别名]
# 或者: from trie.Trie import insert [as 别名]
def test_overlapping_words():
    from trie import Trie
    new_trie = Trie()
    new_trie.insert('words')
    new_trie.insert('trie')
    new_trie.insert('trip')
    assert new_trie.root == {'w': {'o': {'r': {'d': {'s': {'$': '$'}}}}},
                             't': {'r': {'i': {'e': {'$': '$'}, 'p': {'$': '$'}}}}}
开发者ID:wohlfea,项目名称:data-structures2,代码行数:10,代码来源:test_trie.py

示例13: test_2_trie

# 需要导入模块: from trie import Trie [as 别名]
# 或者: from trie.Trie import insert [as 别名]
def test_2_trie():
    """Test that we can insert two words into the Trie."""
    from trie import Trie
    new_trie = Trie()
    new_trie.insert('words')
    new_trie.insert('trie')
    assert new_trie.root == {'w': {'o': {'r': {'d': {'s': {'$': '$'}}}}},
                             't': {'r': {'i': {'e': {'$': '$'}}}}}
开发者ID:wohlfea,项目名称:data-structures2,代码行数:10,代码来源:test_trie.py

示例14: test_basic

# 需要导入模块: from trie import Trie [as 别名]
# 或者: from trie.Trie import insert [as 别名]
 def test_basic(self):
     trie = Trie()
     trie.insert("cat")
     self.assertEqual(trie.__len__(), 1)
     self.assertTrue(trie.__contains__("cat"))
     self.assertEqual([x for x in trie.__iter__()], ["cat"])
     self.assertTrue(trie.contains_prefix("ca"))
     self.assertEqual([x for x in trie.prefix_iter("ca")], ["cat"])
开发者ID:themailman05,项目名称:Trie,代码行数:10,代码来源:basic_trie_tests.py

示例15: test_remove_long

# 需要导入模块: from trie import Trie [as 别名]
# 或者: from trie.Trie import insert [as 别名]
 def test_remove_long(self):
     trie = Trie()
     key = 'abc'
     data = 123
     trie.insert( key, data )
     self.assertIsNotNone(trie.get(key))
     self.assertEqual(trie.remove(key), data)
     self.assertIsNone(trie.get(key))
开发者ID:mpchoy,项目名称:python-trie,代码行数:10,代码来源:trie_test.py


注:本文中的trie.Trie.insert方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。