public void JFK() { var jfk = new HumanName("president john 'jack' fitzgerald kennedy"); Assert.AreEqual("president", jfk.Title); Assert.AreEqual("john", jfk.First); Assert.AreEqual("fitzgerald", jfk.Middle); Assert.AreEqual("kennedy", jfk.Last); Assert.AreEqual(string.Empty, jfk.Suffix); Assert.AreEqual("jack", jfk.Nickname); Assert.AreEqual("president john fitzgerald kennedy", jfk.FullName); Assert.AreEqual("kennedy", jfk.LastBase); Assert.AreEqual(string.Empty, jfk.LastPrefixes); jfk.Normalize(); Assert.AreEqual("President", jfk.Title); Assert.AreEqual("John", jfk.First); Assert.AreEqual("Fitzgerald", jfk.Middle); Assert.AreEqual("Kennedy", jfk.Last); Assert.AreEqual(string.Empty, jfk.Suffix); Assert.AreEqual("Jack", jfk.Nickname); Assert.AreEqual("President John Fitzgerald Kennedy", jfk.FullName); Assert.AreEqual("Kennedy", jfk.LastBase); Assert.AreEqual(string.Empty, jfk.LastPrefixes); }
public void DifferentInputsSameValues() { var fml = new HumanName("john x smith"); var lfm = new HumanName("smith, john x"); Assert.IsTrue(fml == lfm); }
public void BlankInput() { var parsed = new HumanName(string.Empty); Assert.AreEqual(string.Empty, parsed.First); Assert.AreEqual(string.Empty, parsed.Middle); Assert.AreEqual(string.Empty, parsed.Last); Assert.AreEqual(string.Empty, parsed.Title); Assert.AreEqual(string.Empty, parsed.Nickname); Assert.AreEqual(string.Empty, parsed.Suffix); }
public void LastBaseAndPrefixes() { var parsed = new HumanName("John Smith"); Assert.AreEqual("Smith", parsed.Last); Assert.AreEqual(string.Empty, parsed.LastPrefixes); Assert.AreEqual("Smith", parsed.LastBase); parsed = new HumanName("johannes van der waals"); Assert.AreEqual("johannes", parsed.First); Assert.AreEqual("van der", parsed.LastPrefixes); // specifically, the prefixes to the last name Assert.AreEqual("waals", parsed.LastBase); // only the base component of the last name Assert.AreEqual("van der waals", parsed.Last); // the full last name, combined parsed.Normalize(); Assert.AreEqual("Johannes", parsed.First); Assert.AreEqual("van der", parsed.LastPrefixes); Assert.AreEqual("Waals", parsed.LastBase); Assert.AreEqual("van der Waals", parsed.Last); }
/// <summary> /// The main parse method for the parser. This method is run upon assignment to the /// fullName attribute or instantiation. /// /// Basic flow is to hand off to `pre_process` to handle nicknames. It /// then splits on commas and chooses a code path depending on the number of commas. /// `parsePieces` then splits those parts on spaces and /// `joinOnConjunctions` joins any pieces next to conjunctions. /// </summary> private void ParseFullName() { if (ParseMultipleNames) { if (_FullName.Contains('&')) { var split = _FullName.IndexOf('&'); var primary = _FullName.Substring(0, split); var secondary = _FullName.Substring(split + 1); AdditionalName = new HumanName(secondary); _FullName = primary; } else if (_FullName.ToLowerInvariant().Contains(" and ")) { var split = _FullName.IndexOf(" and ", StringComparison.InvariantCultureIgnoreCase); var primary = _FullName.Substring(0, split); var secondary = _FullName.Substring(split + 5 /* length of " and " */); AdditionalName = new HumanName(secondary); _FullName = primary; } } ParseNicknames(ref _FullName, out _NicknameList); // break up fullName by commas var parts = _FullName .Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries) .Select(part => part.Trim()) .ToList(); if (parts.Count == 0) { // Edge case where the input was all in parens and has become the nickname // See https://github.com/aeshirey/NameParserSharp/issues/8 } else if (parts.Count == 1) { // no commas, title first middle middle middle last suffix // part[0] var pieces = ParsePieces(parts); for (var i = 0; i < pieces.Length; i++) { var piece = pieces[i]; var nxt = i == pieces.Length - 1 ? string.Empty : pieces[i + 1]; // title must have a next piece, unless it's just a title if (IsTitle(piece) && (!string.IsNullOrEmpty(nxt) || pieces.Length == 1)) { // some last names appear as titles (https://github.com/aeshirey/NameParserSharp/issues/9) // if we've already parsed out titles, first, or middle names, something appearing as a title may in fact be a last name if (_FirstList.Count > 0 || _MiddleList.Count > 0) { _LastList.Add(piece); } else { _TitleList.Add(piece); } } else if (string.IsNullOrEmpty(First)) { _FirstList.Add(piece); } else if (AreSuffixes(pieces.Skip(i + 1))) { _LastList.Add(piece); _SuffixList = _SuffixList.Concat(pieces.Skip(i + 1)).ToList(); break; } else if (!string.IsNullOrEmpty(nxt)) { // another component exists, so this is likely a middle name _MiddleList.Add(piece); } else if (!ParseMultipleNames || AdditionalName == null) { // no additional name. some last names can appear to be suffixes. try to figure this out if (_LastList.Count > 0 && IsSuffix(piece)) { _SuffixList.Add(piece); } else { _LastList.Add(piece); } } else if (AdditionalName._LastList.Any() && IsAnInitial(piece)) { // the additional name has a last, and this one looks like a middle. we'll save as a middle and later will copy the last name. _MiddleList.Add(piece); } else { _LastList.Add(piece); } } } else if (AreSuffixes(parts[1].Split(' '))) { // suffix comma: title first middle last [suffix], suffix [suffix] [, suffix] // parts[0], parts[1:...] _SuffixList = _SuffixList.Concat(parts.Skip(1)).ToList(); var pieces = ParsePieces(parts[0].Split(new[] { ' ' })); for (var i = 0; i < pieces.Length; i++) { var piece = pieces[i]; var nxt = i == pieces.Length - 1 ? string.Empty : pieces[i + 1]; if (IsTitle(piece) && (!string.IsNullOrEmpty(nxt) || pieces.Length == 1)) { _TitleList.Add(piece); continue; } if (string.IsNullOrEmpty(First)) { _FirstList.Add(piece); continue; } if (AreSuffixes(pieces.Skip(i + 1))) { _LastList.Add(piece); _SuffixList = pieces.Skip(i + 1).Concat(_SuffixList).ToList(); break; } // correct for when we have "John D" with an AdditionalName={Catherine, T, MacArthur}. // We should not see this as being First=John, Last=D; rather, First=John, Middle=D, Last=<AdditionalName.Last> if (!string.IsNullOrEmpty(nxt)) { // another component exists, so this is likely a middle name _MiddleList.Add(piece); } else if (!ParseMultipleNames || AdditionalName == null) { // no additional name, so treat this as a last _LastList.Add(piece); } else if (AdditionalName._LastList.Any() && IsAnInitial(piece)) { // the additional name has a last, and this one looks like a middle. we'll save as a middle and later will copy the last name. _MiddleList.Add(piece); } else { _LastList.Add(piece); } } } else { // lastname comma: last [suffix], title first middles[,] suffix [,suffix] // parts[0], parts[1], parts[2:...] var pieces = ParsePieces(parts[1].Split(new[] { ' ' }), 1); // lastname part may have suffixes in it var lastnamePieces = ParsePieces(parts[0].Split(new[] { ' ' }), 1); foreach (var piece in lastnamePieces) { // the first one is always a last name, even if it look like a suffix if (IsSuffix(piece) && _LastList.Any()) { _SuffixList.Add(piece); } else { _LastList.Add(piece); } } for (var i = 0; i < pieces.Length; i++) { var piece = pieces[i]; var nxt = i == pieces.Length - 1 ? string.Empty : pieces[i + 1]; if (IsTitle(piece) && (!string.IsNullOrEmpty(nxt) || pieces.Length == 1)) { _TitleList.Add(piece); } else if (string.IsNullOrEmpty(First)) { _FirstList.Add(piece); } else if (IsSuffix(piece)) { _SuffixList.Add(piece); } else { _MiddleList.Add(piece); } } if (parts.Count() >= 3 && !string.IsNullOrEmpty(parts[2])) { _SuffixList = _SuffixList.Concat(parts.Skip(2)).ToList(); } } IsUnparsable = !_TitleList.Any() && !_FirstList.Any() && !_MiddleList.Any() && !_LastList.Any() && !_SuffixList.Any() && !_NicknameList.Any(); PostProcessFirstnames(); PostProcessLastname(); PostProcessAdditionalName(); }
public void NicknameAtBeginning_SingleQuote() { var parsed = new HumanName("'TREY' ROBERT HENRY BUSH III"); Assert.AreEqual(parsed.First, "ROBERT"); Assert.AreEqual(parsed.Middle, "HENRY"); Assert.AreEqual(parsed.Last, "BUSH"); Assert.AreEqual(parsed.Nickname, "TREY"); Assert.AreEqual(parsed.Suffix, "III"); }
public void TwoNames_TitleFirstInitialMiddleInitialLast() { HumanName.ParseMultipleNames = true; var parsed = new HumanName("Mr S R Bloggs and Miss L B Jones"); Assert.AreEqual("Mr", parsed.Title); Assert.AreEqual("S", parsed.First); Assert.AreEqual("R", parsed.Middle); Assert.AreEqual("Bloggs", parsed.Last); Assert.IsNotNull(parsed.AdditionalName); Assert.AreEqual("Miss", parsed.AdditionalName.Title); Assert.AreEqual("L", parsed.AdditionalName.First); Assert.AreEqual("B", parsed.AdditionalName.Middle); Assert.AreEqual("Jones", parsed.AdditionalName.Last); Assert.IsNull(parsed.AdditionalName.AdditionalName); }
public void TwoNames_MacAthur() { HumanName.ParseMultipleNames = true; var parsed = new HumanName("John D. and Catherine T. MacArthur"); Assert.AreEqual("John", parsed.First); Assert.AreEqual("D.", parsed.Middle); Assert.AreEqual("MacArthur", parsed.Last); Assert.IsNotNull(parsed.AdditionalName); Assert.AreEqual("Catherine", parsed.AdditionalName.First); Assert.AreEqual("T.", parsed.AdditionalName.Middle); Assert.AreEqual("MacArthur", parsed.AdditionalName.Last); Assert.IsNull(parsed.AdditionalName.AdditionalName); }
public void TwoCommaWithMiddleName() { var parsed = new HumanName("Surname, John Middle, III"); Assert.AreEqual(parsed.First, "John"); Assert.AreEqual(parsed.Middle, "Middle"); Assert.AreEqual(parsed.Last, "Surname"); Assert.AreEqual(parsed.Suffix, "III"); }
public void TitleFirstOrLastName() { var mrJones = new HumanName("Mr. Jones"); Assert.AreEqual("Mr.", mrJones.Title); Assert.AreEqual(string.Empty, mrJones.First); Assert.AreEqual(string.Empty, mrJones.Middle); Assert.AreEqual("Jones", mrJones.Last); Assert.AreEqual(string.Empty, mrJones.Suffix); Assert.AreEqual(string.Empty, mrJones.Nickname); Assert.AreEqual("Jones", mrJones.LastBase); Assert.AreEqual(string.Empty, mrJones.LastPrefixes); var uncleAdam = new HumanName("Uncle Adam"); Assert.AreEqual("Uncle", uncleAdam.Title); Assert.AreEqual("Adam", uncleAdam.First); Assert.AreEqual(string.Empty, uncleAdam.Middle); Assert.AreEqual(string.Empty, uncleAdam.Last); Assert.AreEqual(string.Empty, uncleAdam.Suffix); Assert.AreEqual(string.Empty, uncleAdam.Nickname); Assert.AreEqual(string.Empty, uncleAdam.LastBase); Assert.AreEqual(string.Empty, uncleAdam.LastPrefixes); }
public void ThreeNames() { HumanName.ParseMultipleNames = true; var johnSmith = new HumanName("Mr John Smith and Mrs Jane Doe and President Abraham Lincoln"); Assert.IsNotNull(johnSmith.AdditionalName); var janeDoe = johnSmith.AdditionalName; Assert.IsNotNull(janeDoe.AdditionalName); var abrahamLincoln = janeDoe.AdditionalName; Assert.AreEqual("Mr", johnSmith.Title); Assert.AreEqual("John", johnSmith.First); Assert.AreEqual("Smith", johnSmith.Last); Assert.AreEqual("Mrs", janeDoe.Title); Assert.AreEqual("Jane", janeDoe.First); Assert.AreEqual("Doe", janeDoe.Last); Assert.AreEqual("President", abrahamLincoln.Title); Assert.AreEqual("Abraham", abrahamLincoln.First); Assert.AreEqual("Lincoln", abrahamLincoln.Last); }
public void NullInput() { var parsed = new HumanName(null); }
public void Nixon() { var nixon = new HumanName("mr president richard (dick) nixon"); Assert.AreEqual("mr president", nixon.Title); Assert.AreEqual("richard", nixon.First); Assert.AreEqual(string.Empty, nixon.Middle); Assert.AreEqual("nixon", nixon.Last); Assert.AreEqual(string.Empty, nixon.Suffix); Assert.AreEqual("dick", nixon.Nickname); Assert.AreEqual("mr president richard nixon", nixon.FullName); Assert.AreEqual("nixon", nixon.LastBase); Assert.AreEqual(string.Empty, nixon.LastPrefixes); nixon.Normalize(); Assert.AreEqual("Mr President", nixon.Title); Assert.AreEqual("Richard", nixon.First); Assert.AreEqual(string.Empty, nixon.Middle); Assert.AreEqual("Nixon", nixon.Last); Assert.AreEqual(string.Empty, nixon.Suffix); Assert.AreEqual("Dick", nixon.Nickname); Assert.AreEqual("Mr President Richard Nixon", nixon.FullName); Assert.AreEqual("Nixon", nixon.LastBase); Assert.AreEqual(string.Empty, nixon.LastPrefixes); }