本文整理汇总了Python中trie.Trie.autocomplete方法的典型用法代码示例。如果您正苦于以下问题:Python Trie.autocomplete方法的具体用法?Python Trie.autocomplete怎么用?Python Trie.autocomplete使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类trie.Trie
的用法示例。
在下文中一共展示了Trie.autocomplete方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_auto_complete_bolo
# 需要导入模块: from trie import Trie [as 别名]
# 或者: from trie.Trie import autocomplete [as 别名]
def test_auto_complete_bolo():
"""Test that auto complete will return the one word we are looking for."""
from trie import Trie
t = Trie()
t.insert("catty")
t.insert("church")
t.insert("crutch")
t.insert("cats")
t.insert("dog")
t.insert("cat")
t.insert("at")
assert t.autocomplete("c") == ['cat', 'cats', 'catty', 'church']
assert t.autocomplete("ca") == ['cat', 'cats', 'catty']
assert t.autocomplete("d") == ["dog"]
示例2: test_auto_complete_nonexist
# 需要导入模块: from trie import Trie [as 别名]
# 或者: from trie.Trie import autocomplete [as 别名]
def test_auto_complete_nonexist():
"""Test autocomplete for a word that does not exist in trie."""
from trie import Trie
t = Trie()
t.insert("catty")
t.insert("church")
t.insert("crutch")
t.insert("cats")
t.insert("dog")
assert t.autocomplete("p") == []
示例3: test_autocomplete_on_non_existent_token
# 需要导入模块: from trie import Trie [as 别名]
# 或者: from trie.Trie import autocomplete [as 别名]
def test_autocomplete_on_non_existent_token():
"""Test that autocomplete returns empty list if token not in trie."""
from trie import Trie
trie = Trie()
token1 = "rifle"
token2 = "adze"
token3 = "rifleman"
trie.insert(token1)
trie.insert(token2)
trie.insert(token3)
auto = trie.autocomplete('goat')
assert auto == []
示例4: test_autocomplete_greater_than_five_possibilities
# 需要导入模块: from trie import Trie [as 别名]
# 或者: from trie.Trie import autocomplete [as 别名]
def test_autocomplete_greater_than_five_possibilities():
"""Test that autocomplete only returns a list of 4 when more available."""
from trie import Trie
trie = Trie()
rain = 'rain'
rain_s = "rain's"
rainy = 'rainy'
raining = 'raining'
rainstorm = 'rainstorm'
word_list = [rain, rain_s, rainy, raining, rainstorm]
for indx in word_list:
trie.insert(indx)
assert len(trie.autocomplete(rain)) == 4
示例5: test_autocomplete_two_words_from_root
# 需要导入模块: from trie import Trie [as 别名]
# 或者: from trie.Trie import autocomplete [as 别名]
def test_autocomplete_two_words_from_root():
"""Test that autocomplete functions when input token is at the root."""
from trie import Trie
trie = Trie()
token1 = "rifle"
token2 = "adze"
token3 = "rifleman"
trie.insert(token1)
trie.insert(token2)
trie.insert(token3)
auto = trie.autocomplete('rif')
assert token1 in auto
assert token3 in auto
assert token2 not in auto
示例6: test_autocomplete_not_from_root
# 需要导入模块: from trie import Trie [as 别名]
# 或者: from trie.Trie import autocomplete [as 别名]
def test_autocomplete_not_from_root():
"""Test that autocomplete functions when input token is in trie not root."""
from trie import Trie
trie = Trie()
rifle = "rifle"
adze = "adze"
rifleman = "rifleman"
owl = "owl"
owled = "owled"
owl_s = "owl's"
word_list = [rifle, adze, rifleman, owl, owled, owl_s]
for indx in word_list:
trie.insert(indx)
auto = trie.autocomplete('owl')
assert owl_s in auto
assert owled in auto
assert owl in auto
assert adze not in auto
示例7: SongServer
# 需要导入模块: from trie import Trie [as 别名]
# 或者: from trie.Trie import autocomplete [as 别名]
class SongServer(object):
def __init__(self, nbrs, topics, ids, song_mapping):
self.song_cache = {}
self.nbrs = nbrs
self.topics = topics
self.ids = ids
self.song_mapping = song_mapping
self.mid_to_id = {}
for i, mid in enumerate(self.ids):
self.mid_to_id[mid] = i
self.autocomplete = Trie()
self.name_map = {}
for mid, song in song_mapping.iteritems():
name = song[1]
fixed_name = name.lower().decode('utf-8')
if name.lower() not in self.name_map:
self.name_map[fixed_name] = set()
if mid in self.mid_to_id:
self.name_map[fixed_name].add((self.mid_to_id[mid], name, song[0]))
self.autocomplete.insert(fixed_name)
app = self.app = Flask(__name__, static_url_path='', static_folder='static/')
def jsonp(f):
"""Wraps JSONified output for JSONP"""
@wraps(f)
def decorated_function(*args, **kwargs):
callback = request.args.get('callback', False)
if callback:
content = str(callback) + '(' + str(f().data) + ')'
return app.response_class(content, mimetype='application/json')
else:
return f(*args, **kwargs)
return decorated_function
@app.route('/')
def index():
return app.send_static_file('index.html')
@app.route('/api/get_id')
@jsonp
@use_args({
'id': fields.Int(required=True),
'youtube': fields.Bool(default=False)
})
def get_id(args):
# if args['id'] in self.song_cache:
# return jsonify(**self.song_cache[args['id']])
topic = self.topics[args['id']].argmax()
song = self.song_mapping[self.ids[args['id']]]
aw = artwork.get_artwork(song)
youtube_id = None
if args['youtube']:
youtube_id = artwork.get_video(song)
result = {
'status': 1,
'result': {
'id': args['id'],
'name': song[1],
'genre': topic,
'artist': song[0],
'artwork': aw,
'youtube': youtube_id,
}
}
# self.song_cache[args['id']] = result
return jsonify(**result);
@app.route('/api/get_neighbors')
@jsonp
@use_args({
'id': fields.Int(required=True),
'k': fields.Int(default=11)
})
def get_neighbors(args):
_, neighbors = self.nbrs.kneighbors(self.topics[args['id']], n_neighbors=args['k'] + 1)
neighbors = neighbors[0, 1:].tolist()
genres = [self.topics[n].argmax() for n in neighbors]
return jsonify(**{
'status': 1,
'result': zip(neighbors, genres)
})
@app.route('/api/get_song')
@jsonp
@use_args({
'name': fields.Str(required=True),
})
def get_song(args):
if len(args['name']) < 3:
return jsonify(**{
'status': 1,
'result': []
})
results = self.autocomplete.autocomplete(args['name'].lower())
final_results = []
for result in results:
for final_result in list(self.name_map[result]):
final_results.append({
#.........这里部分代码省略.........
示例8: test_auto_complete_solo
# 需要导入模块: from trie import Trie [as 别名]
# 或者: from trie.Trie import autocomplete [as 别名]
def test_auto_complete_solo():
"""Test that auto complete will return the one word we are looking for."""
from trie import Trie
t = Trie()
t.insert("apple")
assert t.autocomplete("a") == ["apple"]
示例9: test_auto_complete_empty
# 需要导入模块: from trie import Trie [as 别名]
# 或者: from trie.Trie import autocomplete [as 别名]
def test_auto_complete_empty():
"""Test the auto complete works for nothing in tree."""
from trie import Trie
t = Trie()
assert t.autocomplete("a") == []