// English and Mandarin are both subject-verb-object, so approximate
// literal translations can be done trivially. Example:
/*
#include "en_to_zh.h"
#include <iostream>
int main(){
std::cout
<< EnToZh( "This is an English-to-Chinese translation." )
<< std::endl;
return 0;
}
*/
#include <map>
#include <string>
#include <vector>
const std::map<std::string,std::string> translate_en_zh = {
{ " ", "" },
{ ",", ", " },
{ ".", ". " },
{ ":", ": " },
{ ";", "; " },
{ "?", "? " },
{ "!", "! " },
{ "that", "那" }, // Nà
{ "the", "这" }, // Zhè
{ "this", "这" },
{ "yes", "是" }, // Shì
{ "am", "是" },
{ "m", "是" },
{ "are", "是" },
{ "be", "是" },
{ "is", "是" },
{ "got", "是" },
{ "exist", "是" },
{ "exists", "是" },
{ "will", "是" },
{ "and", "和" }, // Hé
{ "but", "但" }, // Dàn
{ "however", "但" },
{ "yet", "但" },
{ "a", "一个" }, // Yīgè
{ "an", "一个" },
{ "i", "我" }, // Wǒ
{ "me", "我" },
{ "myself", "我" },
{ "my", "我的" },
{ "or", "或" },
{ "all", "所有" },
{ "enough", "夠" },
{ "sufficient", "夠" },
{ "suffice", "夠" },
{ "english", "英文" },
{ "to", "到" },
{ "chinese", "普通话" },
{ "mandarin", "普通话" },
{ "translation", "翻译" },
{ "number", "数字" },
{ "of", "的" },
{ "unique", "独特的" },
{ "words", "字" },
{ "keep", "保留" },
{ "reserve", "保留" },
{ "don't", "無" },
{ "none", "無" },
{ "need", "需" },
{ "only", "只" },
{ "bake", "烤" },
{ "sweet", "甜點" },
{ "sweets", "甜點" },
{ "dessert", "甜點" },
{ "limited", "有限的" },
{ "scant", "有限的" },
{ "no", "不" },
{ "not", "不" },
{ "tea", "茶" },
{ "you", "你" },
{ "yourself", "你自己" },
{ "thyself", "你自己" },
{ "your", "你的" },
{ "phrase", "短语" },
{ "cake", "蛋糕" },
{ "cave", "洞穴" },
{ "pan", "盘" },
{ "plate", "盘" },
{ "test", "测试" },
{ "testing", "测试" },
{ "base", "基地" },
{ "bases", "基地" },
{ "belong", "属于" },
{ "belongs", "属于" },
{ "us", "我们" },
{ "we", "我们" },
{ "ourselves", "我们" },
{ "each", "每个" },
{ "every", "每个" },
{ "man", "男人" },
{ "men", "男人" },
{ "person", "人" },
{ "people", "人" },
{ "should", "应该" },
{ "has", "有" },
{ "have", "有" },
{ "thing", "事物" },
{ "things", "事物" },
{ "good", "好" },
{ "dog", "狗" },
{ "hound", "猎犬" },
{ "horse", "马" },
{ "bridle", "辔" },
{ "bear", "熊" },
{ "stuffed", "塞满" },
{ "die", "死" },
{ "dead", "死个" },
{ "by", "将" },
{ "would", "将" },
{ "way", "道路" },
{ "worry", "担心" },
{ "many", "多" },
{ "more", "多" },
{ "much", "多" },
{ "numerous", "多" },
{ "about", "关于" },
{ "regarding", "关于" },
{ "thereof", "关于" },
{ "what", "什么" },
{ "its", "它的" },
{ "it's", "它是" },
{ "guard", "守卫" },
{ "guarding", "守卫" },
{ "child", "孩子" },
{ "do", "做" },
{ "with", "和" },
{ "life", "生命" },
{ "any", "任何" },
{ "whichever", "任何" },
{ "whatsoever", "任何" },
{ "random", "随机的" },
{ "knowledge", "智慧" },
{ "wisdom", "智慧" },
{ "world", "世界" },
{ "confectioner", "甜品师" },
{ "shaman", "巫" },
{ "witch", "巫" },
{ "small", "小" },
{ "little", "小" },
{ "tiny", "小" },
{ "young", "小" },
{ "few", "很少" },
{ "make", "制作" },
{ "manufacture", "制作" },
{ "in", "在" },
{ "at", "在" },
{ "can", "可以" },
{ "could", "可以" },
{ "may", "可以" },
{ "wise", "明智的" },
{ "sagely", "明智的" },
{ "sagacious", "明智的" },
{ "mushroom", "蘑菇" },
{ "mushrooms", "蘑菇" },
{ "pumpkin", "南瓜" },
{ "pumpkins", "南瓜" },
{ "squash", "南瓜" },
{ "apple", "苹果" },
{ "apples", "苹果" },
{ "orange", "橘子" },
{ "oranges", "橘子" },
{ "tangerine", "橘子" },
{ "tangerines", "橘子" },
{ "berry", "浆果" },
{ "berries", "浆果" },
{ "raw", "生的" },
{ "baked", "烤的" },
{ "cooked", "熟的" },
{ "meat", "肉" },
{ "fillet", "鱼片" },
{ "pastry", "糕点" },
{ "crust", "皮" },
{ "lard", "猪油" },
{ "wood", "木头" },
{ "timber", "木头" },
{ "basket", "篮" },
{ "baskets", "篮" },
{ "frost", "霜" },
{ "coconut", "椰子" },
{ "coconuts", "椰子" },
{ "feather", "羽毛" },
{ "feathers", "羽毛" },
{ "plumage", "羽毛" },
{ "ostrich", "鸵鸟" },
{ "ostriches", "鸵鸟" },
{ "egg", "蛋" },
{ "eggs", "蛋" },
{ "warrior", "战士" },
{ "warriors", "战士们" },
{ "soldier", "士兵" },
{ "soldiers", "士兵" },
{ "campfire", "营火" },
{ "stone", "石" },
{ "stones", "石" },
{ "grind", "研磨" },
{ "grinding", "研磨" },
{ "mill", "磨" },
{ "milling", "磨" },
{ "magic", "魔" },
{ "wolf", "狼" },
{ "wolves", "狼" },
{ "wolfish", "狼" },
{ "gold", "金" },
{ "golden", "金" },
{ "aurum", "金" },
{ "knife", "刀" },
{ "knives", "刀" },
{ "obsidian", "黑曜石" },
{ "dragonglass", "黑曜石" },
{ "guardian", "监护人" },
{ "guardians", "监护人" },
{ "yeti", "雪人" },
{ "yetis", "雪人" },
{ "rose", "玫瑰" },
{ "roses", "玫瑰花" },
{ "sentry", "哨兵" },
{ "sentinel", "哨兵" },
{ "bush", "衬套" },
{ "frostbush", "霜衬套" },
{ "flame", "焰" },
{ "eagle", "鹰" },
{ "hawk", "鹰" },
{ "ancestor", "祖先" },
{ "goat", "山羊" },
{ "goats", "山羊" },
{ "ram", "公羊" },
{ "horn", "角" },
{ "clay", "黏土" },
{ "milk", "牛奶" },
{ "eggnog", "蛋酒" },
{ "custard", "卡仕达酱" },
{ "flour", "面粉" },
{ "batter", "面糊" },
{ "grain", "粮食" },
{ "pepper", "胡椒" },
{ "peppers", "胡椒" },
{ "habanero", "哈瓦那人" },
{ "ghost", "鬼" },
{ "split", "分裂" },
{ "fire", "火" },
{ "kit", "套件" },
{ "roasted", "烤" },
{ "crispbread", "薄脆" },
{ "tack", "硬面团" },
{ "almost", "差不多" },
{ "spice", "香草" },
{ "can't", "不能" },
{ "cannot", "不能" },
{ "rest", "休息" },
{ "here", "这里" },
{ "continue", "继续" },
{ "empty", "空的" },
{ "auto", "自动的" },
{ "automatic", "自动的" },
{ "save", "保存" },
{ "give", "给" },
{ "talk", "谈谈" },
{ "want", "想要的" },
{ "that's", "那是" },
{ "brilliant", "杰出的" },
{ "now", "现在" },
{ "new", "新" },
{ "formula", "配方" },
{ "recipe", "配方" },
{ "winnerberry", "优胜者莓" }, // Not a word, I know.
{ "one", "一" },
{ "two", "两个" },
{ "three", "三个" },
{ "four", "四个" },
{ "five", "五个" },
{ "six", "六个" },
{ "seven", "七个" },
{ "eight", "八个" },
{ "nine", "九个" },
{ "ten", "十个" }
};
inline std::string EnToZh( const std::string &str ){
std::vector<std::string> tokens;
std::string t_new;
for( char c : str ){
if((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '\''){
// Alphabetical characters are added to the current token.
t_new.push_back( c );
}else{
// Non-alphabetical characters get their own tokens.
tokens.push_back( t_new );
t_new = "";
tokens.push_back( std::string( 1, c ) );
}
}
if( t_new.length() > 0 ) tokens.push_back( t_new );
std::string result;
for( std::string &t : tokens ){
// Lowercase the token for searching.
std::string t_lower;
for( char c : t ){
t_lower.push_back(
( c >= 'A' && c <= 'Z' ) ? c - 'A' + 'a' : c
);
}
auto t_it = translate_en_zh.find( t_lower );
if( t_it == translate_en_zh.end() ){
// No match found. Use the original token.
result += t;
// Add a space if the token is a word.
if( t.length() > 1 ) result += " ";
}else{
// Match found.
result += t_it->second;
}
}
return result;
}