• Jump To … +
    abbreviations.js adjectives.js convertables.js dates.js demonyms.js firstnames.js honourifics.js irregular_nouns.js irregular_verbs.js misc.js multiples.js numbers.js organisations.js phrasal_verbs.js places.js uncountables.js verbs.js fns.js index.js lexicon.js negate.js passive_voice.js contractions.js fancy_lumping.js grammar_rules.js parts_of_speech.js phrasal_verbs.js tagger.js word_rules.js question.js sentence.js statement.js tense.js adjective.js to_adverb.js to_comparative.js to_noun.js to_superlative.js adverb.js to_adjective.js is_acronym.js article.js date.js date_rules.js is_date.js parse_date.js is_plural.js is_uncountable.js noun.js is_organisation.js organisation.js gender.js is_person.js parse_name.js person.js is_place.js place.js pluralize.js pronoun.js singularize.js is_value.js numbers.js to_number.js units.js value.js term.js conjugate.js from_infinitive.js predict_form.js suffix_rules.js to_actor.js to_infinitive.js negate.js verb.js sentence_parser.js text.js
  • term.js

  • ¶
    'use strict';
    const is_acronym = require('./is_acronym');
    
    class Term {
      constructor(str, tag) {
  • ¶

    fail-safe

        if (str === null || str === undefined) {
          str = '';
        }
        str = (str).toString();
  • ¶

    set .text

        this.text = str;
  • ¶

    the normalised working-version of the word

        this.normal = '';
  • ¶

    if it’s a contraction, the ‘hidden word’

        this.implicit = '';
  • ¶

    set .normal

        this.rebuild();
  • ¶

    the reasoning behind it’s part-of-speech

        this.reason = '';
  • ¶

    these are orphaned POS that have no methods

        let types = {
          Determiner: 'Determiner',
          Conjunction: 'Conjunction',
          Preposition: 'Preposition',
          Posessive: 'Posessive',
        };
        this.pos = {};
        this.tag = types[tag] || '?';
  • ¶

    record them in pos{}

        if (types[tag]) {
          this.pos[types[tag]] = true;
        }
      }
  • ¶

    when the text changes, rebuild derivative fields

      rebuild() {
        this.text = this.text || '';
        this.text = this.text.trim();
        this.normal = '';
        this.normalize();
      }
      changeTo(str) {
        this.text = str;
        this.rebuild();
      }
  • ¶

    Term methods..

      is_capital() {
        if (this.text.match(/[A-Z][a-z]/)) { //tranditional capital
          return true;
        }
        return false;
      }
  • ¶

    FBI or F.B.I.

      is_acronym() {
        return is_acronym(this.text);
      }
  • ¶

    working word

      normalize() {
        let str = this.text || '';
        str = str.toLowerCase();
        str = str.replace(/[,\.!:;\?\(\)]/, '');
        str = str.replace(/’/g, '\'');
        str = str.replace(/"/g, '');
  • ¶

    coerce single curly quotes

        str = str.replace(/[\u2018\u2019\u201A\u201B\u2032\u2035]+/g, '\'');
  • ¶

    coerce double curly quotes

        str = str.replace(/[\u201C\u201D\u201E\u201F\u2033\u2036]+/g, '"');
        if (!str.match(/[a-z0-9]/i)) {
          return '';
        }
        this.normal = str;
        return this.normal;
      }
    }
    
    Term.fn = Term.prototype;
  • ¶

    let t = new Term(‘NSA’); console.log(t.britishize());

    module.exports = Term;