Public
Edited
Jan 23, 2024
2 stars
Insert cell
Insert cell
Insert cell
{
let matches = [
{ match: 'john c .' },
{ match: 'john foo' },
{ match: 'john .? reilly'},
]
//compile them into an efficient index
let net = nlp.buildNet(matches)

// pass some text through it
let doc = nlp(sentence)
let res = doc.sweep(net)
//return a list of unique results
let m = res.view.settle()
return m.text()
}
Insert cell
Insert cell
{
let matches = [
{ match: 'john c .', tag:'Actor1' },
{ match: 'john foo', tag:'Foo' },
{ match: 'john .? reilly', tag:'Actor2'},
]
//compile them into an index
let net = nlp.buildNet(matches)

// throw some text at it
let doc = nlp(sentence)
let res = doc.sweep(net)

// highlight any #Actor2 terms
return html`${doc.html({'.red':'#Actor2+'})}`
}
Insert cell
Insert cell
{
let matches = [
{ match: 'juicy fruit', tag: 'Singular', freeze: true }, // ✅
{ match: 'juicy', tag: 'Adjective' }, // ❌
]
let doc = nlp(`i ate juicy fruit on the front steps`)
let net = nlp.buildNet(matches)
doc.sweep(net)
// see that 'juicy fruit' is unchanged
return doc.match('#Singular+').text()
}
Insert cell
Insert cell
{
let matches = [
{ match: 'united', tag: 'Adjective', safe:true }, // ❌
]
let doc = nlp(`i visited the United Arab Emirates`)
let net = nlp.buildNet(matches)
doc.sweep(net)
// see that 'united' is a country, and not an adjective
return doc.match('#Country+').text()
}
Insert cell
Insert cell
{
let doc = nlp('i swim in the lake. i walk in the road')
let net = nlp.buildNet([{ match: 'i (swim|walk) in the .', notIf: 'in the (park|lake)' }])
let m = doc.match(net)
return m.out('array')
}
Insert cell
Insert cell
{
let doc = nlp('i swim in the lake')
let net = nlp.buildNet([{ match: 'i #Verb in the [.]', group: 0, notIf: 'swim' }])
let m = doc.match(net)
return m.out('array')
}
Insert cell
Insert cell
{
let matches = [
{ match: 'john c #LastName' }, // first match
{ match: 'foo' },
{ match: '#Person+'}, // this would also match
]
let net = nlp.buildNet(matches)
let doc = nlp(sentence)
let { found } = doc.sweep(net, { matchOne: true }) //stop at 1
return found[0]
}
Insert cell
Insert cell
{
let matches = [
{ match: 'john c #LastName' }, // first match
{ match: 'foo' },
{ match: '#Person+'}, // matches both actors
]
let net = nlp.buildNet(matches)
let doc = nlp(sentence)
let { found } = doc.sweep(net)
//draw-up a little html
return html`<table>
<thead>
<td>index</td> <td>match</td> <td>results</td>
</thead>
${found.map((res,i)=> {
return '<tr><td>'+i+'</td><td>' + res.match + '</td><td>' + JSON.stringify(res.view.out('array')) + '</td></tr>'
})}
</table>`
}
Insert cell
Insert cell
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