forked from HIT-SCIR/ltp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
2. 修复了 自定义词典 不起效果的问题
- Loading branch information
Showing
3 changed files
with
18 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,11 @@ | ||
#! /usr/bin/env python | ||
# -*- coding: utf-8 -*_ | ||
# Author: jeffrey | ||
# Author: Yunlong Feng <[email protected]> | ||
|
||
import os | ||
from typing import List | ||
|
||
import pygtrie | ||
|
||
|
||
|
@@ -43,24 +46,14 @@ def add_words(self, words): | |
for word in words: | ||
self[word] = True | ||
|
||
def maximum_forward_matching(self, text: str): | ||
def maximum_forward_matching(self, text: List[str]): | ||
maximum_matching_pos = [] | ||
start = 0 | ||
text_len = len(text.strip()) | ||
while start < text_len: | ||
text_len = len(text) | ||
for start in range(text_len - 1): | ||
candidate = None | ||
for end in range(1, self.max_window + 1): | ||
if start + end - 1 < text_len and self[text[start:end]]: | ||
candidate = (start, start + end) | ||
if end == self.max_window: | ||
if candidate: | ||
maximum_matching_pos.append(candidate) | ||
start = candidate[1] - 1 | ||
break | ||
elif start + end - 1 >= text_len: | ||
if candidate: | ||
maximum_matching_pos.append(candidate) | ||
start = candidate[1] - 1 | ||
break | ||
start = start + 1 | ||
for end in range(start + 1, min(text_len, start + self.max_window + 1)): | ||
if self.get("".join(text[start:end]), False): | ||
candidate = (start, end) | ||
if candidate: | ||
maximum_matching_pos.append(candidate) | ||
return maximum_matching_pos |