Public
Edited
Feb 2, 2024
9 stars
Insert cell
Insert cell
{
const myPlugin= {
/** add a method */
api: (View) => {
View.prototype.beNice = function () {
this.match('#Infinitive').prepend('kindly')
return this
}
},
/** add some tags */
tags: {
Character: {
isA: 'Person', // any automatically-set tags (can be array)
notA: 'Adjective', // conflicting tags (can be array)
color: 'red' // debug color - green|red|blue|magenta|cyan|yellow|black
},
},
/** add some words to the lexicon */
words: {
gonzo: 'MaleName',
kermit: 'Frog',
'minnie mouse': 'Character',
},
/** add words to the stronger, more adamant lexicon */
frozen: {
'juicy fruit': 'Treat'
},
/** post-process tagger */
compute: {
postProcess: (doc) => {
doc.match('light the lights').tag('#Verb . #Plural')
}
},

/** change conjugations/inflections */
irregulars: {
// co-erce verb transformations
get: {
pastTense: 'gotten',
presentTense: 'gets',
gerund: 'gettin'
},
// same for adjectives
sly:{
comparative:'slyer',
superlative:'slyest'
}
},
/** run it on init */
hooks:['postProcess']
}
// add the plugin to compromise
nlp.plugin(myPlugin)
//see it running:
return nlp(`wash the floor`).beNice().text()
}
Insert cell
Insert cell
{
let doc = nlp(`gonzo, minnie mouse and kermit the frog`)
doc = doc.splitAfter('@hasComma')
let m = doc.match('#Character+')
return m.out('array')
}
Insert cell
{
nlp.plugin({
//add new tags
tags: {
TimePeriod: {
isA: 'Noun'
},
Dinosaur: {
isA: 'Animal',
},
Animal: {
isA: 'Noun',
},
},
// add new words to the lexicon
words: {
'pangaea': 'Noun',
't rex': 'Dinosaur',
'sauropod': 'Dinosaur'
},
// add methods to run after the tagger does
compute: {
postProcess: (doc) => {
doc.match('/.opod$/').tag('Dinosaur')
doc.match("the #TitleCase (era|epoch)").tag("TimePeriod")
}
},
/** run it on init */
hooks:['postProcess']
})
//now nlp will act properly-
let doc = nlp('i saw a huge T-Rex and a sauropod');
//return these new tagged-results.
return doc.match('#Animal+').out('array')
}
Insert cell
Insert cell
{
nlp.extend((_Doc, world) => {
world.addPlurals({ neglectee: 'neglecterinos' })
})
const doc = nlp('neglectee')
return doc.nouns().toPlural().text()
}
Insert cell
Insert cell
{
nlp.plugin({
// normal lexicon
words:{
juicy:'Adjective' // 🤔 maybe
},
// frozen lexicon
frozen: {
'juicy fruit': 'Singular', // ✅
},
})
let doc = nlp(`i ate some juicy fruit.`)
return doc.match('#Singular').text()
}
Insert cell
Insert cell
{
nlp.plugin({
mutate: (world) => {
let termMethods = world.methods.one.termMethods
termMethods.hasVowel = function (term) {
return /[aeiou]/.test(term.text)
}
}
})

return nlp('one two bb gg three').terms().if('@hasVowel').text()
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
nlp.version
Insert cell
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more