compromise
Version:
modest natural language processing
89 lines (85 loc) • 1.81 kB
JavaScript
import { hasHyphen, splitHyphens } from './01-hyphens.js'
import combineRanges from './03-ranges.js'
import combineSlashes from './02-slashes.js'
import { toList } from '../../model/_lib.js'
const wordlike = /\S/
const isBoundary = /^[!?.]+$/
const naiiveSplit = /(\S+)/
const notWord = new Set(toList(`
.
?
!
:
;
-
–
—
--
...
(
)
[
]
"
'
\`
«
»
*
•
`))
//turn a string into an array of strings (naiive for now, lumped later)
const splitWords = function (str, model) {
let result = []
let arr = []
//start with a naiive split
str ||= ''
if (typeof str === 'number') {
str = String(str)
}
if (Array.isArray(str)) {
return str
}
const words = str.split(naiiveSplit)
for (let i = 0; i < words.length; i++) {
//split 'one-two'
if (hasHyphen(words[i], model)) {
arr = arr.concat(splitHyphens(words[i]))
continue
}
arr.push(words[i])
}
//greedy merge whitespace+arr to the right
let carry = ''
for (let i = 0; i < arr.length; i++) {
const word = arr[i]
//if it's more than a whitespace
if (wordlike.test(word) && !notWord.has(word) && !isBoundary.test(word)) {
//put whitespace on end of previous term, if possible
if (result.length > 0) {
result[result.length - 1] += carry
result.push(word)
} else {
//otherwise, but whitespace before
result.push(carry + word)
}
carry = ''
} else {
carry += word
}
}
//handle last one
if (carry) {
if (result.length === 0) {
result[0] = ''
}
result[result.length - 1] += carry //put it on the end
}
// combine 'one / two'
result = combineSlashes(result)
result = combineRanges(result)
// remove empty results
result = result.filter(s => s)
return result
}
export default splitWords