예제 #1
0
 def test_startswith(self):
     pd = PrefixSet()
     keys = [''.join(combo) for combo in itertools.product('abc', repeat=3)]
     for key in reversed(keys):
         pd.add(key)
     subset = [k for k in keys if k.startswith('ab')]
     self.assertSequenceEqual(subset, list(pd.startswith('ab')))
 def test_startswith(self):
     pd = PrefixSet()
     keys = ["".join(combo) for combo in itertools.product("abc", repeat=3)]
     for key in reversed(keys):
         pd.add(key)
     subset = [k for k in keys if k.startswith("ab")]
     self.assertSequenceEqual(subset, list(pd.startswith("ab")))
예제 #3
0
 def test_startswith_empty(self):
     pd = PrefixSet()
     pd.add('a')
     self.assertSequenceEqual([], list(pd.startswith('b')))
 def test_startswith_empty(self):
     pd = PrefixSet()
     pd.add("a")
     self.assertSequenceEqual([], list(pd.startswith("b")))
예제 #5
0
파일: places.py 프로젝트: zh-tbug/bixin
# -*- coding: utf-8 -*-

import json
import os
import re
from prefixtree import PrefixSet

file = os.path.join(os.path.dirname(__file__), "../dictionaries/areas.json")
places = os.path.join(os.path.dirname(__file__), "../dictionaries/places.txt")

import ast
ps = PrefixSet()
with open(file, 'r') as f,\
        open(places, 'w') as out:
    content = f.read()
    spaces = re.findall('"[^"]+"', content)
    for s in spaces:
        space = ast.literal_eval(s)
        ps.add(space)
        out.write("%s 30000 ns\n" % space)  # 北京 34488 ns

assert "大连" not in ps

assert ps.startswith("大连")

for x in ps.startswith("大连"):
    print(x)