Skip to content

Commit

Permalink
Converted some function objects to classes
Browse files Browse the repository at this point in the history
  • Loading branch information
Hugo-ter-Doest committed Apr 16, 2024
1 parent aa93a84 commit ea383bd
Show file tree
Hide file tree
Showing 3 changed files with 206 additions and 200 deletions.
230 changes: 116 additions & 114 deletions lib/natural/analyzers/sentence_analyzer.js
Expand Up @@ -25,7 +25,7 @@ THE SOFTWARE.
const _ = require('underscore')

/*
Sentences Analizer Class
Sentences Analyzer Class
From http://www.writingcentre.uottawa.ca/hypergrammar/sntpurps.html
Take a POS input and analyse it for
Expand All @@ -44,148 +44,150 @@ const _ = require('underscore')
- Show Preposition Phrases
*/

const Sentences = function (pos, callbackFunction) {
this.posObj = pos
this.senType = null
callbackFunction(this)
}

Sentences.prototype.part = function (callbackFunction) {
const subject = []
const predicat = []
let verbFound = false
class SentenceAnalyzer {
constructor (pos, callbackFunction) {
this.posObj = pos
this.senType = null
callbackFunction(this)
}

part (callbackFunction) {
const subject = []
const predicat = []
let verbFound = false

this.prepositionPhrases()
this.prepositionPhrases()

for (let i = 0; i < this.posObj.tags.length; i++) {
if (this.posObj.tags[i].pos === 'VB') {
if (i === 0) {
verbFound = true
} else {
// We need to Test for any EX before the VB
if (this.posObj.tags[i - 1].pos !== 'EX') {
for (let i = 0; i < this.posObj.tags.length; i++) {
if (this.posObj.tags[i].pos === 'VB') {
if (i === 0) {
verbFound = true
} else {
predicat.push(this.posObj.tags[i].token)
// We need to Test for any EX before the VB
if (this.posObj.tags[i - 1].pos !== 'EX') {
verbFound = true
} else {
predicat.push(this.posObj.tags[i].token)
}
}
}
}

// Add Pronoun Phrase (pp) Or Subject Phrase (sp)
if (!verbFound) {
if (this.posObj.tags[i].pp !== true) { this.posObj.tags[i].spos = 'SP' }
// Add Pronoun Phrase (pp) Or Subject Phrase (sp)
if (!verbFound) {
if (this.posObj.tags[i].pp !== true) { this.posObj.tags[i].spos = 'SP' }

subject.push(this.posObj.tags[i].token)
} else {
if (this.posObj.tags[i].pp !== true) { this.posObj.tags[i].spos = 'PP' }
subject.push(this.posObj.tags[i].token)
} else {
if (this.posObj.tags[i].pp !== true) { this.posObj.tags[i].spos = 'PP' }

predicat.push(this.posObj.tags[i].token)
predicat.push(this.posObj.tags[i].token)
}
}
}

if (subject.length === 0) {
this.posObj.tags.push({ token: 'You', spos: 'SP', pos: 'PRP', added: true })
}
if (subject.length === 0) {
this.posObj.tags.push({ token: 'You', spos: 'SP', pos: 'PRP', added: true })
}

callbackFunction(this)
}
callbackFunction(this)
}

// Takes POS and removes IN to NN or NNS
// Adds a PP for each prepositionPhrases
Sentences.prototype.prepositionPhrases = function () {
let remove = false
// Takes POS and removes IN to NN or NNS
// Adds a PP for each prepositionPhrases
prepositionPhrases () {
let remove = false

for (let i = 0; i < this.posObj.tags.length; i++) {
if (this.posObj.tags[i].pos.match('IN')) {
remove = true
}
for (let i = 0; i < this.posObj.tags.length; i++) {
if (this.posObj.tags[i].pos.match('IN')) {
remove = true
}

if (remove) {
this.posObj.tags[i].pp = true
}
if (remove) {
this.posObj.tags[i].pp = true
}

if (this.posObj.tags[i].pos.match('NN')) {
remove = false
if (this.posObj.tags[i].pos.match('NN')) {
remove = false
}
}
}
}

Sentences.prototype.subjectToString = function () {
return this.posObj.tags.map(function (t) {
if (t.spos === 'SP' || t.spos === 'S') {
return t.token
} else return null
}).join(' ')
}
subjectToString () {
return this.posObj.tags.map(function (t) {
if (t.spos === 'SP' || t.spos === 'S') {
return t.token
} else return null
}).join(' ')
}

Sentences.prototype.predicateToString = function () {
return this.posObj.tags.map(function (t) {
if (t.spos === 'PP' || t.spos === 'P') {
return t.token
} else return null
}).join(' ')
}
predicateToString () {
return this.posObj.tags.map(function (t) {
if (t.spos === 'PP' || t.spos === 'P') {
return t.token
} else return null
}).join(' ')
}

Sentences.prototype.implicitYou = function () {
for (let i = 0; i < this.posObj.tags.length; i++) {
if (this.posObj.tags[i].added) {
return true
implicitYou () {
for (let i = 0; i < this.posObj.tags.length; i++) {
if (this.posObj.tags[i].added) {
return true
}
}

return false
}

return false
}
toString () {
return this.posObj.tags.map(function (t) { return t.token }).join(' ')
}

Sentences.prototype.toString = function () {
return this.posObj.tags.map(function (t) { return t.token }).join(' ')
}
// This is quick and incomplete.
type (cbf) {
const callbackFunction = cbf || false

// This is quick and incomplete.
Sentences.prototype.type = function (cbf) {
const callbackFunction = cbf || false

// Check for implicit you before popping a tag.
const implicitYou = this.implicitYou()

// FIXME - punct seems useless
let lastElement = this.posObj.punct()
// console.log(lastElement)
lastElement = (lastElement.length !== 0) ? lastElement.pop() : this.posObj.tags.pop()
// console.log(lastElement)

if (lastElement.pos !== '.') {
if (implicitYou) {
this.senType = 'COMMAND'
} else if (_.contains(['WDT', 'WP', 'WP$', 'WRB'], this.posObj.tags[0].pos)) {
// Sentences that start with: who, what where when why and how, then they are questions
this.senType = 'INTERROGATIVE'
} else if (_.contains(['PRP'], lastElement.pos)) {
// Sentences that end in a Personal pronoun are most likely questions
// eg. We should run away, should we [?]
// eg. You want to see that again, do you [?]
this.senType = 'INTERROGATIVE'
} else {
this.senType = 'UNKNOWN'
}
} else {
switch (lastElement.token) {
case '?':
// Check for implicit you before popping a tag.
const implicitYou = this.implicitYou()

// FIXME - punct seems useless
let lastElement = this.posObj.punct()
// console.log(lastElement)
lastElement = (lastElement.length !== 0) ? lastElement.pop() : this.posObj.tags.pop()
// console.log(lastElement)

if (lastElement.pos !== '.') {
if (implicitYou) {
this.senType = 'COMMAND'
} else if (_.contains(['WDT', 'WP', 'WP$', 'WRB'], this.posObj.tags[0].pos)) {
// Sentences that start with: who, what where when why and how, then they are questions
this.senType = 'INTERROGATIVE'
} else if (_.contains(['PRP'], lastElement.pos)) {
// Sentences that end in a Personal pronoun are most likely questions
// eg. We should run away, should we [?]
// eg. You want to see that again, do you [?]
this.senType = 'INTERROGATIVE'
break
case '!':
this.senType = (implicitYou) ? 'COMMAND' : 'EXCLAMATORY'
break
case '.':
this.senType = (implicitYou) ? 'COMMAND' : 'DECLARATIVE'
break
} else {
this.senType = 'UNKNOWN'
}
} else {
switch (lastElement.token) {
case '?':
this.senType = 'INTERROGATIVE'
break
case '!':
this.senType = (implicitYou) ? 'COMMAND' : 'EXCLAMATORY'
break
case '.':
this.senType = (implicitYou) ? 'COMMAND' : 'DECLARATIVE'
break
}
}
}

if (callbackFunction && _.isFunction(callbackFunction)) {
callbackFunction(this)
} else {
return this.senType
if (callbackFunction && _.isFunction(callbackFunction)) {
callbackFunction(this)
} else {
return this.senType
}
}
}

module.exports = Sentences
module.exports = SentenceAnalyzer

0 comments on commit ea383bd

Please sign in to comment.