@echogarden/text-segmentation 0.2.1 → 0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +31 -25
- package/dist/EastAsianCharacterPatterns.d.ts +0 -6
- package/dist/EastAsianCharacterPatterns.js +6 -6
- package/dist/EastAsianCharacterPatterns.js.map +1 -1
- package/dist/Patterns.d.ts +4 -29
- package/dist/Patterns.js +137 -61
- package/dist/Patterns.js.map +1 -1
- package/dist/Suppressions.d.ts +1 -0
- package/dist/Suppressions.js +19 -9
- package/dist/Suppressions.js.map +1 -1
- package/dist/Test.js +48 -5
- package/dist/Test.js.map +1 -1
- package/dist/TextSegmentation.d.ts +3 -4
- package/dist/TextSegmentation.js +68 -39
- package/dist/TextSegmentation.js.map +1 -1
- package/dist/WordSequence.d.ts +5 -3
- package/dist/WordSequence.js +13 -6
- package/dist/WordSequence.js.map +1 -1
- package/dist/utilities/Utilities.d.ts +1 -0
- package/dist/utilities/Utilities.js +7 -0
- package/dist/utilities/Utilities.js.map +1 -1
- package/package.json +4 -4
- package/src/EastAsianCharacterPatterns.ts +6 -6
- package/src/Patterns.ts +180 -87
- package/src/Suppressions.ts +20 -9
- package/src/Test.ts +55 -5
- package/src/TextSegmentation.ts +91 -51
- package/src/WordSequence.ts +17 -7
- package/src/utilities/Utilities.ts +10 -0
package/dist/WordSequence.js
CHANGED
|
@@ -1,16 +1,17 @@
|
|
|
1
1
|
export class WordSequence {
|
|
2
2
|
entries = [];
|
|
3
|
-
|
|
4
|
-
const
|
|
3
|
+
addWord(text, textStartOffset, isPunctuation) {
|
|
4
|
+
const startOffset = textStartOffset;
|
|
5
|
+
const endOffset = startOffset + text.length;
|
|
5
6
|
this.entries.push({
|
|
6
|
-
text
|
|
7
|
+
text,
|
|
7
8
|
startOffset,
|
|
8
9
|
endOffset,
|
|
9
10
|
isPunctuation
|
|
10
11
|
});
|
|
11
12
|
}
|
|
12
13
|
getWordRange(startIndex, endIndex) {
|
|
13
|
-
return
|
|
14
|
+
return this.getEntryRange(startIndex, endIndex).map(entry => entry.text);
|
|
14
15
|
}
|
|
15
16
|
*iterateWordRange(startIndex, endIndex) {
|
|
16
17
|
for (let i = startIndex; i < endIndex; i++) {
|
|
@@ -39,7 +40,7 @@ export class WordSequence {
|
|
|
39
40
|
slicedSequence.entries = this.entries.slice(startIndex, endIndex);
|
|
40
41
|
return slicedSequence;
|
|
41
42
|
}
|
|
42
|
-
get
|
|
43
|
+
get wordArray() {
|
|
43
44
|
return this.entries.map(entry => entry.text);
|
|
44
45
|
}
|
|
45
46
|
get firstWord() {
|
|
@@ -54,6 +55,12 @@ export class WordSequence {
|
|
|
54
55
|
get lastEntry() {
|
|
55
56
|
return this.entries[this.length - 1];
|
|
56
57
|
}
|
|
58
|
+
get punctuationEntries() {
|
|
59
|
+
return this.entries.filter(entry => entry.isPunctuation === true);
|
|
60
|
+
}
|
|
61
|
+
get punctuationWords() {
|
|
62
|
+
return this.entries.filter(entry => entry.isPunctuation === true).map(entry => entry.text);
|
|
63
|
+
}
|
|
57
64
|
get nonPunctuationEntries() {
|
|
58
65
|
return this.entries.filter(entry => entry.isPunctuation === false);
|
|
59
66
|
}
|
|
@@ -61,7 +68,7 @@ export class WordSequence {
|
|
|
61
68
|
return this.entries.filter(entry => entry.isPunctuation === false).map(entry => entry.text);
|
|
62
69
|
}
|
|
63
70
|
get text() {
|
|
64
|
-
return this.
|
|
71
|
+
return this.wordArray.join('');
|
|
65
72
|
}
|
|
66
73
|
get length() {
|
|
67
74
|
return this.entries.length;
|
package/dist/WordSequence.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"WordSequence.js","sourceRoot":"","sources":["../src/WordSequence.ts"],"names":[],"mappings":"AAAA,MAAM,OAAO,YAAY;IACxB,OAAO,GAAgB,EAAE,CAAA;IAEzB,
|
|
1
|
+
{"version":3,"file":"WordSequence.js","sourceRoot":"","sources":["../src/WordSequence.ts"],"names":[],"mapp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,CAAC,MAAM,CAAC,KAAK,CAAC,EAAE,CAAC,KAAK,CAAC,aAAa,KAAK,KAAK,CAAC,CAAA;IACnE,CAAC;IAED,IAAI,mBAAmB;QACtB,OAAO,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,EAAE,CAAC,KAAK,CAAC,aAAa,KAAK,KAAK,CAAC,CAAC,GAAG,CAAC,KAAK,CAAC,EAAE,CAAC,KAAK,CAAC,IAAI,CAAC,CAAA;IAC5F,CAAC;IAED,IAAI,IAAI;QACP,OAAO,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,EAAE,CAAC,CAAA;IAC/B,CAAC;IAED,IAAI,MAAM;QACT,OAAO,IAAI,CAAC,OAAO,CAAC,MAAM,CAAA;IAC3B,CAAC;CACD"}
|
|
@@ -21,4 +21,11 @@ export function extractSuppressions(entries) {
|
|
|
21
21
|
}
|
|
22
22
|
return suppressions;
|
|
23
23
|
}
|
|
24
|
+
export function getShortLanguageCode(langCode) {
|
|
25
|
+
const dashIndex = langCode.indexOf('-');
|
|
26
|
+
if (dashIndex == -1) {
|
|
27
|
+
return langCode;
|
|
28
|
+
}
|
|
29
|
+
return langCode.substring(0, dashIndex).toLowerCase();
|
|
30
|
+
}
|
|
24
31
|
//# sourceMappingURL=Utilities.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Utilities.js","sourceRoot":"","sources":["../../src/utilities/Utilities.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAW,MAAM,iBAAiB,CAAA;AAEtD,MAAM,UAAU,aAAa,CAAC,GAAW,EAAE,MAAM,GAAG,CAAC;IACpD,MAAM,UAAU,GAAG,EAAE,IAAI,MAAM,CAAA;IAE/B,OAAO,IAAI,CAAC,KAAK,CAAC,GAAG,GAAG,UAAU,CAAC,GAAG,UAAU,CAAA;AACjD,CAAC;AAED,MAAM,UAAU,oBAAoB,CAAC,WAAoB;IACxD,MAAM,MAAM,GAAG,WAAW,CAA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CAAA;QAE5C,IAAI,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC;YACvB,aAAa,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;QACzB,CAAC;IACF,CAAC;IAED,OAAO,aAAa,CAAA;AACrB,CAAC;AAED,MAAM,UAAU,mBAAmB,CAAC,OAAiC;IACpE,MAAM,YAAY,GAAa,EAAE,CAAA;IAEjC,KAAK,MAAM,KAAK,IAAI,OAAO,EAAE,CAAC;QAC7B,YAAY,CAAC,IAAI,CAAC,KAAK,CAAC,WAAW,CAAC,CAAA;IACrC,CAAC;IAED,OAAO,YAAY,CAAA;AACpB,CAAC;AAED,MAAM,UAAU,oBAAoB,CAAC,QAAgB;IACpD,MAAM,SAAS,GAAG,QAAQ,CAAC,OAAO,CAAC,GAAG,CAAC,CAAA;IAEvC,IAAI,SAAS,IAAI,CAAC,CAAC,EAAE,CAAC;QACrB,OAAO,QAAQ,CAAA;IAChB,CAAC;IAED,OAAO,QAAQ,CAAC,SAAS,CAAC,CAAC,EAAE,SAAS,CAAC,CAAC,WAAW,EAAE,CAAA;AACtD,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@echogarden/text-segmentation",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.1",
|
|
4
4
|
"description": "A library for multilingual word, phrase and sentence segmentation.",
|
|
5
5
|
"author": "Rotem Dan",
|
|
6
6
|
"license": "MIT",
|
|
@@ -39,10 +39,10 @@
|
|
|
39
39
|
"regexp-composer": "../../regexp-composer"
|
|
40
40
|
},
|
|
41
41
|
"dependencies": {
|
|
42
|
-
"regexp-composer": "^0.
|
|
42
|
+
"regexp-composer": "^0.3.0"
|
|
43
43
|
},
|
|
44
44
|
"peerDependencies": {
|
|
45
|
-
"@echogarden/icu-segmentation-wasm": "^0.1
|
|
45
|
+
"@echogarden/icu-segmentation-wasm": "^0.2.1"
|
|
46
46
|
},
|
|
47
47
|
"peerDependenciesMeta": {
|
|
48
48
|
"@echogarden/icu-segmentation-wasm": {
|
|
@@ -50,6 +50,6 @@
|
|
|
50
50
|
}
|
|
51
51
|
},
|
|
52
52
|
"devDependencies": {
|
|
53
|
-
"@types/node": "^22.15.
|
|
53
|
+
"@types/node": "^22.15.18"
|
|
54
54
|
}
|
|
55
55
|
}
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import { anyOf, buildRegExp, codepointRange } from 'regexp-composer'
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
const chineseCharacterRanges = anyOf(
|
|
4
4
|
codepointRange('4E00', '9FFF'), // Main
|
|
5
5
|
codepointRange('3400', '4DBF'), // CJK Unified Ideographs Extension A
|
|
6
6
|
codepointRange('20000', '2A6DF') // CJK Unified Ideographs Extension B
|
|
7
7
|
)
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
const japaneseHiraganaCharacterRanges = anyOf(
|
|
10
10
|
codepointRange('3040', '309F'), // Hiragana
|
|
11
11
|
codepointRange('1AFF0', '1AFFF'), // Kana Extended-B
|
|
12
12
|
codepointRange('1B000', '1B0FF'), // Kana Supplement
|
|
@@ -14,7 +14,7 @@ export const japaneseHiraganaCharacterRanges = anyOf(
|
|
|
14
14
|
codepointRange('1B130', '1B16F'), // Small Kana Extension
|
|
15
15
|
)
|
|
16
16
|
|
|
17
|
-
|
|
17
|
+
const japaneseKatakanaCharacterRanges = anyOf(
|
|
18
18
|
codepointRange('30A0', '30FF'), // Katakana
|
|
19
19
|
codepointRange('31F0', '31FF'), // Katakana Phonetic Extensions
|
|
20
20
|
codepointRange('3200', '32FF'), // Enclosed CJK Letters and Months
|
|
@@ -25,16 +25,16 @@ export const japaneseKatakanaCharacterRanges = anyOf(
|
|
|
25
25
|
codepointRange('1B130', '1B16F'), // Small Kana Extension
|
|
26
26
|
)
|
|
27
27
|
|
|
28
|
-
|
|
28
|
+
const thaiLetterRanges = anyOf(
|
|
29
29
|
codepointRange('0E00', '0E7F')
|
|
30
30
|
)
|
|
31
31
|
|
|
32
|
-
|
|
32
|
+
const khmerLetterRanges = anyOf(
|
|
33
33
|
codepointRange('1780', '17FF'), // Letters
|
|
34
34
|
codepointRange('19E0', '19FF'), // Symbols
|
|
35
35
|
)
|
|
36
36
|
|
|
37
|
-
|
|
37
|
+
const eastAsianCharRanges = anyOf(
|
|
38
38
|
chineseCharacterRanges,
|
|
39
39
|
japaneseHiraganaCharacterRanges,
|
|
40
40
|
japaneseKatakanaCharacterRanges,
|
package/src/Patterns.ts
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
|
-
import { anyOf, buildRegExp, charRange, inputEnd, inputStart, matches, oneOrMore, possibly, repeated, tab, unicodeProperty, whitespace } from 'regexp-composer'
|
|
1
|
+
import { anyOf, buildRegExp, charRange, inputEnd, inputStart, matches, oneOrMore, possibly, repeated, tab, unicodeProperty, whitespace, zeroOrMore } from 'regexp-composer'
|
|
2
2
|
|
|
3
|
+
////////////////////////////////////////////////////////////////////////////////////////////////
|
|
4
|
+
// Pattern builder methods
|
|
5
|
+
////////////////////////////////////////////////////////////////////////////////////////////////
|
|
3
6
|
export function buildWordOrNumberPattern(suppressions: string[]) {
|
|
4
7
|
return anyOf(
|
|
5
8
|
buildSuppressionPattern(suppressions),
|
|
@@ -18,95 +21,142 @@ export function buildSuppressionPattern(suppressions: string[]) {
|
|
|
18
21
|
}
|
|
19
22
|
|
|
20
23
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
|
21
|
-
//
|
|
24
|
+
// Single character patterns
|
|
22
25
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
+
const letterPattern = unicodeProperty('Letter')
|
|
27
|
+
const markPattern = unicodeProperty('Mark')
|
|
28
|
+
|
|
29
|
+
const letterOrMarkPattern = anyOf(
|
|
30
|
+
letterPattern,
|
|
31
|
+
markPattern,
|
|
32
|
+
)
|
|
33
|
+
|
|
34
|
+
const apostrophPattern = anyOf(`'`, `’`, `‘`)
|
|
35
|
+
|
|
36
|
+
const punctuationPattern = unicodeProperty('Punctuation')
|
|
37
|
+
const digitPattern = unicodeProperty('Decimal_Number')
|
|
38
|
+
const arabicNumeralPattern = charRange('0', '9')
|
|
39
|
+
|
|
40
|
+
const emojiPattern = unicodeProperty('Emoji')
|
|
26
41
|
|
|
27
|
-
|
|
42
|
+
const percentageCharacters = ['%']
|
|
43
|
+
const currencyCharacters = ['$', '¥', '€', '£', '₩', '₭', '₽', '₫', '฿', '¢', '₮', '؋', '₦', '₱', '₴', '₪']
|
|
44
|
+
|
|
45
|
+
const percentageOrCurrencyCharacterPattern = anyOf(...percentageCharacters, ...currencyCharacters)
|
|
46
|
+
|
|
47
|
+
////////////////////////////////////////////////////////////////////////////////////////////////
|
|
48
|
+
// Numeric patterns
|
|
49
|
+
////////////////////////////////////////////////////////////////////////////////////////////////
|
|
50
|
+
const numericSeparatorPattern =
|
|
28
51
|
matches(
|
|
29
52
|
anyOf('.', ',', '٬', '_'), {
|
|
30
53
|
ifPrecededBy: arabicNumeralPattern,
|
|
31
54
|
ifFollowedBy: arabicNumeralPattern
|
|
32
55
|
})
|
|
33
56
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
ifPrecededBy: arabicNumeralPattern,
|
|
37
|
-
ifFollowedBy: arabicNumeralPattern
|
|
38
|
-
})
|
|
57
|
+
const dimensionsPattern = matches([
|
|
58
|
+
oneOrMore(arabicNumeralPattern),
|
|
39
59
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
arabicNumeralPattern,
|
|
43
|
-
|
|
44
|
-
|
|
60
|
+
oneOrMore([
|
|
61
|
+
'x',
|
|
62
|
+
oneOrMore(arabicNumeralPattern),
|
|
63
|
+
]),
|
|
64
|
+
], {
|
|
65
|
+
ifPrecededBy: anyOf(whitespace, punctuationPattern, inputStart),
|
|
66
|
+
ifFollowedBy: anyOf(whitespace, punctuationPattern, inputEnd)
|
|
67
|
+
})
|
|
45
68
|
|
|
46
|
-
|
|
69
|
+
const spacedThousandsSeparatorPattern =
|
|
47
70
|
matches(
|
|
48
71
|
' ', {
|
|
49
72
|
ifPrecededBy: arabicNumeralPattern,
|
|
50
|
-
ifFollowedBy:
|
|
73
|
+
ifFollowedBy: [
|
|
74
|
+
repeated(3, arabicNumeralPattern),
|
|
75
|
+
anyOf(whitespace, punctuationPattern, inputEnd)
|
|
76
|
+
]
|
|
51
77
|
})
|
|
52
78
|
|
|
53
|
-
|
|
79
|
+
const numericSignPattern =
|
|
54
80
|
matches(
|
|
55
81
|
anyOf('-', '+'), {
|
|
56
|
-
ifPrecededBy: anyOf(whitespace, punctuationPattern),
|
|
82
|
+
ifPrecededBy: anyOf(whitespace, punctuationPattern, inputStart),
|
|
57
83
|
ifFollowedBy: arabicNumeralPattern,
|
|
58
84
|
})
|
|
59
85
|
|
|
60
|
-
|
|
61
|
-
|
|
86
|
+
const numberPattern = [
|
|
87
|
+
possibly(numericSignPattern),
|
|
88
|
+
digitPattern,
|
|
89
|
+
|
|
90
|
+
zeroOrMore(anyOf(
|
|
62
91
|
digitPattern,
|
|
63
92
|
numericSeparatorPattern,
|
|
64
93
|
spacedThousandsSeparatorPattern,
|
|
65
|
-
|
|
66
|
-
|
|
94
|
+
))
|
|
95
|
+
]
|
|
96
|
+
|
|
97
|
+
const exponentPattern = [
|
|
98
|
+
anyOf('e', 'E'),
|
|
99
|
+
possibly(anyOf('+', '-')),
|
|
100
|
+
oneOrMore(arabicNumeralPattern),
|
|
67
101
|
]
|
|
68
102
|
|
|
69
|
-
const
|
|
70
|
-
|
|
103
|
+
const numberPossiblyFollowedByExponentOrLettersPattern = [
|
|
104
|
+
numberPattern,
|
|
105
|
+
|
|
106
|
+
possibly(anyOf(
|
|
107
|
+
exponentPattern,
|
|
71
108
|
|
|
72
|
-
|
|
109
|
+
matches(
|
|
110
|
+
zeroOrMore(unicodeProperty('Letter')), {
|
|
111
|
+
ifNotFollowedBy: digitPattern,
|
|
112
|
+
}),
|
|
113
|
+
))
|
|
114
|
+
]
|
|
73
115
|
|
|
74
|
-
|
|
116
|
+
const precedingPercentageOrCurrencyPattern =
|
|
75
117
|
matches([
|
|
76
|
-
|
|
118
|
+
percentageOrCurrencyCharacterPattern,
|
|
77
119
|
numberPattern,
|
|
78
120
|
], {
|
|
79
121
|
ifNotPrecededBy: digitPattern,
|
|
80
|
-
ifFollowedBy: anyOf(whitespace, punctuationPattern),
|
|
122
|
+
ifFollowedBy: anyOf(whitespace, punctuationPattern, inputEnd),
|
|
81
123
|
})
|
|
82
124
|
|
|
83
|
-
|
|
125
|
+
const followingPercentageOrCurrencyPattern =
|
|
84
126
|
matches([
|
|
85
127
|
numberPattern,
|
|
86
|
-
|
|
128
|
+
percentageOrCurrencyCharacterPattern,
|
|
87
129
|
], {
|
|
88
|
-
ifPrecededBy: anyOf(whitespace, punctuationPattern),
|
|
89
|
-
ifNotFollowedBy: digitPattern,
|
|
130
|
+
ifPrecededBy: anyOf(whitespace, punctuationPattern, inputStart),
|
|
131
|
+
ifNotFollowedBy: anyOf(digitPattern),
|
|
90
132
|
})
|
|
91
133
|
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
134
|
+
const percentageOrCurrencyPattern = anyOf(
|
|
135
|
+
precedingPercentageOrCurrencyPattern,
|
|
136
|
+
followingPercentageOrCurrencyPattern
|
|
95
137
|
)
|
|
96
138
|
|
|
97
139
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
|
98
|
-
//
|
|
140
|
+
// Time patterns
|
|
99
141
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
142
|
+
const timePattern = matches([
|
|
143
|
+
anyOf(arabicNumeralPattern, [charRange('0', '1'), arabicNumeralPattern], ['2', charRange('0', '3')]),
|
|
144
|
+
|
|
145
|
+
repeated([1, 2], [
|
|
146
|
+
':',
|
|
147
|
+
anyOf(arabicNumeralPattern, [charRange('0', '5'), arabicNumeralPattern]),
|
|
148
|
+
])
|
|
149
|
+
], {
|
|
150
|
+
ifPrecededBy: anyOf(whitespace, punctuationPattern, inputStart),
|
|
151
|
+
ifNotPrecededBy: ':',
|
|
152
|
+
ifFollowedBy: anyOf(whitespace, punctuationPattern, inputEnd),
|
|
153
|
+
ifNotFollowedBy: ':',
|
|
154
|
+
})
|
|
108
155
|
|
|
109
|
-
|
|
156
|
+
////////////////////////////////////////////////////////////////////////////////////////////////
|
|
157
|
+
// Letter patterns
|
|
158
|
+
////////////////////////////////////////////////////////////////////////////////////////////////
|
|
159
|
+
const dottedAbbreviationSequencePattern =
|
|
110
160
|
matches(
|
|
111
161
|
anyOf(
|
|
112
162
|
[
|
|
@@ -135,23 +185,14 @@ export const dottedAbbreviationSequencePattern =
|
|
|
135
185
|
ifNotFollowedBy: anyOf(letterOrMarkPattern, digitPattern)
|
|
136
186
|
})
|
|
137
187
|
|
|
138
|
-
|
|
139
|
-
export const wordCharacterPattern =
|
|
188
|
+
const wordCharacterPattern =
|
|
140
189
|
anyOf(
|
|
141
190
|
letterPattern,
|
|
142
191
|
markPattern,
|
|
143
192
|
digitPattern,
|
|
144
193
|
)
|
|
145
194
|
|
|
146
|
-
|
|
147
|
-
matches(
|
|
148
|
-
anyOf('-', '_', '·', '‧', '&'), {
|
|
149
|
-
|
|
150
|
-
ifPrecededBy: letterOrMarkPattern,
|
|
151
|
-
ifFollowedBy: letterOrMarkPattern
|
|
152
|
-
})
|
|
153
|
-
|
|
154
|
-
export const wordInnerApostrophPattern =
|
|
195
|
+
const wordInnerApostrophPattern =
|
|
155
196
|
matches(
|
|
156
197
|
apostrophPattern, {
|
|
157
198
|
|
|
@@ -159,67 +200,119 @@ export const wordInnerApostrophPattern =
|
|
|
159
200
|
ifFollowedBy: letterOrMarkPattern,
|
|
160
201
|
})
|
|
161
202
|
|
|
162
|
-
|
|
203
|
+
const wordStartApostrophPattern =
|
|
163
204
|
matches(
|
|
164
205
|
apostrophPattern, {
|
|
165
206
|
|
|
166
207
|
ifPrecededBy: whitespace,
|
|
167
|
-
ifFollowedBy: [letterOrMarkPattern, letterOrMarkPattern, whitespace]
|
|
208
|
+
ifFollowedBy: [letterOrMarkPattern, letterOrMarkPattern, anyOf(whitespace, inputEnd)]
|
|
168
209
|
})
|
|
169
210
|
|
|
170
|
-
|
|
211
|
+
const basicWordPattern =
|
|
171
212
|
oneOrMore(
|
|
172
213
|
anyOf(
|
|
173
214
|
wordCharacterPattern,
|
|
174
|
-
wordSeparatorPattern,
|
|
175
215
|
wordInnerApostrophPattern,
|
|
176
216
|
),
|
|
177
217
|
)
|
|
178
218
|
|
|
179
|
-
|
|
219
|
+
const hyphenatedWordPattern = [
|
|
220
|
+
basicWordPattern,
|
|
221
|
+
|
|
222
|
+
oneOrMore([
|
|
223
|
+
'-',
|
|
224
|
+
basicWordPattern,
|
|
225
|
+
]),
|
|
226
|
+
]
|
|
227
|
+
|
|
228
|
+
const dotSeparatedWordPattern = matches([
|
|
229
|
+
basicWordPattern,
|
|
230
|
+
|
|
231
|
+
oneOrMore([
|
|
232
|
+
'.',
|
|
233
|
+
basicWordPattern,
|
|
234
|
+
]),
|
|
235
|
+
], {
|
|
236
|
+
//ifPrecededBy: anyOf(whitespace, inputStart),
|
|
237
|
+
//ifFollowedBy: anyOf(whitespace, inputEnd)
|
|
238
|
+
})
|
|
239
|
+
|
|
240
|
+
const interpunctSeparatedWordPattern = [
|
|
241
|
+
basicWordPattern,
|
|
242
|
+
|
|
243
|
+
oneOrMore([
|
|
244
|
+
anyOf('·', '‧'),
|
|
245
|
+
basicWordPattern,
|
|
246
|
+
]),
|
|
247
|
+
]
|
|
248
|
+
|
|
249
|
+
const underscoreSeparatedWordPattern = [
|
|
250
|
+
basicWordPattern,
|
|
251
|
+
|
|
252
|
+
oneOrMore([
|
|
253
|
+
'_',
|
|
254
|
+
basicWordPattern,
|
|
255
|
+
]),
|
|
256
|
+
]
|
|
257
|
+
|
|
258
|
+
const wordSegmentPattern = anyOf(
|
|
259
|
+
timePattern,
|
|
260
|
+
dimensionsPattern,
|
|
261
|
+
percentageOrCurrencyPattern,
|
|
262
|
+
numberPossiblyFollowedByExponentOrLettersPattern,
|
|
263
|
+
|
|
264
|
+
emojiPattern,
|
|
265
|
+
interpunctSeparatedWordPattern,
|
|
266
|
+
dotSeparatedWordPattern,
|
|
180
267
|
dottedAbbreviationSequencePattern,
|
|
181
|
-
|
|
182
|
-
percentagePattern,
|
|
183
|
-
numberPattern,
|
|
268
|
+
underscoreSeparatedWordPattern,
|
|
184
269
|
basicWordPattern,
|
|
185
270
|
)
|
|
186
271
|
|
|
187
272
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
|
188
|
-
//
|
|
273
|
+
// Phrase separation patterns
|
|
189
274
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
|
190
|
-
|
|
275
|
+
const phraseSeparatorCharacters = [',', '、', ',', '،', ';', ';', ':', ':', '—']
|
|
191
276
|
|
|
192
|
-
|
|
277
|
+
const phraseSeparatorPattern = [
|
|
193
278
|
inputStart,
|
|
194
|
-
anyOf(...
|
|
279
|
+
anyOf(...phraseSeparatorCharacters),
|
|
195
280
|
inputEnd
|
|
196
|
-
]
|
|
197
|
-
|
|
198
|
-
export const sentenceSeparators = ['.', '。', '?', '?', '!', '!', '\n']
|
|
281
|
+
]
|
|
199
282
|
|
|
200
|
-
|
|
283
|
+
const phraseSeparatorTrailingPunctuationPattern = [
|
|
201
284
|
inputStart,
|
|
202
|
-
anyOf(...
|
|
285
|
+
anyOf(...phraseSeparatorCharacters, ' ', tab),
|
|
203
286
|
inputEnd
|
|
204
|
-
]
|
|
287
|
+
]
|
|
205
288
|
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
])
|
|
289
|
+
////////////////////////////////////////////////////////////////////////////////////////////////
|
|
290
|
+
// Sentence separation patterns
|
|
291
|
+
////////////////////////////////////////////////////////////////////////////////////////////////
|
|
292
|
+
const sentenceSeparatorCharacters = ['.', '。', '?', '?', '!', '!', '\n']
|
|
211
293
|
|
|
212
|
-
|
|
294
|
+
const sentenceSeparatorPattern = [
|
|
213
295
|
inputStart,
|
|
214
|
-
anyOf(...
|
|
296
|
+
anyOf(...sentenceSeparatorCharacters),
|
|
215
297
|
inputEnd
|
|
216
|
-
]
|
|
298
|
+
]
|
|
217
299
|
|
|
218
|
-
|
|
300
|
+
const sentenceSeparatorTrailingPunctuationPattern = [
|
|
219
301
|
inputStart,
|
|
220
|
-
|
|
302
|
+
anyOf('"', '”', '’', ')', ']', '}', '»', ...sentenceSeparatorCharacters, ...phraseSeparatorCharacters, oneOrMore(whitespace)),
|
|
221
303
|
inputEnd
|
|
222
|
-
]
|
|
304
|
+
]
|
|
223
305
|
|
|
306
|
+
////////////////////////////////////////////////////////////////////////////////////////////////
|
|
307
|
+
// Prebuilt regular expressions
|
|
308
|
+
////////////////////////////////////////////////////////////////////////////////////////////////
|
|
224
309
|
export const wordCharacterRegExp = buildRegExp(wordCharacterPattern)
|
|
225
310
|
export const whitespacePatternRegExp = buildRegExp(whitespace)
|
|
311
|
+
|
|
312
|
+
export const letterPatternGlobalRegExp = buildRegExp(letterPattern, { global: true })
|
|
313
|
+
|
|
314
|
+
export const phraseSeparatorRegExp = buildRegExp(phraseSeparatorPattern)
|
|
315
|
+
export const phraseSeparatorTrailingPunctuationRegExp = buildRegExp(phraseSeparatorTrailingPunctuationPattern)
|
|
316
|
+
|
|
317
|
+
export const sentenceSeparatorRegExp = buildRegExp(sentenceSeparatorPattern)
|
|
318
|
+
export const sentenceSeparatorTrailingPunctuationRegExp = buildRegExp(sentenceSeparatorTrailingPunctuationPattern)
|