@@ -14,29 +14,26 @@ export function smartTerms(
1414 tokens : string [ ] ,
1515 zhDictionary : string [ ]
1616) : SmartTerm [ ] {
17- const terms : SmartTerm [ ] = [ ] ;
17+ const tokenTerms = tokens . map ( ( token ) => {
18+ if ( / \p{ Unified_Ideograph} / u. test ( token ) ) {
19+ return cutZhWords ( token , zhDictionary ) ;
20+ } else {
21+ return [ { value : token } ] ;
22+ }
23+ } ) ;
1824
19- function cutMixedWords ( subTokens : string [ ] , carry : SmartTerm ) : void {
20- if ( subTokens . length === 0 ) {
25+ // Get all possible combinations of terms.
26+ const terms : SmartTerm [ ] = [ ] ;
27+ function combine ( index : number , carry : SmartTerm ) : void {
28+ if ( index === tokenTerms . length ) {
2129 terms . push ( carry ) ;
2230 return ;
2331 }
24- const token = subTokens [ 0 ] ;
25- if ( / \p{ Unified_Ideograph} / u. test ( token ) ) {
26- const terms = cutZhWords ( token , zhDictionary ) ;
27- for ( const term of terms ) {
28- const nextCarry = carry . concat ( ...term ) ;
29- cutMixedWords ( subTokens . slice ( 1 ) , nextCarry ) ;
30- }
31- } else {
32- const nextCarry = carry . concat ( {
33- value : token ,
34- } ) ;
35- cutMixedWords ( subTokens . slice ( 1 ) , nextCarry ) ;
32+ for ( const term of tokenTerms [ index ] ) {
33+ combine ( index + 1 , carry . concat ( term ) ) ;
3634 }
3735 }
38-
39- cutMixedWords ( tokens , [ ] ) ;
36+ combine ( 0 , [ ] ) ;
4037
4138 return terms ;
4239}
0 commit comments