Skip to content

Commit

Permalink
✨ 正規表現の後方参照と同様の機能でお嬢様言葉に変換可能にいたしましたわ~❗
Browse files Browse the repository at this point in the history
  • Loading branch information
jiro4989 committed Jun 23, 2022
1 parent fdab98c commit 5b131b0
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 3 deletions.
22 changes: 19 additions & 3 deletions ojosama.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,14 @@ func convertContinuousConditions(tokens []tokenizer.Token, tokenPos int, opt *Co

n := tokenPos + len(mc.Conditions) - 1
result := mc.Value

// FIXME: 書き方が汚い
data := tokenizer.NewTokenData(tokens[tokenPos])
surface := data.Surface
if appendablePrefix(data) {
surface = "お" + surface
}
result = strings.ReplaceAll(result, "@1", surface)
if mc.AppendLongNote {
result, n = appendLongNote(result, tokens, n, opt)
}
Expand Down Expand Up @@ -293,14 +301,22 @@ func convert(data tokenizer.TokenData, tokens []tokenizer.Token, i int, surface
return result, nounKeep, i
}

// appendPrefix は surface の前に「お」を付ける。
func appendPrefix(data tokenizer.TokenData, tokens []tokenizer.Token, i int, surface string, nounKeep bool) (string, bool) {
func appendablePrefix(data tokenizer.TokenData) bool {
if !equalsFeatures(data.Features, []string{"名詞", "一般"}) && !equalsFeatures(data.Features[:2], []string{"名詞", "固有名詞"}) {
return surface, false
return false
}

// 丁寧語の場合は「お」を付けない
if isPoliteWord(data) {
return false
}

return true
}

// appendPrefix は surface の前に「お」を付ける。
func appendPrefix(data tokenizer.TokenData, tokens []tokenizer.Token, i int, surface string, nounKeep bool) (string, bool) {
if !appendablePrefix(data) {
return surface, false
}

Expand Down
7 changes: 7 additions & 0 deletions ojosama_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,13 @@ func TestConvert(t *testing.T) {
opt: nil,
wantErr: false,
},
{
desc: "正常系: ",
src: "アホだ。カラスや。バナナじゃ。",
want: "おアホですの。おカラスですの。おバナナですの。",
opt: nil,
wantErr: false,
},
}

for _, tt := range tests {
Expand Down
44 changes: 44 additions & 0 deletions rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,50 @@ var (
newCond(assistantParallel, "や"),
},
},

{
Value: "@1ですの",
Conditions: []convertConditions{
{{Type: convertTypeFeatures, Value: nounsGeneral}},
newCond(auxiliaryVerb, "じゃ"),
},
},
{
Value: "@1ですの",
Conditions: []convertConditions{
{{Type: convertTypeFeatures, Value: nounsGeneral}},
newCond(auxiliaryVerb, "だ"),
},
},
{
Value: "@1ですの",
Conditions: []convertConditions{
{{Type: convertTypeFeatures, Value: nounsGeneral}},
newCond(auxiliaryVerb, "や"),
},
},

{
Value: "@1ですの",
Conditions: []convertConditions{
{{Type: convertTypeFeatures, Value: pronounGeneral}},
newCond(auxiliaryVerb, "じゃ"),
},
},
{
Value: "@1ですの",
Conditions: []convertConditions{
{{Type: convertTypeFeatures, Value: pronounGeneral}},
newCond(auxiliaryVerb, "だ"),
},
},
{
Value: "@1ですの",
Conditions: []convertConditions{
{{Type: convertTypeFeatures, Value: pronounGeneral}},
newCond(auxiliaryVerb, "や"),
},
},
}

// excludeRules は変換処理を無視するルール。
Expand Down

0 comments on commit 5b131b0

Please sign in to comment.