-
Notifications
You must be signed in to change notification settings - Fork 0
/
Chars.py
151 lines (118 loc) · 4.87 KB
/
Chars.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
import random
import string
class BasicChars():
'''
Chars containing letters, digits and punctuation
'''
def __init__(self):
super().__init__()
self.blank = '\x00'
self.chars = string.ascii_letters + string.digits
# with open('data/chars/kanaChars.txt', 'r', encoding='utf-8') as f:
# self.chars = self.chars + f.read()
with open('data/chars/punctuationChars.txt', 'r', encoding='utf-8') as f:
self.chars = self.blank + self.chars + f.read()
def export(self, path):
with open(path, 'w', encoding='utf-8') as f:
f.write(self.chars)
class LatinChars(BasicChars):
'''
Contains all common Latin-script alphabets. Source: https://en.wikipedia.org/wiki/List_of_Latin-script_alphabets
'''
def __init__(self):
super().__init__()
with open('data/chars/latinChars.txt', 'r', encoding='utf-8') as f:
self.chars = self.chars + f.read()
def generateRandomText(self, max_length=20):
text = ' '.join([''.join(random.sample(self.chars[1:], random.randint(2, 10))) for _ in range(random.randint(1, 10))])[:max_length]
text = list(text)
if random.random() < 0.1:
text = text[:-5] if len(text) > 10 else text
start = random.randrange(0, len(text))
text.insert(start, "\"")
text.insert(random.randrange(start, len(text)), "\"")
if random.random() < 0.1:
text = text[:-5] if len(text) > 10 else text
start = random.randrange(0, len(text))
text.insert(start, "\'")
text.insert(random.randrange(start, len(text)), "\'")
return ''.join(text)
class ChineseChars(BasicChars):
'''
Base class for all Chinese chars. Add Chinese punctuation chars.
'''
def __init__(self):
super().__init__()
with open('data/chars/chinesePunctuationChars.txt', 'r', encoding='utf-8') as f:
self.chars = self.chars + f.read()
def generateRandomText(self, max_length=22):
text = random.sample(self.chars[1:], random.randint(3, max_length))
if random.random() < 0.2:
text = text[:-8] if len(text) > 10 else text
text.insert(random.randrange(0, len(text)), ''.join(random.sample(string.ascii_letters, random.randint(3, 7))))
if random.random() < 0.1:
text = text[:-5] if len(text) > 10 else text
start = random.randrange(0, len(text))
text.insert(start, "『")
text.insert(random.randrange(start, len(text)), "』")
if random.random() < 0.1:
text = text[:-5] if len(text) > 10 else text
start = random.randrange(0, len(text))
text.insert(start, "「")
text.insert(random.randrange(start, len(text)), "」")
return ''.join(text)
class TC3600Chars(ChineseChars):
'''
Chars containing BaseChars and frequently used 3500 simplified chinese chars
'''
def __init__(self):
super().__init__()
with open('data/chars/TC3600Chars.txt', 'r', encoding='utf-8') as f:
self.chars = self.chars + f.read()
class SC3500Chars(ChineseChars):
'''
Chars containing BaseChars and frequently used 3500 simplified chinese chars
'''
def __init__(self):
super().__init__()
with open('data/chars/SC3500Chars.txt', 'r', encoding='utf-8') as f:
self.chars = self.chars + f.read()
class SC5000Chars(ChineseChars):
'''
Chars containing BaseChars and frequently used 5000 simplified chinese chars
'''
def __init__(self):
super().__init__()
with open('data/chars/SC5000Chars.txt', 'r', encoding='utf-8') as f:
self.chars = self.chars + f.read()
class TC5000Chars(ChineseChars):
'''
Chars containing BaseChars and frequently used 5000 traditional chinese chars
'''
def __init__(self):
super().__init__()
with open('data/chars/TC5000Chars.txt', 'r', encoding='utf-8') as f:
self.chars = self.chars + f.read()
class SC7000Chars(ChineseChars):
'''
Chars containing BaseChars and frequently used 7000 simplified chinese chars
'''
def __init__(self):
super().__init__()
with open('data/chars/SC7000Chars.txt', 'r', encoding='utf-8') as f:
self.chars = self.chars + f.read()
class TinyCJKChars(ChineseChars):
'''
Chars containing BaseChars and frequently used 8000 CJK chars
'''
def __init__(self):
super().__init__()
with open('data/chars/TinyCJKChars.txt', 'r', encoding='utf-8') as f:
self.chars = self.chars + f.read()
class CJKChars(ChineseChars):
'''
Chars containing BaseChars and Unicode CJK Unified Ideographs (No extensions)
'''
def __init__(self):
super().__init__()
self.chars = self.chars + ''.join([chr(c) for c in range(0x4e00, 0x9fa6)])