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


Python Trie.search方法代码示例

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


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

示例1: test_search_6

# 需要导入模块: from trie import Trie [as 别名]
# 或者: from trie.Trie import search [as 别名]
    def test_search_6(self):
        trie = Trie()
        key = "ate"
        trie.insert( key )

        matches = trie.search( "any", 2 )
        self.assertEqual(len(matches), 1)
        self.assertTrue(key in matches)

        matches = trie.search( "any", 1 )
        self.assertEqual(len(matches), 0)
开发者ID:mpchoy,项目名称:python-trie,代码行数:13,代码来源:trie_test.py

示例2: test_search_7

# 需要导入模块: from trie import Trie [as 别名]
# 或者: from trie.Trie import search [as 别名]
    def test_search_7(self):
        trie = Trie()
        key = "tea"
        trie.insert( key )

        matches = trie.search( "pea", 1 )
        self.assertEqual(len(matches), 1)
        self.assertTrue(key in matches)
开发者ID:mpchoy,项目名称:python-trie,代码行数:10,代码来源:trie_test.py

示例3: test_search_8

# 需要导入模块: from trie import Trie [as 别名]
# 或者: from trie.Trie import search [as 别名]
    def test_search_8(self):
        trie = Trie()
        key_tea = "tea"
        key_pet = "pet"
        trie.insert( key_tea )
        trie.insert( key_pet )

        matches = trie.search( "pea", 1 )
        self.assertEqual(len(matches), 2)
        self.assertTrue(key_tea in matches)
        self.assertTrue(key_pet in matches)
开发者ID:mpchoy,项目名称:python-trie,代码行数:13,代码来源:trie_test.py

示例4: test_search_5

# 需要导入模块: from trie import Trie [as 别名]
# 或者: from trie.Trie import search [as 别名]
    def test_search_5(self):
        trie = Trie()
        key_ace = "ace"
        key_ate = "ate"
        trie.insert( key_ace )
        trie.insert( key_ate )

        matches = trie.search( "axe", 1 )
        self.assertEqual(len(matches), 2)
        self.assertTrue(key_ace in matches)
        self.assertTrue(key_ate in matches)
开发者ID:mpchoy,项目名称:python-trie,代码行数:13,代码来源:trie_test.py

示例5: test_search_2

# 需要导入模块: from trie import Trie [as 别名]
# 或者: from trie.Trie import search [as 别名]
    def test_search_2(self):
        trie = Trie()
        key_at = "at"
        key_as = "as"
        trie.insert( key_at )
        trie.insert( key_as )

        matches = trie.search( "ax", 1 )
        self.assertEqual(len(matches), 2)
        self.assertTrue(key_at in matches)
        self.assertTrue(key_as in matches)
开发者ID:mpchoy,项目名称:python-trie,代码行数:13,代码来源:trie_test.py

示例6: Trie

# 需要导入模块: from trie import Trie [as 别名]
# 或者: from trie.Trie import search [as 别名]
#        | |\
#        e r t
#
#The LOUDS bit-string is then
#[1, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0]

words = ['an', 'i', 'of', 'one', 'our', 'out']
test_set = {
    'out': 11,   #'t' in 'out' is at 11th node in the tree
    'our': 10,   #'r' in 'our' is at 10th node in the tree
    'of': 6,
    'i': 3,
    'an': 5,
    'one': 9,
    'ant': None  #'ant' is not in the tree
}

trie = Trie(words)
for query, answer in test_set.items():
    result = trie.search(query)
    print("query: {!s:>5}  result: {!s:>5}  answer: {!s:>5}".format(
        query, result, answer))

##parent    [0, 0, 1, 1, 1, 1, 2, 2, 3, 4, 4, 4, 4, 5, 6, 7, 7, 8, 8, 8, 9, 0, 1]
##bit_array [1, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0]
##child     [1, -, 2, 3, 4, -, 5, -, -, 6, 7, 8, -, -, -, 9, -, 0, 1, -, -, -, -]
##index     [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2]

#labels ['', '', 'a', 'i', 'o', 'n', 'f', 'n', 'u', 'e', 'r', 't']
##index [ 0,  1,   2,   3,   4,   5,   6,   7,   8,   9,   0,  1]
开发者ID:IshitaTakeshi,项目名称:Louds-Trie,代码行数:32,代码来源:test.py

示例7: Word

# 需要导入模块: from trie import Trie [as 别名]
# 或者: from trie.Trie import search [as 别名]
class Word():
	def __init__(self):
		fd=open('dictionary','rb')
		self.dic=pickle.load(fd)
		fd.close()
		if(not os.path.exists('finished.trie')):
			self.finished=Trie()
		else:
			fd=open('finished.trie','rb')
			self.finished=pickle.load(fd)
#		fd=open('finished.trie','rb')
#		self.finished=pickle.load(fd)
#		fd.close()
#		fd=open('remaining.trie','rb')
#		self.finished=pickle.load(fd)
#		fd.close()

	def dumpster(self,fname,obj):
		fd=open(fname,'wb')
		pickle.dump(obj,fd)
		fd.close()

	def addWordFromDictionary(self,word):
		meaning,example,synonym=pd.getEntry(word)
		#print meaning
		#print synonym
		if(synonym ==[] and meaning==[]):
			return False
		return synonym,meaning,example

	def addWord(self,word):
		arr=self.addWordFromDictionary(word)
		if(arr==False):
			return arr
		self.dic[word]=[[],[],[],[],[]]
		synonym,meaning,example=arr
		self.dic[word][0].extend(synonym)
		self.dic[word][1].extend(meaning)
		self.dic[word][2].extend(example)
		self.finished.insert(word)
		self.dumpster('dictionary',self.dic)
		self.dumpster('finished.trie',self.finished)
		return True

	def mineWords(self,count):
		if(not os.path.exists('dictionary')):
			self.dic={}
		else:
			fd=open('dictionary','rb')
			self.dic=pickle.load(fd)
		if(not os.path.exists('remaining.trie')):
			fd=open('dictionary','rb')
			obj=pickle.load(fd)
			self.remaining=Trie()
			for word in obj.keys():
				self.remaining.insert(word)
		else:
			fd=open('remaining.trie','rb')
			self.remaining=pickle.load(fd)
		print self.finished.getSize()
		print self.remaining.getSize()
		for i in xrange(count):
			if(i%2):
				time.sleep(0.75)
			newword=self.remaining.getWord()
			print newword
			if(self.finished.search(newword)):
				continue
			ret=self.addWordFromDictionary(newword)
			if(ret):
				self.dic[newword]=ret
			self.finished.insert(newword)
			self.remaining.delete(newword)
		self.dumpster('dictionary',self.dic)
		self.dumpster('finished.trie',self.finished)
		self.dumpster('remaining.trie',self.remaining)
		print self.finished.getSize()
		print self.remaining.getSize()
开发者ID:r-arun,项目名称:word,代码行数:80,代码来源:wordSpider.py


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