Upload khmer_segmentation.py with huggingface_hub
Browse files- khmer_segmentation.py +7 -10
khmer_segmentation.py
CHANGED
|
@@ -58,11 +58,11 @@ class GazetteerMasker:
|
|
| 58 |
return self.pattern.sub(repl, text), spans
|
| 59 |
|
| 60 |
def restore(self, tokens: List[str], spans: List[str]) -> List[str]:
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
return
|
| 66 |
|
| 67 |
|
| 68 |
class NonKhmerMasker:
|
|
@@ -78,11 +78,8 @@ class NonKhmerMasker:
|
|
| 78 |
return NON_KHMER_BLOCK.sub(repl, text), spans
|
| 79 |
|
| 80 |
def restore(self, tokens: List[str], spans: List[str]) -> List[str]:
|
| 81 |
-
|
| 82 |
-
for t in tokens
|
| 83 |
-
m = _NK_PATTERN.match(t)
|
| 84 |
-
out.append(spans[int(m.group(1))] if m else t)
|
| 85 |
-
return out
|
| 86 |
|
| 87 |
|
| 88 |
class LatinSplitter:
|
|
|
|
| 58 |
return self.pattern.sub(repl, text), spans
|
| 59 |
|
| 60 |
def restore(self, tokens: List[str], spans: List[str]) -> List[str]:
|
| 61 |
+
# .sub(), not .match(): khmer-nltk can fuse a masked span directly onto adjacent
|
| 62 |
+
# Khmer text with no space (e.g. "ការ" + mask -> one token "ការ3"), and
|
| 63 |
+
# .match() only fires when the sentinel is the ENTIRE token — otherwise it silently
|
| 64 |
+
# leaves the raw sentinel + index number in the output instead of the real span.
|
| 65 |
+
return [_GAZ_PATTERN.sub(lambda m: spans[int(m.group(1))], t) for t in tokens]
|
| 66 |
|
| 67 |
|
| 68 |
class NonKhmerMasker:
|
|
|
|
| 78 |
return NON_KHMER_BLOCK.sub(repl, text), spans
|
| 79 |
|
| 80 |
def restore(self, tokens: List[str], spans: List[str]) -> List[str]:
|
| 81 |
+
# See GazetteerMasker.restore() — same fused-token bug, same .sub() fix.
|
| 82 |
+
return [_NK_PATTERN.sub(lambda m: spans[int(m.group(1))], t) for t in tokens]
|
|
|
|
|
|
|
|
|
|
| 83 |
|
| 84 |
|
| 85 |
class LatinSplitter:
|