本文整理汇总了Python中trie.Trie.delete方法的典型用法代码示例。如果您正苦于以下问题:Python Trie.delete方法的具体用法?Python Trie.delete怎么用?Python Trie.delete使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类trie.Trie
的用法示例。
在下文中一共展示了Trie.delete方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: TrieTest
# 需要导入模块: from trie import Trie [as 别名]
# 或者: from trie.Trie import delete [as 别名]
class TrieTest(TestCase):
def setUp(self):
self.trie = Trie()
self.trie.insert(u'测试1')
self.trie.insert(u'abcd')
self.trie.insert(u'5678')
self.trie.insert(u'你')
def test_exists(self):
self.assertEquals(self.trie.match(u'测试2'), False)
self.assertEquals(self.trie.match(u'测试1'), True)
self.assertEquals(self.trie.match(u'我'), False)
self.assertEquals(self.trie.match(u'你'), True)
self.assertEquals(self.trie.match(u'abcdefg'), True)
self.assertEquals(self.trie.match(u'567'), False)
def test_delete(self):
self.assertEquals(self.trie.delete(u'测试2'), False)
self.assertEquals(self.trie.match(u'测试1'), True)
self.assertEquals(self.trie.delete(u'测试1'), True)
self.assertEquals(self.trie.match(u'测试1'), False)
self.trie.insert(u'1234')
self.trie.insert(u'12345678')
self.trie.insert(u'123456789')
self.assertEquals(self.trie.delete(u'1234'), True)
self.assertEquals(self.trie.match(u'12345678'), True)
self.assertEquals(self.trie.delete(u'123456789'), True)
self.assertEquals(self.trie.match(u'12345678'), True)
示例2: test_09_delete
# 需要导入模块: from trie import Trie [as 别名]
# 或者: from trie.Trie import delete [as 别名]
def test_09_delete(self):
t = Trie()
l = ["one", "two", "three", "onesee", "spam", "spammers", "spamhouse", "spams"]
for i in l:
t[i] = i
self.assertEqual(sorted(t.find_all("spam")), sorted(l[-4:]))
self.assertEqual(t.delete("spam"), "spam")
self.assertEqual(sorted(t.find_all("spam")), sorted(l[-3:]))
self.assertRaises(KeyError, t.delete, "four")
self.assertEqual(sorted(t.find_all("spam")), sorted(l[-3:]))
s, n = t.get_node("spam")
self.assertFalse(n.has_value)
示例3: Word
# 需要导入模块: from trie import Trie [as 别名]
# 或者: from trie.Trie import delete [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()