Akron | 7f1097f | 2021-09-21 16:00:29 +0200 | [diff] [blame] | 1 | package datok |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 2 | |
| 3 | import ( |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 4 | "bytes" |
Akron | bd40680 | 2021-08-11 18:39:13 +0200 | [diff] [blame] | 5 | "fmt" |
| 6 | "os" |
Akron | 03ca425 | 2021-08-11 13:32:53 +0200 | [diff] [blame] | 7 | "regexp" |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 8 | "strings" |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 9 | "testing" |
| 10 | |
| 11 | "github.com/stretchr/testify/assert" |
| 12 | ) |
| 13 | |
Akron | 9fb63af | 2021-10-28 01:15:53 +0200 | [diff] [blame] | 14 | var dat *DaTokenizer |
| 15 | |
Akron | df27581 | 2022-03-27 12:54:46 +0200 | [diff] [blame] | 16 | func ttokenizeStr(tok Tokenizer, str string) string { |
Akron | ec835ad | 2021-08-11 18:23:22 +0200 | [diff] [blame] | 17 | b := make([]byte, 0, 2048) |
| 18 | w := bytes.NewBuffer(b) |
Akron | df27581 | 2022-03-27 12:54:46 +0200 | [diff] [blame] | 19 | return strings.Join(ttokenize(tok, w, str), "\n") |
Akron | ec835ad | 2021-08-11 18:23:22 +0200 | [diff] [blame] | 20 | } |
| 21 | |
Akron | 1c34ce6 | 2021-09-23 23:27:39 +0200 | [diff] [blame] | 22 | func ttokenize(tok Tokenizer, w *bytes.Buffer, str string) []string { |
Akron | ec835ad | 2021-08-11 18:23:22 +0200 | [diff] [blame] | 23 | w.Reset() |
Akron | 1c34ce6 | 2021-09-23 23:27:39 +0200 | [diff] [blame] | 24 | ok := tok.Transduce(strings.NewReader(str), w) |
Akron | ec835ad | 2021-08-11 18:23:22 +0200 | [diff] [blame] | 25 | if !ok { |
| 26 | return []string{} |
| 27 | } |
| 28 | obj := regexp.MustCompile("\n+") |
| 29 | |
| 30 | tokens := obj.Split(w.String(), -1) |
| 31 | return tokens[:len(tokens)-1] |
| 32 | } |
| 33 | |
Akron | c9c0eae | 2021-10-22 19:49:43 +0200 | [diff] [blame] | 34 | func TestDoubleArraySimpleString(t *testing.T) { |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 35 | assert := assert.New(t) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 36 | // bau | bauamt |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 37 | tok := LoadFomaFile("testdata/bauamt.fst") |
Akron | f2120ca | 2021-08-03 16:26:41 +0200 | [diff] [blame] | 38 | dat := tok.ToDoubleArray() |
Akron | df27581 | 2022-03-27 12:54:46 +0200 | [diff] [blame] | 39 | |
| 40 | b := make([]byte, 0, 2048) |
| 41 | w := bytes.NewBuffer(b) |
| 42 | var tokens []string |
| 43 | |
| 44 | tokens = ttokenize(dat, w, "ibauamt") |
| 45 | assert.Equal("i", tokens[0]) |
| 46 | assert.Equal("bauamt", tokens[1]) |
| 47 | |
| 48 | tokens = ttokenize(dat, w, "ibbauamt") |
| 49 | assert.Equal("i", tokens[0]) |
| 50 | |
| 51 | assert.Equal("b", tokens[1]) |
| 52 | assert.Equal("bauamt", tokens[2]) |
| 53 | |
| 54 | tokens = ttokenize(dat, w, "bau") |
| 55 | assert.Equal("bau", tokens[0]) |
| 56 | |
| 57 | tokens = ttokenize(dat, w, "baum") |
| 58 | assert.Equal("bau", tokens[0]) |
| 59 | assert.Equal("m", tokens[1]) |
| 60 | |
| 61 | tokens = ttokenize(dat, w, "baudibauamt") |
| 62 | assert.Equal("bau", tokens[0]) |
| 63 | assert.Equal("d", tokens[1]) |
| 64 | assert.Equal("i", tokens[2]) |
| 65 | assert.Equal("bauamt", tokens[3]) |
Akron | 8ef408b | 2021-08-02 22:11:04 +0200 | [diff] [blame] | 66 | } |
Akron | 75ebe7f | 2021-08-03 10:34:10 +0200 | [diff] [blame] | 67 | |
Akron | c9c0eae | 2021-10-22 19:49:43 +0200 | [diff] [blame] | 68 | func TestDoubleArraySimpleBranches(t *testing.T) { |
Akron | 75ebe7f | 2021-08-03 10:34:10 +0200 | [diff] [blame] | 69 | assert := assert.New(t) |
Akron | 75ebe7f | 2021-08-03 10:34:10 +0200 | [diff] [blame] | 70 | // (bau | wahl) (amt | en) |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 71 | tok := LoadFomaFile("testdata/wahlamt.fst") |
Akron | f2120ca | 2021-08-03 16:26:41 +0200 | [diff] [blame] | 72 | dat := tok.ToDoubleArray() |
Akron | df27581 | 2022-03-27 12:54:46 +0200 | [diff] [blame] | 73 | |
| 74 | b := make([]byte, 0, 2048) |
| 75 | w := bytes.NewBuffer(b) |
| 76 | var tokens []string |
| 77 | |
| 78 | tokens = ttokenize(dat, w, "bau") |
| 79 | assert.Equal("bau", tokens[0]) |
| 80 | |
| 81 | tokens = ttokenize(dat, w, "bauamt") |
| 82 | assert.Equal("bauamt", tokens[0]) |
| 83 | |
| 84 | tokens = ttokenize(dat, w, "wahlamt") |
| 85 | assert.Equal("wahlamt", tokens[0]) |
| 86 | |
| 87 | tokens = ttokenize(dat, w, "bauen") |
| 88 | assert.Equal("bauen", tokens[0]) |
| 89 | |
| 90 | tokens = ttokenize(dat, w, "wahlen") |
| 91 | assert.Equal("wahlen", tokens[0]) |
| 92 | |
| 93 | tokens = ttokenize(dat, w, "baum") |
| 94 | assert.Equal("bau", tokens[0]) |
| 95 | assert.Equal("m", tokens[1]) |
Akron | 75ebe7f | 2021-08-03 10:34:10 +0200 | [diff] [blame] | 96 | } |
Akron | 730a79c | 2021-08-03 11:05:29 +0200 | [diff] [blame] | 97 | |
Akron | df27581 | 2022-03-27 12:54:46 +0200 | [diff] [blame] | 98 | func TestDoubleArraySimpleTokenizer(t *testing.T) { |
Akron | 730a79c | 2021-08-03 11:05:29 +0200 | [diff] [blame] | 99 | assert := assert.New(t) |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 100 | tok := LoadFomaFile("testdata/simpletok.fst") |
Akron | f2120ca | 2021-08-03 16:26:41 +0200 | [diff] [blame] | 101 | dat := tok.ToDoubleArray() |
Akron | df27581 | 2022-03-27 12:54:46 +0200 | [diff] [blame] | 102 | assert.Equal(ttokenizeStr(dat, "bau"), "bau") |
| 103 | assert.Equal(ttokenizeStr(dat, "bad"), "bad") |
| 104 | assert.Equal(ttokenizeStr(dat, "wald gehen"), "wald\ngehen") |
Akron | 730a79c | 2021-08-03 11:05:29 +0200 | [diff] [blame] | 105 | } |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 106 | |
Akron | c9c0eae | 2021-10-22 19:49:43 +0200 | [diff] [blame] | 107 | func TestDoubleArraySimpleTokenizerTransduce(t *testing.T) { |
Akron | 84d68e6 | 2021-08-04 17:06:52 +0200 | [diff] [blame] | 108 | assert := assert.New(t) |
| 109 | tok := LoadFomaFile("testdata/simpletok.fst") |
Akron | 84d68e6 | 2021-08-04 17:06:52 +0200 | [diff] [blame] | 110 | dat := tok.ToDoubleArray() |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 111 | |
| 112 | r := strings.NewReader(" wald gehen Da kann\t man was \"erleben\"!") |
| 113 | b := make([]byte, 0, 2048) |
| 114 | w := bytes.NewBuffer(b) |
Akron | 524c543 | 2021-08-05 14:14:27 +0200 | [diff] [blame] | 115 | var tokens []string |
Akron | 524c543 | 2021-08-05 14:14:27 +0200 | [diff] [blame] | 116 | dat.Transduce(r, w) |
| 117 | tokens = strings.Split(w.String(), "\n") |
Akron | a854faa | 2021-10-22 19:31:08 +0200 | [diff] [blame] | 118 | assert.Equal(len(tokens), 11) |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 119 | assert.Equal("wald", tokens[0]) |
| 120 | assert.Equal("gehen", tokens[1]) |
| 121 | assert.Equal("Da", tokens[2]) |
| 122 | assert.Equal("kann", tokens[3]) |
| 123 | assert.Equal("man", tokens[4]) |
| 124 | assert.Equal("was", tokens[5]) |
| 125 | assert.Equal("\"erleben\"", tokens[6]) |
| 126 | |
Akron | 524c543 | 2021-08-05 14:14:27 +0200 | [diff] [blame] | 127 | r = strings.NewReader(" In den Wald gehen? -- Da kann\t man was \"erleben\"!") |
| 128 | w.Reset() |
| 129 | dat.Transduce(r, w) |
| 130 | tokens = strings.Split(w.String(), "\n") |
| 131 | assert.Equal("In", tokens[0]) |
| 132 | assert.Equal("den", tokens[1]) |
| 133 | assert.Equal("Wald", tokens[2]) |
| 134 | assert.Equal("gehen", tokens[3]) |
| 135 | assert.Equal("?", tokens[4]) |
| 136 | assert.Equal("--", tokens[5]) |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 137 | |
Akron | 524c543 | 2021-08-05 14:14:27 +0200 | [diff] [blame] | 138 | r = strings.NewReader(" g? -- D") |
| 139 | w.Reset() |
| 140 | dat.Transduce(r, w) |
| 141 | tokens = strings.Split(w.String(), "\n") |
| 142 | assert.Equal("g", tokens[0]) |
| 143 | assert.Equal("?", tokens[1]) |
| 144 | assert.Equal("--", tokens[2]) |
| 145 | assert.Equal("D", tokens[3]) |
| 146 | assert.Equal("", tokens[4]) |
Akron | 6e70dc8 | 2021-08-11 11:33:18 +0200 | [diff] [blame] | 147 | assert.Equal("", tokens[5]) |
Akron | a854faa | 2021-10-22 19:31:08 +0200 | [diff] [blame] | 148 | assert.Equal(7, len(tokens)) |
Akron | 068874c | 2021-08-04 15:19:56 +0200 | [diff] [blame] | 149 | } |
| 150 | |
Akron | c9c0eae | 2021-10-22 19:49:43 +0200 | [diff] [blame] | 151 | func TestDoubleArrayReadWriteTokenizer(t *testing.T) { |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 152 | assert := assert.New(t) |
Akron | 64ffd9a | 2021-08-03 19:55:21 +0200 | [diff] [blame] | 153 | tok := LoadFomaFile("testdata/simpletok.fst") |
Akron | f2120ca | 2021-08-03 16:26:41 +0200 | [diff] [blame] | 154 | dat := tok.ToDoubleArray() |
Akron | df27581 | 2022-03-27 12:54:46 +0200 | [diff] [blame] | 155 | assert.Equal(ttokenizeStr(dat, "bau"), "bau") |
| 156 | assert.Equal(ttokenizeStr(dat, "bad"), "bad") |
| 157 | assert.Equal(ttokenizeStr(dat, "wald gehen"), "wald\ngehen") |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 158 | |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 159 | b := make([]byte, 0, 1024) |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 160 | buf := bytes.NewBuffer(b) |
| 161 | n, err := dat.WriteTo(buf) |
| 162 | assert.Nil(err) |
Akron | 29e306f | 2021-09-02 18:29:56 +0200 | [diff] [blame] | 163 | assert.Equal(int64(296), n) |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 164 | |
| 165 | dat2 := ParseDatok(buf) |
| 166 | assert.NotNil(dat2) |
| 167 | assert.Equal(dat.array, dat2.array) |
| 168 | assert.Equal(dat.sigma, dat2.sigma) |
| 169 | assert.Equal(dat.epsilon, dat2.epsilon) |
| 170 | assert.Equal(dat.unknown, dat2.unknown) |
| 171 | assert.Equal(dat.identity, dat2.identity) |
| 172 | assert.Equal(dat.final, dat2.final) |
| 173 | assert.Equal(dat.LoadFactor(), dat2.LoadFactor()) |
Akron | df27581 | 2022-03-27 12:54:46 +0200 | [diff] [blame] | 174 | assert.Equal(ttokenizeStr(dat2, "bau"), "bau") |
| 175 | assert.Equal(ttokenizeStr(dat2, "bad"), "bad") |
| 176 | assert.Equal(ttokenizeStr(dat2, "wald gehen"), "wald\ngehen") |
Akron | 4fa28b3 | 2021-08-27 10:55:41 +0200 | [diff] [blame] | 177 | |
Akron | 92704eb | 2021-08-27 10:59:46 +0200 | [diff] [blame] | 178 | assert.Equal(dat.TransCount(), 17) |
| 179 | assert.Equal(dat2.TransCount(), 17) |
Akron | 6247a5d | 2021-08-03 19:18:28 +0200 | [diff] [blame] | 180 | } |
| 181 | |
Akron | c9c0eae | 2021-10-22 19:49:43 +0200 | [diff] [blame] | 182 | func TestDoubleArrayIgnorableMCS(t *testing.T) { |
Akron | e396a93 | 2021-10-19 01:06:13 +0200 | [diff] [blame] | 183 | |
| 184 | // This test relies on final states. That's why it is |
| 185 | // not working correctly anymore. |
| 186 | |
Akron | 31f3c06 | 2021-08-27 10:15:13 +0200 | [diff] [blame] | 187 | assert := assert.New(t) |
| 188 | // File has MCS in sigma but not in net |
| 189 | tok := LoadFomaFile("testdata/ignorable_mcs.fst") |
| 190 | assert.NotNil(tok) |
| 191 | dat := tok.ToDoubleArray() |
| 192 | assert.NotNil(dat) |
| 193 | |
| 194 | b := make([]byte, 0, 2048) |
| 195 | w := bytes.NewBuffer(b) |
| 196 | var tokens []string |
| 197 | |
| 198 | // Is only unambigous when transducing strictly greedy! |
Akron | e396a93 | 2021-10-19 01:06:13 +0200 | [diff] [blame] | 199 | assert.True(dat.Transduce(strings.NewReader("ab<ab>a"), w)) |
Akron | 31f3c06 | 2021-08-27 10:15:13 +0200 | [diff] [blame] | 200 | tokens = strings.Split(w.String(), "\n") |
Akron | a854faa | 2021-10-22 19:31:08 +0200 | [diff] [blame] | 201 | assert.Equal("a\nb\n<ab>a\n\n\n", w.String()) |
Akron | 31f3c06 | 2021-08-27 10:15:13 +0200 | [diff] [blame] | 202 | assert.Equal("a", tokens[0]) |
| 203 | assert.Equal("b", tokens[1]) |
Akron | e396a93 | 2021-10-19 01:06:13 +0200 | [diff] [blame] | 204 | assert.Equal("<ab>a", tokens[2]) |
Akron | a854faa | 2021-10-22 19:31:08 +0200 | [diff] [blame] | 205 | assert.Equal(6, len(tokens)) |
Akron | 92704eb | 2021-08-27 10:59:46 +0200 | [diff] [blame] | 206 | assert.Equal(dat.TransCount(), 15) |
Akron | 31f3c06 | 2021-08-27 10:15:13 +0200 | [diff] [blame] | 207 | } |
| 208 | |
Akron | c9c0eae | 2021-10-22 19:49:43 +0200 | [diff] [blame] | 209 | func TestDoubleArrayFullTokenizer(t *testing.T) { |
Akron | 3a063ef | 2021-08-05 19:36:35 +0200 | [diff] [blame] | 210 | assert := assert.New(t) |
Akron | 9fb63af | 2021-10-28 01:15:53 +0200 | [diff] [blame] | 211 | |
| 212 | if dat == nil { |
| 213 | dat = LoadDatokFile("testdata/tokenizer.datok") |
| 214 | } |
Akron | 3a063ef | 2021-08-05 19:36:35 +0200 | [diff] [blame] | 215 | assert.NotNil(dat) |
| 216 | assert.True(dat.LoadFactor() >= 70) |
| 217 | assert.Equal(dat.epsilon, 1) |
| 218 | assert.Equal(dat.unknown, 2) |
| 219 | assert.Equal(dat.identity, 3) |
Akron | d0c6e10 | 2021-12-09 10:30:29 +0100 | [diff] [blame] | 220 | // assert.Equal(dat.final, 142) |
| 221 | // assert.Equal(len(dat.sigma), 137) |
Akron | fac8abc | 2021-11-10 07:19:59 +0100 | [diff] [blame] | 222 | // assert.True(len(dat.array) > 3000000) |
| 223 | // assert.True(dat.maxSize > 3000000) |
Akron | df27581 | 2022-03-27 12:54:46 +0200 | [diff] [blame] | 224 | assert.Equal(ttokenizeStr(dat, "bau"), "bau") |
| 225 | assert.Equal(ttokenizeStr(dat, "bad"), "bad") |
| 226 | assert.Equal(ttokenizeStr(dat, "wald gehen"), "wald\ngehen") |
Akron | 740f3d7 | 2021-08-03 12:12:34 +0200 | [diff] [blame] | 227 | } |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 228 | |
Akron | c9c0eae | 2021-10-22 19:49:43 +0200 | [diff] [blame] | 229 | func TestDoubleArrayTokenizerBranch(t *testing.T) { |
Akron | 941f215 | 2021-09-26 15:14:25 +0200 | [diff] [blame] | 230 | assert := assert.New(t) |
| 231 | tok := LoadTokenizerFile("testdata/simpletok.datok") |
| 232 | assert.NotNil(tok) |
| 233 | assert.Equal(tok.Type(), "DATOK") |
| 234 | |
| 235 | tok = LoadTokenizerFile("testdata/simpletok.matok") |
| 236 | assert.NotNil(tok) |
| 237 | assert.Equal(tok.Type(), "MATOK") |
| 238 | } |
| 239 | |
Akron | c9c0eae | 2021-10-22 19:49:43 +0200 | [diff] [blame] | 240 | func XTestDoubleArrayFullTokenizerBuild(t *testing.T) { |
Akron | a0bded5 | 2021-08-11 15:48:02 +0200 | [diff] [blame] | 241 | assert := assert.New(t) |
| 242 | tok := LoadFomaFile("testdata/tokenizer.fst") |
| 243 | dat := tok.ToDoubleArray() |
Akron | de18e90 | 2021-08-27 09:34:12 +0200 | [diff] [blame] | 244 | assert.NotNil(dat) |
| 245 | // n, err := dat.Save("testdata/tokenizer.datok") |
| 246 | // assert.Nil(err) |
| 247 | // assert.True(n > 500) |
Akron | a0bded5 | 2021-08-11 15:48:02 +0200 | [diff] [blame] | 248 | } |
| 249 | |
Akron | c9c0eae | 2021-10-22 19:49:43 +0200 | [diff] [blame] | 250 | func TestDoubleArrayFullTokenizerTransduce(t *testing.T) { |
Akron | 3610f10 | 2021-08-08 14:13:25 +0200 | [diff] [blame] | 251 | assert := assert.New(t) |
| 252 | |
Akron | 9fb63af | 2021-10-28 01:15:53 +0200 | [diff] [blame] | 253 | if dat == nil { |
| 254 | dat = LoadDatokFile("testdata/tokenizer.datok") |
| 255 | } |
| 256 | |
Akron | 3610f10 | 2021-08-08 14:13:25 +0200 | [diff] [blame] | 257 | assert.NotNil(dat) |
| 258 | |
Akron | 3610f10 | 2021-08-08 14:13:25 +0200 | [diff] [blame] | 259 | b := make([]byte, 0, 2048) |
| 260 | w := bytes.NewBuffer(b) |
| 261 | var tokens []string |
| 262 | |
Akron | 03ca425 | 2021-08-11 13:32:53 +0200 | [diff] [blame] | 263 | assert.True(dat.Transduce(strings.NewReader("tra. u Du?"), w)) |
Akron | 3610f10 | 2021-08-08 14:13:25 +0200 | [diff] [blame] | 264 | |
| 265 | tokens = strings.Split(w.String(), "\n") |
Akron | a854faa | 2021-10-22 19:31:08 +0200 | [diff] [blame] | 266 | assert.Equal("tra\n.\n\nu\nDu\n?\n\n\n", w.String()) |
Akron | 3610f10 | 2021-08-08 14:13:25 +0200 | [diff] [blame] | 267 | assert.Equal("tra", tokens[0]) |
| 268 | assert.Equal(".", tokens[1]) |
Akron | b4bbb47 | 2021-08-09 11:49:38 +0200 | [diff] [blame] | 269 | assert.Equal("", tokens[2]) |
Akron | c5d8d43 | 2021-08-10 16:48:44 +0200 | [diff] [blame] | 270 | assert.Equal("u", tokens[3]) |
Akron | b4bbb47 | 2021-08-09 11:49:38 +0200 | [diff] [blame] | 271 | assert.Equal("Du", tokens[4]) |
| 272 | assert.Equal("?", tokens[5]) |
Akron | 3610f10 | 2021-08-08 14:13:25 +0200 | [diff] [blame] | 273 | assert.Equal("", tokens[6]) |
Akron | 1594cb8 | 2021-08-11 11:14:56 +0200 | [diff] [blame] | 274 | assert.Equal("", tokens[7]) |
Akron | a854faa | 2021-10-22 19:31:08 +0200 | [diff] [blame] | 275 | assert.Equal("", tokens[8]) |
| 276 | assert.Equal(9, len(tokens)) |
Akron | 03ca425 | 2021-08-11 13:32:53 +0200 | [diff] [blame] | 277 | |
| 278 | w.Reset() |
| 279 | assert.True(dat.Transduce(strings.NewReader("\"John Doe\"@xx.com"), w)) |
Akron | a854faa | 2021-10-22 19:31:08 +0200 | [diff] [blame] | 280 | assert.Equal("\"\nJohn\nDoe\n\"\n@xx\n.\n\ncom\n\n\n", w.String()) |
Akron | 3f8571a | 2021-08-05 11:18:10 +0200 | [diff] [blame] | 281 | } |
Akron | b7e1f13 | 2021-08-10 11:52:31 +0200 | [diff] [blame] | 282 | |
Akron | c9c0eae | 2021-10-22 19:49:43 +0200 | [diff] [blame] | 283 | func TestDoubleArrayFullTokenizerSentenceSplitter(t *testing.T) { |
Akron | b7e1f13 | 2021-08-10 11:52:31 +0200 | [diff] [blame] | 284 | assert := assert.New(t) |
Akron | 9fb63af | 2021-10-28 01:15:53 +0200 | [diff] [blame] | 285 | |
| 286 | if dat == nil { |
| 287 | dat = LoadDatokFile("testdata/tokenizer.datok") |
| 288 | } |
| 289 | |
Akron | b7e1f13 | 2021-08-10 11:52:31 +0200 | [diff] [blame] | 290 | assert.NotNil(dat) |
| 291 | |
| 292 | b := make([]byte, 0, 2048) |
| 293 | w := bytes.NewBuffer(b) |
| 294 | var sentences []string |
| 295 | |
| 296 | // testSentSplitterSimple |
Akron | 1594cb8 | 2021-08-11 11:14:56 +0200 | [diff] [blame] | 297 | assert.True(dat.Transduce(strings.NewReader("Der alte Mann."), w)) |
| 298 | sentences = strings.Split(w.String(), "\n\n") |
| 299 | |
Akron | a854faa | 2021-10-22 19:31:08 +0200 | [diff] [blame] | 300 | assert.Equal("Der\nalte\nMann\n.\n\n\n", w.String()) |
Akron | 1594cb8 | 2021-08-11 11:14:56 +0200 | [diff] [blame] | 301 | assert.Equal("Der\nalte\nMann\n.", sentences[0]) |
Akron | a854faa | 2021-10-22 19:31:08 +0200 | [diff] [blame] | 302 | assert.Equal("\n", sentences[1]) |
| 303 | assert.Equal(2, len(sentences)) |
Akron | 1594cb8 | 2021-08-11 11:14:56 +0200 | [diff] [blame] | 304 | |
| 305 | w.Reset() |
| 306 | assert.True(dat.Transduce(strings.NewReader("Der Vorsitzende der Abk. hat gewählt."), w)) |
| 307 | sentences = strings.Split(w.String(), "\n\n") |
Akron | a854faa | 2021-10-22 19:31:08 +0200 | [diff] [blame] | 308 | assert.Equal(2, len(sentences)) |
Akron | 1594cb8 | 2021-08-11 11:14:56 +0200 | [diff] [blame] | 309 | assert.Equal("Der\nVorsitzende\nder\nAbk.\nhat\ngewählt\n.", sentences[0]) |
Akron | a854faa | 2021-10-22 19:31:08 +0200 | [diff] [blame] | 310 | assert.Equal("\n", sentences[1]) |
Akron | 1594cb8 | 2021-08-11 11:14:56 +0200 | [diff] [blame] | 311 | |
| 312 | w.Reset() |
| 313 | assert.True(dat.Transduce(strings.NewReader(""), w)) |
Akron | b7e1f13 | 2021-08-10 11:52:31 +0200 | [diff] [blame] | 314 | sentences = strings.Split(w.String(), "\n\n") |
Akron | a854faa | 2021-10-22 19:31:08 +0200 | [diff] [blame] | 315 | assert.Equal(2, len(sentences)) |
| 316 | assert.Equal("", sentences[0]) |
Akron | 1594cb8 | 2021-08-11 11:14:56 +0200 | [diff] [blame] | 317 | |
| 318 | w.Reset() |
| 319 | assert.True(dat.Transduce(strings.NewReader("Gefunden auf wikipedia.org."), w)) |
| 320 | sentences = strings.Split(w.String(), "\n\n") |
| 321 | assert.Equal(len(sentences), 2) |
| 322 | |
| 323 | w.Reset() |
| 324 | assert.True(dat.Transduce(strings.NewReader("Ich bin unter korap@ids-mannheim.de erreichbar."), w)) |
| 325 | sentences = strings.Split(w.String(), "\n\n") |
| 326 | assert.Equal(len(sentences), 2) |
| 327 | |
Akron | 6e70dc8 | 2021-08-11 11:33:18 +0200 | [diff] [blame] | 328 | w.Reset() |
| 329 | assert.True(dat.Transduce(strings.NewReader("Unsere Website ist https://korap.ids-mannheim.de/?q=Baum"), w)) |
| 330 | sentences = strings.Split(w.String(), "\n\n") |
| 331 | assert.Equal("Unsere\nWebsite\nist\nhttps://korap.ids-mannheim.de/?q=Baum", sentences[0]) |
Akron | a854faa | 2021-10-22 19:31:08 +0200 | [diff] [blame] | 332 | assert.Equal("\n", sentences[1]) |
| 333 | assert.Equal(2, len(sentences)) |
Akron | 1594cb8 | 2021-08-11 11:14:56 +0200 | [diff] [blame] | 334 | |
Akron | 6e70dc8 | 2021-08-11 11:33:18 +0200 | [diff] [blame] | 335 | w.Reset() |
| 336 | assert.True(dat.Transduce(strings.NewReader("Unser Server ist 10.0.10.51."), w)) |
| 337 | sentences = strings.Split(w.String(), "\n\n") |
Akron | a854faa | 2021-10-22 19:31:08 +0200 | [diff] [blame] | 338 | assert.Equal("\n", sentences[1]) |
| 339 | assert.Equal(2, len(sentences)) |
Akron | 1594cb8 | 2021-08-11 11:14:56 +0200 | [diff] [blame] | 340 | |
Akron | 6e70dc8 | 2021-08-11 11:33:18 +0200 | [diff] [blame] | 341 | w.Reset() |
| 342 | assert.True(dat.Transduce(strings.NewReader("Zu 50.4% ist es sicher"), w)) |
| 343 | sentences = strings.Split(w.String(), "\n\n") |
| 344 | assert.Equal(len(sentences), 2) |
Akron | 1594cb8 | 2021-08-11 11:14:56 +0200 | [diff] [blame] | 345 | |
Akron | 6e70dc8 | 2021-08-11 11:33:18 +0200 | [diff] [blame] | 346 | w.Reset() |
| 347 | assert.True(dat.Transduce(strings.NewReader("Der Termin ist am 5.9.2018"), w)) |
| 348 | sentences = strings.Split(w.String(), "\n\n") |
| 349 | assert.Equal(len(sentences), 2) |
Akron | 1594cb8 | 2021-08-11 11:14:56 +0200 | [diff] [blame] | 350 | |
Akron | 6e70dc8 | 2021-08-11 11:33:18 +0200 | [diff] [blame] | 351 | w.Reset() |
| 352 | assert.True(dat.Transduce(strings.NewReader("Ich habe die readme.txt heruntergeladen"), w)) |
| 353 | sentences = strings.Split(w.String(), "\n\n") |
Akron | a854faa | 2021-10-22 19:31:08 +0200 | [diff] [blame] | 354 | assert.Equal(2, len(sentences)) |
Akron | 6e70dc8 | 2021-08-11 11:33:18 +0200 | [diff] [blame] | 355 | assert.Equal("Ich\nhabe\ndie\nreadme.txt\nheruntergeladen", sentences[0]) |
Akron | a854faa | 2021-10-22 19:31:08 +0200 | [diff] [blame] | 356 | assert.Equal("\n", sentences[1]) |
Akron | 1594cb8 | 2021-08-11 11:14:56 +0200 | [diff] [blame] | 357 | |
Akron | 6e70dc8 | 2021-08-11 11:33:18 +0200 | [diff] [blame] | 358 | w.Reset() |
| 359 | assert.True(dat.Transduce(strings.NewReader("Ausschalten!!! Hast Du nicht gehört???"), w)) |
| 360 | sentences = strings.Split(w.String(), "\n\n") |
Akron | a854faa | 2021-10-22 19:31:08 +0200 | [diff] [blame] | 361 | assert.Equal(3, len(sentences)) |
Akron | 6e70dc8 | 2021-08-11 11:33:18 +0200 | [diff] [blame] | 362 | assert.Equal("Ausschalten\n!!!", sentences[0]) |
| 363 | assert.Equal("Hast\nDu\nnicht\ngehört\n???", sentences[1]) |
Akron | a854faa | 2021-10-22 19:31:08 +0200 | [diff] [blame] | 364 | assert.Equal("\n", sentences[2]) |
Akron | 1594cb8 | 2021-08-11 11:14:56 +0200 | [diff] [blame] | 365 | |
Akron | 4af79f1 | 2021-08-11 14:48:17 +0200 | [diff] [blame] | 366 | w.Reset() |
| 367 | assert.True(dat.Transduce(strings.NewReader("Ich wohne in der Weststr. und Du?"), w)) |
| 368 | sentences = strings.Split(w.String(), "\n\n") |
| 369 | assert.Equal(len(sentences), 2) |
Akron | 1594cb8 | 2021-08-11 11:14:56 +0200 | [diff] [blame] | 370 | |
| 371 | /* |
| 372 | Test: |
| 373 | "\"Ausschalten!!!\", sagte er. \"Hast Du nicht gehört???\""), w)) |
| 374 | */ |
Akron | b7e1f13 | 2021-08-10 11:52:31 +0200 | [diff] [blame] | 375 | } |
Akron | 03ca425 | 2021-08-11 13:32:53 +0200 | [diff] [blame] | 376 | |
Akron | c9c0eae | 2021-10-22 19:49:43 +0200 | [diff] [blame] | 377 | func TestDoubleArrayFullTokenizerTokenSplitter(t *testing.T) { |
Akron | 03ca425 | 2021-08-11 13:32:53 +0200 | [diff] [blame] | 378 | assert := assert.New(t) |
Akron | 9fb63af | 2021-10-28 01:15:53 +0200 | [diff] [blame] | 379 | |
| 380 | if dat == nil { |
| 381 | dat = LoadDatokFile("testdata/tokenizer.datok") |
| 382 | } |
| 383 | |
Akron | 03ca425 | 2021-08-11 13:32:53 +0200 | [diff] [blame] | 384 | assert.NotNil(dat) |
| 385 | |
| 386 | b := make([]byte, 0, 2048) |
| 387 | w := bytes.NewBuffer(b) |
| 388 | var tokens []string |
| 389 | |
| 390 | // testTokenizerSimple |
Akron | ec835ad | 2021-08-11 18:23:22 +0200 | [diff] [blame] | 391 | tokens = ttokenize(dat, w, "Der alte Mann") |
Akron | 03ca425 | 2021-08-11 13:32:53 +0200 | [diff] [blame] | 392 | assert.Equal(tokens[0], "Der") |
| 393 | assert.Equal(tokens[1], "alte") |
| 394 | assert.Equal(tokens[2], "Mann") |
| 395 | assert.Equal(len(tokens), 3) |
| 396 | |
Akron | ec835ad | 2021-08-11 18:23:22 +0200 | [diff] [blame] | 397 | tokens = ttokenize(dat, w, "Der alte Mann.") |
Akron | 03ca425 | 2021-08-11 13:32:53 +0200 | [diff] [blame] | 398 | assert.Equal(tokens[0], "Der") |
| 399 | assert.Equal(tokens[1], "alte") |
| 400 | assert.Equal(tokens[2], "Mann") |
| 401 | assert.Equal(tokens[3], ".") |
| 402 | assert.Equal(len(tokens), 4) |
| 403 | |
| 404 | // testTokenizerAbbr |
Akron | ec835ad | 2021-08-11 18:23:22 +0200 | [diff] [blame] | 405 | tokens = ttokenize(dat, w, "Der Vorsitzende der F.D.P. hat gewählt") |
Akron | 03ca425 | 2021-08-11 13:32:53 +0200 | [diff] [blame] | 406 | assert.Equal(tokens[0], "Der") |
| 407 | assert.Equal(tokens[1], "Vorsitzende") |
| 408 | assert.Equal(tokens[2], "der") |
| 409 | assert.Equal(tokens[3], "F.D.P.") |
| 410 | assert.Equal(tokens[4], "hat") |
| 411 | assert.Equal(tokens[5], "gewählt") |
| 412 | assert.Equal(len(tokens), 6) |
| 413 | // Ignored in KorAP-Tokenizer |
| 414 | |
| 415 | // testTokenizerHost1 |
Akron | ec835ad | 2021-08-11 18:23:22 +0200 | [diff] [blame] | 416 | tokens = ttokenize(dat, w, "Gefunden auf wikipedia.org") |
Akron | 03ca425 | 2021-08-11 13:32:53 +0200 | [diff] [blame] | 417 | assert.Equal(tokens[0], "Gefunden") |
| 418 | assert.Equal(tokens[1], "auf") |
| 419 | assert.Equal(tokens[2], "wikipedia.org") |
| 420 | assert.Equal(len(tokens), 3) |
| 421 | |
| 422 | // testTokenizerWwwHost |
Akron | ec835ad | 2021-08-11 18:23:22 +0200 | [diff] [blame] | 423 | tokens = ttokenize(dat, w, "Gefunden auf www.wikipedia.org") |
Akron | 03ca425 | 2021-08-11 13:32:53 +0200 | [diff] [blame] | 424 | assert.Equal("Gefunden", tokens[0]) |
| 425 | assert.Equal("auf", tokens[1]) |
| 426 | assert.Equal("www.wikipedia.org", tokens[2]) |
| 427 | assert.Equal(3, len(tokens)) |
| 428 | |
| 429 | // testTokenizerWwwUrl |
Akron | ec835ad | 2021-08-11 18:23:22 +0200 | [diff] [blame] | 430 | tokens = ttokenize(dat, w, "Weitere Infos unter www.info.biz/info") |
Akron | 03ca425 | 2021-08-11 13:32:53 +0200 | [diff] [blame] | 431 | assert.Equal("www.info.biz/info", tokens[3]) |
| 432 | |
| 433 | // testTokenizerFtpHost |
| 434 | /* |
| 435 | tokens = tokenize(dat, w, "Kann von ftp.download.org heruntergeladen werden") |
| 436 | assert.Equal("Kann", tokens[0]) |
| 437 | assert.Equal("von", tokens[1]) |
| 438 | assert.Equal("ftp.download.org", tokens[2]) |
| 439 | assert.Equal(5, len(tokens)) |
| 440 | // Ignored in KorAP-Tokenizer |
| 441 | */ |
| 442 | |
| 443 | // testTokenizerDash |
Akron | ec835ad | 2021-08-11 18:23:22 +0200 | [diff] [blame] | 444 | tokens = ttokenize(dat, w, "Das war -- spitze") |
Akron | 03ca425 | 2021-08-11 13:32:53 +0200 | [diff] [blame] | 445 | assert.Equal(tokens[0], "Das") |
| 446 | assert.Equal(tokens[1], "war") |
| 447 | assert.Equal(tokens[2], "--") |
| 448 | assert.Equal(tokens[3], "spitze") |
| 449 | assert.Equal(len(tokens), 4) |
| 450 | |
| 451 | // testTokenizerEmail1 |
Akron | ec835ad | 2021-08-11 18:23:22 +0200 | [diff] [blame] | 452 | tokens = ttokenize(dat, w, "Ich bin unter korap@ids-mannheim.de erreichbar.") |
Akron | 03ca425 | 2021-08-11 13:32:53 +0200 | [diff] [blame] | 453 | assert.Equal(tokens[0], "Ich") |
| 454 | assert.Equal(tokens[1], "bin") |
| 455 | assert.Equal(tokens[2], "unter") |
| 456 | assert.Equal(tokens[3], "korap@ids-mannheim.de") |
| 457 | assert.Equal(tokens[4], "erreichbar") |
| 458 | assert.Equal(tokens[5], ".") |
| 459 | assert.Equal(len(tokens), 6) |
| 460 | |
| 461 | // testTokenizerEmail2 |
Akron | ec835ad | 2021-08-11 18:23:22 +0200 | [diff] [blame] | 462 | tokens = ttokenize(dat, w, "Oder unter korap[at]ids-mannheim[dot]de.") |
Akron | 03ca425 | 2021-08-11 13:32:53 +0200 | [diff] [blame] | 463 | assert.Equal(tokens[0], "Oder") |
| 464 | assert.Equal(tokens[1], "unter") |
| 465 | assert.Equal(tokens[2], "korap[at]ids-mannheim[dot]de") |
| 466 | assert.Equal(tokens[3], ".") |
| 467 | assert.Equal(len(tokens), 4) |
| 468 | |
| 469 | // testTokenizerEmail3 |
Akron | ec835ad | 2021-08-11 18:23:22 +0200 | [diff] [blame] | 470 | tokens = ttokenize(dat, w, "Oder unter korap(at)ids-mannheim(dot)de.") |
Akron | 03ca425 | 2021-08-11 13:32:53 +0200 | [diff] [blame] | 471 | assert.Equal(tokens[0], "Oder") |
| 472 | assert.Equal(tokens[1], "unter") |
| 473 | assert.Equal(tokens[2], "korap(at)ids-mannheim(dot)de") |
| 474 | assert.Equal(tokens[3], ".") |
| 475 | assert.Equal(len(tokens), 4) |
| 476 | // Ignored in KorAP-Tokenizer |
| 477 | |
| 478 | // testTokenizerDoNotAcceptQuotedEmailNames |
Akron | ec835ad | 2021-08-11 18:23:22 +0200 | [diff] [blame] | 479 | tokens = ttokenize(dat, w, "\"John Doe\"@xx.com") |
Akron | 03ca425 | 2021-08-11 13:32:53 +0200 | [diff] [blame] | 480 | assert.Equal("\"", tokens[0]) |
| 481 | assert.Equal("John", tokens[1]) |
| 482 | assert.Equal("Doe", tokens[2]) |
| 483 | assert.Equal("\"", tokens[3]) |
| 484 | assert.Equal("@xx", tokens[4]) |
| 485 | assert.Equal(".", tokens[5]) // Differs - as the sentence splitter splits here! |
| 486 | assert.Equal("com", tokens[6]) |
| 487 | assert.Equal(7, len(tokens)) |
| 488 | |
| 489 | // testTokenizerTwitter |
Akron | ec835ad | 2021-08-11 18:23:22 +0200 | [diff] [blame] | 490 | tokens = ttokenize(dat, w, "Folgt @korap und #korap") |
Akron | 03ca425 | 2021-08-11 13:32:53 +0200 | [diff] [blame] | 491 | assert.Equal(tokens[0], "Folgt") |
| 492 | assert.Equal(tokens[1], "@korap") |
| 493 | assert.Equal(tokens[2], "und") |
| 494 | assert.Equal(tokens[3], "#korap") |
| 495 | assert.Equal(len(tokens), 4) |
| 496 | |
| 497 | // testTokenizerWeb1 |
Akron | ec835ad | 2021-08-11 18:23:22 +0200 | [diff] [blame] | 498 | tokens = ttokenize(dat, w, "Unsere Website ist https://korap.ids-mannheim.de/?q=Baum") |
Akron | 03ca425 | 2021-08-11 13:32:53 +0200 | [diff] [blame] | 499 | assert.Equal(tokens[0], "Unsere") |
| 500 | assert.Equal(tokens[1], "Website") |
| 501 | assert.Equal(tokens[2], "ist") |
| 502 | assert.Equal(tokens[3], "https://korap.ids-mannheim.de/?q=Baum") |
| 503 | assert.Equal(len(tokens), 4) |
| 504 | |
| 505 | // testTokenizerWeb2 |
Akron | ec835ad | 2021-08-11 18:23:22 +0200 | [diff] [blame] | 506 | tokens = ttokenize(dat, w, "Wir sind auch im Internet (https://korap.ids-mannheim.de/?q=Baum)") |
Akron | 03ca425 | 2021-08-11 13:32:53 +0200 | [diff] [blame] | 507 | assert.Equal(tokens[0], "Wir") |
| 508 | assert.Equal(tokens[1], "sind") |
| 509 | assert.Equal(tokens[2], "auch") |
| 510 | assert.Equal(tokens[3], "im") |
| 511 | assert.Equal(tokens[4], "Internet") |
| 512 | assert.Equal(tokens[5], "(") |
| 513 | assert.Equal(tokens[6], "https://korap.ids-mannheim.de/?q=Baum") |
| 514 | assert.Equal(tokens[7], ")") |
| 515 | assert.Equal(len(tokens), 8) |
| 516 | // Ignored in KorAP-Tokenizer |
| 517 | |
| 518 | // testTokenizerWeb3 |
Akron | ec835ad | 2021-08-11 18:23:22 +0200 | [diff] [blame] | 519 | tokens = ttokenize(dat, w, "Die Adresse ist https://korap.ids-mannheim.de/?q=Baum.") |
Akron | 03ca425 | 2021-08-11 13:32:53 +0200 | [diff] [blame] | 520 | assert.Equal(tokens[0], "Die") |
| 521 | assert.Equal(tokens[1], "Adresse") |
| 522 | assert.Equal(tokens[2], "ist") |
| 523 | assert.Equal(tokens[3], "https://korap.ids-mannheim.de/?q=Baum") |
| 524 | assert.Equal(tokens[4], ".") |
| 525 | assert.Equal(len(tokens), 5) |
| 526 | // Ignored in KorAP-Tokenizer |
| 527 | |
| 528 | // testTokenizerServer |
Akron | ec835ad | 2021-08-11 18:23:22 +0200 | [diff] [blame] | 529 | tokens = ttokenize(dat, w, "Unser Server ist 10.0.10.51.") |
Akron | 03ca425 | 2021-08-11 13:32:53 +0200 | [diff] [blame] | 530 | assert.Equal(tokens[0], "Unser") |
| 531 | assert.Equal(tokens[1], "Server") |
| 532 | assert.Equal(tokens[2], "ist") |
| 533 | assert.Equal(tokens[3], "10.0.10.51") |
| 534 | assert.Equal(tokens[4], ".") |
| 535 | assert.Equal(len(tokens), 5) |
| 536 | |
| 537 | // testTokenizerNum |
Akron | ec835ad | 2021-08-11 18:23:22 +0200 | [diff] [blame] | 538 | tokens = ttokenize(dat, w, "Zu 50,4% ist es sicher") |
Akron | 03ca425 | 2021-08-11 13:32:53 +0200 | [diff] [blame] | 539 | assert.Equal(tokens[0], "Zu") |
| 540 | assert.Equal(tokens[1], "50,4%") |
| 541 | assert.Equal(tokens[2], "ist") |
| 542 | assert.Equal(tokens[3], "es") |
| 543 | assert.Equal(tokens[4], "sicher") |
| 544 | assert.Equal(len(tokens), 5) |
| 545 | // Differs from KorAP-Tokenizer |
| 546 | |
| 547 | // testTokenizerDate |
Akron | ec835ad | 2021-08-11 18:23:22 +0200 | [diff] [blame] | 548 | tokens = ttokenize(dat, w, "Der Termin ist am 5.9.2018") |
Akron | 03ca425 | 2021-08-11 13:32:53 +0200 | [diff] [blame] | 549 | assert.Equal(tokens[0], "Der") |
| 550 | assert.Equal(tokens[1], "Termin") |
| 551 | assert.Equal(tokens[2], "ist") |
| 552 | assert.Equal(tokens[3], "am") |
| 553 | assert.Equal(tokens[4], "5.9.2018") |
| 554 | assert.Equal(len(tokens), 5) |
| 555 | |
Akron | ec835ad | 2021-08-11 18:23:22 +0200 | [diff] [blame] | 556 | tokens = ttokenize(dat, w, "Der Termin ist am 5/9/2018") |
Akron | 03ca425 | 2021-08-11 13:32:53 +0200 | [diff] [blame] | 557 | assert.Equal(tokens[0], "Der") |
| 558 | assert.Equal(tokens[1], "Termin") |
| 559 | assert.Equal(tokens[2], "ist") |
| 560 | assert.Equal(tokens[3], "am") |
| 561 | assert.Equal(tokens[4], "5/9/2018") |
| 562 | assert.Equal(len(tokens), 5) |
| 563 | |
| 564 | // testTokenizerDateRange |
| 565 | /* |
| 566 | tokens = tokenize(dat, w, "Der Termin war vom 4.-5.9.2018") |
| 567 | assert.Equal(tokens[0], "Der") |
| 568 | assert.Equal(tokens[1], "Termin") |
| 569 | assert.Equal(tokens[2], "war") |
| 570 | assert.Equal(tokens[3], "vom") |
| 571 | assert.Equal(tokens[4], "4.") |
| 572 | assert.Equal(tokens[5], "-") |
| 573 | assert.Equal(tokens[6], "5.9.2018") |
| 574 | assert.Equal(len(tokens), 7) |
| 575 | // Ignored in KorAP-Tokenizer |
| 576 | */ |
| 577 | |
| 578 | // testTokenizerEmoji1 |
Akron | ec835ad | 2021-08-11 18:23:22 +0200 | [diff] [blame] | 579 | tokens = ttokenize(dat, w, "Das ist toll! ;)") |
Akron | 03ca425 | 2021-08-11 13:32:53 +0200 | [diff] [blame] | 580 | assert.Equal(tokens[0], "Das") |
| 581 | assert.Equal(tokens[1], "ist") |
| 582 | assert.Equal(tokens[2], "toll") |
| 583 | assert.Equal(tokens[3], "!") |
| 584 | assert.Equal(tokens[4], ";)") |
| 585 | assert.Equal(len(tokens), 5) |
| 586 | |
| 587 | // testTokenizerRef1 |
Akron | ec835ad | 2021-08-11 18:23:22 +0200 | [diff] [blame] | 588 | tokens = ttokenize(dat, w, "Kupietz und Schmidt (2018): Korpuslinguistik") |
Akron | 03ca425 | 2021-08-11 13:32:53 +0200 | [diff] [blame] | 589 | assert.Equal(tokens[0], "Kupietz") |
| 590 | assert.Equal(tokens[1], "und") |
| 591 | assert.Equal(tokens[2], "Schmidt") |
| 592 | assert.Equal(tokens[3], "(2018)") |
| 593 | assert.Equal(tokens[4], ":") |
| 594 | assert.Equal(tokens[5], "Korpuslinguistik") |
| 595 | assert.Equal(len(tokens), 6) |
| 596 | // Differs from KorAP-Tokenizer! |
| 597 | |
| 598 | // testTokenizerRef2 () { |
Akron | ec835ad | 2021-08-11 18:23:22 +0200 | [diff] [blame] | 599 | tokens = ttokenize(dat, w, "Kupietz und Schmidt [2018]: Korpuslinguistik") |
Akron | 03ca425 | 2021-08-11 13:32:53 +0200 | [diff] [blame] | 600 | assert.Equal(tokens[0], "Kupietz") |
| 601 | assert.Equal(tokens[1], "und") |
| 602 | assert.Equal(tokens[2], "Schmidt") |
| 603 | assert.Equal(tokens[3], "[2018]") |
| 604 | assert.Equal(tokens[4], ":") |
| 605 | assert.Equal(tokens[5], "Korpuslinguistik") |
| 606 | assert.Equal(len(tokens), 6) |
| 607 | // Differs from KorAP-Tokenizer! |
| 608 | |
| 609 | // testTokenizerOmission1 () { |
Akron | ec835ad | 2021-08-11 18:23:22 +0200 | [diff] [blame] | 610 | tokens = ttokenize(dat, w, "Er ist ein A****loch!") |
Akron | 03ca425 | 2021-08-11 13:32:53 +0200 | [diff] [blame] | 611 | assert.Equal(tokens[0], "Er") |
| 612 | assert.Equal(tokens[1], "ist") |
| 613 | assert.Equal(tokens[2], "ein") |
| 614 | assert.Equal(tokens[3], "A****loch") |
| 615 | assert.Equal(tokens[4], "!") |
| 616 | assert.Equal(len(tokens), 5) |
| 617 | |
| 618 | // testTokenizerOmission2 |
Akron | ec835ad | 2021-08-11 18:23:22 +0200 | [diff] [blame] | 619 | tokens = ttokenize(dat, w, "F*ck!") |
Akron | 03ca425 | 2021-08-11 13:32:53 +0200 | [diff] [blame] | 620 | assert.Equal(tokens[0], "F*ck") |
| 621 | assert.Equal(tokens[1], "!") |
| 622 | assert.Equal(len(tokens), 2) |
| 623 | |
| 624 | // testTokenizerOmission3 () { |
Akron | ec835ad | 2021-08-11 18:23:22 +0200 | [diff] [blame] | 625 | tokens = ttokenize(dat, w, "Dieses verf***** Kleid!") |
Akron | 03ca425 | 2021-08-11 13:32:53 +0200 | [diff] [blame] | 626 | assert.Equal(tokens[0], "Dieses") |
| 627 | assert.Equal(tokens[1], "verf*****") |
| 628 | assert.Equal(tokens[2], "Kleid") |
| 629 | assert.Equal(tokens[3], "!") |
| 630 | assert.Equal(len(tokens), 4) |
| 631 | |
| 632 | // Probably interpreted as HOST |
| 633 | // testTokenizerFileExtension1 |
Akron | ec835ad | 2021-08-11 18:23:22 +0200 | [diff] [blame] | 634 | tokens = ttokenize(dat, w, "Ich habe die readme.txt heruntergeladen") |
Akron | 03ca425 | 2021-08-11 13:32:53 +0200 | [diff] [blame] | 635 | assert.Equal(tokens[0], "Ich") |
| 636 | assert.Equal(tokens[1], "habe") |
| 637 | assert.Equal(tokens[2], "die") |
| 638 | assert.Equal(tokens[3], "readme.txt") |
| 639 | assert.Equal(tokens[4], "heruntergeladen") |
| 640 | assert.Equal(len(tokens), 5) |
| 641 | |
| 642 | // Probably interpreted as HOST |
| 643 | // testTokenizerFileExtension2 |
Akron | ec835ad | 2021-08-11 18:23:22 +0200 | [diff] [blame] | 644 | tokens = ttokenize(dat, w, "Nimm die README.TXT!") |
Akron | 03ca425 | 2021-08-11 13:32:53 +0200 | [diff] [blame] | 645 | assert.Equal(tokens[0], "Nimm") |
| 646 | assert.Equal(tokens[1], "die") |
| 647 | assert.Equal(tokens[2], "README.TXT") |
| 648 | assert.Equal(tokens[3], "!") |
| 649 | assert.Equal(len(tokens), 4) |
| 650 | |
| 651 | // Probably interpreted as HOST |
| 652 | // testTokenizerFileExtension3 |
Akron | ec835ad | 2021-08-11 18:23:22 +0200 | [diff] [blame] | 653 | tokens = ttokenize(dat, w, "Zeig mir profile.jpeg") |
Akron | 03ca425 | 2021-08-11 13:32:53 +0200 | [diff] [blame] | 654 | assert.Equal(tokens[0], "Zeig") |
| 655 | assert.Equal(tokens[1], "mir") |
| 656 | assert.Equal(tokens[2], "profile.jpeg") |
| 657 | assert.Equal(len(tokens), 3) |
| 658 | |
| 659 | // testTokenizerFile1 |
Akron | 03ca425 | 2021-08-11 13:32:53 +0200 | [diff] [blame] | 660 | |
Akron | ec835ad | 2021-08-11 18:23:22 +0200 | [diff] [blame] | 661 | tokens = ttokenize(dat, w, "Zeig mir c:\\Dokumente\\profile.docx") |
Akron | e8837b5 | 2021-08-11 17:29:58 +0200 | [diff] [blame] | 662 | assert.Equal(tokens[0], "Zeig") |
| 663 | assert.Equal(tokens[1], "mir") |
| 664 | assert.Equal(tokens[2], "c:\\Dokumente\\profile.docx") |
| 665 | assert.Equal(len(tokens), 3) |
Akron | 03ca425 | 2021-08-11 13:32:53 +0200 | [diff] [blame] | 666 | |
Akron | e8837b5 | 2021-08-11 17:29:58 +0200 | [diff] [blame] | 667 | // testTokenizerFile2 |
Akron | ec835ad | 2021-08-11 18:23:22 +0200 | [diff] [blame] | 668 | tokens = ttokenize(dat, w, "Gehe zu /Dokumente/profile.docx") |
Akron | e8837b5 | 2021-08-11 17:29:58 +0200 | [diff] [blame] | 669 | assert.Equal(tokens[0], "Gehe") |
| 670 | assert.Equal(tokens[1], "zu") |
| 671 | assert.Equal(tokens[2], "/Dokumente/profile.docx") |
| 672 | assert.Equal(len(tokens), 3) |
Akron | 03ca425 | 2021-08-11 13:32:53 +0200 | [diff] [blame] | 673 | |
Akron | e8837b5 | 2021-08-11 17:29:58 +0200 | [diff] [blame] | 674 | // testTokenizerFile3 |
Akron | ec835ad | 2021-08-11 18:23:22 +0200 | [diff] [blame] | 675 | tokens = ttokenize(dat, w, "Zeig mir c:\\Dokumente\\profile.jpeg") |
Akron | e8837b5 | 2021-08-11 17:29:58 +0200 | [diff] [blame] | 676 | assert.Equal(tokens[0], "Zeig") |
| 677 | assert.Equal(tokens[1], "mir") |
| 678 | assert.Equal(tokens[2], "c:\\Dokumente\\profile.jpeg") |
| 679 | assert.Equal(len(tokens), 3) |
| 680 | // Ignored in KorAP-Tokenizer |
Akron | 03ca425 | 2021-08-11 13:32:53 +0200 | [diff] [blame] | 681 | |
Akron | fd92d7e | 2021-08-11 16:31:43 +0200 | [diff] [blame] | 682 | // testTokenizerPunct |
Akron | ec835ad | 2021-08-11 18:23:22 +0200 | [diff] [blame] | 683 | tokens = ttokenize(dat, w, "Er sagte: \"Es geht mir gut!\", daraufhin ging er.") |
Akron | fd92d7e | 2021-08-11 16:31:43 +0200 | [diff] [blame] | 684 | assert.Equal(tokens[0], "Er") |
| 685 | assert.Equal(tokens[1], "sagte") |
| 686 | assert.Equal(tokens[2], ":") |
| 687 | assert.Equal(tokens[3], "\"") |
| 688 | assert.Equal(tokens[4], "Es") |
| 689 | assert.Equal(tokens[5], "geht") |
| 690 | assert.Equal(tokens[6], "mir") |
| 691 | assert.Equal(tokens[7], "gut") |
| 692 | assert.Equal(tokens[8], "!") |
| 693 | assert.Equal(tokens[9], "\"") |
| 694 | assert.Equal(tokens[10], ",") |
| 695 | assert.Equal(tokens[11], "daraufhin") |
| 696 | assert.Equal(tokens[12], "ging") |
| 697 | assert.Equal(tokens[13], "er") |
| 698 | assert.Equal(tokens[14], ".") |
| 699 | assert.Equal(len(tokens), 15) |
Akron | 03ca425 | 2021-08-11 13:32:53 +0200 | [diff] [blame] | 700 | |
| 701 | // testTokenizerPlusAmpersand |
Akron | ec835ad | 2021-08-11 18:23:22 +0200 | [diff] [blame] | 702 | tokens = ttokenize(dat, w, ""Das ist von C&A!"") |
| 703 | assert.Equal(tokens[0], """) |
| 704 | assert.Equal(tokens[1], "Das") |
| 705 | assert.Equal(tokens[2], "ist") |
| 706 | assert.Equal(tokens[3], "von") |
| 707 | assert.Equal(tokens[4], "C&A") |
| 708 | assert.Equal(tokens[5], "!") |
| 709 | assert.Equal(tokens[6], """) |
| 710 | assert.Equal(len(tokens), 7) |
Akron | 03ca425 | 2021-08-11 13:32:53 +0200 | [diff] [blame] | 711 | |
| 712 | // testTokenizerLongEnd |
Akron | ec835ad | 2021-08-11 18:23:22 +0200 | [diff] [blame] | 713 | tokens = ttokenize(dat, w, "Siehst Du?!!?") |
Akron | 03ca425 | 2021-08-11 13:32:53 +0200 | [diff] [blame] | 714 | assert.Equal(tokens[0], "Siehst") |
| 715 | assert.Equal(tokens[1], "Du") |
| 716 | assert.Equal(tokens[2], "?!!?") |
| 717 | assert.Equal(len(tokens), 3) |
| 718 | |
| 719 | // testTokenizerIrishO |
Akron | ec835ad | 2021-08-11 18:23:22 +0200 | [diff] [blame] | 720 | tokens = ttokenize(dat, w, "Peter O'Toole") |
Akron | 03ca425 | 2021-08-11 13:32:53 +0200 | [diff] [blame] | 721 | assert.Equal(tokens[0], "Peter") |
| 722 | assert.Equal(tokens[1], "O'Toole") |
| 723 | assert.Equal(len(tokens), 2) |
| 724 | |
| 725 | // testTokenizerAbr |
Akron | ec835ad | 2021-08-11 18:23:22 +0200 | [diff] [blame] | 726 | tokens = ttokenize(dat, w, "Früher bzw. später ...") |
Akron | 03ca425 | 2021-08-11 13:32:53 +0200 | [diff] [blame] | 727 | assert.Equal(tokens[0], "Früher") |
| 728 | assert.Equal(tokens[1], "bzw.") |
| 729 | assert.Equal(tokens[2], "später") |
| 730 | assert.Equal(tokens[3], "...") |
| 731 | assert.Equal(len(tokens), 4) |
| 732 | |
| 733 | // testTokenizerUppercaseRule |
Akron | ec835ad | 2021-08-11 18:23:22 +0200 | [diff] [blame] | 734 | tokens = ttokenize(dat, w, "Es war spät.Morgen ist es früh.") |
Akron | 03ca425 | 2021-08-11 13:32:53 +0200 | [diff] [blame] | 735 | assert.Equal(tokens[0], "Es") |
| 736 | assert.Equal(tokens[1], "war") |
| 737 | assert.Equal(tokens[2], "spät") |
| 738 | assert.Equal(tokens[3], ".") |
| 739 | assert.Equal(tokens[4], "Morgen") |
| 740 | assert.Equal(tokens[5], "ist") |
| 741 | assert.Equal(tokens[6], "es") |
| 742 | assert.Equal(tokens[7], "früh") |
| 743 | assert.Equal(tokens[8], ".") |
| 744 | assert.Equal(len(tokens), 9) |
| 745 | // Ignored in KorAP-Tokenizer |
| 746 | |
| 747 | // testTokenizerOrd |
Akron | ec835ad | 2021-08-11 18:23:22 +0200 | [diff] [blame] | 748 | tokens = ttokenize(dat, w, "Sie erreichte den 1. Platz!") |
Akron | a0bded5 | 2021-08-11 15:48:02 +0200 | [diff] [blame] | 749 | assert.Equal(tokens[0], "Sie") |
| 750 | assert.Equal(tokens[1], "erreichte") |
| 751 | assert.Equal(tokens[2], "den") |
| 752 | assert.Equal(tokens[3], "1.") |
| 753 | assert.Equal(tokens[4], "Platz") |
| 754 | assert.Equal(tokens[5], "!") |
| 755 | assert.Equal(len(tokens), 6) |
Akron | 03ca425 | 2021-08-11 13:32:53 +0200 | [diff] [blame] | 756 | |
| 757 | // testNoZipOuputArchive |
Akron | ec835ad | 2021-08-11 18:23:22 +0200 | [diff] [blame] | 758 | tokens = ttokenize(dat, w, "Archive: Ich bin kein zip\n") |
Akron | 03ca425 | 2021-08-11 13:32:53 +0200 | [diff] [blame] | 759 | assert.Equal(tokens[0], "Archive") |
| 760 | assert.Equal(tokens[1], ":") |
| 761 | assert.Equal(tokens[2], "Ich") |
| 762 | assert.Equal(tokens[3], "bin") |
| 763 | assert.Equal(tokens[4], "kein") |
| 764 | assert.Equal(tokens[5], "zip") |
| 765 | assert.Equal(6, len(tokens)) |
| 766 | |
| 767 | // testTokenizerStrasse |
Akron | ec835ad | 2021-08-11 18:23:22 +0200 | [diff] [blame] | 768 | tokens = ttokenize(dat, w, "Ich wohne in der Weststr. und Du?") |
Akron | 4af79f1 | 2021-08-11 14:48:17 +0200 | [diff] [blame] | 769 | assert.Equal(tokens[4], "Weststr.") |
| 770 | assert.Equal(8, len(tokens)) |
Akron | 03ca425 | 2021-08-11 13:32:53 +0200 | [diff] [blame] | 771 | |
| 772 | // germanTokenizerKnowsGermanOmissionWords |
Akron | ec835ad | 2021-08-11 18:23:22 +0200 | [diff] [blame] | 773 | tokens = ttokenize(dat, w, "D'dorf Ku'damm Lu'hafen M'gladbach W'schaft") |
Akron | 03ca425 | 2021-08-11 13:32:53 +0200 | [diff] [blame] | 774 | assert.Equal("D'dorf", tokens[0]) |
| 775 | assert.Equal("Ku'damm", tokens[1]) |
| 776 | assert.Equal("Lu'hafen", tokens[2]) |
| 777 | assert.Equal("M'gladbach", tokens[3]) |
| 778 | assert.Equal("W'schaft", tokens[4]) |
| 779 | assert.Equal(5, len(tokens)) |
| 780 | |
| 781 | // germanTokenizerDoesNOTSeparateGermanContractions |
Akron | ec835ad | 2021-08-11 18:23:22 +0200 | [diff] [blame] | 782 | tokens = ttokenize(dat, w, "mach's macht's was'n ist's haste willste kannste biste kriegste") |
Akron | 03ca425 | 2021-08-11 13:32:53 +0200 | [diff] [blame] | 783 | assert.Equal("mach's", tokens[0]) |
| 784 | assert.Equal("macht's", tokens[1]) |
| 785 | assert.Equal("was'n", tokens[2]) |
| 786 | assert.Equal("ist's", tokens[3]) |
| 787 | assert.Equal("haste", tokens[4]) |
| 788 | assert.Equal("willste", tokens[5]) |
| 789 | assert.Equal("kannste", tokens[6]) |
| 790 | assert.Equal("biste", tokens[7]) |
| 791 | assert.Equal("kriegste", tokens[8]) |
| 792 | assert.Equal(9, len(tokens)) |
| 793 | |
| 794 | /* |
| 795 | @Test |
| 796 | public void englishTokenizerSeparatesEnglishContractionsAndClitics () { |
| 797 | DerekoDfaTokenizer_en tok = new DerekoDfaTokenizer_en(); |
| 798 | tokens = tokenize(dat, w, "I've we'll you'd I'm we're Peter's isn't") |
| 799 | assert.Equal("'ve", tokens[1]); |
| 800 | assert.Equal("'ll", tokens[3]); |
| 801 | assert.Equal("'d", tokens[5]); |
| 802 | assert.Equal("'m", tokens[7]); |
| 803 | assert.Equal("'re", tokens[9]); |
| 804 | assert.Equal("'s", tokens[11]); |
| 805 | assert.Equal("is", tokens[12]); |
| 806 | assert.Equal("n't", tokens[13]); |
| 807 | assert.Equal(14, len(tokens)); |
| 808 | } |
| 809 | |
| 810 | @Test |
| 811 | public void frenchTokenizerKnowsFrenchAbbreviations () { |
| 812 | DerekoDfaTokenizer_fr tok = new DerekoDfaTokenizer_fr(); |
| 813 | tokens = tokenize(dat, w, "Approx. en juill. 2004 mon prof. M. Foux m'a dit qu'il faut faire exerc. no. 4, et lire pp. 27-30.") |
| 814 | assert.Equal("Approx.", tokens[0]); |
| 815 | assert.Equal("juill.", tokens[2]); |
| 816 | assert.Equal("prof.", tokens[5]); |
| 817 | assert.Equal("exerc.", tokens[15]); |
| 818 | assert.Equal("no.", tokens[16]); |
| 819 | assert.Equal("pp.", tokens[21]); |
| 820 | } |
| 821 | |
| 822 | @Test |
| 823 | public void frenchTokenizerKnowsFrenchContractions () { |
| 824 | DerekoDfaTokenizer_fr tok = new DerekoDfaTokenizer_fr(); |
| 825 | tokens = tokenize(dat, w, "J'ai j'habite qu'il d'un jusqu'à Aujourd'hui D'accord Quelqu'un Presqu'île") |
| 826 | assert.Equal("J'", tokens[0]); |
| 827 | assert.Equal("j'", tokens[2]); |
| 828 | assert.Equal("qu'", tokens[4]); |
| 829 | assert.Equal("d'", tokens[6]); |
| 830 | assert.Equal("jusqu'", tokens[8]); |
| 831 | assert.Equal("Aujourd'hui", tokens[10]); |
| 832 | assert.Equal("D'", tokens[11]); // ’ |
| 833 | assert.Equal("Quelqu'un", tokens[13]); // ’ |
| 834 | assert.Equal("Presqu'île", tokens[14]); // ’ |
| 835 | } |
| 836 | |
| 837 | @Test |
| 838 | public void frenchTokenizerKnowsFrenchClitics () { |
| 839 | DerekoDfaTokenizer_fr tok = new DerekoDfaTokenizer_fr(); |
| 840 | tokens = tokenize(dat, w, "suis-je sont-elles ") |
| 841 | assert.Equal("suis", tokens[0]); |
| 842 | assert.Equal("-je", tokens[1]); |
| 843 | assert.Equal("sont", tokens[2]); |
| 844 | assert.Equal("-elles", tokens[3]); |
| 845 | } |
| 846 | |
| 847 | @Test |
| 848 | public void testEnglishTokenizerScienceAbbreviations () { |
| 849 | DerekoDfaTokenizer_en tok = new DerekoDfaTokenizer_en(); |
| 850 | tokens = tokenize(dat, w, "Approx. in Sept. 1954, Assoc. Prof. Dr. R. J. Ewing reviewed articles on Enzymol. Bacteriol. effects later published in Nutr. Rheumatol. No. 12 and Nº. 13., pp. 17-18.") |
| 851 | assert.Equal("Approx.", tokens[0]); |
| 852 | assert.Equal("in", tokens[1]); |
| 853 | assert.Equal("Sept.", tokens[2]); |
| 854 | assert.Equal("1954", tokens[3]); |
| 855 | assert.Equal(",", tokens[4]); |
| 856 | assert.Equal("Assoc.", tokens[5]); |
| 857 | assert.Equal("Prof.", tokens[6]); |
| 858 | assert.Equal("Dr.", tokens[7]); |
| 859 | assert.Equal("R.", tokens[8]); |
| 860 | assert.Equal("J.", tokens[9]); |
| 861 | assert.Equal("Ewing", tokens[10]); |
| 862 | assert.Equal("reviewed", tokens[11]); |
| 863 | assert.Equal("articles", tokens[12]); |
| 864 | assert.Equal("on", tokens[13]); |
| 865 | assert.Equal("Enzymol.", tokens[14]); |
| 866 | assert.Equal("Bacteriol.", tokens[15]); |
| 867 | assert.Equal("effects", tokens[16]); |
| 868 | assert.Equal("later", tokens[17]); |
| 869 | assert.Equal("published", tokens[18]); |
| 870 | assert.Equal("in", tokens[19]); |
| 871 | assert.Equal("Nutr.", tokens[20]); |
| 872 | assert.Equal("Rheumatol.", tokens[21]); |
| 873 | assert.Equal("No.", tokens[22]); |
| 874 | assert.Equal("12", tokens[23]); |
| 875 | assert.Equal("and", tokens[24]); |
| 876 | assert.Equal("Nº.", tokens[25]); |
| 877 | assert.Equal("13.", tokens[26]); |
| 878 | assert.Equal(",", tokens[27]); |
| 879 | assert.Equal("pp.", tokens[28]); |
| 880 | assert.Equal("17-18", tokens[29]); |
| 881 | assert.Equal(".", tokens[30]); |
| 882 | } |
| 883 | |
| 884 | @Test |
| 885 | public void englishTokenizerCanGuessWhetherIIsAbbrev () { |
| 886 | DerekoDfaTokenizer_en tok = new DerekoDfaTokenizer_en(); |
| 887 | tokens = tokenize(dat, w, "M. I. Baxter was born during World War I. So was I. He went to the Peter I. Hardy school. So did I.") |
| 888 | assert.Equal("I.", tokens[1]); |
| 889 | assert.Equal("I", tokens[8]); |
| 890 | assert.Equal(".", tokens[9]); |
| 891 | assert.Equal("I", tokens[12]); |
| 892 | assert.Equal(".", tokens[13]); |
| 893 | } |
| 894 | |
| 895 | @Test |
| 896 | public void testZipOuputArchive () { |
| 897 | |
| 898 | final ByteArrayOutputStream clearOut = new ByteArrayOutputStream(); |
| 899 | System.setOut(new PrintStream(clearOut)); |
| 900 | tokens = tokenize(dat, w, "Archive: ich/bin/ein.zip\n") |
| 901 | assert.Equal(0, len(tokens)); |
| 902 | } |
| 903 | */ |
| 904 | /* |
| 905 | |
| 906 | @Test |
| 907 | public void testTextBreakOutputArchive () throws InstantiationException, IllegalAccessException, ClassNotFoundException { |
| 908 | DerekoDfaTokenizer_de tok = (DerekoDfaTokenizer_de) new KorapTokenizer.Builder() |
| 909 | .tokenizerClassName(DerekoDfaTokenizer_de.class.getName()) |
| 910 | .printOffsets(true) |
| 911 | .build(); |
| 912 | Span[] tokens = tok.tokenizePos("Text1\004\nText2 Hallo\004Rumsdibums\004Das freut mich sehr.\n"); |
| 913 | assert.Equal("Text1", tokens[0].getType()); |
| 914 | assert.Equal(len(tokens), 9 ); |
| 915 | } |
| 916 | */ |
| 917 | } |
Akron | bd40680 | 2021-08-11 18:39:13 +0200 | [diff] [blame] | 918 | |
Akron | df27581 | 2022-03-27 12:54:46 +0200 | [diff] [blame] | 919 | func TestDoubleArrayFullTokenizerSentenceSplitterBug1(t *testing.T) { |
| 920 | assert := assert.New(t) |
| 921 | |
| 922 | if dat == nil { |
| 923 | dat = LoadDatokFile("testdata/tokenizer.datok") |
| 924 | } |
| 925 | |
| 926 | b := make([]byte, 0, 2048) |
| 927 | w := bytes.NewBuffer(b) |
| 928 | var sentences []string |
| 929 | |
| 930 | text := `Wüllersdorf war aufgestanden. »Ich finde es furchtbar, daß Sie recht haben, aber Sie haben recht. Ich quäle Sie nicht länger mit meinem 'Muß es sein?'. Die Welt ist einmal, wie sie ist, und die Dinge verlaufen nicht, wie wir wollen, sondern wie die andern wollen. Das mit dem 'Gottesgericht', wie manche hochtrabend versichern, ist freilich ein Unsinn, nichts davon, umgekehrt, unser Ehrenkultus ist ein Götzendienst, aber wir müssen uns ihm unterwerfen, solange der Götze gilt.«` |
| 931 | |
| 932 | w.Reset() |
| 933 | assert.True(dat.Transduce(strings.NewReader(text), w)) |
| 934 | sentences = strings.Split(w.String(), "\n\n") |
Akron | b428755 | 2022-03-27 14:11:24 +0200 | [diff] [blame] | 935 | assert.Equal(len(sentences), 6) |
Akron | df27581 | 2022-03-27 12:54:46 +0200 | [diff] [blame] | 936 | assert.Equal("Wüllersdorf\nwar\naufgestanden\n.", sentences[0]) |
| 937 | assert.Equal("»\nIch\nfinde\nes\nfurchtbar\n,\ndaß\nSie\nrecht\nhaben\n,\naber\nSie\nhaben\nrecht\n.", sentences[1]) |
Akron | b428755 | 2022-03-27 14:11:24 +0200 | [diff] [blame] | 938 | assert.Equal("Ich\nquäle\nSie\nnicht\nlänger\nmit\nmeinem\n'\nMuß\nes\nsein\n?\n'\n.", sentences[2]) |
| 939 | assert.Equal("Die\nWelt\nist\neinmal\n,\nwie\nsie\nist\n,\nund\ndie\nDinge\nverlaufen\nnicht\n,\nwie\nwir\nwollen\n,\nsondern\nwie\ndie\nandern\nwollen\n.", sentences[3]) |
| 940 | assert.Equal("Das\nmit\ndem\n'\nGottesgericht\n'\n,\nwie\nmanche\nhochtrabend\nversichern\n,\nist\nfreilich\nein\nUnsinn\n,\nnichts\ndavon\n,\numgekehrt\n,\nunser\nEhrenkultus\nist\nein\nGötzendienst\n,\naber\nwir\nmüssen\nuns\nihm\nunterwerfen\n,\nsolange\nder\nGötze\ngilt\n.\n«", sentences[4]) |
Akron | df27581 | 2022-03-27 12:54:46 +0200 | [diff] [blame] | 941 | } |
| 942 | |
Akron | c9c0eae | 2021-10-22 19:49:43 +0200 | [diff] [blame] | 943 | func TestDoubleArrayLoadFactor1(t *testing.T) { |
Akron | 29e306f | 2021-09-02 18:29:56 +0200 | [diff] [blame] | 944 | assert := assert.New(t) |
| 945 | tok := LoadFomaFile("testdata/abbr_bench.fst") |
| 946 | dat := tok.ToDoubleArray() |
| 947 | assert.True(dat.LoadFactor() > 88) |
| 948 | } |
| 949 | |
Akron | c9c0eae | 2021-10-22 19:49:43 +0200 | [diff] [blame] | 950 | func TestDoubleArrayFullTokenizerXML(t *testing.T) { |
Akron | 4c2a1ad | 2021-08-31 00:35:53 +0200 | [diff] [blame] | 951 | assert := assert.New(t) |
| 952 | |
Akron | 9fb63af | 2021-10-28 01:15:53 +0200 | [diff] [blame] | 953 | if dat == nil { |
| 954 | dat = LoadDatokFile("testdata/tokenizer.datok") |
| 955 | } |
| 956 | |
Akron | 4c2a1ad | 2021-08-31 00:35:53 +0200 | [diff] [blame] | 957 | assert.NotNil(dat) |
| 958 | |
| 959 | b := make([]byte, 0, 2048) |
| 960 | w := bytes.NewBuffer(b) |
| 961 | var tokens []string |
| 962 | |
| 963 | tokens = ttokenize(dat, w, "Das <b>beste</b> Fußballspiel") |
| 964 | assert.Equal("Das", tokens[0]) |
| 965 | assert.Equal("<b>", tokens[1]) |
| 966 | assert.Equal("beste", tokens[2]) |
| 967 | assert.Equal("</b>", tokens[3]) |
| 968 | assert.Equal("Fußballspiel", tokens[4]) |
| 969 | assert.Equal(5, len(tokens)) |
| 970 | |
| 971 | tokens = ttokenize(dat, w, "Das <b class=\"c\">beste</b> Fußballspiel") |
| 972 | assert.Equal("Das", tokens[0]) |
| 973 | assert.Equal("<b class=\"c\">", tokens[1]) |
| 974 | assert.Equal("beste", tokens[2]) |
| 975 | assert.Equal("</b>", tokens[3]) |
| 976 | assert.Equal("Fußballspiel", tokens[4]) |
| 977 | assert.Equal(5, len(tokens)) |
| 978 | |
| 979 | tokens = ttokenize(dat, w, "der<x y=\"alte \"> <x x> alte</x> etc. et. Mann.") |
| 980 | assert.Equal("der", tokens[0]) |
| 981 | assert.Equal("<x y=\"alte \">", tokens[1]) |
| 982 | assert.Equal("<x x>", tokens[2]) |
| 983 | assert.Equal("alte", tokens[3]) |
| 984 | assert.Equal("</x>", tokens[4]) |
| 985 | assert.Equal("etc.", tokens[5]) |
| 986 | assert.Equal("et", tokens[6]) |
| 987 | assert.Equal(".", tokens[7]) |
| 988 | assert.Equal("Mann", tokens[8]) |
| 989 | assert.Equal(".", tokens[9]) |
| 990 | assert.Equal(10, len(tokens)) |
| 991 | } |
| 992 | |
Akron | c9c0eae | 2021-10-22 19:49:43 +0200 | [diff] [blame] | 993 | func BenchmarkDoubleArrayTransduce(b *testing.B) { |
Akron | bd40680 | 2021-08-11 18:39:13 +0200 | [diff] [blame] | 994 | bu := make([]byte, 0, 2048) |
| 995 | w := bytes.NewBuffer(bu) |
| 996 | |
| 997 | s := `Der Vorsitzende der Abk. hat gewählt. Gefunden auf wikipedia.org. Ich bin unter korap@ids-mannheim.de erreichbar. |
| 998 | Unsere Website ist https://korap.ids-mannheim.de/?q=Baum. Unser Server ist 10.0.10.51. Zu 50.4% ist es sicher. |
| 999 | Der Termin ist am 5.9.2018. |
| 1000 | Ich habe die readme.txt heruntergeladen. |
| 1001 | Ausschalten!!! Hast Du nicht gehört??? |
| 1002 | Ich wohne in der Weststr. und Du? Kupietz und Schmidt [2018]: Korpuslinguistik. Dieses verf***** Kleid! Ich habe die readme.txt heruntergeladen. |
| 1003 | Er sagte: \"Es geht mir gut!\", daraufhin ging er. "Das ist von C&A!" Früher bzw. später ... Sie erreichte den 1. Platz! |
| 1004 | Archive: Ich bin kein zip. D'dorf Ku'damm Lu'hafen M'gladbach W'schaft. |
| 1005 | Mach's macht's was'n ist's haste willste kannste biste kriegste.` |
| 1006 | r := strings.NewReader(s) |
| 1007 | |
| 1008 | dat := LoadDatokFile("testdata/tokenizer.datok") |
| 1009 | |
Akron | df37a55 | 2021-09-02 12:16:08 +0200 | [diff] [blame] | 1010 | b.ResetTimer() |
| 1011 | |
Akron | bd40680 | 2021-08-11 18:39:13 +0200 | [diff] [blame] | 1012 | for i := 0; i < b.N; i++ { |
| 1013 | w.Reset() |
| 1014 | r.Reset(s) |
| 1015 | ok := dat.Transduce(r, w) |
| 1016 | if !ok { |
| 1017 | fmt.Println("Fail!") |
| 1018 | fmt.Println(w.String()) |
| 1019 | os.Exit(1) |
| 1020 | } |
| 1021 | } |
Akron | bd40680 | 2021-08-11 18:39:13 +0200 | [diff] [blame] | 1022 | } |
Akron | bb4aac5 | 2021-08-13 00:52:27 +0200 | [diff] [blame] | 1023 | |
Akron | 6f1c16c | 2021-08-17 10:45:42 +0200 | [diff] [blame] | 1024 | // This test is deprecated as the datok file changes over time |
| 1025 | func XBenchmarkLoadDatokFile(b *testing.B) { |
Akron | bb4aac5 | 2021-08-13 00:52:27 +0200 | [diff] [blame] | 1026 | for i := 0; i < b.N; i++ { |
| 1027 | dat := LoadDatokFile("testdata/tokenizer.datok") |
| 1028 | if dat == nil { |
| 1029 | fmt.Println("Fail!") |
| 1030 | os.Exit(1) |
| 1031 | } |
| 1032 | } |
| 1033 | } |
| 1034 | |
Akron | c9c0eae | 2021-10-22 19:49:43 +0200 | [diff] [blame] | 1035 | func BenchmarkDoubleArrayConstruction(b *testing.B) { |
Akron | 6f1c16c | 2021-08-17 10:45:42 +0200 | [diff] [blame] | 1036 | tok := LoadFomaFile("testdata/simple_bench.fst") |
Akron | df37a55 | 2021-09-02 12:16:08 +0200 | [diff] [blame] | 1037 | b.ResetTimer() |
Akron | 6f1c16c | 2021-08-17 10:45:42 +0200 | [diff] [blame] | 1038 | for i := 0; i < b.N; i++ { |
| 1039 | dat := tok.ToDoubleArray() |
| 1040 | if dat == nil { |
| 1041 | fmt.Println("Fail!") |
| 1042 | os.Exit(1) |
| 1043 | } |
| 1044 | } |
| 1045 | } |
| 1046 | |
Akron | c9c0eae | 2021-10-22 19:49:43 +0200 | [diff] [blame] | 1047 | func BenchmarkDoubleArrayLarger(b *testing.B) { |
Akron | 7b1faa6 | 2021-09-02 16:10:21 +0200 | [diff] [blame] | 1048 | tok := LoadFomaFile("testdata/abbr_bench.fst") |
| 1049 | b.ResetTimer() |
| 1050 | for i := 0; i < b.N; i++ { |
| 1051 | dat := tok.ToDoubleArray() |
| 1052 | if dat == nil { |
| 1053 | fmt.Println("Fail!") |
| 1054 | os.Exit(1) |
| 1055 | } |
| 1056 | } |
| 1057 | } |
| 1058 | |
Akron | bb4aac5 | 2021-08-13 00:52:27 +0200 | [diff] [blame] | 1059 | // 2021-08-11 (go 1.16) |
| 1060 | // go test -bench=. -test.benchmem |
| 1061 | // BenchmarkTransduce-4 19069 60609 ns/op 11048 B/op 137 allocs/op |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 1062 | // 2021-08-12 (go 1.16) |
Akron | bb4aac5 | 2021-08-13 00:52:27 +0200 | [diff] [blame] | 1063 | // BenchmarkTransduce-4 20833 55241 ns/op 9676 B/op 3 allocs/op |
| 1064 | // BenchmarkLoadDatokFile-4 4 258418169 ns/op 29916470 B/op 5697 allocs/op |
| 1065 | // BenchmarkTransduce-4 19430 58133 ns/op 18696 B/op 3 allocs/op |
| 1066 | // BenchmarkLoadDatokFile-4 8 139071939 ns/op 203158377 B/op 5742 allocs/op |
Akron | f1a1650 | 2021-08-16 15:24:38 +0200 | [diff] [blame] | 1067 | // 2021-08-16 |
| 1068 | // BenchmarkTransduce-4 22251 49989 ns/op 17370 B/op 3 allocs/op |
| 1069 | // BenchmarkLoadDatokFile-4 8 138937532 ns/op 203158327 B/op 5742 allocs/op |
| 1070 | // BenchmarkTransduce-4 22005 48665 ns/op 17472 B/op 3 allocs/op |
| 1071 | // BenchmarkLoadDatokFile-4 7 143143934 ns/op 203158450 B/op 5743 allocs/op |
Akron | ea46e8a | 2021-08-17 00:36:31 +0200 | [diff] [blame] | 1072 | // BenchmarkTransduce-4 34939 34363 ns/op 14056 B/op 3 allocs/op |
| 1073 | // BenchmarkLoadDatokFile-4 7 149511609 ns/op 203217193 B/op 5915 allocs/op |
Akron | 6f1c16c | 2021-08-17 10:45:42 +0200 | [diff] [blame] | 1074 | // 2021-08-17 |
| 1075 | // BenchmarkTransduce-4 31204 32678 ns/op 14752 B/op 3 allocs/op |
| 1076 | // BenchmarkToDoubleArray-4 44138 26850 ns/op 10704 B/op 29 allocs/op |
Akron | de18e90 | 2021-08-27 09:34:12 +0200 | [diff] [blame] | 1077 | // BenchmarkTransduce-4 29376 34562 ns/op 15157 B/op 3 allocs/op |
| 1078 | // BenchmarkToDoubleArray-4 54441 21355 ns/op 10704 B/op 29 allocs/op |
Akron | df37a55 | 2021-09-02 12:16:08 +0200 | [diff] [blame] | 1079 | // 2021-09-02 - New tokenizer - fixed loading |
Akron | 7b1faa6 | 2021-09-02 16:10:21 +0200 | [diff] [blame] | 1080 | // BenchmarkTransduce-4 40149 31515 ns/op 8240 B/op 3 allocs/op |
| 1081 | // BenchmarkToDoubleArray-4 51043 22586 ns/op 10702 B/op 29 allocs/op |
| 1082 | // BenchmarkToDoubleArrayLarger-4 3 396009639 ns/op 6352293 B/op 2575 allocs/op |
| 1083 | // BenchmarkTransduce-4 38698 31900 ns/op 8240 B/op 3 allocs/op |
| 1084 | // BenchmarkToDoubleArray-4 50644 21569 ns/op 11151 B/op 14 allocs/op |
| 1085 | // BenchmarkToDoubleArrayLarger-4 3 441260766 ns/op 6942336 B/op 30 allocs/op |
| 1086 | // BenchmarkTransduce-4 39966 30835 ns/op 8240 B/op 3 allocs/op |
| 1087 | // BenchmarkToDoubleArray-4 50720 24863 ns/op 11091 B/op 46 allocs/op |
| 1088 | // BenchmarkToDoubleArrayLarger-4 3 432523828 ns/op 6413381 B/op 5122 allocs/op |
Akron | 679b486 | 2021-09-02 16:59:26 +0200 | [diff] [blame] | 1089 | // 2021-09-02 - xCheckSkip() with .9 |
| 1090 | // BenchmarkTransduce-4 36325 38501 ns/op 8240 B/op 3 allocs/op |
| 1091 | // BenchmarkToDoubleArray-4 66858 19286 ns/op 10607 B/op 29 allocs/op |
| 1092 | // BenchmarkToDoubleArrayLarger-4 18 67428011 ns/op 6360604 B/op 2578 allocs/op |
Akron | 29e306f | 2021-09-02 18:29:56 +0200 | [diff] [blame] | 1093 | // 2021-09-02 - xCheckSkipNiu() with .9 and >= 3 |
| 1094 | // BenchmarkTransduce-4 37105 27714 ns/op 8240 B/op 3 allocs/op |
| 1095 | // BenchmarkToDoubleArray-4 76600 15973 ns/op 10703 B/op 29 allocs/op |
| 1096 | // BenchmarkToDoubleArrayLarger-4 21 55161934 ns/op 6357889 B/op 2578 allocs/op |
Akron | 28031b7 | 2021-10-02 13:07:25 +0200 | [diff] [blame] | 1097 | // 2021-09-30 - Go 1.17.1 |
| 1098 | // BenchmarkTransduce-4 47222 25962 ns/op 8240 B/op 3 allocs/op |
| 1099 | // BenchmarkToDoubleArray-4 69192 17355 ns/op 10704 B/op 29 allocs/op |
| 1100 | // BenchmarkToDoubleArrayLarger-4 16 65042885 ns/op 6357794 B/op 2576 allocs/op |
| 1101 | // BenchmarkTransduceMatrix-4 45404 25156 ns/op 8240 B/op 3 allocs/op |
Akron | 094a4e8 | 2021-10-02 18:37:00 +0200 | [diff] [blame] | 1102 | // 2021-10-02 |
| 1103 | // BenchmarkTransduce-4 47676 25398 ns/op 8240 B/op 3 allocs/op |
| 1104 | // BenchmarkToDoubleArray-4 71919 16083 ns/op 10702 B/op 29 allocs/op |
| 1105 | // BenchmarkToDoubleArrayLarger-4 16 68012819 ns/op 6357920 B/op 2578 allocs/op |
| 1106 | // BenchmarkTransduceMatrix-4 51529 23678 ns/op 8240 B/op 3 allocs/op |
Akron | e396a93 | 2021-10-19 01:06:13 +0200 | [diff] [blame] | 1107 | // 2021-10-12 - Introduction of Callbacks in Matrix |
| 1108 | // BenchmarkTransduce-4 46947 26043 ns/op 8240 B/op 3 allocs/op |
| 1109 | // BenchmarkToDoubleArray-4 65192 16501 ns/op 10703 B/op 29 allocs/op |
| 1110 | // BenchmarkToDoubleArrayLarger-4 15 69263576 ns/op 6357859 B/op 2577 allocs/op |
| 1111 | // BenchmarkTransduceMatrix-4 49928 26313 ns/op 12408 B/op 6 allocs/op |
| 1112 | // 2021-10-18 - Introduction of Callbacks in DA |
| 1113 | // BenchmarkTransduce-4 41055 30058 ns/op 12408 B/op 6 allocs/op |
| 1114 | // BenchmarkToDoubleArray-4 64672 17659 ns/op 10703 B/op 29 allocs/op |
| 1115 | // BenchmarkToDoubleArrayLarger-4 15 71640553 ns/op 6357865 B/op 2577 allocs/op |
| 1116 | // BenchmarkTransduceMatrix-4 47036 26009 ns/op 12408 B/op 6 allocs/op |
Akron | a854faa | 2021-10-22 19:31:08 +0200 | [diff] [blame] | 1117 | // 2021-10-21 - Simplify DA code to ignore final states |
| 1118 | // BenchmarkTransduce-4 41365 33766 ns/op 12408 B/op 6 allocs/op |
| 1119 | // BenchmarkToDoubleArray-4 63663 17675 ns/op 10703 B/op 29 allocs/op |
| 1120 | // BenchmarkToDoubleArrayLarger-4 16 83535733 ns/op 6357874 B/op 2577 allocs/op |
| 1121 | // BenchmarkTransduceMatrix-4 45362 25258 ns/op 12408 B/op 6 allocs/op |
Akron | 98fbfef | 2021-10-23 17:02:11 +0200 | [diff] [blame] | 1122 | // 2021-10-22 - Introduxe EOT |
Akron | c9c0eae | 2021-10-22 19:49:43 +0200 | [diff] [blame] | 1123 | // BenchmarkDoubleArrayTransduce-4 43820 27661 ns/op 12408 B/op 6 allocs/op |
| 1124 | // BenchmarkDoubleArrayConstruction-4 68259 16608 ns/op 10703 B/op 29 allocs/op |
| 1125 | // BenchmarkDoubleArrayLarger-4 16 69889532 ns/op 6357901 B/op 2578 allocs/op |
| 1126 | // BenchmarkMatrixTransduce-4 49426 25105 ns/op 12408 B/op 6 allocs/op |
Akron | 98fbfef | 2021-10-23 17:02:11 +0200 | [diff] [blame] | 1127 | // 2021-10-23 - Improve offset handling |
| 1128 | // BenchmarkDoubleArrayTransduce-4 41890 29729 ns/op 12408 B/op 6 allocs/op |
| 1129 | // BenchmarkDoubleArrayConstruction-4 74510 15879 ns/op 10703 B/op 29 allocs/op |
| 1130 | // BenchmarkDoubleArrayLarger-4 18 73752383 ns/op 6357956 B/op 2579 allocs/op |
| 1131 | // BenchmarkMatrixTransduce-4 46870 27140 ns/op 12408 B/op 6 allocs/op |
Akron | 04335c6 | 2021-10-28 11:56:00 +0200 | [diff] [blame] | 1132 | // 2021-10-28 - Finalize feature compatibility with KorAP-Tokenizer |
| 1133 | // BenchmarkDoubleArrayTransduce-4 39130 31612 ns/op 28944 B/op 16 allocs/op |
| 1134 | // BenchmarkDoubleArrayConstruction-4 79302 14994 ns/op 10703 B/op 29 allocs/op |
| 1135 | // BenchmarkDoubleArrayLarger-4 18 67942077 ns/op 6357870 B/op 2577 allocs/op |
| 1136 | // BenchmarkMatrixTransduce-4 39536 30510 ns/op 28944 B/op 16 allocs/op |
Akron | 289414f | 2021-11-09 19:56:42 +0100 | [diff] [blame] | 1137 | // 2021-11-09 - go 1.17.3 |
| 1138 | // BenchmarkDoubleArrayTransduce-4 35067 34192 ns/op 28944 B/op 17 allocs/op |
| 1139 | // BenchmarkDoubleArrayConstruction-4 72446 15614 ns/op 10703 B/op 29 allocs/op |
| 1140 | // BenchmarkDoubleArrayLarger-4 16 71058822 ns/op 6357860 B/op 2577 allocs/op |
| 1141 | // BenchmarkMatrixTransduce-4 36703 31891 ns/op 28944 B/op 17 allocs/op |
Akron | fac8abc | 2021-11-10 07:19:59 +0100 | [diff] [blame] | 1142 | // 2021-11-10 - rearranged longest match operator |
Akron | 4880fb6 | 2021-12-05 12:03:05 +0100 | [diff] [blame] | 1143 | // BenchmarkDoubleArrayTransduce-4 34522 33210 ns/op 28944 B/op 17 allocs/op |
| 1144 | // BenchmarkDoubleArrayConstruction-4 66990 16012 ns/op 10703 B/op 29 allocs/op |
| 1145 | // BenchmarkDoubleArrayLarger-4 16 62829878 ns/op 6357823 B/op 2576 allocs/op |
| 1146 | // BenchmarkMatrixTransduce-4 36154 32702 ns/op 28944 B/op 17 allocs/op |
| 1147 | // 2021-12-04 - optimize identity branch |
| 1148 | // BenchmarkDoubleArrayTransduce-4 34903 32255 ns/op 28944 B/op 17 allocs/op |
| 1149 | // BenchmarkDoubleArrayConstruction-4 79394 14561 ns/op 10703 B/op 29 allocs/op |
| 1150 | // BenchmarkDoubleArrayLarger-4 19 60257675 ns/op 6357911 B/op 2577 allocs/op |
| 1151 | // BenchmarkMatrixTransduce-4 35076 30581 ns/op 28944 B/op 17 allocs/op |
Akron | 00cecd1 | 2021-12-05 13:14:03 +0100 | [diff] [blame] | 1152 | // 2021-12-05 - init identity for sigma < 256 |
| 1153 | // BenchmarkDoubleArrayTransduce-4 35284 31918 ns/op 28944 B/op 17 allocs/op |
| 1154 | // BenchmarkDoubleArrayConstruction-4 80342 14504 ns/op 10703 B/op 29 allocs/op |
| 1155 | // BenchmarkDoubleArrayLarger-4 19 60343253 ns/op 6357789 B/op 2575 allocs/op |
| 1156 | // BenchmarkMatrixTransduce-4 34029 30238 ns/op 28944 B/op 17 allocs/op |