ekremcet commited on
Commit
b80c4ea
·
verified ·
1 Parent(s): f241bba

Fix spaces typed in the wrong place (birş ey -> bir şey)

Browse files
Files changed (1) hide show
  1. tinyfix_tr.py +25 -0
tinyfix_tr.py CHANGED
@@ -445,6 +445,7 @@ class TinyFixTR(_Rules):
445
  if not self.doc or any(c in SPEC for c in text): return 0.0
446
  n = sum(1 for t, _, _ in tk if WORD.match(t) and len(t) >= 3); return max(0.0, min(3.0, .6 * n - 2.6)) * 1.3
447
  def fix(self, texts, detail=False):
 
448
  tks = [tokenize(t) for t in texts]; sh = [self.shift(t, k) for t, k in zip(texts, tks)]
449
  base = self.decide; cur = [0]
450
  self._sh = 0.0
@@ -469,6 +470,30 @@ class TinyFixTR(_Rules):
469
  _sh = 0.0
470
 
471
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
472
  def main():
473
  ap = argparse.ArgumentParser(description="Turkish autocorrect: reads lines, writes corrected lines."); ap.add_argument("--mode", choices=("cautious", "full"), default="cautious"); ap.add_argument("text", nargs="*")
474
  a = ap.parse_args(); f = TinyFixTR(mode=a.mode)
 
445
  if not self.doc or any(c in SPEC for c in text): return 0.0
446
  n = sum(1 for t, _, _ in tk if WORD.match(t) and len(t) >= 3); return max(0.0, min(3.0, .6 * n - 2.6)) * 1.3
447
  def fix(self, texts, detail=False):
448
+ texts = [respace(self, t)[0] for t in texts] # a space typed in the wrong place (birş ey -> bir şey)
449
  tks = [tokenize(t) for t in texts]; sh = [self.shift(t, k) for t, k in zip(texts, tks)]
450
  base = self.decide; cur = [0]
451
  self._sh = 0.0
 
470
  _sh = 0.0
471
 
472
 
473
+ WS = re.compile(r"[^\W\d_]+")
474
+ def respace(F, text, minpair=150):
475
+ """a space typed in the wrong place between two words (birş ey -> bir şey, gelm iyor -> gelmiyor): re-split the joined letters where both halves are known
476
+ and often seen together, or join them if the joined form is a known word. Only when at least one of the two typed words is unknown."""
477
+ out, last, ms = [], 0, list(WS.finditer(text))
478
+ i = 0; changes = []
479
+ while i < len(ms) - 1:
480
+ a, b = ms[i], ms[i + 1]
481
+ if text[a.end():b.start()] != " " or not a.group().islower() or not b.group().islower(): i += 1; continue
482
+ u, v = lower(a.group()), lower(b.group())
483
+ if F.valid(u) and F.valid(v) or F.valid(u + "_" + v) or (u, v) in PAIRS: i += 1; continue
484
+ j = u + v; best = None
485
+ if j in F.lex.counts and F.lex.counts[j] >= 50: best = (F.lex.counts[j] * 10, j)
486
+ for k in range(1, len(j)):
487
+ x, y = j[:k], j[k:]
488
+ if (x, y) == (u, v) or len(y) < 2 or (k == 1 and x != "o"): continue
489
+ n = F.lex.counts.get(x + "_" + y, 0) * 10 if x + "_" + y in F.lex.counts else F.big.get(x + " " + y, 0) if F.valid(x) and F.valid(y) else 0
490
+ if n >= minpair and (best is None or n > best[0]): best = (n, x + " " + y)
491
+ if best:
492
+ out.append(text[last:a.start()]); out.append(best[1]); last = b.end(); changes.append((a.group() + " " + b.group(), best[1])); i += 2
493
+ else: i += 1
494
+ out.append(text[last:]); return "".join(out), changes
495
+
496
+
497
  def main():
498
  ap = argparse.ArgumentParser(description="Turkish autocorrect: reads lines, writes corrected lines."); ap.add_argument("--mode", choices=("cautious", "full"), default="cautious"); ap.add_argument("text", nargs="*")
499
  a = ap.parse_args(); f = TinyFixTR(mode=a.mode)