예제 #1
0
        /// <summary>
        /// Construct an instance of PersonName by parsing the text provided.
        /// </summary>
        /// <param name="text">The text to parse into a name.</param>
        public PersonName(string text)
        {
            if (string.IsNullOrWhiteSpace(text))
            {
                throw new ArgumentNullException(nameof(text));
            }

            string working = text.Trim();

            Prefix   = string.Empty;
            Last     = string.Empty;
            First    = string.Empty;
            Middle   = string.Empty;
            Suffix   = string.Empty;
            Nickname = string.Empty;

            // Check for nicknames (in quotes)
            if (working.Contains("\""))
            {
                int x1 = working.IndexOf("\"");
                int x2 = working.IndexOf("\"", x1 + 1);
                if (x2 > x1)
                {
                    Nickname = working.Substring(x1 + 1, x2 - x1 - 1).Trim();
                    working  = text.Replace("\"" + Nickname + "\"", string.Empty);
                }
            }
            else if (working.Contains("'"))
            {
                int x1 = working.IndexOf("'");
                int x2 = working.IndexOf("'", x1 + 1);
                if (x2 > x1)
                {
                    Nickname = working.Substring(x1 + 1, x2 - x1 - 1).Trim();
                    working  = working.Replace("'" + Nickname + "'", string.Empty);
                }
            }

            // Check for known prefixes.
            foreach (var prefix in Honorifics.ForParsingName)
            {
                if (working.StartsWith(prefix, StringComparison.OrdinalIgnoreCase))
                {
                    Prefix  = prefix.Trim();
                    working = working.Substring(prefix.Length).Trim();
                }
            }

            StringBuilder m = new StringBuilder();

            // Check to see if text contains comma.
            if (working.Contains(","))
            {
                int commaInd = working.IndexOf(",");
                int spaceInd = working.IndexOf(" ");
                // If a space occurs before the comma, it's most likely formatted as [First Last, Suffix]
                if (spaceInd > 0 && spaceInd < commaInd)
                {
                    Suffix = working.Substring(commaInd + 1).Trim();
                    var split = splitText(working.Substring(0, commaInd)).ToList();
                    if (split.Count > 2)
                    {
                        First = split.First().Trim();
                        Last  = split.Last().Trim();
                        foreach (var s in split.Skip(1).Take(split.Count - 2))
                        {
                            m.Append(" ");
                            m.Append(s);
                        }
                        Middle = m.ToString().Trim();
                    }
                    else if (split.Count == 2)
                    {
                        First = split.First().Trim();
                        Last  = split.Last().Trim();
                    }
                    else
                    {
                        Last = split[0].Trim();
                    }
                }
                else                     // If there is no space before the comma, it's most likely formatted as [Last, First]
                {
                    Last    = working.Substring(0, commaInd).Trim();
                    working = working.Substring(commaInd + 1).Trim();
                    if (!working.Contains(" "))
                    {
                        First = working.Trim();
                    }
                    else
                    {
                        var split = splitText(working);
                        First = split.First().Trim();
                        foreach (var s in split.Skip(1))
                        {
                            m.Append(" ");
                            m.Append(s.Trim());
                        }
                        Middle = m.ToString().Trim();
                        // Check to see if there's another comma in the remainer, if so, what follows is a suffix
                        if (Middle.Contains(","))
                        {
                            Suffix = Middle.Substring(Middle.IndexOf(",") + 1).Trim();
                            Middle = Middle.Substring(0, Middle.IndexOf(",")).Trim();
                        }
                    }
                }
            }
            else                            // No commas. Search for common suffixes and assume it's formatted as [First Last]
            {
                if (!working.Contains(" ")) // No space. It's just a last name.
                {
                    Last = working.Trim();
                }
                else
                {
                    var    split = splitText(working).ToList();
                    string suff  = string.Empty;
                    foreach (var s in split)
                    {
                        if (CommonSuffixes.Contains(s, StringComparer.OrdinalIgnoreCase))
                        {
                            suff = s;
                        }
                    }
                    if (!string.IsNullOrEmpty(suff))
                    {
                        split.Remove(suff);
                        Suffix = suff.Trim();
                    }
                    if (split.Count == 1)
                    {
                        Last = split.First().Trim();
                    }
                    else if (split.Count == 2)
                    {
                        First = split.First().Trim();
                        Last  = split.Last().Trim();
                    }
                    else
                    {
                        First = split.First().Trim();
                        Last  = split.Last().Trim();
                        foreach (var s in split.Skip(1).Take(split.Count - 2))
                        {
                            m.Append(" ");
                            m.Append(s);
                        }
                        Middle = m.ToString().Trim();
                    }
                }
            }
        }
예제 #2
0
        /// <summary>
        /// Construct an instance of PersonName by parsing the text provided.
        /// </summary>
        /// <param name="text">The text to parse into a name.</param>
        public PersonName(string text)
        {
            if (string.IsNullOrWhiteSpace(text))
            {
                throw new ArgumentNullException(nameof(text));
            }

            // Temp value to be search/changed
            string working = text.Trim();

            // Eliminate all double-spaces.
            while (working.Contains("  "))
            {
                working = working.Replace("  ", " ");
            }

            // Initialize variables
            Prefix   = string.Empty;
            Last     = string.Empty;
            First    = string.Empty;
            Middle   = string.Empty;
            Suffix   = string.Empty;
            Nickname = string.Empty;

            // Check for nicknames (in quotes)
            if (working.Contains("\""))
            {
                int x1 = working.IndexOf("\"");
                int x2 = working.IndexOf("\"", x1 + 1);
                if (x2 > x1)
                {
                    Nickname = working.Substring(x1 + 1, x2 - x1 - 1).Trim();
                    working  = text.Replace("\"" + Nickname + "\"", string.Empty);
                }
            }
            else if (working.Contains("'"))
            {
                int x1 = working.IndexOf("'");
                int x2 = working.IndexOf("'", x1 + 1);
                if (x2 > x1)
                {
                    Nickname = working.Substring(x1 + 1, x2 - x1 - 1).Trim();
                    working  = working.Replace("'" + Nickname + "'", string.Empty);
                }
            }

            // Temp array for catching prefix
            List <string> vsP = new List <string>();

            StringBuilder m = new StringBuilder();

            // Check to see if text contains comma.
            if (working.Contains(","))
            {
                int commaInd = working.IndexOf(",");
                int spaceInd = working.IndexOf(" ");
                // If a space occurs before the comma, it's most likely formatted as [First Last, Suffix]
                if (spaceInd > 0 && spaceInd < commaInd)
                {
                    Suffix  = working.Substring(commaInd + 1).Trim();
                    working = working.Substring(0, commaInd);
                    // Check for known prefixes.
                    foreach (var prefix in Honorifics.ForParsingName)
                    {
                        if (working.ToLower().StartsWith(prefix.ToLower()))
                        {
                            vsP.Add(prefix);
                            working = working.Substring(prefix.Length).Trim();
                        }
                    }
                    Prefix = vsP.Concatenate(" ");
                    vsP.Clear();

                    var split = splitText(working).ToList();
                    if (split.Count > 2)
                    {
                        First = split.First().Trim();
                        Last  = split.Last().Trim();
                        foreach (var s in split.Skip(1).Take(split.Count - 2))
                        {
                            m.Append(" ");
                            m.Append(s);
                        }
                        Middle = m.ToString().Trim();
                    }
                    else if (split.Count == 2)
                    {
                        First = split.First().Trim();
                        Last  = split.Last().Trim();
                    }
                    else
                    {
                        Last = split[0].Trim();
                    }
                }
                else                     // If there is no space before the comma, it's most likely formatted as [Last, First]
                {
                    Last    = working.Substring(0, commaInd).Trim();
                    working = working.Substring(commaInd + 1).Trim();

                    // Check for known prefixes.
                    foreach (var prefix in Honorifics.ForParsingName)
                    {
                        if (working.ToLower().StartsWith(prefix.ToLower()))
                        {
                            vsP.Add(prefix);
                            working = working.Substring(prefix.Length).Trim();
                        }
                    }

                    Prefix = vsP.Concatenate(" ");
                    vsP.Clear();
                    // Check for additional comma. If exists, what follows is suffix
                    int index = working.IndexOf(",");
                    if (index > 0)
                    {
                        Suffix  = working.Substring(index + 1).Trim();
                        working = working.Substring(0, index).Trim();
                    }

                    if (!working.Contains(" "))
                    {
                        First = working.Trim();
                    }
                    else
                    {
                        var split = splitText(working);
                        First = split.First().Trim();
                        foreach (var s in split.Skip(1))
                        {
                            m.Append(" ");
                            m.Append(s.Trim());
                        }
                        Middle = m.ToString().Trim();
                    }
                }
            }
            else                            // No commas. Search for common suffixes and assume it's formatted as [First Last]
            {
                if (!working.Contains(" ")) // No space. It's just a last name.
                {
                    Last = working.Trim();
                }
                else
                {
                    // Check for known prefixes.
                    foreach (var prefix in Honorifics.ForParsingName)
                    {
                        if (working.ToLower().StartsWith(prefix.ToLower()))
                        {
                            vsP.Add(prefix);
                            working = working.Substring(prefix.Length).Trim();
                        }
                    }
                    Prefix = vsP.Concatenate(" ");
                    vsP.Clear();

                    var    split = splitText(working).ToList();
                    string suff  = string.Empty;
                    foreach (var s in split)
                    {
                        if (CommonSuffixes.Contains(s, StringComparer.OrdinalIgnoreCase))
                        {
                            suff = s;
                        }
                    }
                    if (!string.IsNullOrEmpty(suff))
                    {
                        split.Remove(suff);
                        Suffix = suff.Trim();
                    }
                    if (split.Count == 1)
                    {
                        Last = split.First().Trim();
                    }
                    else if (split.Count == 2)
                    {
                        First = split.First().Trim();
                        Last  = split.Last().Trim();
                    }
                    else
                    {
                        First = split.First().Trim();
                        Last  = split.Last().Trim();
                        foreach (var s in split.Skip(1).Take(split.Count - 2))
                        {
                            m.Append(" ");
                            m.Append(s);
                        }
                        Middle = m.ToString().Trim();
                    }
                }
            }
        }