Skip to content

Instantly share code, notes, and snippets.

@ods94065
Last active July 26, 2019 02:42
Show Gist options
  • Save ods94065/5a26e39f519ac5d6bf7bb0233a563595 to your computer and use it in GitHub Desktop.
Save ods94065/5a26e39f519ac5d6bf7bb0233a563595 to your computer and use it in GitHub Desktop.
𐑕𐑨𐑥𐑐𐑩𐑤 𐑐𐑲𐑔𐑪𐑯 𐑕𐑒𐑮𐑦𐑐𐑑 𐑦𐑯 ·𐑖𐑱𐑝𐑾𐑯
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# 𐑩𐑛𐑨𐑐𐑑𐑩𐑛 𐑓𐑮𐑳𐑥: https://metacpan.org/release/Lingua-EN-Alphabet-Shaw/source/lib/Lingua/EN/Alphabet/Shaw.pm
import collections
import itertools
import os.path
import re
import sqlite3
import sys
# '𐑚𐑨𐑯𐑒' 𐑧𐑯𐑵𐑥𐑼𐑱𐑑𐑩𐑛 𐑝𐑨𐑤𐑿𐑟
𐑒_𐑖𐑱𐑝𐑾𐑯_𐑢𐑦𐑒𐑦 = 'W'
𐑒_𐑕𐑰𐑧𐑥𐑿_𐑛𐑦𐑒𐑑 = 'C'
𐑒_𐑨𐑯𐑛𐑮𐑩𐑒𐑤𐑰𐑟 = 'A'
𐑒_𐑳𐑯𐑯𐑴𐑯_𐑕𐑹𐑕 = '?'
𐑒_𐑳𐑯𐑯𐑴𐑯_𐑢𐑻𐑛 = 'U'
𐑒_𐑤𐑦𐑑𐑼𐑩𐑤 = 'L'
# '𐑐𐑰𐑧𐑕' 𐑧𐑯𐑵𐑥𐑼𐑱𐑑𐑩𐑛 𐑝𐑨𐑤𐑿𐑟
𐑒_𐑯𐑬𐑯 = 'n'
𐑒_𐑐𐑮𐑪𐑐𐑼_𐑯𐑬𐑯 = 'g'
𐑒_𐑯𐑪𐑑_𐑐𐑮𐑪𐑐𐑼_𐑯𐑬𐑯 = 'h'
𐑑_𐑮𐑦𐑟𐑳𐑤𐑑 = collections.namedtuple('𐑑_𐑮𐑦𐑟𐑳𐑤𐑑', ['𐑕𐑹𐑕', '𐑑𐑧𐑒𐑕𐑑', '𐑚𐑨𐑯𐑒', '𐑐𐑰𐑧𐑕', '𐑛𐑦𐑕𐑨𐑯𐑚𐑦𐑜𐑿𐑱𐑑𐑩𐑛'])
def _𐑕𐑹𐑕_𐑑_𐑚𐑨𐑯𐑒(𐑕𐑹𐑕):
if 𐑕𐑹𐑕 == 0:
return 𐑒_𐑖𐑱𐑝𐑾𐑯_𐑢𐑦𐑒𐑦
elif 𐑕𐑹𐑕 == 1:
return 𐑒_𐑕𐑰𐑧𐑥𐑿_𐑛𐑦𐑒𐑑
elif 𐑕𐑹𐑕 == 2:
return 𐑒_𐑨𐑯𐑛𐑮𐑩𐑒𐑤𐑰𐑟
else:
return 𐑒_𐑳𐑯𐑯𐑴𐑯_𐑕𐑹𐑕
def _𐑤𐑵𐑒𐑳𐑐(𐑒𐑻𐑕𐑼, 𐑐𐑮𐑧𐑝𐑐𐑰𐑧𐑕, 𐑢𐑻𐑛):
𐑒𐑻𐑕𐑼.execute('select shaw, pos, dab, source from words where latn=?', (𐑢𐑻𐑛.lower(),))
𐑮𐑴𐑟 = 𐑒𐑻𐑕𐑼.fetchall()
if not 𐑮𐑴𐑟:
return 𐑑_𐑮𐑦𐑟𐑳𐑤𐑑(𐑢𐑻𐑛, 𐑢𐑻𐑛, 𐑒_𐑳𐑯𐑯𐑴𐑯_𐑢𐑻𐑛, '', False)
if len(𐑮𐑴𐑟) == 1:
𐑖𐑷, 𐑐𐑰𐑧𐑕, _, 𐑕𐑹𐑕 = 𐑮𐑴𐑟[0]
return 𐑑_𐑮𐑦𐑟𐑳𐑤𐑑(𐑢𐑻𐑛, 𐑖𐑷, _𐑕𐑹𐑕_𐑑_𐑚𐑨𐑯𐑒(𐑕𐑹𐑕), 𐑐𐑰𐑧𐑕, False)
𐑒𐑨𐑯𐑛𐑦𐑛𐑩𐑑 = 𐑮𐑴𐑟[0]
for 𐑮𐑴 in 𐑮𐑴𐑟[1:]:
𐑛𐑨𐑥𐑚 = 𐑮𐑴[2]
if not 𐑛𐑨𐑥𐑚:
continue
if 𐑐𐑮𐑧𐑝𐑐𐑰𐑧𐑕 in 𐑛𐑨𐑥𐑚 or (𐑛𐑨𐑥𐑚 == 𐑒_𐑐𐑮𐑪𐑐𐑼_𐑯𐑬𐑯 and 𐑢𐑻𐑛[0].isupper()) or (𐑛𐑨𐑥𐑚 == 𐑒_𐑯𐑪𐑑_𐑐𐑮𐑪𐑐𐑼_𐑯𐑬𐑯 and 𐑢𐑻𐑛[0].islower()):
𐑒𐑨𐑯𐑛𐑦𐑛𐑩𐑑 = 𐑮𐑴
𐑖𐑷, 𐑐𐑰𐑧𐑕, _, 𐑕𐑹𐑕 = 𐑒𐑨𐑯𐑛𐑦𐑛𐑩𐑑
return 𐑑_𐑮𐑦𐑟𐑳𐑤𐑑(𐑢𐑻𐑛, 𐑖𐑷, _𐑕𐑹𐑕_𐑑_𐑚𐑨𐑯𐑒(𐑕𐑹𐑕), 𐑐𐑰𐑧𐑕, True)
def _𐑩𐑐𐑧𐑯𐑛_𐑤𐑦𐑑𐑼𐑩𐑤(𐑮𐑧𐑟𐑳𐑤𐑑𐑕, 𐑤𐑦𐑑):
if not 𐑤𐑦𐑑:
return
if len(𐑮𐑧𐑟𐑳𐑤𐑑𐑕) and 𐑮𐑧𐑟𐑳𐑤𐑑𐑕[-1].𐑚𐑨𐑯𐑒 == 𐑒_𐑤𐑦𐑑𐑼𐑩𐑤:
# 𐑩𐑐𐑧𐑯𐑛 𐑑 𐑞 𐑤𐑨𐑕𐑑 𐑤𐑦𐑑𐑼𐑩𐑤 𐑢𐑰 𐑕𐑷
𐑮𐑧𐑟𐑳𐑤𐑑𐑕[-1].𐑑𐑧𐑒𐑕𐑑 += 𐑤𐑦𐑑
else:
𐑮𐑧𐑟𐑳𐑤𐑑𐑕.append(𐑑_𐑮𐑦𐑟𐑳𐑤𐑑(𐑤𐑦𐑑, 𐑤𐑦𐑑, 𐑒_𐑤𐑦𐑑𐑼𐑩𐑤, '', False))
_𐑢𐑻𐑛_𐑥𐑨𐑗𐑼 = re.compile(
r"""
(?<!%) # 𐑛𐑴𐑯𐑑 𐑚𐑮𐑱𐑒 𐑳𐑐 𐑐𐑼𐑕𐑧𐑯𐑑 𐑩𐑕𐑒𐑱𐑐𐑕, %d 𐑯𐑯𐑯
(?<!\\) # 𐑛𐑴𐑯𐑑 𐑚𐑮𐑱𐑒 𐑳𐑐 𐑚𐑨𐑒𐑕𐑤𐑨𐑖 𐑩𐑕𐑒𐑱𐑐𐑕, \n 𐑯𐑯𐑯
\b
( [a-z] # 𐑢𐑳𐑯-𐑤𐑧𐑑𐑼 𐑢𐑻𐑛
| [a-z][a-z']*[a-z] # 𐑑𐑵- 𐑹 𐑥𐑹-𐑤𐑧𐑑𐑼 𐑢𐑻𐑛, 𐑐𐑪𐑕𐑦𐑚𐑤𐑰 𐑢𐑦𐑞 𐑩𐑯 𐑩𐑐𐑪𐑕𐑑𐑮𐑩𐑓𐑰
)
\b
""", re.IGNORECASE | re.VERBOSE)
def 𐑑𐑮𐑨𐑯𐑟𐑤𐑦𐑑𐑼𐑱𐑑_𐑛𐑰𐑑𐑱𐑤(𐑒𐑻𐑕𐑼, 𐑑𐑧𐑒𐑕𐑑):
𐑕𐑐𐑤𐑦𐑑𐑑𐑧𐑒𐑕𐑑 = _𐑢𐑻𐑛_𐑥𐑨𐑗𐑼.split(𐑑𐑧𐑒𐑕𐑑)
𐑤𐑦𐑑𐑕 = 𐑕𐑐𐑤𐑦𐑑𐑑𐑧𐑒𐑕𐑑[0::2]
𐑢𐑻𐑛𐑟 = 𐑕𐑐𐑤𐑦𐑑𐑑𐑧𐑒𐑕𐑑[1::2]
𐑮𐑧𐑟𐑳𐑤𐑑𐑕 = []
𐑐𐑮𐑧𐑝𐑐𐑰𐑧𐑕 = 𐑒_𐑯𐑬𐑯 # 𐑕𐑧𐑯𐑕𐑦𐑚𐑩𐑤 𐑛𐑰𐑓𐑶𐑤𐑑
for 𐑤𐑦𐑑, 𐑢𐑻𐑛 in itertools.zip_longest(𐑤𐑦𐑑𐑕, 𐑢𐑻𐑛𐑟):
if 𐑤𐑦𐑑:
_𐑩𐑐𐑧𐑯𐑛_𐑤𐑦𐑑𐑼𐑩𐑤(𐑮𐑧𐑟𐑳𐑤𐑑𐑕, 𐑤𐑦𐑑)
if 𐑢𐑻𐑛:
𐑮𐑧𐑟 = _𐑤𐑵𐑒𐑳𐑐(𐑒𐑻𐑕𐑼, 𐑐𐑮𐑧𐑝𐑐𐑰𐑧𐑕, 𐑢𐑻𐑛)
𐑐𐑮𐑧𐑝𐑐𐑰𐑧𐑕 = 𐑮𐑧𐑟.𐑐𐑰𐑧𐑕
𐑮𐑧𐑟𐑳𐑤𐑑𐑕.append(𐑮𐑧𐑟)
return 𐑮𐑧𐑟𐑳𐑤𐑑𐑕
def 𐑑𐑮𐑨𐑯𐑟𐑤𐑦𐑑𐑼𐑱𐑑(𐑒𐑻𐑕𐑼, 𐑑𐑧𐑒𐑕𐑑):
return ''.join(𐑮𐑧𐑟.𐑑𐑧𐑒𐑕𐑑 for 𐑮𐑧𐑟 in 𐑑𐑮𐑨𐑯𐑟𐑤𐑦𐑑𐑼𐑱𐑑_𐑛𐑰𐑑𐑱𐑤(𐑒𐑻𐑕𐑼, 𐑑𐑧𐑒𐑕𐑑))
def 𐑥𐑱𐑯(𐑸𐑜𐑟):
𐑝𐑲𐑤𐑯𐑱𐑥 = os.path.expanduser('~/.cache/shavian/shavian-set.sqlite')
if not os.path.exists(𐑝𐑲𐑤𐑯𐑱𐑥):
𐑝𐑲𐑤𐑯𐑱𐑥 = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'shavian-set.sqlite')
𐑒𐑩𐑯𐑧𐑒𐑖𐑩𐑯 = sqlite3.connect(𐑝𐑲𐑤𐑯𐑱𐑥)
𐑒𐑻𐑕𐑼 = 𐑒𐑩𐑯𐑧𐑒𐑖𐑩𐑯.cursor()
print(𐑑𐑮𐑨𐑯𐑟𐑤𐑦𐑑𐑼𐑱𐑑(𐑒𐑻𐑕𐑼, sys.stdin.read()))
return 0
if __name__ == '__main__':
𐑤𐑧𐑟 = 𐑥𐑱𐑯(sys.argv[1:])
sys.exit(𐑤𐑧𐑟)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment