Tokenization
Tokenization is the process of segmenting the input into "tokens" (processing units).
Contents |
General principles
In the UNL framework, tokenization follows the principles below:
- The tokenization algorithm goes from left to right.
- Except for digits, the tokenization algorithm is strictly dictionary-based (spaces and punctuation signs have to be inserted in the dictionary in order to be treated as non-temporary entries)
- The tokenization algorithm tries to match first the longest entries in the dictionary.
- The tokenization algorithm observes the frequency of the entries informed the dictionary (the highest frequent entries come first).
- The tokenization algorithm observes the order of the entries in the dictionary (the system selects the first to appear in case of same frequency)
- The tokenization algorithm assigns the feature TEMP (temporary) to the strings that were not found in the dictionary.
- The tokenization algorithm blocks tokens or sequences of tokens prohibited by disambiguation rules.
- In case of several possible candidates, the tokenization algorithm picks the ones induced by disambiguation rules, if any.
Tokenization of natural language input
Tokenization of spaces, punctuation signs and symbols
The tokenization does not have any system-defined tokens except for digits. Any token has be to inserted in the dictionary in order to recognized as a non-temporary entry.
input | dictionary | tokenization output |
---|---|---|
a b | empty | ["a b"], where "a b" = TEMP |
a b | [a] {} "a" (A) <,,>; | [a][" b"], where [" b"] = TEMP |
a b | [b] {} "b" (B) <,,>; | ["a "][b], where ["a "] = TEMP |
a b | [a] {} "a" (A) <,,>; [b] {} "b" (B) <,,>; |
[a][" "][b], where [" "] = TEMP |
a b | [a] {} "a" (A) <,,>; [b] {} "b" (B) <,,>; [ ] {} "" (BLK) <,,>; |
[a][ ][b] (no temporary entries) |
Tokenization of digits
Any sequence of isolated digits (i.e., sided by non-temporary tokens) is considered one single token and receives the features TEMP and DIGIT. The UW is considered to be the digit.
input | dictionary | tokenization output |
---|---|---|
1234 | empty | [1234] {} "1234" (TEMP,DIGIT) <,,>; |
1,234 | empty | [1,234]{}""(TEMP)<,,>; |
1,234 | [,]{}""(COMMA)<,,> | [1]{}""(TEMP,DIGIT)<,,>; [,]{}""(COMMA)<,,>; [234]{}"234"(TEMP,DIGIT)<,,>; |
abc123 | empty | ["abc123"]{}""(TEMP)<,,>; |
abc123 | [abc]{}"abc"{}(A)<,,>; | [abc]{}"abc"(A)<,,>; [123]{}"123"(DIGIT)<,,>; |
Tokenization of temporary entries
Any sequence of characters except digits (and including special symbols, such as space and punctuation signs) not found in the dictionary is considered a temporary entry and receives the feature TEMP. The UW is considered to be empty.
input | dictionary | tokenization output |
---|---|---|
asdfg | empty | ["asdfg"]{}""(TEMP) <,,>; |
asdfg hijkl | empty | ["asdfg hijkl"]{}""(TEMP)<,,>; |
asdfg hijlk | [ ]{}""(BLK)<,,>; | ["asdfg"]{}""(TEMP)<,,>; [ ]{}""(BLK)<,,>; ["hijlk"]{}""(TEMP)<,,>; |
Tokenization of natural language entries
- Dictionary
- [abcde]{}""(...)<...>;
- [abcd]{}""(...)<...>;
- [bcde]{}""(...)<...>;
- [abc]{}""(...)<...>;
- [bcd]{}""(...)<...>;
- [cde]{}""(...)<...>;
- [ab]{}""(...)<...>;
- [bc]{}""(...)<...>;
- [cd]{}""(...)<...>;
- [de]{}""(...)<...>;
- [a]{}""(A1,...)<...>;
- [a]{}""(A2,...)<...>;
- [b]{}""(B1,...)<...>;
- [b]{}""(B2,...)<...>;
- [c]{}""(C1,...)<...>;
- [c]{}""(C2,...)<...>;
- [d]{}""(D1,...)<...>;
- [d]{}""(D2,...)<...>;
- [e]{}""(E1,...)<...>;
- [e]{}""(E2,...)<...>;
- Case 1
- input: "abcde"
- disambiguation rule: NONE
- tokenization output: [abcde] (the longest entry in the dictionary)
- Case 2
- input: "abcde"
- disambiguation rule: ("abcde")=0; (prohibits the sequence "abcde")
- tokenization output: [abcd][e] (the second longest possibility from left to right according to the dictionary)
- Case 3
- input: "abcde"
- disambiguation rules: ("abcde")=0;("abcd")("e")=0;
- tokenization output: [a][bcde] (the third longest possibility from left to right according to the dictionary)
- Case 4
- input: "abcde"
- disambiguation rules: ("abcde")=0;("abcd")("e")=0;("a")("bcde")=0;
- tokenization output: [abc][de] (the fourth longest possibility from left to right according to the dictionary)
- Case 5
- input: "abcde"
- disambiguation rules: ("/.{3,5}/")=0; (prohibits any token made of 3, 4 or 5 characters)
- tokenization output: [ab][cd][e]
- Case 6
- input: "abXcYde"
- disambiguation rule: NONE
- tokenization output: [ab][X][c][Y][de] (where [X] and [Y] are temporary entries)
Properties of tokenization
Positive disambiguation rules only apply over candidates to the same position with the same length
- Case 7
- input: "abcde"
- disambiguation rule: ("abcd")("e")=100; (induces the sequence [abcd][e])
- tokenization output: [abcde] (positive disambiguation rules do not prevail over the principle of longest first)
- Case 8
- input: "abcde"
- disambiguation rule: ("/.{2,5}/")=0; (prohibits any token made of 2, 3, 4 or 5 characters)
- tokenization output: [a,A1][b,B1][c,C1][d,D1][e,E1] (among the candidates, these entries come first in the dictionary)
- Case 9
- input: "abcde"
- disambiguation rule: ("/.{2,5}/")=0; (A2)(B2)=1; (induces the sequence [A2][B2])
- tokenization output: [a,A2][b,B2][c,C1][d,D1][e,E1] (A2 and B2 were chosen instead of A1 and B1)
Positive disambiguation rules apply according to their probability and, inside the same probability, according to their order in the grammar
- Case 10
- input: "abcde"
- disambiguation rule: ("/.{2,5}/")=0; (A2)(B2)=1; (A2)(B1)=1; (induces the sequence [A2][B1])
- tokenization output: [a,A2][b,B2][c,C1][d,D1][e,E1] (the rule [A2][B2] has the some probability as [A2][B1] and comes first in the grammar)
- Case 11
- input: "abcde"
- disambiguation rule: ("/.{2,5}/")=0; (A2)(B2)=1; (A2)(B1)=2; (induces the sequence [A2][B1])
- tokenization output: [a,A2][b,B1][c,C1][d,D1][e,E1] (the priority of the rule [A2][B1] is higher than [A2][B2])
- Case 12
- input: "abcde"
- disambiguation rule: ("/.{2,5}/")=0; (A2)(B2)=1; (B1)(C2)=2; (induces the sequence [A2][B1])
- tokenization output: [a,A1][b,B1][c,C2][d,D1][e,E1] (the priority of the rule [B1][C2] is higher than [A2][B2])
The attribute #FINAL is used to indicate context
- Case 13
- input: "abcde"
- disambiguation rule: ("/.{2,5}/")=0; (A2)(B2)=1; (B1,#FINAL)(C2)=5; (if B1 is fixed, C2 must be chosen)
- tokenization output: [a,A2][b,B2][c,C1][d,D1][e,E1] (the same as case 11; compare with case 12 - note that the rule (B1,#FINAL)(C2) does not induce the choice of B1; it applies only to C2, i.e., it indicates that, if B1, then C2)
The tokenization algorithm backtracks in case of dead-ends.
In case of backtracking, the tokenization starts merging nodes from left to right, but considering their size, i.e., the shortest combinations appear first.
- Case 14
- input: "abcde"
- dictionary:
[a]{}""(...)<...>;
[b]{}""(...)<...>;
[c]{}""(...)<...>;
[d]{}""(...)<...>;
[e]{}""(...)<...>;
- disambiguation rule: ("a")("b")("c")("d")("e")=0;
- tokenization output: [ab][b][c][d][e] (the first combination of shortest temporary nodes from left to right after [a][b][c][d][e], which was blocked by the disambiguation rule. Note that [ab] is not in the dictionary and, therefore, will be treated as TEMP)
Retokenization
Temporary nodes (i.e., nodes having the feature TEMP) may be retokenized. In order to retokenize (split) a non-temporary node, it is necessary to assign the feature TEMP to it (but this does not affect any existing feature, including the headword and the UW).
Examples:
- Infixation: add a the character "c" between the characters "b" and "d" in case of verbs (V) in the past tense (PAS): abde > abcde, bbdd > bbcde, xbdy > xbcdy, etc.
Grammar
- ("/.+bd.+/",V,PAS,%x):=(%x,+TEMP,+SPLIT); (assigns TEMP to the node %x if it is V and PAS and contains the string "bd" inside)
- ("/.+b/",SPLIT,%x1)("/d.+/",SPLIT,%x2):=(%x1,-SPLIT,+MERGE)("c",MERGE)(%x2,[],[[]],-SPLIT,+MERGE); (split the string and add "c" after "b" and before "d")
- (%x,MERGE)(%y,MERGE):=(%x&%y); (merge two adjacent nodes having the feature MERGE)
- (%x,^MERGE)(%y,MERGE)(%z,^MERGE); (delete the feature MERGE if there is nothing else to be merged)
- (%x,^MERGE,^SPLIT,^[[]],TEMP):=(%x,-TEMP); (delete the feature TEMP if there is nothing to be merged or split and the node has a UW)
Trace
0 ("abde",[abde],abde,V,PAS) original node
1 ("abde",[abde],abde,V,PAS,TEMP,SPLIT) after applying rule 1
2 ("ab",[abde],abde,V,PAS,TEMP,MERGE)("c",MERGE)("de",[],[[]],V,PAS,TEMP,MERGE) after applying rule 2 (split "abde" and create "c" in the middle)
3 ("abc",[abde],abde,V,PAS,TEMP,MERGE) after applying rule 3 (merge "ab" with "c")
4 ("abcde",[abde],abde,V,PAS,TEMP,MERGE) after applying rule 3 (merge "abc" with "de")
5 ("abcde",[abde],abde,V,PAS,TEMP) after applying rule 4 (remove MERGE)
6 ("abcde",[abde],abde,V,PAS) after applying rule 5 (remove TEMP)
Observations:
- Splitting operations affect only strings. All the features of the source node, including headword and UW, are preserved in the target nodes
Given the node ("abcd",[abcd],[[abcd]],A),
the retokenization rule ("ab",%x)("cd",%y):=(%x)("1",%z)(%y);
does not affect the original headword, UW and feature, i.e.:
the nodes ("ab") and ("cd") still have, each one, the features [abcd],[[abcd]],A.
Because of that, it is important (as indicated in the rule 2 above), to delete the UW and the headword of all split nodes except one; otherwise, when we merge them back, the UW and the headword will be multiplied, because the merge rule concatenates strings, UWs and headwords. For instance, if the rule 2 in the grammar above was:
2 ("/.+b/",SPLIT,%x1)("/d.+/",SPLIT,%x2):=(%x1,-SPLIT,+MERGE)("c",MERGE)(%x2,-SPLIT,+MERGE); (i.e., if it did not contain [],[[]])
The final result would have been:
6 ("abcde",[abdeabde],abdeabde,V,PAS)
Tokenization of UNL input
- Case 15
- Dictionary:
[x]{}"x"(...)<...>;
[xa]{}"x.@a"(...)<...>;
[xab]{}"x.@a.@b"(...)<...>;
[y]{}"y"(...)<...>;
- Input:
agt(x.@a.@b,y)
- Tokenization output
agt([xab],[y])