MCPcopy Create free account
hub / github.com/baidu/lac / Customization

Class Customization

python/LAC/custom.py:34–135  ·  view source on GitHub ↗

基于AC自动机实现用户干预的功能

Source from the content-addressed store, hash-verified

32
33
34class Customization(object):
35 """
36 基于AC自动机实现用户干预的功能
37 """
38
39 def __init__(self):
40 self.dictitem = {}
41 self.ac = None
42 pass
43
44 def add_word(self, words, sep=None):
45 """装载人工干预词典(单词输入)"""
46 if self.ac is None:
47 self.ac = TriedTree()
48 words = strdecode(words)
49 if sep == None:
50 words = words.strip().split()
51 else:
52 sep = strdecode(sep)
53 words = words.strip().split(sep)
54
55 if len(words) == 0:
56 return
57
58 phrase = ""
59 tags = []
60 offset = []
61 for word in words:
62 if word.rfind('/') < 1:
63 phrase += word
64 tags.append('')
65 else:
66 phrase += word[:word.rfind('/')]
67 tags.append(word[word.rfind('/') + 1:])
68 offset.append(len(phrase))
69
70 if len(phrase) < 2 and tags[0] == '':
71 return
72
73 self.dictitem[phrase] = (tags, offset)
74 self.ac.add_word(phrase)
75
76 def load_customization(self, filename, sep=None):
77 """装载人工干预词典"""
78 self.ac = TriedTree()
79 with open(filename, 'r', encoding='utf8') as f:
80 for line in f:
81 if sep == None:
82 words = line.strip().split()
83 else:
84 sep = strdecode(sep)
85 words = line.strip().split(sep)
86
87 if len(words) == 0:
88 continue
89
90 phrase = ""
91 tags = []

Callers 3

custom.pyFile · 0.70
load_customizationMethod · 0.70
add_wordMethod · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected