Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "translator.h"
- #include <iostream>
- using namespace std;
- #include <fstream>
- #include <cstring>
- #include <string>
- #include <algorithm>
- Dictionary::Dictionary(const char dictFileName[])
- {
- fstream str;
- str.open(dictFileName, ios::in);
- int i=0;
- while (!str.eof())
- {
- str >> englishWord[i];// Here says take first word slot into english array
- str >> elvishWord[i];
- i++;
- }
- numEntries=i;
- }
- Translator::Translator(const char dictFileName[]) : dict(dictFileName)// This is the constructor with an initalizer list
- {
- }
- void Translator::toElvish(char out_s[MAX_WORD_LEN], const char s[MAX_WORD_LEN])
- {
- char s_edit[MAX_WORD_LEN] = {'\0'};
- strcpy(s_edit,s);
- int len = strlen(s_edit);
- s_edit[len] = '\0';
- int i=0;
- do{s_edit[i] ='\0';i++;}while(i<MAX_WORD_LEN);
- char temp[MAX_WORD_LEN] = {'\0'};
- char returned[MAX_WORD_LEN] = {'\0'};
- string str2;
- for (int a = 0; a < len; a++)
- {
- // if it has punctuation should put it into the outer array
- if(s[a]!=' ')
- {
- int b = 0;
- /* if(temp[b] ='.'||','||'!')
- { temp[b] = '.'||','||'!';
- }
- b = 0;*/
- while(s[a] != ' '&& s[a] != ','&& s[a] !='.' && s[a]!='-' && s[a]!='!' && s[a]!='?' && s[a]!='\n' && s[a] != '\0' && s[a]!= '"')// Check is this is a cpitals if so store infro in Boolen
- {// is alpha sa pr sa you -
- temp[b] = s[a];
- // check words to see if they
- if (temp[0]>='A' && temp[0]<='Z')
- {temp[0]= temp[0]-'A' + 'a';}
- b++;
- a++;
- }
- // cycle through the letters and check for caps first letter / after .
- temp[b] = '\0';
- dict.translate(returned, temp);
- //cout << returned << endl;
- str2 = str2 + returned;
- if(s[a] == ' ' || s[a] == ','||s[a] == '.'||s[a] == '-' || s[a] == '*' || s[a]=='!' || s[a]=='?' || s[a] == '\n' || s[a] == '\0' || s[a] == '"') // Did we have captial at point in word if so make cpital this postion
- {
- //cout << s[a]<<endl;
- str2 = str2 + s[a];
- //cout << str2<<endl;}
- strcpy(out_s, str2.c_str());
- // cout << <<endl;
- }
- }
- }
- }
- void Translator::toEnglish(char out_s[MAX_WORD_LEN], const char s[MAX_WORD_LEN])
- {
- char s_edit[MAX_WORD_LEN] = {'\0'};
- strcpy(s_edit,s);
- int len = strlen(s_edit);
- s_edit[len] = '\0';
- int i=0;
- do{s_edit[i] ='\0';i++;}while(i<MAX_WORD_LEN);
- char temp[MAX_WORD_LEN] = {'\0'};
- char returned[MAX_WORD_LEN] = {'\0'};
- string str2;
- //const char *cs;
- for (int a = 0; a < len; a++)
- {
- /*if(s[a]!=' ' || s[a]!='\n')
- {*/
- int b = 0;
- while(s[a] != ' ' && s[a] != '\n' && s[a] != ','&&s[a] !='.' && s[a]!='-' && s[a]!='!' && s[a]!='?' && s[a] != '"')
- {
- if (temp[0]>='A' && temp[0]<='Z')
- {temp[0]= temp[0]-'A' + 'a';}
- temp[b] = s[a];
- b++;
- a++;
- }
- temp[b] = '\0';
- dict.translate2(returned, temp);
- str2 = str2 + returned;
- cout << returned << endl;
- if(s[a] == ' ' || s[a]== '\n' || s[a] == ','||s[a] == '.' ||s[a] == '-' || s[a]== '!' || s[a]== '?' || s[a] == '*' || s[a] == '\0' || s[a] == '"')
- {
- str2 = str2 + s[a];
- strcpy(out_s, str2.c_str());
- }
- }
- }
- void Dictionary::translate(char out_s[MAX_WORD_LEN], const char s[MAX_WORD_LEN])
- {
- int i = 0;
- int len = 0;
- bool notfound = false;
- for (i=0;i<numEntries;i++)
- {
- if (strcmp(englishWord[i], s)==0)
- {
- notfound=true;
- break;}
- }
- if(i<numEntries)
- {
- strcpy(out_s, elvishWord[i]);
- }
- if((i==numEntries) && (!notfound))
- {
- len = strlen(s);
- out_s[0] = '*';
- out_s[len+1] = '*';
- out_s[len+2] = '\0';
- for(int j=0; j<len;j++)
- {
- out_s[j+1] = s[j];
- }
- }
- }
- void Dictionary::translate2(char out_s[MAX_WORD_LEN], const char s[MAX_WORD_LEN])
- {
- int len = 0;
- bool notfound = false;
- int i = 0;
- for (i=0;i<numEntries;i++)
- {
- if (strcmp(elvishWord[i], s)==0)
- {
- notfound = true;
- break;
- }
- }
- if ( i<numEntries)
- {
- strcpy(out_s, englishWord[i]);
- }
- if((i==numEntries) && (!notfound))
- {
- len = strlen(s);
- for( int j=0; j < len-2;j++)
- {
- out_s[j] = s[j+1];
- }
- out_s[len-2] = '\0';
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment