std::string human_format::comma_format(std::string name) { // Split on commas. If there are no commas, return. std::deque < std::string > split_string = split_parts(name, ","); if(split_string.size() < 2) { return name; } std::string output; std::string holding; // Comma formatting while(split_string.size() > 0) { unsigned int split_size = (split_string.size() - 1); if(match_component(split_string[split_size], suffixes)) { if(output.size() == 0) { output.append(split_string[split_size]); } else { output.append(" " + split_string[split_size]); } } else { if(output.size() == 0) { } holding.append(split_string[split_size]); } split_string.pop_back(); } if(holding.size() > 0) { output = holding + output; } return output; }
std::vector < std::string > human_parse::parse_single(std::string name){ // If it's literally just an empty string, throw things. if(name.size() == 0){ throw std::range_error("You have provided an empty string"); } // Split and create output object. std::deque < std::string > split_name = split_parts(name, " "); std::vector < std::string > output(5); // If there's only one element we assume it is a first name and return it. if(split_name.size() == 1){ output[1] = split_name[0]; return output; } // If there are > 1 element and we find a salutation in the first, pop those two elements. if(split_name.size() > 1 && match_component(split_name[0], salutations)){ output[0] = split_name[0]; split_name.pop_front(); output[1] = split_name[0]; split_name.pop_front(); } else { output[1] = split_name[0]; split_name.pop_front(); } // If there's still > 1 elemenent, what about suffixes? if(split_name.size() > 1){ while(split_name.size() > 1 && match_component(split_name[split_name.size() - 1], suffixes)){ if(output[4] == ""){ output[4] = split_name[split_name.size() - 1]; } else { output[4] = split_name[split_name.size() - 1] + " " + output[4]; } split_name.pop_back(); } } // If there's only >1 element, surnames if(split_name.size() > 0){ output[3] = split_name[split_name.size() - 1]; split_name.pop_back(); } else { return output; } // If there is still 1 or more elements we test for compounds while(split_name.size() > 0 && match_component(split_name[split_name.size() - 1], compounds)){ output[3] = split_name[split_name.size() - 1] + " " + output[3]; split_name.pop_back(); } // If we still have elements, those are middle names. if(split_name.size() > 0){ output[2].append(split_name[0]); for(unsigned int i = 1; i < split_name.size(); i++){ output[2].append(" " + split_name[i]); } } return output; }