Keep a closing bracket in the word when it closes one inside it
This commit is contained in:
1 parent
ef0f63b489
commit
db2bcfe31f
2 files changed
+34
No files matched your search
@@ -6,6 +6,21 @@ const isNumber = /[\p{Number}\p{Currency_Symbol}]/u
|
|||||||
const hasAcronym = /^[a-z]\.([a-z]\.)+/i
|
const hasAcronym = /^[a-z]\.([a-z]\.)+/i
|
||||||
const chillin = /[sn]['’]$/
|
const chillin = /[sn]['’]$/
|
||||||
const isFullNumber = /^[(+\-]?\d+(th|st|nd|rd)?[)+\-]?$/
|
const isFullNumber = /^[(+\-]?\d+(th|st|nd|rd)?[)+\-]?$/
|
||||||
|
const openers = { ')': '(', ']': '[', '}': '{' }
|
||||||
|
|
||||||
|
// does this closing bracket close one inside the word, like 'foo(bar)'
|
||||||
|
const closesInside = function (chars, start, end) {
|
||||||
|
const close = chars[end - 1]
|
||||||
|
let depth = 0
|
||||||
|
for (let i = start; i < end - 1; i += 1) {
|
||||||
|
if (chars[i] === openers[close]) {
|
||||||
|
depth += 1
|
||||||
|
} else if (chars[i] === close) {
|
||||||
|
depth -= 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return depth > 0
|
||||||
|
}
|
||||||
|
|
||||||
const normalizePunctuation = function (str, model) {
|
const normalizePunctuation = function (str, model) {
|
||||||
// quick lookup for allowed pre/post punctuation
|
// quick lookup for allowed pre/post punctuation
|
||||||
@@ -68,6 +83,9 @@ const normalizePunctuation = function (str, model) {
|
|||||||
if ((c === '+' || c === ')') && isFullNumber.test(str.trim())) {
|
if ((c === '+' || c === ')') && isFullNumber.test(str.trim())) {
|
||||||
break//done
|
break//done
|
||||||
}
|
}
|
||||||
|
if (Object.hasOwn(openers, c) && closesInside(chars, start, end)) {
|
||||||
|
break//keep it
|
||||||
|
}
|
||||||
// punctuation
|
// punctuation
|
||||||
end--
|
end--
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -29,6 +29,22 @@ test('term punctuation', function (t) {
|
|||||||
t.end()
|
t.end()
|
||||||
})
|
})
|
||||||
|
|
||||||
|
test('closing bracket inside a word', function (t) {
|
||||||
|
const arr = [
|
||||||
|
['foo(bar) foo', 'foo(bar)', ' '],
|
||||||
|
['foo[bar] foo', 'foo[bar]', ' '],
|
||||||
|
['foo{bar} foo', 'foo{bar}', ' '],
|
||||||
|
['f(x), y', 'f(x)', ', '],
|
||||||
|
['(foo) bar', 'foo', ') '],
|
||||||
|
]
|
||||||
|
arr.forEach(([str, text, post]) => {
|
||||||
|
const term = nlp(str).docs[0][0]
|
||||||
|
t.equal(term.text, text, here + `text "${str}"`)
|
||||||
|
t.equal(term.post, post, here + `post "${str}"`)
|
||||||
|
})
|
||||||
|
t.end()
|
||||||
|
})
|
||||||
|
|
||||||
test('modify existing punctuation', function (t) {
|
test('modify existing punctuation', function (t) {
|
||||||
const world = nlp.world()
|
const world = nlp.world()
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user