Debian Codegraph @

test/lua/unit/tokenizer.lua

raw
1 context("Text tokenization test", function()
2 local util = require "rspamd_util"
3 local logger = require "rspamd_logger"
4
5 local cases = {
6 {"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer mattis, nibh",
7 {"Lorem", "ipsum", "dolor", "sit", "amet", "consectetur", "adipiscing", "elit",
8 "Integer", "mattis", "nibh"
9 }
10 },
11 {"Հետաքրքրվողների համար ոտորև ներկայացված",
12 {"Հետաքրքրվողների", "համար", "ոտորև", "ներկայացված"}
13 },
14 {"", {}},
15 {",,,,,", {}},
16 {"word,,,,,word ", {"word", "word"}},
17 {"word", {"word"}},
18 {",,,,word,,,", {"word"}}
19 }
20
21 for i,c in ipairs(cases) do
22 test("Tokenize simple " .. i, function()
23 local w = util.tokenize_text(c[1])
24 if #c[2] == 0 then
25 assert_equal(#w, 0, "must not have tokens " .. c[1])
26 else
27 assert_not_nil(w, "must tokenize " .. c[1])
28
29 for i,wrd in ipairs(w) do
30 assert_equal(wrd, c[2][i])
31 end
32 end
33 end)
34 end
35
36 cases = {
37 {"word https://example.com/path word",
38 {{5, 24}},
39 {"word", "!!EX!!", "word"}
40 },
41 {"համար https://example.com/path համար",
42 {{11, 24}},
43 {"համար", "!!EX!!", "համար"}
44 },
45 {"word https://example.com/path https://example.com/path word",
46 {{5, 24}, {30, 24}},
47 {"word", "!!EX!!", "!!EX!!", "word"}
48 },
49 {"word https://example.com/path https://example.com/path",
50 {{5, 24}, {30, 24}},
51 {"word", "!!EX!!", "!!EX!!"}
52 },
53 {"https://example.com/path https://example.com/path word",
54 {{0, 24}, {25, 24}},
55 {"!!EX!!", "!!EX!!", "word"}
56 },
57 {"https://example.com/path https://example.com/path",
58 {{0, 24}, {25, 24}},
59 {"!!EX!!", "!!EX!!"}
60 },
61 {",,,,https://example.com/path https://example.com/path ",
62 {{4, 24}, {29, 24}},
63 {"!!EX!!", "!!EX!!"}
64 },
65 }
66
67 for i,c in ipairs(cases) do
68 test("Tokenize with exceptions " .. i, function()
69 local w = util.tokenize_text(c[1], c[2])
70 if #c[3] == 0 then
71 assert_equal(#w, 0, "must not have tokens " .. c[1])
72 else
73 assert_not_nil(w, "must tokenize " .. c[1])
74 for i,wrd in ipairs(w) do
75 assert_equal(wrd, c[3][i])
76 end
77 end
78 end)
79 end
80
81 end)