And I decided to implement it in C ++ . Maybe Hashcode will be useful, who knows ... despite the fact that the task is almost elementary (and, I think, standard). I did everything through classes (well, how else !?), so you can embed it into any system without going into the details of the functioning of this class, knowing only one of its methods. Encapsulation, respectively. Anyway, such things are not reasonable to implement not through classes, since there is a behavior here. Please love and respect:
#include<iostream> #include<fstream> #include<clocale> #include<string> #include<vector> #include<algorithm> using namespace std; class Corrector // наш класс, реализующий функционал корректора { private: char* filename; // имя файла, где лежит BAD-TEXT. std::string text; // текст из этого файла std::vector<std::string>lines; // все лексемы( Tok = ' ') void CreateText() { for(vector<string>::iterator itr=lines.begin();itr!=lines.end();++itr) text+=*itr+" "; transform(text.begin(),text.end(),text.begin(),tolower); text[0] = toupper(text[0]); Correct(); } public: Corrector(char* fn):filename(fn){setlocale(LC_ALL,"Russian");} void LoadText() // метод загрузки текста { std::string str; ifstream f(filename); while(!f.eof()) { f>>str; lines.push_back(str); } f.close(); CreateText(); } void Correct() // корректор исходного текста { int i = 0; for(string::iterator itr=text.begin();itr!=text.end();++itr) { i++; if(*itr==',' || *itr=='!' || *itr=='?' || *itr=='.' || *itr==';') if(i>0) if(*(itr+1)!=' ') text.insert(itr+1,' '); if(*itr=='!' || *itr=='?' || *itr=='.') if(i>0) if(*(itr-1)==' ') text.erase(itr-1,itr); if(i>2) if(*(itr-2)=='!' || *(itr-2)=='?' || *(itr-2)=='.') *itr = toupper(*itr); } } void SaveText(char* fn) // Сохранение отформатированного текста в Файл "fn" { ofstream f(fn); f<<text<<endl; f.close(); } void Print(){cout<<text<<endl;} // отладочный метод... }; int main() { setlocale(LC_ALL,"Russian"); Corrector corr("badtext.txt"); // корректор corr.LoadText(); // загружаем текст corr.Print(); // отладочный вызов corr.SaveText("formatted.txt"); system("Pause"); }
Let's say if the source text was like this:
когда-то я был таким же шалопаем,как и вы,поэтому приношу свои извинения !! совершенно безграмотная сволочь(нет,нет,нет!) но а вы,а !? Так,что,"довай,до свыдааания!" !!!
So at the output it turns out like this
Когда-то я был таким же шалопаем, как и вы, поэтому приношу свои извинения!! Совершенно безграмотная сволочь(нет, нет, нет! ) но а вы, а!? Так, что, "довай, до свыдааания! "!!!
The code is written literally "on the knee", so if errors are found, I will correct them ...
PS I apologize for being cumbersome, but this is C ++ ... nevertheless nothing bad - the code is fully embedded and scalable. Waiting for comments (in style including!) =)))
And Vova for this need to ban