compromise
Version:
modest natural language processing
104 lines (93 loc) • 2.25 kB
JavaScript
import eachTerm from '../../../1-one/tokenize/compute/_lib.js'
import { Conj, Prop, Plur, Poss, Pron } from '../tagSet/_lib.js'
// rough connection between compromise tagset and Penn Treebank
// https://www.ling.upenn.edu/courses/Fall_2003/ling001/penn_treebank_pos.html
const mapping = {
// adverbs
// 'Comparative': 'RBR',
// 'Superlative': 'RBS',
Adverb: 'RB',
// adjectives
Comparative: 'JJR',
Superlative: 'JJS',
Adjective: 'JJ',
TO: Conj,
// verbs
Modal: 'MD',
Auxiliary: 'MD',
Gerund: 'VBG', //throwing
PastTense: 'VBD', //threw
Participle: 'VBN', //thrown
PresentTense: 'VBZ', //throws
Infinitive: 'VB', //throw
Particle: 'RP', //phrasal particle
Verb: 'VB', // throw
// pronouns
Pronoun: 'PRP',
// misc
Cardinal: 'CD',
Conjunction: 'CC',
Determiner: 'DT',
Preposition: 'IN',
// 'Determiner': 'WDT',
// 'Expression': 'FW',
QuestionWord: 'WP',
Expression: 'UH',
//nouns
Possessive: 'POS',
ProperNoun: 'NNP',
Person: 'NNP',
Place: 'NNP',
Organization: 'NNP',
Singular: 'NN',
Plural: 'NNS',
Noun: 'NN',
There: 'EX', //'there'
// 'Adverb':'WRB',
// 'Noun':'PDT', //predeterminer
// 'Noun':'NFP', //
// WDT Wh-determiner
// WP Wh-pronoun
// WP$ Possessive wh-pronoun
// WRB Wh-adverb
}
const toPenn = function (term) {
// Inspect the whole token, leaving amounts and attached punctuation alone.
if (/^[*/+=<>|~^×÷±≠≤≥]+$/.test(term.text)) {
return 'SYM'
}
if (term.text === '$') {
return '$'
}
if (term.text === '£' || term.text === '#') {
return '#'
}
// try some ad-hoc ones
if (term.tags.has(Prop) && term.tags.has(Plur)) {
return 'NNPS'
}
if (term.tags.has(Poss) && term.tags.has(Pron)) {
return 'PRP$'
}
if (term.normal === 'there') {
return 'EX'
}
if (term.normal === 'to') {
return 'TO'
}
// run through an ordered list of tags
const arr = term.tagRank || []
for (let i = 0; i < arr.length; i += 1) {
if (Object.hasOwn(mapping, arr[i])) {
return mapping[arr[i]]
}
}
return null
}
const pennTag = function (view) {
view.compute('tagRank')
eachTerm(view.docs, term => {
term.penn = toPenn(term)
})
}
export default pennTag