shamy-tashih 0.2.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Omar Elshamy
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,127 @@
1
+ Metadata-Version: 2.4
2
+ Name: shamy-tashih
3
+ Version: 0.2.0
4
+ Summary: Recover text typed on the wrong keyboard layout, between any two layouts, and clean Arabic typos. Pure Python.
5
+ Author: Omar Elshamy
6
+ License: MIT
7
+ Project-URL: Homepage, https://github.com/omarelshamy25x/shamy-tashih
8
+ Project-URL: Source, https://github.com/omarelshamy25x/shamy-tashih
9
+ Project-URL: Issues, https://github.com/omarelshamy25x/shamy-tashih/issues
10
+ Keywords: keyboard-layout,typing,typos,arabic,russian,korean,pinyin,text-cleaning,python
11
+ Classifier: Programming Language :: Python :: 3
12
+ Classifier: Operating System :: OS Independent
13
+ Classifier: Natural Language :: Arabic
14
+ Classifier: Natural Language :: Russian
15
+ Classifier: Topic :: Text Processing :: Linguistic
16
+ Requires-Python: >=3.8
17
+ Description-Content-Type: text/markdown
18
+ License-File: LICENSE
19
+ Provides-Extra: test
20
+ Requires-Dist: pytest>=7; extra == "test"
21
+ Dynamic: license-file
22
+
23
+ # shamy-tashih
24
+
25
+ Recover text that was typed on the wrong keyboard layout, and clean up Arabic typos.
26
+
27
+ A user forgets to switch the keyboard, types a sentence, and gets `thilm rw]d` or `ghbdtn`
28
+ instead of what they meant. This library turns it back: it knows where every key sits on
29
+ each layout, so it can re-type the same keys in the layout that was meant, in either
30
+ direction, between any two layouts it knows.
31
+
32
+ ```python
33
+ >>> from shamy_tashih import convert, detect, recover
34
+ >>> convert("ghbdtn", "us", "ru") # typed on a US layout, meant Russian
35
+ 'привет'
36
+ >>> convert("привет", "ru", "us") # and the other way round
37
+ 'ghbdtn'
38
+ >>> convert("thilm rw]d", "us", "ar")
39
+ 'فاهمة قصدي'
40
+ >>> recover("thilm rw]d") # no layout named: decide from the text
41
+ 'فاهمة قصدي'
42
+ >>> detect("ghbdtn")[0]
43
+ us -> ar (1.00): لالايفى
44
+ ```
45
+
46
+ Pure Python, no dependencies, no network. Python 3.8 and up.
47
+
48
+ ## Install
49
+
50
+ ```
51
+ pip install shamy-tashih
52
+ ```
53
+
54
+ ## What it does
55
+
56
+ Three functions, in the order you would reach for them.
57
+
58
+ `convert(text, from_layout, to_layout)` re-types `text` exactly: every character is looked
59
+ up in the source layout to find the key that produced it, and that key is read in the
60
+ target layout. Nothing is guessed, so it is safe on mixed text; characters the source
61
+ layout cannot type are copied through.
62
+
63
+ `detect(text, limit=5)` ranks the readings of a line when you do not know the source
64
+ layout. Each candidate carries the pair of layouts, the converted text, a `score` for how
65
+ much the result looks like text in the target layout, a `confidence` (how far ahead of the
66
+ next distinct reading it is) and `needs_ime` for the pinyin case below. The reading where
67
+ the text is already correct is one of the candidates, so "leave it alone" competes with the
68
+ conversions instead of being assumed.
69
+
70
+ `recover(text, target="", source="")` picks one reading. With `target` it is an
71
+ instruction: the keys are re-typed in that layout. Without it, it only fires when one
72
+ reading clearly wins and returns the text unchanged otherwise, because a wrong guess is
73
+ worse than no guess. Text that already reads well is never changed.
74
+
75
+ Arabic also gets `fix()`, the cleanup that has nothing to do with the layout: keyboard
76
+ variant letters (ي/ی, ك/ک), invisible marks, tatweel, repeated letters and stray spaces.
77
+ `fix_layout()` is the Arabic-only shortcut, kept for compatibility with 0.1.0.
78
+
79
+ ## Layouts
80
+
81
+ `us`, `es`, `fr` (AZERTY), `de` (QWERTZ), `tr`, `pl`, `ar`, `fa`, `he`, `ru`, `uk`, `el`,
82
+ `th`, `hi` (Devanagari), `ko` (Dubeolsik). The tables come from the CLDR keyboard data for
83
+ Windows, so they are the layouts a real Windows keyboard installs, not a hand-typed guess.
84
+ Korean is the exception: the CLDR file keeps no jamo, so the Dubeolsik table is written by
85
+ hand and the jamo are joined into syllables the way an input method does (`gksrmf` becomes
86
+ `한글`).
87
+
88
+ ## What it does not do
89
+
90
+ - **It does not translate.** It gives back the same keys in another layout. If the result
91
+ is not a sentence, the input was not mislaid text.
92
+ - **Chinese is only half-solved, and that is honest.** A Chinese user who types with no
93
+ input method gets plain pinyin letters (`nihao`), and letters cannot be turned into
94
+ characters without a dictionary and a language model. `detect` recognises the case and
95
+ returns the pinyin flagged with `needs_ime=True`; `recover` leaves it alone. Everything
96
+ else here works on scripts, not on meaning.
97
+ - **Ambiguity is reported, not hidden.** `ghbdtn` is valid input for several layouts and
98
+ each conversion produces well-formed text in its own script, so `recover` refuses to pick
99
+ one. Pass `target=` when you know what was meant, or read `detect` yourself.
100
+ - **The score is a shape test, not a language model.** For the non-Latin layouts it is the
101
+ share of letters in the target's script; for Latin it adds a vowel check, because
102
+ `ghbdtn` is Latin letters but not a word. It cannot tell Arabic words from Arabic-shaped
103
+ noise.
104
+
105
+ ## Command line
106
+
107
+ ```
108
+ shamy-tashih "thilm rw]d" # Arabic cleanup
109
+ shamy-tashih --to ar "thilm rw]d" # re-type the keys in Arabic
110
+ shamy-tashih --to ru --from us "ghbdtn" # ... and in Russian
111
+ shamy-tashih --detect "ghbdtn" # rank the readings
112
+ shamy-tashih --layouts # list the layouts
113
+ echo "thilm rw]d" | shamy-tashih --to ar
114
+ ```
115
+
116
+ ## Contributing
117
+
118
+ Good first issues are labelled in the tracker. Tests are plain pytest, no network:
119
+
120
+ ```
121
+ pip install -e .[test]
122
+ pytest
123
+ ```
124
+
125
+ ## License
126
+
127
+ MIT.
@@ -0,0 +1,105 @@
1
+ # shamy-tashih
2
+
3
+ Recover text that was typed on the wrong keyboard layout, and clean up Arabic typos.
4
+
5
+ A user forgets to switch the keyboard, types a sentence, and gets `thilm rw]d` or `ghbdtn`
6
+ instead of what they meant. This library turns it back: it knows where every key sits on
7
+ each layout, so it can re-type the same keys in the layout that was meant, in either
8
+ direction, between any two layouts it knows.
9
+
10
+ ```python
11
+ >>> from shamy_tashih import convert, detect, recover
12
+ >>> convert("ghbdtn", "us", "ru") # typed on a US layout, meant Russian
13
+ 'привет'
14
+ >>> convert("привет", "ru", "us") # and the other way round
15
+ 'ghbdtn'
16
+ >>> convert("thilm rw]d", "us", "ar")
17
+ 'فاهمة قصدي'
18
+ >>> recover("thilm rw]d") # no layout named: decide from the text
19
+ 'فاهمة قصدي'
20
+ >>> detect("ghbdtn")[0]
21
+ us -> ar (1.00): لالايفى
22
+ ```
23
+
24
+ Pure Python, no dependencies, no network. Python 3.8 and up.
25
+
26
+ ## Install
27
+
28
+ ```
29
+ pip install shamy-tashih
30
+ ```
31
+
32
+ ## What it does
33
+
34
+ Three functions, in the order you would reach for them.
35
+
36
+ `convert(text, from_layout, to_layout)` re-types `text` exactly: every character is looked
37
+ up in the source layout to find the key that produced it, and that key is read in the
38
+ target layout. Nothing is guessed, so it is safe on mixed text; characters the source
39
+ layout cannot type are copied through.
40
+
41
+ `detect(text, limit=5)` ranks the readings of a line when you do not know the source
42
+ layout. Each candidate carries the pair of layouts, the converted text, a `score` for how
43
+ much the result looks like text in the target layout, a `confidence` (how far ahead of the
44
+ next distinct reading it is) and `needs_ime` for the pinyin case below. The reading where
45
+ the text is already correct is one of the candidates, so "leave it alone" competes with the
46
+ conversions instead of being assumed.
47
+
48
+ `recover(text, target="", source="")` picks one reading. With `target` it is an
49
+ instruction: the keys are re-typed in that layout. Without it, it only fires when one
50
+ reading clearly wins and returns the text unchanged otherwise, because a wrong guess is
51
+ worse than no guess. Text that already reads well is never changed.
52
+
53
+ Arabic also gets `fix()`, the cleanup that has nothing to do with the layout: keyboard
54
+ variant letters (ي/ی, ك/ک), invisible marks, tatweel, repeated letters and stray spaces.
55
+ `fix_layout()` is the Arabic-only shortcut, kept for compatibility with 0.1.0.
56
+
57
+ ## Layouts
58
+
59
+ `us`, `es`, `fr` (AZERTY), `de` (QWERTZ), `tr`, `pl`, `ar`, `fa`, `he`, `ru`, `uk`, `el`,
60
+ `th`, `hi` (Devanagari), `ko` (Dubeolsik). The tables come from the CLDR keyboard data for
61
+ Windows, so they are the layouts a real Windows keyboard installs, not a hand-typed guess.
62
+ Korean is the exception: the CLDR file keeps no jamo, so the Dubeolsik table is written by
63
+ hand and the jamo are joined into syllables the way an input method does (`gksrmf` becomes
64
+ `한글`).
65
+
66
+ ## What it does not do
67
+
68
+ - **It does not translate.** It gives back the same keys in another layout. If the result
69
+ is not a sentence, the input was not mislaid text.
70
+ - **Chinese is only half-solved, and that is honest.** A Chinese user who types with no
71
+ input method gets plain pinyin letters (`nihao`), and letters cannot be turned into
72
+ characters without a dictionary and a language model. `detect` recognises the case and
73
+ returns the pinyin flagged with `needs_ime=True`; `recover` leaves it alone. Everything
74
+ else here works on scripts, not on meaning.
75
+ - **Ambiguity is reported, not hidden.** `ghbdtn` is valid input for several layouts and
76
+ each conversion produces well-formed text in its own script, so `recover` refuses to pick
77
+ one. Pass `target=` when you know what was meant, or read `detect` yourself.
78
+ - **The score is a shape test, not a language model.** For the non-Latin layouts it is the
79
+ share of letters in the target's script; for Latin it adds a vowel check, because
80
+ `ghbdtn` is Latin letters but not a word. It cannot tell Arabic words from Arabic-shaped
81
+ noise.
82
+
83
+ ## Command line
84
+
85
+ ```
86
+ shamy-tashih "thilm rw]d" # Arabic cleanup
87
+ shamy-tashih --to ar "thilm rw]d" # re-type the keys in Arabic
88
+ shamy-tashih --to ru --from us "ghbdtn" # ... and in Russian
89
+ shamy-tashih --detect "ghbdtn" # rank the readings
90
+ shamy-tashih --layouts # list the layouts
91
+ echo "thilm rw]d" | shamy-tashih --to ar
92
+ ```
93
+
94
+ ## Contributing
95
+
96
+ Good first issues are labelled in the tracker. Tests are plain pytest, no network:
97
+
98
+ ```
99
+ pip install -e .[test]
100
+ pytest
101
+ ```
102
+
103
+ ## License
104
+
105
+ MIT.
@@ -0,0 +1,38 @@
1
+ [build-system]
2
+ requires = ["setuptools>=61"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [project]
6
+ name = "shamy-tashih"
7
+ version = "0.2.0"
8
+ description = "Recover text typed on the wrong keyboard layout, between any two layouts, and clean Arabic typos. Pure Python."
9
+ readme = "README.md"
10
+ license = { text = "MIT" }
11
+ authors = [{ name = "Omar Elshamy" }]
12
+ requires-python = ">=3.8"
13
+ keywords = ["keyboard-layout", "typing", "typos", "arabic", "russian", "korean", "pinyin", "text-cleaning", "python"]
14
+ classifiers = [
15
+ "Programming Language :: Python :: 3",
16
+ "Operating System :: OS Independent",
17
+ "Natural Language :: Arabic",
18
+ "Natural Language :: Russian",
19
+ "Topic :: Text Processing :: Linguistic",
20
+ ]
21
+
22
+ [project.urls]
23
+ Homepage = "https://github.com/omarelshamy25x/shamy-tashih"
24
+ Source = "https://github.com/omarelshamy25x/shamy-tashih"
25
+ Issues = "https://github.com/omarelshamy25x/shamy-tashih/issues"
26
+
27
+ [project.scripts]
28
+ shamy-tashih = "shamy_tashih.__main__:main"
29
+
30
+ [project.optional-dependencies]
31
+ test = ["pytest>=7"]
32
+
33
+ [tool.setuptools]
34
+ packages = ["shamy_tashih"]
35
+
36
+ [tool.pytest.ini_options]
37
+ testpaths = ["tests"]
38
+ pythonpath = ["."]
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,89 @@
1
+ """shamy-tashih: recover text typed on the wrong keyboard layout, and clean Arabic typos.
2
+
3
+ The library does one job: work out what a line of text was meant to say when the keyboard
4
+ layout was wrong, for any pair of layouts, in either direction. Every layout is a table
5
+ from physical key position to character, so a conversion is exact, and a small scorer
6
+ ranks the possible readings when the source layout is unknown.
7
+
8
+ Arabic gets one extra module for the mistakes that are not about the layout at all:
9
+ keyboard-variant letters, invisible marks, tatweel, repeated letters and stray spaces.
10
+
11
+ >>> from shamy_tashih import convert, fix_layout, recover
12
+ >>> convert("ghbdtn", "us", "ru")
13
+ '\u043f\u0440\u0438\u0432\u0435\u0442'
14
+ >>> convert("thilm rw]d", "us", "ar")
15
+ '\u0641\u0627\u0647\u0645\u0629 \u0642\u0635\u062f\u064a'
16
+ >>> recover("ghbdtn", target="ru")
17
+ '\u043f\u0440\u0438\u0432\u0435\u0442'
18
+ >>> fix_layout("thilm rw]d")
19
+ '\u0641\u0627\u0647\u0645\u0629 \u0642\u0635\u062f\u064a'
20
+ """
21
+
22
+ from __future__ import annotations
23
+
24
+ from ._detect import has_arabic, is_arabizi
25
+ from ._keyboard import (
26
+ Candidate,
27
+ compose_hangul,
28
+ convert,
29
+ coverage,
30
+ detect,
31
+ layout_label,
32
+ layout_names,
33
+ recover,
34
+ score,
35
+ )
36
+ from ._layout import fix_layout, looks_mislaid, to_arabic_keys, to_latin_keys
37
+ from ._pinyin import looks_like_pinyin
38
+ from ._spacing import fix_spacing, squeeze_repeats
39
+ from ._variants import fix_variants
40
+
41
+ __version__ = "0.2.0"
42
+
43
+ __all__ = [
44
+ "convert",
45
+ "detect",
46
+ "recover",
47
+ "score",
48
+ "coverage",
49
+ "compose_hangul",
50
+ "layout_names",
51
+ "layout_label",
52
+ "looks_like_pinyin",
53
+ "Candidate",
54
+ "fix",
55
+ "fix_layout",
56
+ "fix_spacing",
57
+ "fix_variants",
58
+ "has_arabic",
59
+ "is_arabizi",
60
+ "looks_mislaid",
61
+ "squeeze_repeats",
62
+ "to_arabic_keys",
63
+ "to_latin_keys",
64
+ ]
65
+
66
+
67
+ def fix(text: str, layout: bool = False, digits: bool = False) -> str:
68
+ """Repair the common Arabic typing mistakes in ``text``.
69
+
70
+ By default this folds keyboard-variant letters, removes invisible marks and tatweel,
71
+ squeezes repeated letters and tidies spacing. It never changes the layout unless
72
+ ``layout=True``, because decoding the wrong-layout direction on a line that was not
73
+ mislaid turns real English into Arabic noise. With ``layout=True`` the line is decoded
74
+ only when :func:`looks_mislaid` recognises it.
75
+
76
+ Franco-Arabic is returned unchanged, and so is any text with no Arabic in it and
77
+ nothing to repair. For other languages use :func:`convert`, :func:`detect` and
78
+ :func:`recover`.
79
+ """
80
+ if not isinstance(text, str):
81
+ raise TypeError("fix() expects a str, got %s" % type(text).__name__)
82
+ if is_arabizi(text):
83
+ return text
84
+ out = text
85
+ if layout and looks_mislaid(out):
86
+ out = fix_layout(out)
87
+ out = fix_variants(out, digits=digits)
88
+ out = fix_spacing(out)
89
+ return out
@@ -0,0 +1,62 @@
1
+ """Command line entry point.
2
+
3
+ shamy-tashih "thilm rw]d" clean Arabic typos
4
+ shamy-tashih --to ar "thilm rw]d" type the same keys in Arabic
5
+ shamy-tashih --to ru --from us "ghbdtn" ... and in Russian
6
+ shamy-tashih --detect "ghbdtn" rank the readings of a line
7
+ shamy-tashih --layouts list the layouts
8
+ """
9
+
10
+ from __future__ import annotations
11
+
12
+ import argparse
13
+ import sys
14
+
15
+ from . import __version__, convert, detect, fix, layout_names, looks_mislaid, recover
16
+
17
+
18
+ def main(argv=None) -> int:
19
+ parser = argparse.ArgumentParser(
20
+ prog="shamy_tashih",
21
+ description="Recover text typed on the wrong keyboard layout, and clean Arabic typos.",
22
+ )
23
+ parser.add_argument("text", nargs="*", help="text to work on; reads standard input when empty")
24
+ parser.add_argument("--to", metavar="LAYOUT", help="layout the text was meant to be in, such as ar or ru")
25
+ parser.add_argument("--from", dest="source", metavar="LAYOUT", default="", help="layout that was actually active (default: US)")
26
+ parser.add_argument("--detect", action="store_true", help="rank the possible readings instead of choosing one")
27
+ parser.add_argument("--recover", action="store_true", help="choose a reading only when it is convincing")
28
+ parser.add_argument("--layouts", action="store_true", help="list the known layouts and exit")
29
+ parser.add_argument("--layout", action="store_true", help="decode wrong-layout Arabic")
30
+ parser.add_argument("--digits", action="store_true", help="convert Arabic-Indic digits to ASCII digits")
31
+ parser.add_argument("--version", action="version", version="shamy-tashih " + __version__)
32
+ args = parser.parse_args(argv)
33
+
34
+ if args.layouts:
35
+ sys.stdout.write(" ".join(layout_names()) + "\n")
36
+ return 0
37
+
38
+ text = " ".join(args.text) if args.text else sys.stdin.read()
39
+ if not text:
40
+ parser.error("nothing to work on: pass text or pipe it in")
41
+
42
+ if args.detect:
43
+ for candidate in detect(text):
44
+ mark = " (needs an input method)" if candidate.needs_ime else ""
45
+ sys.stdout.write("%s%s\n" % (candidate, mark))
46
+ return 0
47
+
48
+ if args.to:
49
+ result = convert(text, args.source or "us", args.to)
50
+ elif args.recover or args.layout:
51
+ result = recover(text, target="ar" if args.layout else "")
52
+ if args.layout and not looks_mislaid(text) and result == text:
53
+ sys.stderr.write("note: the input does not look like wrong-layout text, so no layout change was made\n")
54
+ else:
55
+ result = fix(text, digits=args.digits)
56
+
57
+ sys.stdout.write(result if result.endswith("\n") else result + "\n")
58
+ return 0
59
+
60
+
61
+ if __name__ == "__main__":
62
+ raise SystemExit(main())
@@ -0,0 +1,39 @@
1
+ """Telling Arabic, Franco-Arabic (arabizi) and plain Latin text apart."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import re
6
+
7
+ #: Arabic letters, including the presentation forms and the Arabic Supplement block.
8
+ _ARABIC_LETTER = re.compile(r"[\u0621-\u064a\u066e-\u06d3\u0750-\u077f\ufb50-\ufdff\ufe70-\ufefc]")
9
+
10
+ #: The digits arabizi uses for letters: 2 (qaf or hamza), 3 (ayn), 5 (kha), 7 (haa), 9 (sad).
11
+ ARABIZI_DIGITS = frozenset("23579")
12
+
13
+
14
+ def has_arabic(text: str) -> bool:
15
+ """Return True when ``text`` contains at least one Arabic letter."""
16
+ return bool(_ARABIC_LETTER.search(text))
17
+
18
+
19
+ def is_arabizi(text: str) -> bool:
20
+ """Return True when ``text`` looks like Franco-Arabic written in Latin letters.
21
+
22
+ Franco-Arabic puts a digit inside a Latin word where an Arabic letter belongs, as in
23
+ ``3ayez`` or ``a2ol``. That is the test used here: no Arabic letters, at least one
24
+ Latin letter, and at least one arabizi digit sitting next to a letter. Plain English
25
+ such as ``hello there`` has no such digit and is not arabizi, and neither is a line of
26
+ ordinary Arabic.
27
+ """
28
+ if has_arabic(text):
29
+ return False
30
+ if not any(char.isalpha() for char in text):
31
+ return False
32
+ for index, char in enumerate(text):
33
+ if char not in ARABIZI_DIGITS:
34
+ continue
35
+ before = text[index - 1] if index else ""
36
+ after = text[index + 1] if index + 1 < len(text) else ""
37
+ if before.isalpha() or after.isalpha():
38
+ return True
39
+ return False