Anslatortray
anslatortray.hpp
Go to the documentation of this file.
1 /* Anslatortray
2  *
3  * A simple, header-only library to translate from English to Pig Latin.
4  *
5  * MIT License
6  *
7  * Copyright (c) 2018 John Jekel
8  *
9  * Permission is hereby granted, free of charge, to any person obtaining a copy
10  * of this software and associated documentation files (the "Software"), to deal
11  * in the Software without restriction, including without limitation the rights
12  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13  * copies of the Software, and to permit persons to whom the Software is
14  * furnished to do so, subject to the following conditions:
15 
16  * The above copyright notice and this permission notice shall be included in all
17  * copies or substantial portions of the Software.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25  * SOFTWARE.
26 */
27 
40 #ifndef ANSLATORTRAY_HPP
41 #define ANSLATORTRAY_HPP
42 
43 #include <string>
44 #include <sstream>
45 #include <cstdint>
46 #include <iterator>
47 #include <algorithm>
48 #include <cctype>
49 
50 #include <cstring>
51 
56 namespace anslatortray
57 {
65 inline std::string smartWordToPig(const std::string &englishWord);
66 
74 inline std::string translate(const std::string &englishText);
75 
84 inline std::string wordToPig(const std::string &englishWord);
85 
86 //inline constexpr char *wordToPig(char *englishWord);
87 #if __cplusplus >= 201703L
88 inline std::string_view wordToPigSV(std::string_view englishWord);
89 #endif
90 
98 inline std::string wordsToPig(const std::string &englishWords);
99 
108 inline std::string attemptWordToEnglish(const std::string &pig, std::uint64_t beginningVowels = 1);
109 
118 inline std::string changeWords(const std::string &words, std::string wordChanger (const std::string &word));
119 
120 
121 namespace Characters
122 {
123 namespace Letters
124 {
126 constexpr char ALL[] {"aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZ"};
127 
129 constexpr char VOWELS[] {"aAeEiIoOuU"};
131 constexpr char Y[] {"yY"};
133 constexpr char VOWELS_WITH_Y[] {"aAeEiIoOuUyY"};
134 }
135 }
136 }
137 
138 namespace anslatortray
139 {
140 std::string wordToPig(const std::string &englishWord)
141 {
142  std::string::size_type result {englishWord.find_first_of(Characters::Letters::VOWELS_WITH_Y)};//fixme depends on word
143 
144  if (result != std::string::npos)
145  {
146  if (result == 0)
147  return englishWord + "way";
148  else
149  {
150  //word without prefix + word until 1st vowel + "ay"
151  std::string finished {englishWord.substr(result)};
152  finished += englishWord.substr(0, result);
153  finished += "ay";
154 
155  return finished;
156  }
157  }
158  else
159  return englishWord;
160 }
161 
162 /*
163 constexpr char *wordToPig(char *englishWord)
164 {
165  auto wordSize {std::strlen(englishWord)};
166 
167  auto result {std::find_first_of(englishWord[0], englishWord[wordSize], Characters::Letters::VOWELS_WITH_Y[0], Characters::Letters::VOWELS_WITH_Y[std::strlen(Characters::Letters::VOWELS_WITH_Y)])};
168 
169  std::cout << result << std::endl;
170 
171  if (result != '\n')
172  {
173  if (result == 0)
174  return std::strcat(englishWord, "way");
175  else
176  {
177  //std::string noPrefix {englishWord.substr(result)};
178 
179  //std::string suffix {englishWord.substr(0, result)};
180 
181  //suffix += {"ay"};
182 
183  //std::string finished {noPrefix + suffix};
184 
185  //std::transform(std::begin(finished), std::end(finished), std::begin(finished), tolower);
186 
187 
188 
189  return "not done";
190  }
191  }
192 
193  return englishWord;
194 }
195 */
196 
197 std::string smartWordToPig(const std::string &englishWord)
198 {
199  std::string::size_type wordStartIndex {englishWord.find_first_of(Characters::Letters::ALL)};
200 
201  std::string::size_type wordEndIndex {englishWord.find('\'')};
202  if (wordEndIndex == std::string::npos)
203  wordEndIndex = {englishWord.find_last_of(Characters::Letters::ALL) + 1};
204 
205 
206  std::string pig {wordToPig(englishWord.substr(wordStartIndex, wordEndIndex - wordStartIndex))};//2nd param is count between start and end
207  std::transform(std::begin(pig), std::end(pig), std::begin(pig), tolower);//make all letters in new word lower for now//fixme why no std::tolower
208  if (std::isupper(englishWord.substr(wordStartIndex, wordEndIndex - wordStartIndex)[0]))//if original word had capital
209  pig[0] = {static_cast<char> (std::toupper(pig[0]))};//new word should have capital
210 
211 
212  //prefix punctuation + pigified word + suffix punctuation
213  std::string finished {englishWord.substr(0, wordStartIndex)};
214  finished += pig;
215  finished += englishWord.substr(wordEndIndex);
216 
217  return finished;
218 }
219 
220 std::string changeWords(const std::string &words, std::string wordChanger (const std::string &word))
221 {
222  std::stringstream wordStream {words};
223  std::string pigWords {""};
224 
225  //std::transform(std::istream_iterator<std::string> {wordStream}, {}, std::begin(pigWords), [](std::string word){return wordToPig(word);});
226 
227  std::string word {""};
228 
229  while (wordStream >> word)
230  {
231  pigWords += wordChanger(word);
232  pigWords += " ";
233  }
234 
235  /*
236  for (std::string &word : wordStream)
237  {
238  pigWords += wordToPig(word);
239  }
240  */
241 
242  return pigWords;
243 }
244 
245 std::string wordsToPig(const std::string &englishWords)
246 {
247  return changeWords(englishWords, wordToPig);
248 }
249 
250 std::string translate(const std::string &englishText)
251 {
252  return changeWords(englishText, smartWordToPig);
253 }
254 
255 std::string attemptWordToEnglish(const std::string &pig, std::uint64_t beginningVowels)
256 {
257  std::string noAy {pig.substr(0, pig.size() - 2)};//try to take of ay
258 
259  std::string noPrefix {noAy.substr(0, noAy.size() - beginningVowels)};
260  std::string prefix {noAy.substr(noAy.size() - beginningVowels)};
261 
262  return prefix + noPrefix;
263 }
264 
265 #if __cplusplus >= 201703L
266 #endif
267 }
268 
269 #endif // ANSLATORTRAY_H
constexpr char ALL[]
Definition: anslatortray.hpp:126
std::string attemptWordToEnglish(const std::string &pig, std::uint64_t beginningVowels=1)
Tries to translate a word in pig latin back to english.
Definition: anslatortray.hpp:255
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:245
constexpr char VOWELS_WITH_Y[]
Definition: anslatortray.hpp:133
std::string wordToPig(const std::string &englishWord)
Translates a single English word to pig latin.
Definition: anslatortray.hpp:140
std::string changeWords(const std::string &words, std::string wordChanger(const std::string &word))
Helper function to perform an operation on all whitespace-seperated strings given to it...
Definition: anslatortray.hpp:220
constexpr char Y[]
Definition: anslatortray.hpp:131
std::string smartWordToPig(const std::string &englishWord)
Translates a single complex English word to pig latin. (more robust)
Definition: anslatortray.hpp:197
Namespace containing functions to translate from English to Pig Latin.
constexpr char VOWELS[]
Definition: anslatortray.hpp:129
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:250