Advertisement
Guest User

Untitled

a guest
May 11th, 2013
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.95 KB | None | 0 0
  1. //translator.h
  2.  
  3. #ifndef TRANSLATOR_H
  4. #define TRANSLATOR_H
  5.  
  6.  
  7. #include <string>
  8. #include <cstring>
  9.  
  10.  
  11. const int MAXLINE=1000;
  12. const int MAXWORDS=4000;
  13.  
  14. class dict
  15. {
  16. public:
  17. dict (const char dictFileName[]);
  18. string lookup_english(const string inputword);
  19. string lookup_elvish(const string inputword);
  20. private:
  21. string english_dict[MAXWORDS];
  22. string elvish_dict [MAXWORDS];
  23.  
  24. };
  25.  
  26.  
  27.  
  28. class Translator
  29. {
  30. public:
  31. Translator(const char s[]);
  32. void toElvish(char * outputline, const string inputline);
  33. void toEnglish(char * outputline, const string inputline);
  34.  
  35. private:
  36. dict dictionary;
  37.  
  38. };
  39.  
  40. #endif
  41.  
  42. //main.cpp
  43.  
  44. #include <iostream>
  45. #include <fstream>
  46. #include "Translator.h"
  47.  
  48. using namespace std;
  49.  
  50. // maximum number of characters in a line of the text
  51. const int MAXLINE=1000;
  52. int main(int argc, char *argv[])
  53. {
  54. if (argc<2)
  55. {
  56. cout << "No story file specified." << endl;
  57. return -1;
  58. }
  59.  
  60. fstream infile;
  61. infile.open(argv[1], ios::in);
  62.  
  63. if (infile.fail())
  64. {
  65. cout << "Could not open the story file." << endl;
  66. return -1;
  67. }
  68.  
  69. Translator translator("englishtoelvish.txt");
  70. fstream outfile;
  71. outfile.open("story_in_elvish.txt", ios::out);
  72. if (outfile.fail())
  73. {
  74. cout << "Could not open the output file." << endl;
  75. return -1;
  76. }
  77.  
  78. char english_line[MAXLINE], elvish_line[MAXLINE];
  79.  
  80. // Translate the story into Elvish
  81. while (!infile.fail())
  82. {
  83. infile.getline(english_line, MAXLINE, '\n');
  84. if (!infile.fail())
  85. {
  86. translator.toElvish(elvish_line, english_line);
  87. outfile << elvish_line << endl;
  88. }
  89. }
  90. outfile.close();
  91. infile.close();
  92.  
  93. // Read the translated story and re-translate into English
  94. infile.open("story_in_elvish.txt", ios::in);
  95. outfile.open("story_backto_english.txt", ios::out);
  96. while (!infile.eof())
  97. {
  98. infile.getline(elvish_line, MAXLINE, '\n');
  99. if (!infile.fail())
  100. {
  101. translator.toEnglish(english_line, elvish_line);
  102. outfile << english_line << endl;
  103. }
  104. }
  105.  
  106. infile.close();
  107. outfile.close();
  108. }
  109.  
  110. //translator.cpp
  111.  
  112. Translator::Translator(const char dictFileName[]) : dictionary(dictFileName)
  113. {
  114. }
  115.  
  116. void Translator::toElvish(char * outputline, const std::string inputline)
  117. {
  118.  
  119. string inputword;
  120. string outputstore;
  121. string outputword;
  122. int i=0;
  123. while(i<inputline.length())
  124. {
  125. while(isalpha(inputline[i]) || inputline[i]=='-' || inputline[i]=='*')
  126. {
  127. inputword+=inputline[i];
  128. i++;
  129.  
  130. }
  131.  
  132.  
  133. if(inputword[0]>='A' && inputword[0]<='Z')
  134. {
  135. inputword[0]=tolower(inputword[0]);
  136. outputword=dictionary.lookup_english(inputword);
  137. if(outputword[0]=='*')
  138. {
  139. outputword[1]=toupper(outputword[1]);
  140. }
  141. else
  142. {
  143. outputword[0]=toupper(outputword[0]);
  144. }
  145.  
  146. outputstore+=outputword;
  147. }
  148. else
  149. {
  150. outputstore+=dictionary.lookup_english(inputword);
  151. }
  152.  
  153. inputword.clear();
  154.  
  155.  
  156. while(!(isalpha(inputline[i])) && !(inputline[i]=='-') && !(inputline[i]=='*'))
  157. {
  158. outputstore+=inputline[i];
  159. i++;
  160. }
  161.  
  162.  
  163.  
  164.  
  165. }
  166.  
  167. strcpy(outputline, outputstore.c_str());
  168.  
  169. }
  170.  
  171.  
  172. void Translator::toEnglish(char * outputline, const std::string inputline)
  173. {
  174.  
  175. string inputword;
  176. string outputword;
  177. string outputstore;
  178. int i=0;
  179.  
  180. while(i<inputline.length())
  181. {
  182. while(isalpha(inputline[i]) || inputline[i]=='-' || inputline[i]=='*')
  183. {
  184. inputword+=inputline[i];
  185. i++;
  186. }
  187.  
  188. if(inputword[0]=='*')
  189. {
  190. inputword.erase(inputword.begin());
  191. inputword = inputword.substr(0, inputword.size()-1);
  192. outputstore+=inputword;
  193. }
  194. else
  195. if(inputword[0]>='A' && inputword[0]<='Z')
  196. {
  197. inputword[0]=tolower(inputword[0]);
  198. outputword=dictionary.lookup_elvish(inputword);
  199. outputword[0]=toupper(outputword[0]);
  200. outputstore+=outputword;
  201. }
  202. else
  203. {
  204. outputstore+=dictionary.lookup_elvish(inputword);
  205. }
  206.  
  207. inputword.clear();
  208.  
  209.  
  210. while(!(isalpha(inputline[i])) && !(inputline[i]=='-') && !(inputline[i]=='*'))
  211. {
  212. outputstore+=inputline[i];
  213. i++;
  214. }
  215.  
  216.  
  217.  
  218.  
  219. }
  220.  
  221. strcpy(outputline, outputstore.c_str());
  222.  
  223.  
  224. }
  225.  
  226. dict::dict(const char dictFileName[])
  227. {
  228. fstream str;
  229. str.open(dictFileName, ios::in);
  230.  
  231.  
  232.  
  233. int i=0;
  234. std::string wordin_english;
  235. std::string wordin_elvish;
  236. while (!str.eof())
  237. {
  238. str >> wordin_english;
  239. while(!english_dict[i].empty())
  240. i++;
  241. english_dict[i]=wordin_english;
  242.  
  243.  
  244. str >> wordin_elvish;
  245.  
  246. elvish_dict[i]=wordin_elvish;
  247.  
  248. }
  249. }
  250.  
  251.  
  252. string dict::lookup_english(const std::string inputword)
  253. {
  254. string wordout;
  255. int i=0;
  256. while(!(inputword==english_dict[i]))
  257. {
  258. i++;
  259. if(i>1748)
  260. break;
  261. }
  262.  
  263. if(i>1748)
  264. {
  265. wordout+="*";
  266. wordout+=inputword;
  267. wordout+="*";
  268. return wordout;
  269. }
  270. else
  271. {
  272. return elvish_dict[i];
  273. }
  274.  
  275.  
  276. }
  277.  
  278.  
  279. string dict::lookup_elvish(const std::string inputword)
  280. {
  281. string wordout;
  282. int i=0;
  283. while(!(inputword==elvish_dict[i]))
  284. {
  285. i++;
  286. if(i>1748)
  287. break;
  288. }
  289.  
  290. if(i>1748)
  291. {
  292. wordout+="*";
  293. wordout+=inputword;
  294. wordout+="*";
  295. return wordout;
  296. }
  297. else
  298. {
  299. return english_dict[i];
  300. }
  301.  
  302. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement