-
Notifications
You must be signed in to change notification settings - Fork 7
/
dajarep.go
230 lines (213 loc) · 7.13 KB
/
dajarep.go
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package dajarep
import (
"fmt"
"regexp"
"strings"
"unicode"
ipa "github.com/ikawaha/kagome-dict/ipa"
"github.com/ikawaha/kagome/v2/tokenizer"
)
// 単語
type word struct {
str string
kana string
wtype string
}
// 文章
type sentence struct {
str string
kana string
yomi string
words []word
}
// Dajarep :駄洒落を返す
func Dajarep(text string, limit int, debug bool) (dajares []string, debugStrs [][]string) {
sentencesN := getSentences(text, tokenizer.Normal)
sentencesS := getSentences(text, tokenizer.Search)
for i := 0; i < len(sentencesN); i++ {
if kana := isDajare(sentencesN[i], limit, debug); len(kana) != 0 {
dajares = append(dajares, sentencesN[i].str)
debugStrs = append(debugStrs, kana)
} else if kana = isDajare(sentencesS[i], limit, debug); len(kana) != 0 {
dajares = append(dajares, sentencesS[i].str)
debugStrs = append(debugStrs, kana)
}
}
return dajares, debugStrs
}
// 駄洒落かどうかを評価する。
func isDajare(sen sentence, limit int, debug bool) (hitList []string) {
words := sen.words
for i := 0; i < len(words); i++ {
w := words[i]
if debug {
fmt.Println(w)
}
if w.wtype == "名詞" && len([]rune(w.kana)) > limit-1 {
rStr := regexp.MustCompile(w.str)
rKana := regexp.MustCompile(fixWord(w.kana))
hitStr := rStr.FindAllString(sen.str, -1)
hitKana1 := rKana.FindAllString(sen.kana, -1)
hitKana2 := rKana.FindAllString(fixSentence(sen.kana), -1)
hitKana3 := rKana.FindAllString(sen.yomi, -1)
hitKana4 := rKana.FindAllString(fixSentence(sen.yomi), -1)
//ある単語における 原文の一致文字列数<フリガナでの一致文字列数 → 駄洒落の読みが存在
if debug {
fmt.Println(rKana, len(hitStr), sen.kana, len(hitKana1), fixSentence(sen.kana), len(hitKana2))
}
if len(hitStr) > 0 && len(hitStr) < most(len(hitKana1), len(hitKana2), len(hitKana3), len(hitKana4)) {
if !contains(hitList, w.kana) {
hitList = append(hitList, w.kana)
}
}
}
}
return hitList
}
// 置き換え可能な文字を考慮した正規表現を返す。
func fixWord(text string) string {
text = strings.Replace(text, "ッ", "[ツッ]?", -1)
text = strings.Replace(text, "ァ", "[アァ]?", -1)
text = strings.Replace(text, "ィ", "[イィ]?", -1)
text = strings.Replace(text, "ゥ", "[ウゥ]?", -1)
text = strings.Replace(text, "ェ", "[エェ]?", -1)
text = strings.Replace(text, "ォ", "[オォ]?", -1)
text = strings.Replace(text, "ズ", "[スズヅ]", -1)
text = strings.Replace(text, "ヅ", "[ツズヅ]", -1)
text = strings.Replace(text, "ヂ", "[チジヂ]", -1)
text = strings.Replace(text, "ジ", "[シジヂ]", -1)
text = strings.Replace(text, "ガ", "[カガ]", -1)
text = strings.Replace(text, "ギ", "[キギ]", -1)
text = strings.Replace(text, "グ", "[クグ]", -1)
text = strings.Replace(text, "ゲ", "[ケゲ]", -1)
text = strings.Replace(text, "ゴ", "[コゴ]", -1)
text = strings.Replace(text, "ザ", "[サザ]", -1)
text = strings.Replace(text, "ゼ", "[セゼ]", -1)
text = strings.Replace(text, "ゾ", "[ソゾ]", -1)
text = strings.Replace(text, "ダ", "[タダ]", -1)
text = strings.Replace(text, "デ", "[テデ]", -1)
text = strings.Replace(text, "ド", "[トド]", -1)
re := regexp.MustCompile("[ハバパ]")
text = re.ReplaceAllString(text, "[ハバパ]")
re = regexp.MustCompile("[ヒビピ]")
text = re.ReplaceAllString(text, "[ヒビピ]")
re = regexp.MustCompile("[フブプ]")
text = re.ReplaceAllString(text, "[フブプ]")
re = regexp.MustCompile("[ヘベペ]")
text = re.ReplaceAllString(text, "[ヘベペ]")
re = regexp.MustCompile("[ホボポ]")
text = re.ReplaceAllString(text, "[ホボポ]")
re = regexp.MustCompile("([アカサタナハマヤラワャ])ー")
text = re.ReplaceAllString(text, "$1[アァ]?")
re = regexp.MustCompile("([イキシチニヒミリ])ー")
text = re.ReplaceAllString(text, "$1[イィ]?")
re = regexp.MustCompile("([ウクスツヌフムユルュ])ー")
text = re.ReplaceAllString(text, "$1[ウゥ]?")
re = regexp.MustCompile("([エケセテネへメレ])ー")
text = re.ReplaceAllString(text, "$1[イィエェ]?")
re = regexp.MustCompile("([オコソトノホモヨロヲョ])ー")
text = re.ReplaceAllString(text, "$1[ウゥオォ]?")
text = strings.Replace(text, "ャ", "[ヤャ]", -1)
text = strings.Replace(text, "ュ", "[ユュ]", -1)
text = strings.Replace(text, "ョ", "[ヨョ]", -1)
text = strings.Replace(text, "ー", "[ー]?", -1)
// 拗音の判定は要検証
text = strings.Replace(text, "キ[ヤャ]", "(キ[ヤャ]|カ)", -1)
// text = strings.Replace(text, "キ[ユュ]", "(キ[ユュ]|ク)", -1)
// text = strings.Replace(text, "キ[ヨョ]", "(キ[ヨョ]|コ)", -1)
text = strings.Replace(text, "シ[ヤャ]", "(シ[ヤャ]|サ)", -1)
// text = strings.Replace(text, "シ[ユュ]", "(シ[ユュ]|ス)", -1)
text = strings.Replace(text, "シ[ヨョ]", "(シ[ヨョ]|ソ)", -1)
return text
}
// 本文から省略可能文字を消したパターンを返す。
func fixSentence(text string) string {
text = strings.Replace(text, "ッ", "", -1)
text = strings.Replace(text, "ー", "", -1)
text = strings.Replace(text, "、", "", -1)
text = strings.Replace(text, ",", "", -1)
text = strings.Replace(text, " ", "", -1)
text = strings.Replace(text, " ", "", -1)
return text
}
// テキストからsentenceオブジェクトを作る。
func getSentences(text string, mode tokenizer.TokenizeMode) []sentence {
var sentences []sentence
t, err := tokenizer.New(ipa.Dict())
if err != nil {
panic(err)
}
// http://www.serendip.ws/archives/6307
kanaConv := unicode.SpecialCase{
// ひらがなをカタカナに変換
unicode.CaseRange{
0x3041, // Lo: ぁ
0x3093, // Hi: ん
[unicode.MaxCase]rune{
0x30a1 - 0x3041, // UpperCase でカタカナに変換
0, // LowerCase では変換しない
0x30a1 - 0x3041, // TitleCase でカタカナに変換
},
},
}
text = strings.Replace(text, "。", "\n", -1)
text = strings.Replace(text, ".", "\n", -1)
text = strings.Replace(text, "?", "?\n", -1)
text = strings.Replace(text, "!", "!\n", -1)
text = strings.Replace(text, "?", "?\n", -1)
text = strings.Replace(text, "!", "!\n", -1)
text = regexp.QuoteMeta(text)
senstr := strings.Split(text, "\n")
for i := 0; i < len(senstr); i++ {
tokens := t.Analyze(senstr[i], mode)
var words []word
var kana string
var yomi string
for j := 0; j < len(tokens); j++ {
tk := tokens[j]
ft := tk.Features()
if len(ft) > 7 {
w := word{str: ft[6],
kana: ft[7],
wtype: ft[0],
}
words = append(words, w)
kana += ft[7]
yomi += ft[8]
} else if len(ft) == 7 {
lk := strings.ToUpperSpecial(kanaConv, tk.Surface)
w := word{str: lk,
kana: lk,
wtype: ft[0],
}
words = append(words, w)
kana += lk
}
}
sentences = append(sentences,
sentence{
str: senstr[i],
words: words,
kana: kana,
yomi: yomi,
})
}
return sentences
}
func most(num ...int) int {
i := 0
for _, n := range num {
if n > i {
i = n
}
}
return i
}
func contains(s []string, e string) bool {
for _, v := range s {
if e == v {
return true
}
}
return false
}