anonymous-stoicheia commited on
Commit
fa6605d
·
verified ·
1 Parent(s): 26ad77f

Processor: restore_respaced -- fill letters, then re-decide word division

Browse files
Files changed (1) hide show
  1. processing_char_bert.py +27 -0
processing_char_bert.py CHANGED
@@ -295,6 +295,33 @@ class CharBertProcessor:
295
  cap = batch["_cap"]
296
  return self._restore_polytonic(chars, dia, cap, pred_bnd, punct)
297
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
298
  def restore_elastic(self, model, text: str, min_width: int = 1,
299
  mask_dia_boundary: bool = False, predict_punct: bool = True,
300
  sentence_breaks: bool = True, gap_word_breaks: bool = True):
 
295
  cap = batch["_cap"]
296
  return self._restore_polytonic(chars, dia, cap, pred_bnd, punct)
297
 
298
+ def restore_respaced(self, model, text: str, **kw) -> str:
299
+ """Restore a gap, then re-decide word division on the completed text.
300
+
301
+ The five planes are predicted independently in one pass, which is fine when the
302
+ whole context is bare (everything is decided together) but unreliable when a gap
303
+ sits inside already-spaced text: the boundary head sees a half-known segmentation
304
+ and hedges, so a correctly restored word can come back cut in two.
305
+
306
+ This does it in the order an editor would: fill the letters first, throw away the
307
+ spacing entirely, and run the model again over the resulting *scriptio continua*
308
+ with the boundary and diacritic planes unknown everywhere -- the regime the model
309
+ was pretrained on. `text` may carry either a `-` run or a `[N±M]` marker.
310
+ """
311
+ if _ELASTIC_RE.search(text):
312
+ filled, _, _ = self.restore_elastic(model, text, **kw)
313
+ else:
314
+ batch = self(text)
315
+ with torch.no_grad():
316
+ out = model(**{k: v for k, v in batch.items() if not k.startswith("_")})
317
+ filled = self.decode_restoration(out, batch, **kw)
318
+ letters = "".join(ch for ch in unicodedata.normalize("NFD", filled)
319
+ if unicodedata.category(ch).startswith("L"))
320
+ batch = self(letters, mask_planes=["dia", "boundary"], has_boundaries=False)
321
+ with torch.no_grad():
322
+ out = model(**{k: v for k, v in batch.items() if not k.startswith("_")})
323
+ return self.decode_restoration(out, batch)
324
+
325
  def restore_elastic(self, model, text: str, min_width: int = 1,
326
  mask_dia_boundary: bool = False, predict_punct: bool = True,
327
  sentence_breaks: bool = True, gap_word_breaks: bool = True):