Anslatortray
anslatortray.hpp
Go to the documentation of this file.
1 
58 #ifndef ANSLATORTRAY_HPP
59 #define ANSLATORTRAY_HPP
60 
61 #define ANSLATORTRAY_VERSION 0.3.1
62 
63 
64 #if __cplusplus >= 201103L//supports C++11 and later for now
65 
66 #include <string>
67 #include <cstdint>
68 #include <algorithm>
69 #include <cctype>
70 //#include <cstring>
71 //#include <iterator>
72 
73 
78 namespace anslatortray//Definitions
79 {
80  //External Use
89  inline std::string smartWordToPig(const std::string &englishWord);
90 
98  inline std::string translate(const std::string &englishText);
99 
108  inline std::string wordToPig(const std::string &englishWord);
109 
117  inline std::string wordsToPig(const std::string &englishWords);
118 
127  inline std::string attemptWordToEnglish(const std::string &pig, std::uint64_t numBeginningConosoants = 1);
128 
130  constexpr char VOWEL_START_STYLE[] = {"way"};//sometimes "yay" is used
131 
132 
133  //Internal use
135  namespace Characters
136  {
138  namespace Letters
139  {
141  constexpr char ALL[] {"aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZ"};
142 
144  constexpr char VOWELS[] {"aAeEiIoOuU"};
146  constexpr char VOWELS_WITH_Y[] {"aAeEiIoOuUyY"};
148  constexpr char Y[] {"yY"};
149  }
150 
152  constexpr char APOSTROPHE[] {"\'"};//should also have ʼ and ’ but unicode does not play nice with std::string::find_last_of
154  constexpr char WHITESPACE[] {" \t\v\n\r\f"};
155  }
156 
165  inline std::string changeWords(const std::string &words, std::string wordChanger (const std::string &word));
166 }
167 
168 
169 //Implementations
170 namespace anslatortray
171 {
172  std::string smartWordToPig(const std::string &englishWord)
173  {
174  //find the start of the actual word in the string
175  std::string::size_type wordStartIndex {englishWord.find_first_of(Characters::Letters::ALL)};//after any beginning punctuation
176 
177  std::string::size_type wordEndIndex {englishWord.find_last_of(Characters::APOSTROPHE)};//try to find an ending apostrophe for possesion or a contraction, seperate from translation
178  if (wordEndIndex == std::string::npos)//if there is no apostrophe
179  wordEndIndex = {englishWord.find_last_of(Characters::Letters::ALL) + 1};//find the last letter in the string to use as ending
180 
181 
182  //extract it and translate, sanity checking in the process
183  std::string actualWord {};
184  if (wordStartIndex != std::string::npos && wordEndIndex != std::string::npos)//make sure word is sane
185  actualWord = {englishWord.substr(wordStartIndex, wordEndIndex - wordStartIndex)};//2nd param is count between start and end of actual word
186  else
187  return englishWord;//this sanity checking takes also care of the other englishWord.substr in the final assembly phase
188 
189  std::string pig {wordToPig(actualWord)};//translate English word
190 
191 
192  //capatilization handeling
193  if (std::all_of(std::begin(actualWord), std::end(actualWord), isupper) && actualWord != "I")//if entire original word was uppercase (except for the word "I")//fixme why no std::toupper
194  std::transform(std::begin(pig), std::end(pig), std::begin(pig), toupper);//make entire translated word uppercase
195  else
196  {
197  std::transform(std::begin(pig), std::end(pig), std::begin(pig), tolower);//make entire translated word lowercase//fixme why no std::tolower
198 
199  if (std::isupper(actualWord[0]))//if original word had an uppercase first letter
200  pig[0] = {static_cast<char> (std::toupper(pig[0]))};//new word should have uppercase first letter; have to cast int to char
201  }
202 
203 
204  //prefix punctuation + pigified word + suffix punctuation
205  std::string result {englishWord.substr(0, wordStartIndex)};//up to the start of the word
206  result += {pig};//translated word from earlier
207  result += {englishWord.substr(wordEndIndex)};//from end of the word to the end of the string
208  return result;
209  }
210 
211  std::string translate(const std::string &englishText)
212  {
213  return changeWords(englishText, smartWordToPig);
214  }
215 
216  std::string wordToPig(const std::string &englishWord)
217  {
218  const std::string::size_type firstVowel {englishWord.find_first_of(Characters::Letters::VOWELS)};//fixme y being a vowel depends on word
219 
220  if (firstVowel != std::string::npos)//basic sanity checking
221  {
222  if (firstVowel == 0)//word starts with vowel
223  return englishWord + VOWEL_START_STYLE;//just add "way" (or something else)
224  else
225  {
226  //word without beginning consononts + beginning consononts + "ay"
227  std::string result {englishWord.substr(firstVowel)};
228  result += englishWord.substr(0, firstVowel);
229  result += "ay";
230 
231  return result;
232  }
233  }
234 
235  return englishWord;
236  }
237 
238  std::string wordsToPig(const std::string &englishWords)
239  {
240  return changeWords(englishWords, wordToPig);
241  }
242 
243  std::string attemptWordToEnglish(const std::string &pig, std::uint64_t numBeginningConosoants)
244  {
245  std::string noAy {pig.substr(0, pig.size() - 2)};//try to take off ay
246 
247  std::string withoutBeginningConosoants {noAy.substr(0, noAy.size() - numBeginningConosoants)};//take rest of word from front
248  std::string beginningConosoants {noAy.substr(noAy.size() - numBeginningConosoants)};//take beginning conosoants from the end
249 
250  return beginningConosoants + withoutBeginningConosoants;//put back in proper order
251  }
252 
253  std::string changeWords(const std::string &words, std::string wordChanger (const std::string &word))
254  {
255  std::string newWords {""};
256  std::string word {""};
257 
258  std::string::size_type tokenStartIndex {words.find_first_not_of(Characters::WHITESPACE)};
259  std::string::size_type tokenEndIndex {words.find_first_of(Characters::WHITESPACE, tokenStartIndex)};
260 
261  while (tokenStartIndex != std::string::npos)//no more things to tokenize
262  {
263  //tokenize
264  if (tokenEndIndex == std::string::npos)//if there is no more white space (last token in string)
265  word = {words.substr(tokenStartIndex)};//tokenize from last whitespace to end of string
266  else
267  word = {words.substr(tokenStartIndex, tokenEndIndex)};//tokenize between start of token the and next found whitespace
268 
269 
270  //preform wordChanger on each word and add space in between
271  newWords += {wordChanger(word)};
272  newWords += {" "};
273 
274 
275  //find placement of next token
276  tokenStartIndex = {words.find_first_not_of(Characters::WHITESPACE, tokenEndIndex)};//find the next start of a token after whitespace
277  tokenEndIndex = {words.find_first_of(Characters::WHITESPACE, tokenStartIndex)};//fin the next whitespace after start of token
278  }
279 
280  return newWords;
281 
282  //probably best way of doing it (if it worked)
283  //std::transform(std::istream_iterator<std::string> {wordStream}, {}, std::begin(pigWords), [](std::string word){return wordToPig(word);});
284  }
285 }
286 
287 #else
288 #error Sorry, but for the moment, Anslatortray only supports for C++11 and later. For now, please change your compiliation flags accordinaly
289 #endif
290 
291 #endif //ANSLATORTRAY_HPP
constexpr char WHITESPACE[]
Array containing diffrent types of whitespace (internal use)
Definition: anslatortray.hpp:154
constexpr char ALL[]
Array containing all upper and lower case letters (internal use)
Definition: anslatortray.hpp:141
std::string wordsToPig(const std::string &englishWords)
Uses wordToPig and changeWords to perform dumb translation from English to Pig Latin on every word it...
Definition: anslatortray.hpp:238
constexpr char VOWELS_WITH_Y[]
Array containing all upper and lower case vowels (including y) (internal use)
Definition: anslatortray.hpp:146
std::string wordToPig(const std::string &englishWord)
Translates a single English word to Pig Latin.
Definition: anslatortray.hpp:216
std::string changeWords(const std::string &words, std::string wordChanger(const std::string &word))
Helper function to perform an operation on all whitespace-seperated words (tokens) given to it...
Definition: anslatortray.hpp:253
constexpr char Y[]
Array containing upper and lower case y (internal use)
Definition: anslatortray.hpp:148
std::string smartWordToPig(const std::string &englishWord)
Translates a single complex English word to Pig Latin. (more robust)
Definition: anslatortray.hpp:172
Namespace containing functions to translate from English to Pig Latin.
constexpr char VOWELS[]
Array containing all upper and lower case vowels (except for y) (internal use)
Definition: anslatortray.hpp:144
constexpr char VOWEL_START_STYLE[]
Ending to use if word to translate starts with a vowel.
Definition: anslatortray.hpp:130
constexpr char APOSTROPHE[]
Array containing diffrent apostrophes (internal use)
Definition: anslatortray.hpp:152
std::string attemptWordToEnglish(const std::string &pig, std::uint64_t numBeginningConosoants=1)
Tries to translate a word in Pig Latin back to english.
Definition: anslatortray.hpp:243
std::string translate(const std::string &englishText)
Uses smartWordToPig and changeWords to perform translation from English to Pig Latin on every word it...
Definition: anslatortray.hpp:211