Text

Name parsing

People's names split into title, given, middle and family name and suffix, Last, First and ALL CAPS read too, and duplicates like J. Smith and John Smith found.

Version 1.0.0 · 3 functions · 1 crate

#names#forms#import#crm

Functions

What use name_parse.qwer gives a program, with the types Quillo sees. To take only some, or under other names: use name_parse, name_same_person from name_parse (Quillo 0.6).

name_parse(name: str) → anyor an error

The parts of a full name: { title, given, middle, family, suffix, honorifics, initials, first_last, full }, those missing null. It reads "Last, First" too, and fixes ALL CAPS and all lower case ("mcdonald" is "McDonald"). Western names: a name in another order (Chinese, Hungarian) is read given name first. An error when it is not a name.

name_same_person(a: str, b: str) → boolor an error

Whether two written names may be the same person: "J. Smith" and "John Smith" may, "Jane Smith" and "John Smith" may not. For finding duplicate contacts.

name_short(name: str) → stror an error

The name as "J. Smith": the first initial and the family name, for bylines and compact lists.

Get it

$ quillo install name_parse               # name_parse.qwer into this folder$ quillo install name_parse --example     # and its example, name_parse.qwe

Or with curl:

$ curl -O https://quillolang.org/plugins/files/name_parse.qwer$ curl -O https://quillolang.org/plugins/files/name_parse.qwe

Then, in your program, all of it or only what you call:

main.qwe
use name_parse.qweruse name_parse, name_same_person from name_parse      // Quillo 0.6: only these

It uses this crate, which cargo fetches the first time the plugin is built:

[dependencies]
human_name = "2"

Example

name_parse.qwe
// Contacts imported from a spreadsheet: one "name" column split into parts, and duplicates found.////   quillo run name_parse.qwe      then open http://localhost:8080/contactsuse "name_parse.qwer"let column = ["Dr. Martin Luther King, Jr.", "KING, MARTIN L", "ada lovelace", "Mr. John Ronald Reuel Tolkien CBE", "Juan de la Cruz"]route GET /contacts = column | map((c) => { written: c, parts: name_parse(c)? })route GET /duplicates = {    j_smith: name_same_person("J. Smith", "John Smith")?,    jane_john: name_same_person("Jane Smith", "John Smith")?,    king: name_same_person("Dr. Martin Luther King, Jr.", "KING, MARTIN L")?,    byline: name_short("Mr. John Ronald Reuel Tolkien CBE")?,}route GET /parse ? { name: str } = name_parse(name)?
$ quillo run name_parse.qwe$ curl 'localhost:8080/contacts'$ curl 'localhost:8080/duplicates'

Source

name_parse.qwer
//! People's names split into their parts, for forms, CRMs and mail merges: title, given name, middle names, family name and suffix, from "Dr. Martin Luther King, Jr." or "KING, MARTIN L".//! version: 1.0.0//!//! [dependencies]//! human_name = "2"use human_name::Name;use serde_json::{json, Value};fn parse(name: &str) -> Result<Name, String> {    if name.len() > 1000 {        return Err("names up to 1,000 characters".into());    }    Name::parse(name).ok_or_else(|| format!("{:?} does not read as a person's name", name))}/// The parts of a full name: `{ title, given, middle, family, suffix, honorifics, initials, first_last, full }`, those missing null. It reads "Last, First" too, and fixes ALL CAPS and all lower case ("mcdonald" is "McDonald"). Western names: a name in another order (Chinese, Hungarian) is read given name first. An error when it is not a name.pub fn name_parse(name: &str) -> Result<Value, String> {    let n = parse(name)?;    Ok(json!({        "title": n.honorific_prefix(),        "given": n.given_name(),        "middle": n.middle_name().map(|m| m.into_owned()),        "family": n.surname(),        "suffix": n.generational_suffix(),        "honorifics": n.honorific_suffix(),        "initials": n.initials(),        "first_last": n.display_first_last().into_owned(),        "full": n.display_full_with_honorifics().into_owned(),    }))}/// Whether two written names may be the same person: "J. Smith" and "John Smith" may, "Jane Smith" and "John Smith" may not. For finding duplicate contacts.pub fn name_same_person(a: &str, b: &str) -> Result<bool, String> {    Ok(parse(a)?.consistent_with(&parse(b)?))}/// The name as "J. Smith": the first initial and the family name, for bylines and compact lists.pub fn name_short(name: &str) -> Result<String, String> {    Ok(parse(name)?.display_initial_surname().into_owned())}
Thread
77Useful? Like it, or add to its thread below.

Thread 0 posts

On Name parsing: questions, tips, corrections, and code that uses it.

No posts yet. The first one opens the thread.