Skip to content

Commit

Permalink
fix for implicit contractions output
Browse files Browse the repository at this point in the history
  • Loading branch information
spencermountain committed Feb 4, 2021
1 parent 68af78f commit 39590ad
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 5 deletions.
4 changes: 4 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ While all _Major_ releases should be reviewed, our only two _large_ releases are
<!-- #### [Unreleased]
-->

#### 13.9.1 [Feb 2021]
- **[fix]** - matches over a contraction
- **[new]** - add 'implicit' text output

#### 13.9.0 [Feb 2021]
- **[new]** - World.addConjugations() method
- **[new]** - World.addPlurals() method
Expand Down
16 changes: 14 additions & 2 deletions scratch.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,20 @@ nlp.extend(require('./plugins/penn-tags/src'))
done - false
*/

let doc = nlp(`haven't done`)
doc.match(`have done`).debug()
// let doc = nlp(`haven't done`)
// doc.match(`have done`).debug()

// let doc = nlp(`i haven't done it`)
// let m = doc.match(`not done`)
// console.log(`|${m.text()}|`)
// m.debug()

let doc = nlp(`is not foobar isn't`)
doc = doc.terms().unique().debug()
// console.log('|' + nlp(`isn't`).text('implicit') + '|')

// console.log(`|${doc.text()}|`)

// doc.termList().forEach(t => {
// console.log(t.text, t.isImplicit())
// })
Expand Down
2 changes: 1 addition & 1 deletion src/Doc/methods/transform/01-sort.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ exports.unique = function () {
let list = [].concat(this.list)
let obj = {}
list = list.filter(p => {
let str = p.text('reduced').trim()
let str = p.text('reduced').trim() || p.text('implicit').trim()
if (obj.hasOwnProperty(str) === true) {
return false
}
Expand Down
12 changes: 12 additions & 0 deletions src/Phrase/methods/02-text.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ exports.text = function (options = {}, isFirst, isLast) {
implicit: true,
reduced: true,
}
} else if (options === 'implicit') {
options = {
punctuation: true,
implicit: true,
whitespace: true,
trim: true,
}
} else if (options === 'root') {
options = {
titlecase: false,
Expand All @@ -57,6 +64,10 @@ exports.text = function (options = {}, isFirst, isLast) {
isFull = true
}
let text = terms.reduce((str, t, i) => {
// don't output intro space for a contraction-match i'm good => "[am] good"
if (i === 0 && t.text === '' && t.implicit !== null && !options.implicit) {
return str
}
options.last = isLast && i === terms.length - 1
let showPre = true
let showPost = true
Expand All @@ -71,6 +82,7 @@ exports.text = function (options = {}, isFirst, isLast) {
}
}
let txt = t.textOut(options, showPre, showPost)
// console.log(terms)
// if (options.titlecase && i === 0) {
// txt = titleCase(txt)
// }
Expand Down
3 changes: 3 additions & 0 deletions src/Phrase/methods/05-json.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ exports.json = function (options = {}, world) {
if (options.reduced) {
res.reduced = this.text('reduced')
}
if (options.implicit) {
res.implicit = this.text('implicit')
}
if (options.root) {
res.root = this.text('root')
}
Expand Down
4 changes: 2 additions & 2 deletions tests/match/match-contraction.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ test('match-contractions', function (t) {
t.equal(m.text(), `haven't`, 'first-half-found')

m = doc.match(`not done`)
t.equal(m.text(), ` done`, 'second-half-found')
t.equal(m.text(), `done`, 'second-half-found')

m = doc.match(`haven't`)
t.equal(m.text(), `haven't`, 'match-contraction')
Expand Down Expand Up @@ -80,7 +80,7 @@ test('contraction-optional', function (t) {
t.equal(m.text(), `i'm`, `i am?`)

m = doc.match(`am glad?`)
t.equal(m.text(), ` glad`, `am glad?`)
t.equal(m.text(), `glad`, `am glad?`)

m = doc.match(`i am? glad`)
t.equal(m.text(), `i'm glad`, `i am? glad`)
Expand Down
8 changes: 8 additions & 0 deletions tests/output/text.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,14 @@ test('text-reduced', function (t) {
t.end()
})

test('text-implicit', function (t) {
let doc = nlp(`My dog isn't good, he's the best!`)
const str = 'My dog is not good, he is the best!'
t.equal(doc.json({ implicit: true })[0].implicit, str, 'json(implicit)')
t.equal(doc.text('implicit'), str, 'text(implicit): ')
t.end()
})

test('text-root', function (t) {
let doc = nlp(`My dog LOVES pizza, and grapes!!`)
const str = 'my dog love pizza and grape'
Expand Down
1 change: 1 addition & 0 deletions tests/unique.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,6 @@ test('unique-normalize', function (t) {
doc = nlp(`is not isn't`)
doc = doc.terms().unique()
t.equal(doc.text(), 'is not', 'normalize-contraction')
t.equal(doc.length, 2, 'implicit words are uniqued')
t.end()
})

0 comments on commit 39590ad

Please sign in to comment.