Fix spaces typed in the wrong place (birş ey -> bir şey)
Browse files444 unmodified lines44544644744844945045118 unmodified lines470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499444 unmodified lines if not self.doc or any(c in SPEC for c in text): return 0.0 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 def fix(self, texts, detail=False): texts = [respace(self, t)[0] for t in texts] # a space typed in the wrong place (birş ey -> bir şey) tks = [tokenize(t) for t in texts]; sh = [self.shift(t, k) for t, k in zip(texts, tks)] base = self.decide; cur = [0] self._sh = 0.018 unmodified lines _sh = 0.0
WS = re.compile(r"[^\W\d_]+")def respace(F, text, minpair=150): """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 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.""" out, last, ms = [], 0, list(WS.finditer(text)) i = 0; changes = [] while i < len(ms) - 1: a, b = ms[i], ms[i + 1] if text[a.end():b.start()] != " " or not a.group().islower() or not b.group().islower(): i += 1; continue u, v = lower(a.group()), lower(b.group()) if F.valid(u) and F.valid(v) or F.valid(u + "_" + v) or (u, v) in PAIRS: i += 1; continue j = u + v; best = None if j in F.lex.counts and F.lex.counts[j] >= 50: best = (F.lex.counts[j] * 10, j) for k in range(1, len(j)): x, y = j[:k], j[k:] if (x, y) == (u, v) or len(y) < 2 or (k == 1 and x != "o"): continue 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 if n >= minpair and (best is None or n > best[0]): best = (n, x + " " + y) if best: out.append(text[last:a.start()]); out.append(best[1]); last = b.end(); changes.append((a.group() + " " + b.group(), best[1])); i += 2 else: i += 1 out.append(text[last:]); return "".join(out), changes
def main(): 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="*") a = ap.parse_args(); f = TinyFixTR(mode=a.mode)tinyfix_tr.py+25