Anslatortray
anslatortray.hpp
Go to the documentation of this file.
1 
58 #ifndef ANSLATORTRAY_HPP
59 #define ANSLATORTRAY_HPP
60 
61 #if __cplusplus >= 201103L//supports C++11 and later for now
62 
63 
64 /* Headers */
65 #include <string>
66 #include <cstdint>
67 #include <algorithm>
68 #include <cctype>
69 //#include <cstring>
70 //#include <iterator>
71 
72 
73 #define ANSLATORTRAY_VERSION 0.3.3
74 
79 namespace anslatortray//Definitions
80 {
81  /* Definitions */
82 
83  /* Behavior altering definitions (safe to change) */
84 
85  //decent speed boost sacrifising replication of capital letters in translated word in smartWordToPig; recomended based on needs
86  //#define ANSLATORTRAY_IGNORE_CAPS
87 
88  //extremly small speed boost sacrifising sanity checking; words without vowels/letters will cause problems; not recomended unless speed despratly needed needed
89  //#define ANSLATORTRAY_SKIP_SANITY_CHECKS
90 
91  //disables uncommonly used functions and other unnecerary things to reduce memory/binary footprints; does little that is actually worth it
92  //#define ANSLATORTRAY_TINY_TRANSLATOR
93 
95  constexpr char VOWEL_START_STYLE[] = {"way"};//sometimes "yay" is used
96 
97 
98  /* External use definitions */
107  inline std::string smartWordToPig(const std::string &englishWord);
108 
116  inline std::string translate(const std::string &englishText);
117 
126  inline std::string wordToPig(const std::string &englishWord);
127 
128  #ifndef ANSLATORTRAY_TINY_TRANSLATOR
129 
137  inline std::string wordsToPig(const std::string &englishWords);
138  #endif
139 
140  #ifndef ANSLATORTRAY_TINY_TRANSLATOR
141 
150  inline std::string attemptWordToEnglish(const std::string &pig, std::uint64_t numBeginningConosoants = 1);
151  #endif
152 
153 
154  /* Internal use definitions */
156  namespace Characters
157  {
159  namespace Letters
160  {
162  constexpr char ALL[] {"aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZ"};
163 
165  constexpr char VOWELS[] {"aAeEiIoOuU"};
166 
167  #ifndef ANSLATORTRAY_TINY_TRANSLATOR
168 
169  constexpr char VOWELS_WITH_Y[] {"aAeEiIoOuUyY"};
171  constexpr char Y[] {"yY"};
172  #endif
173  }
174 
176  constexpr char APOSTROPHE[] {"\'"};//should also have ʼ and ’ but unicode does not play nice with std::string::find_last_of
178  constexpr char WHITESPACE[] {" \t\v\n\r\f"};
179  }
180 
189  inline std::string changeWords(const std::string &words, std::string wordChanger (const std::string &word));
190 }
191 
192 
193 /* Implementations */
194 namespace anslatortray
195 {
196  std::string smartWordToPig(const std::string &englishWord)
197  {
198  //find the start of the actual word in the string
199  std::string::size_type wordStartIndex {englishWord.find_first_of(Characters::Letters::ALL)};//after any beginning punctuation
200 
201  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
202  if (wordEndIndex == std::string::npos)//if there is no apostrophe
203  wordEndIndex = {englishWord.find_last_of(Characters::Letters::ALL) + 1};//find the last letter in the string to use as ending
204 
205 
206  //sanity check
207  #ifndef ANSLATORTRAY_SKIP_SANITY_CHECKS
208  if (wordStartIndex == std::string::npos || wordEndIndex == std::string::npos)//make sure word indexes are sane
209  return englishWord;//if not, return original word as there are no letters
210  #endif
211 
212 
213  //extract word and translate
214  std::string actualWord {englishWord.substr(wordStartIndex, wordEndIndex - wordStartIndex)};//2nd param is count between start and end of actual word
215  std::string pig {wordToPig(actualWord)};//translate English word
216 
217 
218  //capatilization handeling
219  #ifndef ANSLATORTRAY_IGNORE_CAPS
220  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
221  std::transform(std::begin(pig), std::end(pig), std::begin(pig), toupper);//make entire translated word uppercase
222  else
223  {
224  std::transform(std::begin(pig), std::end(pig), std::begin(pig), tolower);//make entire translated word lowercase//fixme why no std::tolower
225 
226  if (std::isupper(actualWord[0]))//if original word had an uppercase first letter
227  pig[0] = {static_cast<char> (std::toupper(pig[0]))};//new word should have uppercase first letter; have to cast int to char
228  }
229  #endif
230 
231  //prefix punctuation + pigified word + suffix punctuation
232  std::string result {englishWord.substr(0, wordStartIndex)};//up to the start of the word
233  result += {pig};//translated word from earlier
234  result += {englishWord.substr(wordEndIndex)};//from end of the word to the end of the string
235  return result;
236  }
237 
238  std::string translate(const std::string &englishText)
239  {
240  return changeWords(englishText, smartWordToPig);
241  }
242 
243  std::string wordToPig(const std::string &englishWord)
244  {
245  const std::string::size_type firstVowel {englishWord.find_first_of(Characters::Letters::VOWELS)};//fixme y being a vowel depends on word
246 
247  #ifndef ANSLATORTRAY_SKIP_SANITY_CHECKS
248  if (firstVowel == std::string::npos)//basic sanity checking
249  return englishWord;
250  #endif
251 
252 
253  if (firstVowel == 0)//word starts with vowel
254  return englishWord + VOWEL_START_STYLE;//just add "way" (or something else)//note, += has little to no benifit here
255  else
256  {
257  //word without beginning consononts + beginning consononts + "ay"
258  std::string result {englishWord.substr(firstVowel)};
259  result += {englishWord.substr(0, firstVowel)};
260  result += {"ay"};
261 
262  return result;
263  }
264  }
265 
266  #ifndef ANSLATORTRAY_TINY_TRANSLATOR
267  std::string wordsToPig(const std::string &englishWords)
268  {
269  return changeWords(englishWords, wordToPig);
270  }
271  #endif
272 
273  #ifndef ANSLATORTRAY_TINY_TRANSLATOR
274  std::string attemptWordToEnglish(const std::string &pig, std::uint64_t numBeginningConosoants)
275  {
276  std::string noAy {pig.substr(0, pig.size() - 2)};//try to take off ay (2 characters)
277 
278  std::string withoutBeginningConosoants {noAy.substr(0, noAy.size() - numBeginningConosoants)};//take rest of word from front
279  std::string beginningConosoants {noAy.substr(noAy.size() - numBeginningConosoants)};//take beginning conosoants from the end
280 
281  return beginningConosoants + withoutBeginningConosoants;//put back in proper order
282  }
283  #endif
284 
285  std::string changeWords(const std::string &words, std::string wordChanger (const std::string &word))
286  {
287  std::string newWords {};
288  std::string word {};
289 
290  std::string::size_type tokenStartIndex {words.find_first_not_of(Characters::WHITESPACE)};//start of token
291  std::string::size_type tokenEndIndex {words.find_first_of(Characters::WHITESPACE, tokenStartIndex)};//first whitespace after token
292 
293  while (tokenStartIndex != std::string::npos)//no more things to tokenize
294  {
295  //tokenize
296  if (tokenEndIndex == std::string::npos)//if there is no more white space (last token in string)
297  word = {words.substr(tokenStartIndex)};//tokenize from last whitespace to end of string
298  else//tokenize between start of token the and next found whitespace (2nd param is count between the two)
299  word = {words.substr(tokenStartIndex, tokenEndIndex - tokenStartIndex)};
300 
301 
302  //preform wordChanger on each word and add space in between
303  newWords += {wordChanger(word)};
304  newWords += {" "};
305 
306 
307  //find placement of next token
308  tokenStartIndex = {words.find_first_not_of(Characters::WHITESPACE, tokenEndIndex)};//find the next start of a token after whitespace
309  tokenEndIndex = {words.find_first_of(Characters::WHITESPACE, tokenStartIndex)};//fin the next whitespace after start of token
310  }
311 
312  return newWords;
313 
314  //probably best way of doing it (if it worked)
315  //std::transform(std::istream_iterator<std::string> {wordStream}, {}, std::begin(pigWords), [](std::string word){return wordToPig(word);});
316  }
317 }
318 
319 #else
320 #error Sorry, but for the moment, Anslatortray only supports for C++11 and later. For now, please change your compiliation flags accordinaly
321 #endif
322 
323 #endif //ANSLATORTRAY_HPP
constexpr char WHITESPACE[]
Array containing diffrent types of whitespace (internal use)
Definition: anslatortray.hpp:178
constexpr char ALL[]
Array containing all upper and lower case letters (internal use)
Definition: anslatortray.hpp:162
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:267
constexpr char VOWELS_WITH_Y[]
Array containing all upper and lower case vowels (including y) (internal use)
Definition: anslatortray.hpp:169
std::string wordToPig(const std::string &englishWord)
Translates a single English word to Pig Latin.
Definition: anslatortray.hpp:243
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:285
constexpr char Y[]
Array containing upper and lower case y (internal use)
Definition: anslatortray.hpp:171
std::string smartWordToPig(const std::string &englishWord)
Translates a single complex English word to Pig Latin. (more robust)
Definition: anslatortray.hpp:196
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:165
constexpr char VOWEL_START_STYLE[]
Ending to use if word to translate starts with a vowel.
Definition: anslatortray.hpp:95
constexpr char APOSTROPHE[]
Array containing diffrent apostrophes (internal use)
Definition: anslatortray.hpp:176
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:274
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:238