Skip to content

Instantly share code, notes, and snippets.

@dvtate
Created May 28, 2016 23:02
Show Gist options
  • Save dvtate/07837e4cd49feff1372a914dfbce6bfb to your computer and use it in GitHub Desktop.
Save dvtate/07837e4cd49feff1372a914dfbce6bfb to your computer and use it in GitHub Desktop.
#include<iostream>
#include<cstdlib>
#include<sstream> //std::stringstream
#include<string> //str.length(), npos etc.
#include<algorithm> //replace()
#include<boost/algorithm/string/replace.hpp> //boost::replace_all()
//custom functions:
int countSubstring(const std::string& str, const std::string& sub);
template <typename T>//copied from internet (to_string() not working)
std::string NumberToString(T Number){
std::stringstream ss;
ss << Number;
return ss.str();
}
//Toki Pona functions:
int texttonumber(std::string text);
std::string numbertotext(int num);
std::string texttorunes(std::string text);
std::string runestotext(std::string text);
int usernum;
std::string paragraph = "telo li anpa tan pimeja walo suli\
mi pilin e waso lon poka mi\
waso li tawa kon li tawa ma luka luka weka wan\
mi kepeken e nimi kepeken ala kalama\
waso li alasa e kon mi\
\
waso li tawa anpa tawa ma anpa\
li tawa kon lon sitelen lape mi\
li tawa sewi e mi. mi lape.\
waso li tawa e mi tan sijelo mi.\
mi weka e ni.\
\
mi mute li tawa kon lon sewi nena ma\
lon telo wawa.\
mi lukin e kasi suli ni:\
kasi li tawa wawa tan wawa pi kon en telo\
ma tomo li lape kiwen\
\
jan pi wawa kon li pana e luka tawa ni:\
jan li pana e nimi toki tawa jan pi oko wan.\
mi lukin e tenpo pini e tenpo ni \
e tenpo kama sama wan sama tenpo pi ala uta.\
\
mi ken lukin e ma telo pi wawa kon \
e nasin pi sona ala lon sijelo mi.\
jan pi kiwen en kasi li tawa musi lon sike mi.\
tenpo ale la ni li sitelen lape anu seme?";
int main(){
for(int i = 0; i < 30; i++)
std::cout <<numbertotext(i) <<" = " <<texttonumber(numbertotext(i)) <<std::endl;
std::cout <<"\n\nEnter a number:";
std::cin >>usernum;
std::cout <<numbertotext(usernum) <<std::endl;
std::cout <<"\n\nmi mute li tawa = " <<texttorunes("mi mute li tawa") <<std::endl;
std::cout <<texttorunes("mi mute li tawa") <<" = " <<runestotext(texttorunes("mi mute li tawa")) <<std::endl;
std::cout <<texttorunes(paragraph);
}
int texttonumber(std::string text){
int value = 0;
value += 100 * countSubstring(text, "ala");
value += 20 * countSubstring(text, "mute");
value += 5 * countSubstring(text, "luka");
value += 2 * countSubstring(text, "tu");
value += 1 * countSubstring(text, "wan");
return value;
}
std::string numbertotext(int num){//convert Toki Pona numbers into Toki Pona text
if (num < 0)
return "Toki Pona doesn't use negative numbers";
else if (num == 0)
return "0 = ali";
std::string text = NumberToString(num) + " = ";
while (num > 0) {
if (num >= 100) {
text += "ala ";
num -= 100;
} else if (num >= 20) {
text += "mute ";
num -= 20;
} else if (num >= 5) {
text += "luka ";
num -= 5;
} else if (num >= 2) {
text += "tu ";
num -= 2;
} else if (num == 1) {
text += "wan ";
num -= 1;
}
}
return text;
}
std::string texttorunes(std::string text){
boost::replace_all(text, "p", "ᛈ");
boost::replace_all(text, "t", "ᛏ");
boost::replace_all(text, "k", "ᚲ");
boost::replace_all(text, "s", "ᛊ");
boost::replace_all(text, "m", "ᛗ");
boost::replace_all(text, "n", "ᚾ");
boost::replace_all(text, "l", "ᛚ");
boost::replace_all(text, "j", "ᛃ");
boost::replace_all(text, "w", "ᚹ");
boost::replace_all(text, "a", "ᚨ");
boost::replace_all(text, "e", "ᛖ");
boost::replace_all(text, "i", "ᛁ");
boost::replace_all(text, "o", "ᛟ");
boost::replace_all(text, "u", "ᚢ");
return text;
}
std::string runestotext(std::string text){
boost::replace_all(text, "ᛈ", "p");
boost::replace_all(text, "ᛏ", "t");
boost::replace_all(text, "ᚲ", "k");
boost::replace_all(text, "ᛊ", "s");
boost::replace_all(text, "ᛗ", "m");
boost::replace_all(text, "ᚾ", "n");
boost::replace_all(text, "ᛚ", "l");
boost::replace_all(text, "ᛃ", "j");
boost::replace_all(text, "ᚹ", "w");
boost::replace_all(text, "ᚨ", "a");
boost::replace_all(text, "ᛖ", "e");
boost::replace_all(text, "ᛁ", "i");
boost::replace_all(text, "ᛟ", "o");
boost::replace_all(text, "ᚢ", "u");
return text;
}
int countSubstring(const std::string& str, const std::string& sub){
if (sub.length() == 0)
return 0;
int count = 0;
for(size_t offset = str.find(sub); offset != std::string::npos;
offset = str.find(sub,offset + sub.length())) ++count;
return count;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment