Created
December 25, 2015 19:26
-
-
Save iamOgunyinka/1c3d12560032deb4aa4a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
I Emi | |
am ni | |
run sare | |
and ati | |
but sugbon | |
is ni | |
helter-skelter sihin-sohun | |
name oruko |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <vector> | |
#include <tuple> | |
#include <sstream> | |
#include <algorithm> | |
#include <fstream> | |
template<typename Iter, typename Compare > | |
void last_elem_sort( Iter beg, Iter last, Compare && cmp) { | |
auto last_elem = last - 1; | |
auto iter = std::lower_bound( beg, last_elem, *last_elem, cmp ); | |
std::rotate( iter, last_elem, last ); | |
} | |
using string_pair = std::pair< std::string, std::string >; | |
struct comp | |
{ | |
bool operator()( string_pair const & a, string_pair const & b ){ | |
return std::make_tuple( a.first, a.second ) < std::make_tuple( b.first, b.second ); | |
} | |
}; | |
class Dictionary | |
{ | |
std::vector<string_pair> dict; | |
void insert_impl( string_pair && a ){ | |
dict.push_back( std::move( a ) ); | |
last_elem_sort( std::begin( dict ), std::end( dict ), comp() ); | |
} | |
public: | |
Dictionary( std::string const & filename ) | |
{ | |
std::ifstream file{ filename }; | |
if( file ){ | |
std::string word{}, meaning{}; | |
while( file >> word >> meaning ){ | |
insert( word, meaning ); | |
} | |
file.close(); | |
} | |
} | |
friend std::ostream &operator<<( std::ostream & os, Dictionary const & d ){ | |
for( auto const & wm: d.dict ) os << wm.first << ": " << wm.second << std::endl; | |
return os; | |
} | |
void insert( std::string const & word, std::string const & meaning ) | |
{ | |
string_pair word_meaning { word, meaning }; | |
if( std::binary_search( std::begin( dict ), std::end( dict ), word_meaning ) ) return; | |
insert_impl( std::move( word_meaning ) ); | |
} | |
void remove( std::string const & word ) | |
{ | |
dict.erase( std::remove_if( std::begin( dict ), std::end( dict ), [&]( string_pair const & s ) { return s.first == word; } ), dict.end() ); | |
} | |
std::string lookup( std::string const & word ) | |
{ | |
auto iter = std::find_if( std::begin( dict ), std::end( dict ), [&]( string_pair const & s ) { return s.first == word; } ); | |
return iter == dict.end() ? word: iter->second; | |
} | |
std::string translate( std::string const & s ) | |
{ | |
std::istringstream ss { s }; | |
std::string temp, result; | |
while( ss >> temp ){ | |
if( ispunct( temp[ temp.size() - 1 ] ) ){ | |
result += lookup( temp.substr( 0, temp.size() - 1 ) ); | |
result.push_back( temp[ temp.size() - 1 ] ); | |
} else { | |
result += lookup( temp ); | |
result.push_back( ' ' ); | |
} | |
} | |
return result; | |
} | |
}; | |
int main() | |
{ | |
Dictionary oxford_dictionary { "dict_word.txt" }; | |
std::string input; | |
std::getline( std::cin, input ); | |
auto translation = oxford_dictionary.translate( input ); | |
std::cout << translation << std::endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment