-
Notifications
You must be signed in to change notification settings - Fork 6
/
chinese-gtranslate-poem.py
executable file
·49 lines (40 loc) · 1.42 KB
/
chinese-gtranslate-poem.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/usr/bin/env python
# -*- coding: utf-8
# Automates the generation of the original text side of
# google translate poems like the ones in this thread:
# https://twitter.com/metasynthie/status/859743651282509824
# Uses chinese rather than japanese because random kanji
# should have more interesting meaning than random kana.
# However, it's straightforward to replace the range list
# with character ranges for kana.
# We omit extensions past A because unichr() doesn't like
# unicode characters higher than 0x10k
jpMode=False
jpMode=True
import codecs
import sys
UTF8Writer = codecs.getwriter('utf8')
sys.stdout = UTF8Writer(sys.stdout)
from random import Random
random=Random()
def randCharInRange(start, end):
rangeLen=end-start
codePoint=start+random.choice(range(0, rangeLen))
if(codePoint<255):
return chr(codePoint)
return unichr(codePoint)
ranges=[]
if(jpMode):
ranges.append((0x30a0, 0x30ff)) # katakana
ranges.append((0x3040, 0x309f)) # hirigana
ranges.append((0x4e00,0x9fff)) # CJK Unified Ideographs - common
ranges.append((0x3400,0x4dbf)) # CJK Unified Ideographs Extension A - rare
#ranges.append((0x20000,0x2a6df)) # CJK Unified Ideographs Extension B - rare, historic
#ranges.append((0x2a700,0x2b73f)) # CJK Unified Ideographs Extension C - rare, historic
ax=""
for i in range(0, 20):
candidates=[]
for r in ranges:
candidates.append(randCharInRange(r[0], r[1]))
ax+=random.choice(candidates)
print(ax)