Skip to content

Commit 36855dd

Browse files
committed
Added new codec: navajo
1 parent 55b0c90 commit 36855dd

4 files changed

Lines changed: 54 additions & 0 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ This library extends the native `codecs` library and provides some new encodings
2727
`leetspeak` | text <-> leetspeak encoded text | based on minimalistic elite speaking rules
2828
`markdown` | markdown --> HTML | unidirectional
2929
`morse` | text <-> morse encoded text | uses whitespace as a separator
30+
`navajo` | text <-> Navajo | only handles letters (not full words from the Navajo dictionary)
3031
`octal` | text <-> octal digits | dummy octal conversion (converts to 3-digits groups)
3132
`ordinal` | text <-> ordinal digits | dummy character ordinals conversion (converts to 3-digits groups)
3233
`radio` | text <-> radio words | aka NATO or radio phonetic alphabet

codext/languages/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from .braille import *
33
from .leetspeak import *
44
from .morse import *
5+
from .navajo import *
56
from .radio import *
67
from .tomtom import *
78

codext/languages/navajo.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# -*- coding: UTF-8 -*-
2+
"""Navajo Codec - Navajo code content encoding.
3+
4+
This codec:
5+
- en/decodes strings from str to str
6+
- en/decodes strings from bytes to bytes
7+
- decodes file content to str (read)
8+
- encodes file content from str to bytes (write)
9+
"""
10+
from ..__common__ import *
11+
12+
13+
__examples__ = {'enc-dec(navajo)': ["this is a test", "THIS\nIS\nA\nTEST"]}
14+
15+
16+
# source: https://www.history.navy.mil/research/library/online-reading-room/title-list-alphabetically/n/navajo-code-talker-dictionary.html
17+
ENCMAP = {
18+
'A': ["WOL-LA-CHEE", "BE-LA-SANA", "TSE-NILL"], 'B': ["NA-HASH-CHID", "SHUSH", "TOISH-JEH"],
19+
'C': ["MOASI", "TLA-GIN", "BA-GOSHI"], 'D': ["BE", "CHINDI", "LHA-CHA-EH"], 'E': ["AH-JAH", "DZEH", "AH-NAH"],
20+
'F': ["CHUO", "TSA-E-DONIN-EE", "MA-E"], 'G': ["AH-TAD", "KLIZZIE", "JEHA"], 'H': ["TSE-GAH", "CHA", "LIN"],
21+
'I': ["TKIN", "YEH-HES", "A-CHI"], 'J': ["TKELE-CHO-G", "AH-YA-TSINNE", "YIL-DOI"],
22+
'K': ["JAD-HO-LONI", "BA-AH-NE-DI-TININ", "KLIZZIE-YAZZIE"], 'L': ["DIBEH-YAZZIE", "AH-JAD", "NASH-DOIE-TSO"],
23+
'M': ["TSIN-TLITI", "BE-TAS-TNI", "NA-AS-TSO-SI"], 'N': ["TSAH", "A-CHIN"],
24+
'O': ["A-KHA", "TLO-CHIN", "NE-AHS-JAH"], 'P': ["CLA-GI-AIH", "BI-SO-DIH", "NE-ZHONI"], 'Q': "CA-YEILTH",
25+
'R': ["GAH", "DAH-NES-TSA", "AH-LOSZ"], 'S': ["DIBEH", "KLESH"], 'T': ["D-AH", "A-WOH", "THAN-ZIE"],
26+
'U': ["SHI-DA", "NO-DA-IH"], 'V': "A-KEH-DI-GLINI", 'W': "GLOE-IH", 'X': "AL-NA-AS-DZOH", 'Y': "TSAH-AS-ZIH",
27+
'Z': "BESH-DO-TLIZ",
28+
' ': "-", '\n': "\n",
29+
'0': "0", '1': "1", '2': "2", '3': "3", '4': "4", '5': "5", '6': "6", '7': "7", '8': "8", '9': "9",
30+
}
31+
32+
33+
add_map("navajo", ENCMAP, ignore_case="both", sep=" ", pattern=r"^navajo$")
34+

docs/encodings.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,24 @@ It supports of course letters and digits, but also a few special characters: `.,
226226

227227
-----
228228

229+
### Navajo Code
230+
231+
It implements the letters from the [Navajo Code Talkers' Dictionary](https://www.history.navy.mil/research/library/online-reading-room/title-list-alphabetically/n/navajo-code-talker-dictionary.html). It conserves digits and newlines.
232+
233+
**Codec** | **Conversions** | **Aliases** | **Comment**
234+
:---: | :---: | --- | ---
235+
`navajo` | text <-> Navajo | |
236+
237+
```python
238+
>>> import codext
239+
>>> codext.encode("this is a test 123", "navajo")
240+
'a-woh cha tkin klesh - a-chi klesh - be-la-sana - a-woh dzeh klesh a-woh - 1 2 3'
241+
>>> codext.decode("a-woh cha tkin klesh - a-chi klesh - be-la-sana - a-woh dzeh klesh a-woh - 1 2 3", "navajo")
242+
'this is a test 123'
243+
```
244+
245+
-----
246+
229247
### Octal
230248

231249
This simple codec converts characters into their octal values.

0 commit comments

Comments
 (0)