Skip to content

Commit c669452

Browse files
authored
Merge pull request #54 from javecs/feature/simple-tokens
数字をまとめる機能を追加しました。
2 parents f2c26d7 + 513a8c8 commit c669452

5 files changed

Lines changed: 85 additions & 5 deletions

File tree

src/main/kotlin/xyz/javecs/tools/text2expr/rules/RuleBuilder.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package xyz.javecs.tools.text2expr.rules
22

3-
import com.atilika.kuromoji.ipadic.Token
43
import com.atilika.kuromoji.ipadic.Tokenizer
54
import xyz.javecs.tools.expr.Calculator
65
import xyz.javecs.tools.text2expr.utils.normalize
@@ -25,7 +24,7 @@ class RuleBuilder(source: String, val name: String = "") {
2524
parser.visit(parser(source).text2expr())
2625
}
2726

28-
private fun indexOf(word: Word, tokens: List<Token>, start: Int, end: Int): Pair<Int, Boolean> {
27+
private fun indexOf(word: Word, tokens: List<SimpleToken>, start: Int, end: Int): Pair<Int, Boolean> {
2928
for (i in start..end) {
3029
val token = tokens[i]
3130
var matched = 0
@@ -54,7 +53,7 @@ class RuleBuilder(source: String, val name: String = "") {
5453
.toTypedArray()
5554

5655
fun matches(text: String, recognizedId: (id: Pair<String, String>) -> Unit = {}): Pair<Boolean, Double> {
57-
val tokens = tokenizer.tokenize(text)
56+
val tokens = tokenizer.tokenize(text).toSimpleTokens()
5857
var offset = BaseOffset
5958
var remained = rule().size
6059
rule().forEach {
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package xyz.javecs.tools.text2expr.rules
2+
3+
import com.atilika.kuromoji.ipadic.Token
4+
5+
private val Number = ""
6+
7+
fun List<Token>.toSimpleTokens(): List<SimpleToken> {
8+
val merged = ArrayList<SimpleToken>()
9+
for (token in this) {
10+
if (merged.isEmpty()) {
11+
merged.add(SimpleToken(token))
12+
continue
13+
}
14+
if (token.partOfSpeechLevel2 == Number || token.surface == ".") {
15+
val last = merged.last()
16+
if (last.partOfSpeechLevel2 == Number) {
17+
last.surface = last.surface + token.surface
18+
continue
19+
}
20+
}
21+
merged.add(SimpleToken(token))
22+
}
23+
return merged
24+
}
25+
26+
class SimpleToken(token: Token) {
27+
var surface: String = ""
28+
var partOfSpeechLevel1: String = ""
29+
var partOfSpeechLevel2: String = ""
30+
var partOfSpeechLevel3: String = ""
31+
var partOfSpeechLevel4: String = ""
32+
var baseForm: String = ""
33+
var reading: String = ""
34+
var pronunciation: String = ""
35+
36+
init {
37+
surface = token.surface
38+
partOfSpeechLevel1 = token.partOfSpeechLevel1
39+
partOfSpeechLevel2 = token.partOfSpeechLevel2
40+
partOfSpeechLevel3 = token.partOfSpeechLevel3
41+
partOfSpeechLevel4 = token.partOfSpeechLevel4
42+
baseForm = token.baseForm
43+
reading = token.reading
44+
pronunciation = token.pronunciation
45+
}
46+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package xyz.javecs.tools.text2expr.test.kotlin
2+
3+
import com.atilika.kuromoji.ipadic.Tokenizer
4+
import org.junit.Test
5+
import xyz.javecs.tools.text2expr.rules.toSimpleTokens
6+
import kotlin.test.assertEquals
7+
8+
class SimpleTokenTest {
9+
val tokenizer = Tokenizer()
10+
11+
@Test fun toSimpleTokens1() {
12+
val tokens = tokenizer.tokenize("3.5").toSimpleTokens()
13+
assertEquals(1, tokens.size)
14+
assertEquals("3.5", tokens[0].surface)
15+
}
16+
17+
@Test fun toSimpleTokens2() {
18+
val tokens = tokenizer.tokenize("3.5マイルは?").toSimpleTokens()
19+
assertEquals(4, tokens.size)
20+
assertEquals("3.5", tokens[0].surface)
21+
assertEquals("マイル", tokens[1].surface)
22+
assertEquals("", tokens[2].surface)
23+
assertEquals("", tokens[3].surface)
24+
}
25+
26+
@Test fun toSimpleTokens3() {
27+
val tokens = tokenizer.tokenize("1 2 3.1 4 5").toSimpleTokens()
28+
assertEquals(9, tokens.size)
29+
assertEquals("3.1", tokens[4].surface)
30+
}
31+
32+
}

src/test/kotlin/xyz/javecs/tools/text2expr/test/kotlin/rules/arithmetic/ArithmeticTest.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ class ArithmeticTest {
1313
assertEquals("16", text2Expr.eval("4 かける 4"))
1414
assertEquals("60", text2Expr.eval("30 × 2"))
1515
assertEquals("120", text2Expr.eval("24 x 5"))
16+
assertEquals("7", text2Expr.eval("3.5x2"))
17+
assertEquals("6", text2Expr.eval("3. x 2"))
1618
}
1719

1820
@Test fun arithmetic2() {

src/test/kotlin/xyz/javecs/tools/text2expr/test/kotlin/rules/length/LengthTest.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@ class LengthTest {
88

99
val text2Expr = Text2Expr()
1010

11-
@Test fun mileToKilometer() {
11+
@Test fun milesToKilometers1() {
1212
assertEquals("151.27796", text2Expr.eval("94マイルは何キロメートルですか?"))
1313
assertEquals("151.27796", text2Expr.eval("94マイルは何キロですか?"))
1414
assertEquals("151.27796", text2Expr.eval("94mileは何kmですか?"))
1515
assertEquals("1.60934", text2Expr.eval("マイルは、なんキロメートル?"))
16+
assertEquals("2.41401", text2Expr.eval("1.5マイルは、何キロ?"))
1617
}
1718

18-
@Test fun kilometerToMile() {
19+
@Test fun kilometersToMiles1() {
1920
assertEquals("1.864113576", text2Expr.eval("3キロメートルは、何マイルですか?"))
2021
assertEquals("1.864113576", text2Expr.eval("3キロは、何マイルですか?"))
2122
assertEquals("1.864113576", text2Expr.eval("3kmは、何mileですか?"))

0 commit comments

Comments
 (0)