|
1 | 1 | # NameParserSharp
|
2 | 2 |
|
3 |
| -Based upon [nameparser 0.36](https://pypi.python.org/pypi/nameparser), NameParserSharp is a C# library that parses a human name into constituent fields `Title`, `First`, `Middle`, `Last`, `Suffix`, and `Nickname` from the `HumanName` class. For example: |
| 3 | +Based upon python [nameparser 0.36](https://pypi.python.org/pypi/nameparser), NameParserSharp is a C# library that parses a human name into constituent fields `Title`, `First`, `Middle`, `Last`, `Suffix`, and `Nickname` from the `HumanName` class. NameParserSharp implements the functionality of the Python project on which it is based in a C# idiomatic way. It also, |
| 4 | +* eliminates nearly all regular expressions for efficiency |
| 5 | +* adds unit tests |
| 6 | +* improves nickname handling to expand delimiters: `John (Jack) Torrence` == `John 'Jack' Torrence` == `John "Jack" Torrence` |
| 7 | +* parses out multiple names from a single string as you might expect, as in `mr john and mrs jane doe` |
| 8 | + |
| 9 | + |
| 10 | +## Installation |
4 | 11 |
|
| 12 | +### Using NuGet Package Manager |
| 13 | +```powershell |
| 14 | +Install-Package NameParserSharp |
| 15 | +``` |
| 16 | +### Using .NET CLI |
| 17 | +```bash |
| 18 | +dotnet add package NameParserSharp |
| 19 | +``` |
| 20 | +## Quick Start |
| 21 | +### Basic Usage |
| 22 | +Start parsing names by creating an instance of the HumanName class with the full name string. Access the parsed components through the provided properties. |
5 | 23 | ```c#
|
6 | 24 | var jfk = new HumanName("president john 'jack' f kennedy");
|
7 | 25 |
|
8 |
| -// person.Title == "president" |
9 |
| -// person.First == "john" |
10 |
| -// person.Middle == "f" |
11 |
| -// person.Last == "kennedy" |
12 |
| -// person.Nickname == "jack" |
| 26 | +// Accessing name components |
| 27 | +Console.WriteLine(jfk.Title); // Output: president |
| 28 | +Console.WriteLine(jfk.First); // Output: john |
| 29 | +Console.WriteLine(jfk.Middle); // Output: f |
| 30 | +Console.WriteLine(jfk.Last); // Output: kennedy |
| 31 | +Console.WriteLine(jfk.Nickname); // Output: jack |
13 | 32 |
|
| 33 | +// Parsing an alternative format |
14 | 34 | var jfk_alt = new HumanName("kennedy, president john (jack) f");
|
15 |
| - |
16 | 35 | Assert.IsTrue(jfk == jfk_alt);
|
17 | 36 | ```
|
| 37 | +### Parsing Multiple Names |
| 38 | +Enable the parsing of multiple names within a single string by setting the ParseMultipleNames flag to true. This is useful for handling inputs like "John D. and Catherine T. MacArthur." |
| 39 | +```c# |
| 40 | +// Enable parsing of multiple names |
| 41 | +HumanName.ParseMultipleNames = true; |
| 42 | + |
| 43 | +// Example full name containing two individuals |
| 44 | +string fullName = "John D. and Catherine T. MacArthur"; |
| 45 | + |
| 46 | +// Instantiate the HumanName class with the full name |
| 47 | +HumanName name = new HumanName(fullName); |
| 48 | + |
| 49 | +// Display primary name components |
| 50 | +Console.WriteLine("Primary Name:"); |
| 51 | +Console.WriteLine($" First Name: {name.First}"); |
| 52 | +Console.WriteLine($" Middle Name: {name.Middle}"); |
| 53 | +Console.WriteLine($" Last Name: {name.Last}"); |
| 54 | +Console.WriteLine($" Suffix: {name.Suffix}"); |
| 55 | +Console.WriteLine($" Nickname: {name.Nickname}"); |
| 56 | +Console.WriteLine($" Title: {name.Title}"); |
| 57 | +Console.WriteLine(); |
| 58 | + |
| 59 | +// Check and display additional name components if present |
| 60 | +if (name.AdditionalName != null) |
| 61 | +{ |
| 62 | + Console.WriteLine("Additional Name:"); |
| 63 | + Console.WriteLine($" First Name: {name.AdditionalName.First}"); |
| 64 | + Console.WriteLine($" Middle Name: {name.AdditionalName.Middle}"); |
| 65 | + Console.WriteLine($" Last Name: {name.AdditionalName.Last}"); |
| 66 | + Console.WriteLine($" Suffix: {name.AdditionalName.Suffix}"); |
| 67 | + Console.WriteLine($" Nickname: {name.AdditionalName.Nickname}"); |
| 68 | + Console.WriteLine($" Title: {name.AdditionalName.Title}"); |
| 69 | +} |
| 70 | +``` |
| 71 | +### Normalization |
| 72 | +Ensure consistency by normalizing and capitalizing name components using the `Normalize` method. This method formats each part of the name appropriately. |
| 73 | +```c# |
| 74 | +var rawName = "juan de garcia"; |
| 75 | +var name = new HumanName(rawName); |
| 76 | + |
| 77 | +// Normalize the name |
| 78 | +name.Normalize(); |
| 79 | + |
| 80 | +Console.WriteLine(name.FullName); // Output: Juan de Garcia |
| 81 | +``` |
18 | 82 |
|
19 |
| -NameParserSharp implements the functionality of the Python project on which it is based in a C# idiomatic way. It also, |
20 | 83 |
|
21 |
| -* eliminates nearly all regular expressions for efficiency |
22 |
| -* adds unit tests |
23 |
| -* improves nickname handling to expand delimiters: `John (Jack) Torrence` == `John 'Jack' Torrence` == `John "Jack" Torrence` |
24 |
| -* parses out multiple names from a single string as you might expect, as in `mr john and mrs jane doe` |
25 | 84 |
|
26 |
| -NameParserSharp is available as a NuGet package: `Install-Package NameParserSharp` |
|
0 commit comments