Skip to content

Commit

Permalink
refactor last class used
Browse files Browse the repository at this point in the history
  • Loading branch information
spencermountain committed Apr 4, 2017
1 parent c16dac8 commit 8a2dd23
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 95 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ r.out('text')
<td align="center">
<b>
<a href="https://unpkg.com/compromise@latest/builds/compromise.min.js">
220k
200k
</a>
</b>
<div>
Expand Down
6 changes: 2 additions & 4 deletions scratch.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,5 @@ const fresh = require('./test/unit/lib/freshPrince.js');
// r.tag('#Person');
// console.timeEnd('tag');

let r = nlp('john is nice').sentences().toNegative();
console.log(r.out('text'));
// console.log(r.found);
// console.log(r.out());
let r = nlp('work with F.B.I.').nouns();
r.debug();
48 changes: 26 additions & 22 deletions src/result/subset/ngrams/endGrams.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,32 @@ const Ngrams = require('./index');
const getGrams = require('./getGrams');

//like an n-gram, but only the endings of matches
class EndGrams extends Ngrams {
const EndGrams = function(arr, lexicon, reference) {
Ngrams.call(this, arr, lexicon, reference);
};

static find(r, n, size) {
let opts = {
size: [1, 2, 3, 4],
edge: 'end'
};
//only look for bigrams, for example
if (size) {
opts.size = [size];
}
//fetch them
let arr = getGrams(r, opts);
r = new EndGrams(arr);
//default sort
r.sort();
//grab top one, or something
if (typeof n === 'number') {
r = r.get(n);
}
return r;
}
}
//Inherit properties
EndGrams.prototype = Object.create(Ngrams.prototype);

//like an n-gram, but only the startings of matches
EndGrams.find = function(r, n, size) {
let opts = {
size: [1, 2, 3, 4],
edge: 'end'
};
//only look for bigrams, for example
if (size) {
opts.size = [size];
}
//fetch them
let arr = getGrams(r, opts);
r = new EndGrams(arr);
//default sort
r.sort();
//grab top one, or something
if (typeof n === 'number') {
r = r.get(n);
}
return r;
};
module.exports = EndGrams;
49 changes: 27 additions & 22 deletions src/result/subset/ngrams/startGrams.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,34 @@
const Ngrams = require('./index');
const getGrams = require('./getGrams');

//like an n-gram, but only the startings of matches
class StartGrams extends Ngrams {
const StartGrams = function(arr, lexicon, reference) {
Ngrams.call(this, arr, lexicon, reference);
};

//Inherit properties
StartGrams.prototype = Object.create(Ngrams.prototype);

static find(r, n, size) {
let opts = {
size: [1, 2, 3, 4],
edge: 'start'
};
//only look for bigrams, for example
if (size) {
opts.size = [size];
}
//fetch them
let arr = getGrams(r, opts);
r = new StartGrams(arr);
//default sort
r.sort();
//grab top one, or something
if (typeof n === 'number') {
r = r.get(n);
}
return r;
//like an n-gram, but only the startings of matches
StartGrams.find = function(r, n, size) {
let opts = {
size: [1, 2, 3, 4],
edge: 'start'
};
//only look for bigrams, for example
if (size) {
opts.size = [size];
}
}
//fetch them
let arr = getGrams(r, opts);
r = new StartGrams(arr);
//default sort
r.sort();
//grab top one, or something
if (typeof n === 'number') {
r = r.get(n);
}
return r;
};


module.exports = StartGrams;
100 changes: 54 additions & 46 deletions src/term/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,55 +3,63 @@ const fns = require('./paths').fns;
const build_whitespace = require('./whitespace');
const makeUID = require('./makeUID');

class Term {
constructor(str) {
this._text = fns.ensureString(str);
this.tags = {};
//seperate whitespace from the text
let parsed = build_whitespace(this._text);
this.whitespace = parsed.whitespace;
this._text = parsed.text;
// console.log(this.whitespace, this._text);
this.parent = null;
this.silent_term = '';
//has this term been modified
this.dirty = false;
this.normalize();
//make a unique id for this term
this.uid = makeUID(this.normal);
}
set text(str) {
str = str || '';
this._text = str.trim();
this.dirty = true;
if (this._text !== str) {
this.whitespace = build_whitespace(str);
const Term = function(str) {
this._text = fns.ensureString(str);
this.tags = {};
//seperate whitespace from the text
let parsed = build_whitespace(this._text);
this.whitespace = parsed.whitespace;
this._text = parsed.text;
// console.log(this.whitespace, this._text);
this.parent = null;
this.silent_term = '';
//has this term been modified
this.dirty = false;
this.normalize();
//make a unique id for this term
this.uid = makeUID(this.normal);

//getters/setters
Object.defineProperty(this, 'text', {
get: function() {
return this._text;
},
set: function(txt) {
txt = txt || '';
this._text = txt.trim();
this.dirty = true;
if (this._text !== txt) {
this.whitespace = build_whitespace(txt);
}
this.normalize();
}
this.normalize();
}
get text() {
return this._text;
}
get isA() {
return 'Term';
}
/** where in the sentence is it? zero-based. */
index() {
let ts = this.parentTerms;
if (!ts) {
return null;
});
//bit faster than .constructor.name or w/e
Object.defineProperty(this, 'isA', {
get: function() {
return 'Term';
}
return ts.terms.indexOf(this);
}
/** make a copy with no references to the original */
clone() {
let term = new Term(this._text, null);
term.tags = fns.copy(this.tags);
term.whitespace = fns.copy(this.whitespace);
term.silent_term = this.silent_term;
return term;
});
};


/** where in the sentence is it? zero-based. */
Term.prototype.index = function() {
let ts = this.parentTerms;
if (!ts) {
return null;
}
}
return ts.terms.indexOf(this);
};
/** make a copy with no references to the original */
Term.prototype.clone = function() {
let term = new Term(this._text, null);
term.tags = fns.copy(this.tags);
term.whitespace = fns.copy(this.whitespace);
term.silent_term = this.silent_term;
return term;
};

require('./methods/normalize')(Term);
require('./methods/misc')(Term);
require('./methods/out')(Term);
Expand Down

0 comments on commit 8a2dd23

Please sign in to comment.