-
Notifications
You must be signed in to change notification settings - Fork 1
/
yuhao.py
executable file
·181 lines (153 loc) · 5.97 KB
/
yuhao.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#!/usr/local/bin/python3
import re
import json
import requests
from Common import getCurrentVersion as ver
from Common import file
from Common import loadYml
from Common import load
from Common import gen
from Common import conf
globalProxySettings = {
"http": "http://127.0.0.1:7890"
}
def parseCsvSp(content, head=True, web=False):
delim = ","
lines = content.splitlines()
if(head == True):
del lines[0]
finItems = []
for line in lines:
items = line.split(delim)
if web == True:
fcItem = {
"char": items[0],
"fullcode": items[2]
}
else:
fcItem = {
"char": items[0],
"fullcode": items[1]
}
finItems.append(fcItem)
return finItems
def toLowercase(ctt):
return ctt.lower()
def convertSpecialAlphabetVariation(fullcode):
return re.sub(r'ⓐ|ⓑ|ⓒ|ⓓ|ⓔ|ⓕ|ⓖ|ⓗ|ⓘ|ⓙ|ⓚ|ⓛ|ⓜ|ⓝ|ⓞ|ⓟ|ⓠ|ⓡ|ⓢ|ⓣ|ⓤ|ⓥ|ⓦ|ⓧ|ⓨ|ⓩ',
lambda x: chr(ord(x.group()) - 9424), fullcode)
def parseFullDict(filePath):
skip = False
csv = ""
with open(filePath, 'r', encoding='utf-8') as infile:
for line in infile:
if line.strip() == '---':
skip = True
continue
if line.strip() == "...":
skip = False
continue
if line.strip() and skip == False and not line.startswith('#') and not line.startswith('---') and not line.startswith("..."):
char, code = line.strip().split('\t')
csv += char
csv += ","
csv += code
csv += "\n"
print(csv.split("\n")[0])
return csv
def downloadFullCode(variant="light",
repo="forFudan/yuhao",
domain="raw.githubusercontent.com",
commit="main",
enableProxy=True,
ghproxy=True,
timeout=30):
url = "https://{0}/{1}/{3}/{2}/chaifen/%E5%AE%87%E6%B5%A9%E8%BC%B8%E5%85%A5%E6%B3%95%E5%85%A8%E6%BC%A2%E5%AD%97%E6%8B%86%E5%88%86%E8%A1%A8.csv".format(domain, repo, variant, commit)
useGhproxy = input("Would you like to have ghproxy enabled? (Y/n)")
if useGhproxy == "Y" or useGhproxy == "y" or useGhproxy == "":
url = "https://mirror.ghproxy.com/{0}".format(url)
print("Requesting fullcode...")
if(enableProxy == False):
fullCode = requests.get(url, stream=True, timeout=timeout)
else:
fullCode = requests.get(url, stream=True, proxies=globalProxySettings, timeout=timeout)
print("Parsing fullcode...")
fullCode = fullCode.content.decode('utf-8')
#fullCode.raw.decode_content = True # Content-Encoding
print(fullCode[0:20])
return fullCode
def fullCodeDict(fullcodeobj):
return {obj['char']: obj for obj in fullcodeobj}
def getFullCodeForChar(char, fullcodedict):
char_info = fullcodedict.get(char)
return char_info["fullcode"] if char_info is not None else None
def preprocessFullCode(fullcode):
fullCodeObj = parseCsvSp(fullcode, web=True)
sanitizedFullCode = []
for char in fullCodeObj:
sanitizedCode = convertSpecialAlphabetVariation(char["fullcode"])
sanitizedCode = toLowercase(sanitizedCode)
char["fullcode"] = sanitizedCode
sanitizedFullCode.append(char)
return sanitizedFullCode
def getInputCharForPhrase(phrase, fullCode):
phraseLength = len(phrase)
inputChar = ""
if(phraseLength) == 2:
inputChar += getFullCodeForChar(phrase[0], fullCode)[0]
inputChar += getFullCodeForChar(phrase[0], fullCode)[1]
inputChar += getFullCodeForChar(phrase[1], fullCode)[0]
inputChar += getFullCodeForChar(phrase[1], fullCode)[1]
elif(phraseLength) == 3:
inputChar += getFullCodeForChar(phrase[0], fullCode)[0]
inputChar += getFullCodeForChar(phrase[1], fullCode)[0]
inputChar += getFullCodeForChar(phrase[2], fullCode)[0]
inputChar += getFullCodeForChar(phrase[2], fullCode)[1]
elif(phraseLength) >= 4:
inputChar += getFullCodeForChar(phrase[0], fullCode)[0]
inputChar += getFullCodeForChar(phrase[1], fullCode)[0]
inputChar += getFullCodeForChar(phrase[2], fullCode)[0]
inputChar += getFullCodeForChar(phrase[-1], fullCode)[0]
elif(phraseLength) == 1:
inputChar = getFullCodeForChar(phrase[0], fullCode)
else:
inputChar = ""
return inputChar
def genYuhao():
print("Generating...\n")
print("This script may fetch the latest version of the fulldict from GitHub so as to conform with the Hamster input method on iOS.")
userConfirmation = input("Would you like to use the fulldict from web? (y/N)")
if userConfirmation == "Y" or userConfirmation == "y":
fullCode = downloadFullCode()
fullCode = preprocessFullCode(fullCode)
else:
fullDictPath = "{0}/yuhao/yuhao.full.dict.yaml".format(conf.prodPath)
fullCode = parseFullDict(fullDictPath)
fullCode = parseCsvSp(fullCode, head=False)
fullCodeDictVar = fullCodeDict(fullCode)
tab = "\t"
bigArray = gen.genFe()
result = genSkeleton()
for item in bigArray:
inputChar = getInputCharForPhrase(item, fullCodeDictVar)
#print("{0} -> {1}".format(item, inputChar))
result += item
result += tab
result += inputChar
result += "\n"
return result
def genSkeleton():
text = "---\n"
text += "name: {0}\n".format('"yuhao.genshin"')
text += "version: {0}\n".format(ver.get())
text += "sort: original\n"
text += "columns:\n"
text += " - text\n"
text += " - code\n"
text += "..."
text += "\n\n"
return text
if __name__ == '__main__':
text = genYuhao()
combinedPath = "./{0}/{1}".format(conf.buildPath, "yuhao.genshin.dict.yaml")
file.FileOperations.writeToFile(text, combinedPath)