-
Notifications
You must be signed in to change notification settings - Fork 5
/
text.py
169 lines (141 loc) · 5.94 KB
/
text.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
# coding=utf-8
# Text module for drastikbot2
#
# Transform textual input to various other styles.
# Copyright (C) 2019, 2021 drastik.org
#
# This file is part of drastikbot.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, version 3 only.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
import random
class Module:
bot_commands = [
"ae", "text-c", "text-nc", "text-s", "text-ns", "flag", "cirrus",
"strike", "strikethrough"
]
manual = {
"desc": "Text transformation tools",
"bot_commands": {
"ae": {"usage": lambda p: f"{p}ae <text>",
"info": "Example: Hello, World!"},
"text-c": {"usage": lambda p: f"{p}text-c <text>",
"info": "Example: ⒽⒺⓁⓁⓄ, ⓌⓄⓇⓁⒹ!"},
"text-nc": {"usage": lambda p: f"{p}text-nc <text>",
"info": "Example: 🅗🅔🅛🅛🅞, 🅦🅞🅡🅛🅓!"},
"text-s": {"usage": lambda p: f"{p}text-s <text>",
"info": "Example: 🄷🄴🄻🄻🄾, 🅆🄾🅁🄻🄳!"},
"text-ns": {"usage": lambda p: f"{p}text-ns <text>",
"info": "Example: 🅷🅴🅻🅻🅾, 🆆🅾🆁🅻🅳!"},
"flag": {"usage": lambda p: f"{p}flag <text>",
"info": ("Transforms two letter country codes to"
" regional indicator symbols.")},
"cirrus": {"usage": lambda p: f"{p}cirrus <text>",
"info": "Example: Hello, WWorld!"},
"strike": {"usage": lambda p: f"{p}strike <text>",
"info": "Example: H̶e̶l̶l̶o̶,̶ ̶W̶o̶r̶l̶d̶!̶",
"alias": ["strikethrough"]},
"strikethrough": {
"usage": lambda p: f"{p}strikethrough <text>",
"info": "Example: H̶e̶l̶l̶o̶,̶ ̶W̶o̶r̶l̶d̶!̶",
"alias": ["strike"]
}
}
}
# ====================================================================
# Translation maps
# ====================================================================
# https://en.wikipedia.org/wiki/Halfwidth_and_fullwidth_forms_(Unicode_block)
FULLWIDTH_MAP = dict((i, i + 0xFEE0) for i in range(0x21, 0x7F))
FULLWIDTH_MAP[0x20] = 0x3000
# https://en.wikipedia.org/wiki/Enclosed_Alphanumerics
_CIRCLED_NUM_MAP = dict((i, (i - 0x31) + 0x2460) for i in range(0x30, 0x3A))
_CIRCLED_NUM_MAP[0x30] = 0x24EA # Set the actual Circled digit zero character
_CIRCLED_ALP_U_MAP = dict((i, (i - 0x41) + 0x24B6) for i in range(0x41, 0x5B))
_CIRCLED_ALP_L_MAP = dict((i, (i - 0x61) + 0x24B6) for i in range(0x61, 0x7B))
CIRCLED_MAP = {**_CIRCLED_NUM_MAP, **_CIRCLED_ALP_U_MAP, **_CIRCLED_ALP_L_MAP}
# https://en.wikipedia.org/wiki/Enclosed_Alphanumeric_Supplement
_NEGATIVE_CIRCLED_ALP_U_MAP = dict(
(i, (i - 0x41) + 0x1F150) for i in range(0x41, 0x5B))
_NEGATIVE_CIRCLED_ALP_L_MAP = dict(
(i, (i - 0x61) + 0x1F150) for i in range(0x61, 0x7B))
NEGATIVE_CIRCLED_MAP = {
**_NEGATIVE_CIRCLED_ALP_U_MAP,
**_NEGATIVE_CIRCLED_ALP_L_MAP
}
_SQUARED_ALP_U_MAP = dict((i, (i - 0x41) + 0x1F130) for i in range(0x41, 0x5B))
_SQUARED_ALP_L_MAP = dict((i, (i - 0x61) + 0x1F130) for i in range(0x61, 0x7B))
SQUARED_MAP = {**_SQUARED_ALP_U_MAP, **_SQUARED_ALP_L_MAP}
_NEGATIVE_SQUARED_ALP_U_MAP = dict(
(i, (i - 0x41) + 0x1F170) for i in range(0x41, 0x5B))
_NEGATIVE_SQUARED_ALP_L_MAP = dict(
(i, (i - 0x61) + 0x1F170) for i in range(0x61, 0x7B))
NEGATIVE_SQUARED_MAP = {
**_NEGATIVE_SQUARED_ALP_U_MAP,
**_NEGATIVE_SQUARED_ALP_L_MAP
}
# https://en.wikipedia.org/wiki/Regional_Indicator_Symbol
_REGIONAL_INDICATOR_SYMBOL_U_MAP = dict(
(i, (i - 0x41) + 0x1F1E6) for i in range(0x41, 0x5B))
_REGIONAL_INDICATOR_SYMBOL_L_MAP = dict(
(i, (i - 0x61) + 0x1F1E6) for i in range(0x61, 0x7B))
REGIONAL_INDICATOR_SYMBOL_MAP = {
**_REGIONAL_INDICATOR_SYMBOL_U_MAP,
**_REGIONAL_INDICATOR_SYMBOL_L_MAP
}
# ====================================================================
# Transormation functions
# ====================================================================
def cirrus(text):
words = text.split()
wc = len(words)
cc = 0
for i in range(wc):
if random.uniform(0, 1) < 0.38:
cc += 1
words[i] = f"{words[i][0]}{words[i]}"
if cc == 0:
i = random.randint(0, wc - 1)
words[i] = f"{words[i][0]}{words[i]}"
return " ".join(words)
def strikethrough(text):
return "\u0336".join(text) + '\u0336'
# ====================================================================
# Main
# ====================================================================
translate_map_d = {
"ae": FULLWIDTH_MAP,
"text-c": CIRCLED_MAP,
"text-nc": NEGATIVE_CIRCLED_MAP,
"text-s": SQUARED_MAP,
"text-ns": NEGATIVE_SQUARED_MAP,
"flag": REGIONAL_INDICATOR_SYMBOL_MAP,
}
transform_fun_d = {
"cirrus": cirrus,
"strike": strikethrough,
"strikethrough": strikethrough
}
def main(i, irc):
msgtarget = i.msg.get_msgtarget()
botcmd = i.msg.get_botcmd()
prefix = i.msg.get_botcmd_prefix()
args = i.msg.get_args()
if not args:
m = f"{prefix}{botcmd} <text>"
elif botcmd in transform_fun_d:
m = transform_fun_d[botcmd](args)
elif botcmd in translate_map_d:
m = args.translate(translate_map_d[botcmd])
if botcmd == "ae" and m == args:
m = args.replace("", " ")[1: -1]
irc.out.notice(msgtarget, m)