Skip to content

Instantly share code, notes, and snippets.

@cmbaughman
Last active January 29, 2023 23:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cmbaughman/7d6b10fdb0b625f76d0d47717962cd03 to your computer and use it in GitHub Desktop.
Save cmbaughman/7d6b10fdb0b625f76d0d47717962cd03 to your computer and use it in GitHub Desktop.
Using Compromise.js to get name formats right.
const nlp = require("compromise");
const nameParser = require("name-parser");
// Function to determine the name format
function determineNameFormat(name) {
// Patterns to match different name formats
const firstLast = nlp(name).match("#Honorific #FirstName #LastName").out("array");
const lastFirst = nlp(name).match("#LastName, #FirstName #Honorific").out("array");
const middleName = nlp(name).match("#FirstName #MiddleName #LastName").out("array");
const middleInitial = nlp(name).match("#FirstName #MiddleInitial #LastName").out("array");
const hyphenatedName = nlp(name).match("#FirstName #LastName-#LastName").out("array");
const lastFirstMiddle = nlp(name).match("#LastName #FirstName #MiddleName").out("array");
const businessName = nlp(name).match("#Business").out("array");
// Check for presence of different name formats
if (firstLast.length > 0) {
console.log("Honorific First Name Last Name format");
} else if (lastFirst.length > 0) {
console.log("Last Name, First Name Honorific format");
} else if (middleName.length > 0) {
console.log("First Name Middle Name Last Name format");
} else if (middleInitial.length > 0) {
console.log("First Name Middle Initial Last Name format");
} else if (hyphenatedName.length > 0) {
console.log("First Name Last Name-Last Name format");
} else if (lastFirstMiddle.length > 0) {
console.log("Last Name First Name Middle Name format");
} else if (businessName.length > 0) {
console.log("Business Name format");
} else {
// Use name-parser to parse the name and determine the format
const parsedName = nameParser.parse(name);
console.log(`Parsed Name: ${parsedName.firstName} ${parsedName.middleName} ${parsedName.lastName}`);
}
}
determineNameFormat("John Michael Smith");
// Output: First Name Middle Name Last Name format
determineNameFormat("Smith, John Michael");
// Output: Last Name, First Name Middle Name format
determineNameFormat("Smith John Michael");
// Output: Last Name First Name Middle Name format
determineNameFormat("John Michael-Smith");
// Output: First Name Last Name-Last Name format
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment