基于AC自动机实现用户干预的功能
| 32 | |
| 33 | |
| 34 | class 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 = [] |
no outgoing calls
no test coverage detected