-
-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add support for user-defined POS for all OOV handlers
the feature is gated by userPos flag of OOV handlers, which must be set to true for this feature to work
- Loading branch information
1 parent
9bd6a41
commit 229e3c3
Showing
8 changed files
with
159 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
src/test/java/com/worksap/nlp/sudachi/OovProviderPluginTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
/* | ||
* Copyright (c) 2022 Works Applications Co., Ltd. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.worksap.nlp.sudachi | ||
|
||
import com.worksap.nlp.sudachi.dictionary.Grammar | ||
import com.worksap.nlp.sudachi.dictionary.POS | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
import kotlin.test.assertFails | ||
import kotlin.test.assertIs | ||
|
||
class OovProviderPluginTest { | ||
class FakeOovProvider : OovProviderPlugin() { | ||
override fun provideOOV( | ||
inputText: InputText?, | ||
offset: Int, | ||
otherWords: Long, | ||
result: MutableList<LatticeNodeImpl>? | ||
): Int { | ||
throw NotImplementedError("not implemented") | ||
} | ||
|
||
override fun setUp(grammar: Grammar?) { | ||
val kind = settings.getString("posKind", "forbid") | ||
val pos = POS(settings.getStringList("pos")) | ||
posId = posIdOf(grammar, pos, kind) | ||
} | ||
|
||
var posId: Short = -1 | ||
} | ||
|
||
@Test | ||
fun posIdOfWorks() { | ||
val cfg = TestDictionary.user0Cfg() | ||
cfg.addOovProviderPlugin(FakeOovProvider::class.java) | ||
.addList("pos", "名詞", "普通名詞", "一般", "*", "*", "*") | ||
val inst = DictionaryFactory().create(cfg) as JapaneseDictionary | ||
val plugin = assertIs<FakeOovProvider>(inst.oovProviderPlugins.last()) | ||
assertEquals(4, plugin.posId) | ||
} | ||
|
||
@Test | ||
fun posIdOfWorksNewPos() { | ||
val cfg = TestDictionary.user0Cfg() | ||
cfg.addOovProviderPlugin(FakeOovProvider::class.java) | ||
.addList("pos", "名詞", "普通名詞", "一般", "*", "*", "new") | ||
.add("posKind", "allow") | ||
val inst = DictionaryFactory().create(cfg) as JapaneseDictionary | ||
val plugin = assertIs<FakeOovProvider>(inst.oovProviderPlugins.last()) | ||
assertEquals(8, plugin.posId) | ||
} | ||
|
||
@Test | ||
fun failInvalidName() { | ||
val cfg = TestDictionary.user0Cfg() | ||
cfg.addOovProviderPlugin(FakeOovProvider::class.java) | ||
.addList("pos", "名詞", "普通名詞", "一般", "*", "*", "*") | ||
.add("posKind", "test") | ||
assertFails { DictionaryFactory().create(cfg) } | ||
} | ||
|
||
@Test | ||
fun failInvalidPos() { | ||
val cfg = TestDictionary.user0Cfg() | ||
cfg.addOovProviderPlugin(FakeOovProvider::class.java) | ||
.addList("pos", "名詞", "普通名詞", "一般", "*", "*", "test") | ||
assertFails { DictionaryFactory().create(cfg) } | ||
} | ||
|
||
@Test | ||
fun doubleRegisterReturnsSamePosId() { | ||
val cfg = TestDictionary.user0Cfg() | ||
cfg.addOovProviderPlugin(FakeOovProvider::class.java) | ||
.addList("pos", "名詞", "普通名詞", "一般", "*", "*", "new") | ||
.add("posKind", "allow") | ||
cfg.addOovProviderPlugin(FakeOovProvider::class.java) | ||
.addList("pos", "名詞", "普通名詞", "一般", "*", "*", "new") | ||
.add("posKind", "allow") | ||
val inst = DictionaryFactory().create(cfg) as JapaneseDictionary | ||
val oovPlugins = inst.oovProviderPlugins | ||
val p1 = assertIs<FakeOovProvider>(oovPlugins[oovPlugins.size - 2]) | ||
assertEquals(8, p1.posId) | ||
val p2 = assertIs<FakeOovProvider>(oovPlugins[oovPlugins.size - 1]) | ||
assertEquals(8, p2.posId) | ||
} | ||
} |