UNPKG

compromise

Version:

modest natural language processing

47 lines (42 loc) • 1.61 kB
/* eslint-disable regexp/no-dupe-characters-character-class */ import { findPairs, stripPairs } from '../_pairs.js' const pairs = { '\u0022': '\u0022', // 'StraightDoubleQuotes' '\uFF02': '\uFF02', // 'StraightDoubleQuotesWide' '\u0027': '\u0027', // 'StraightSingleQuotes' '\u201C': '\u201D', // 'CommaDoubleQuotes' '\u2018': '\u2019', // 'CommaSingleQuotes' '\u201F': '\u201D', // 'CurlyDoubleQuotesReversed' '\u201B': '\u2019', // 'CurlySingleQuotesReversed' '\u201E': '\u201D', // 'LowCurlyDoubleQuotes' '\u2E42': '\u201D', // 'LowCurlyDoubleQuotesReversed' '\u201A': '\u2019', // 'LowCurlySingleQuotes' '\u00AB': '\u00BB', // 'AngleDoubleQuotes' «, » '\u2039': '\u203A', // 'AngleSingleQuotes' // Prime 'non quotation' '\u2035': '\u2032', // 'PrimeSingleQuotes' '\u2036': '\u2033', // 'PrimeDoubleQuotes' '\u2037': '\u2034', // 'PrimeTripleQuotes' // Prime 'quotation' variation '\u301D': '\u301E', // 'PrimeDoubleQuotes' '\u0060': '\u00B4', // 'PrimeSingleQuotes' '\u301F': '\u301E', // 'LowPrimeDoubleQuotesReversed' } const hasOpen = RegExp('[' + Object.keys(pairs).join('') + ']') const hasClosed = RegExp('[' + Object.values(pairs).join('') + ']') const findEnd = function (terms, i) { const have = terms[i].pre.match(hasOpen)[0] || '' if (!have || !pairs[have]) { return null } const want = pairs[have] for (; i < terms.length; i += 1) { if (terms[i].post && terms[i].post.match(want)) { return i } } return null } const find = findPairs(hasOpen, findEnd) const strip = stripPairs(hasOpen, hasClosed) export { find, strip }