• 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
  • parse_name.js

  • ¶
    'use strict';
    const firstnames = require('../../../data/firstnames');
    const honourifics = require('../../../data/honourifics').reduce(function(h, s) {
      h[s] = true;
      return h;
    }, {});
    
    const parse_name = function(str) {
    
      let words = str.split(' ');
      let o = {
        honourific: null,
        firstName: null,
        middleName: null,
        lastName: null,
      };
  • ¶

    first-word honourific

      if (honourifics[words[0]]) {
        o.honourific = words[0];
        words = words.slice(1, words.length);
      }
  • ¶

    last-word honourific

      if (honourifics[words[words.length - 1]]) {
        o.honourific = words[words.length - 1];
        words = words.slice(0, words.length - 1);
      }
  • ¶

    see if the first word is now a known first-name

      if (firstnames[words[0]]) {
        o.firstName = words[0];
        words = words.slice(1, words.length);
      } else {
  • ¶

    ambiguous one-word name

        if (words.length === 1) {
          return o;
        }
  • ¶

    looks like an unknown first-name

        o.firstName = words[0];
        words = words.slice(1, words.length);
      }
  • ¶

    assume the remaining is ‘[middle..] [last]’

      if (words[words.length - 1]) {
        o.lastName = words[words.length - 1];
        words = words.slice(0, words.length - 1);
      }
      o.middleName = words.join(' ');
      return o;
    };
    
    module.exports = parse_name;
  • ¶

    console.log(parse_name(‘john smith’));