-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtext2mecab.py
130 lines (121 loc) · 4.19 KB
/
text2mecab.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
# coding: UTF-8
# text2mecab.py for python-jtalk
import re
import unicodedata
CODE = "utf-8"
predic = None
def text2mecab_setup():
global predic
if predic is None:
predic = [
[re.compile("\r"), ""],
[re.compile("\n"), ""],
[re.compile(" "), " "],
[re.compile("!"), "!"],
[re.compile('"'), "”"],
[re.compile("#"), "#"],
[re.compile("\\$"), "$"],
[re.compile("%"), "%"],
[re.compile("&"), "&"],
[re.compile("'"), "’"],
[re.compile("\\("), "("],
[re.compile("\\)"), ")"],
[re.compile("\\*"), "*"],
[re.compile("\\+"), "+"],
[re.compile(","), ","],
[re.compile("\\-"), "−"],
[re.compile("\\."), "."],
[re.compile("\\/"), "/"],
[re.compile("0"), "0"],
[re.compile("1"), "1"],
[re.compile("2"), "2"],
[re.compile("3"), "3"],
[re.compile("4"), "4"],
[re.compile("5"), "5"],
[re.compile("6"), "6"],
[re.compile("7"), "7"],
[re.compile("8"), "8"],
[re.compile("9"), "9"],
[re.compile(":"), ":"],
[re.compile(";"), ";"],
[re.compile("<"), "<"],
[re.compile("="), "="],
[re.compile(">"), ">"],
[re.compile("\?"), "?"],
[re.compile("@"), "@"],
[re.compile("A"), "A"],
[re.compile("B"), "B"],
[re.compile("C"), "C"],
[re.compile("D"), "D"],
[re.compile("E"), "E"],
[re.compile("F"), "F"],
[re.compile("G"), "G"],
[re.compile("H"), "H"],
[re.compile("I"), "I"],
[re.compile("J"), "J"],
[re.compile("K"), "K"],
[re.compile("L"), "L"],
[re.compile("M"), "M"],
[re.compile("N"), "N"],
[re.compile("O"), "O"],
[re.compile("P"), "P"],
[re.compile("Q"), "Q"],
[re.compile("R"), "R"],
[re.compile("S"), "S"],
[re.compile("T"), "T"],
[re.compile("U"), "U"],
[re.compile("V"), "V"],
[re.compile("W"), "W"],
[re.compile("X"), "X"],
[re.compile("Y"), "Y"],
[re.compile("Z"), "Z"],
[re.compile("\\["), "["],
[re.compile("\\\\"), "¥"],
[re.compile("\\]"), "]"],
[re.compile("\\^"), "^"],
[re.compile("_"), "_"],
[re.compile("`"), "‘"],
[re.compile("a"), "a"],
[re.compile("b"), "b"],
[re.compile("c"), "c"],
[re.compile("d"), "d"],
[re.compile("e"), "e"],
[re.compile("f"), "f"],
[re.compile("g"), "g"],
[re.compile("h"), "h"],
[re.compile("i"), "i"],
[re.compile("j"), "j"],
[re.compile("k"), "k"],
[re.compile("l"), "l"],
[re.compile("m"), "m"],
[re.compile("n"), "n"],
[re.compile("o"), "o"],
[re.compile("p"), "p"],
[re.compile("q"), "q"],
[re.compile("r"), "r"],
[re.compile("s"), "s"],
[re.compile("t"), "t"],
[re.compile("u"), "u"],
[re.compile("v"), "v"],
[re.compile("w"), "w"],
[re.compile("x"), "x"],
[re.compile("y"), "y"],
[re.compile("z"), "z"],
[re.compile("{"), "{"],
[re.compile("\\|"), "|"],
[re.compile("}"), "}"],
[re.compile("~"), "〜"],
[re.compile("�"), "?"], # u+fffd
]
def text2mecab_convert(s):
for p in predic:
try:
s = re.sub(p[0], p[1], s)
except:
pass
return s
def text2mecab(txt, CODE_=CODE):
text2mecab_setup()
txt = unicodedata.normalize("NFKC", txt)
txt = text2mecab_convert(txt)
return txt.encode(CODE_, "ignore")