ハックニードプラティティ。 ボットの少年が書いたように

私は16歳で、男子学生です。 少し前まで、ボットを書くというアイデアに出会いました...いや、PHPの部分ではなく、不必要なサイトにぶら下がっています。 また、「!天気」などのフレーズに対する無駄な応答者でもありません。



ボットは、デスクトップ上の「話し手」としてエンターテイメントのために考案されました。 ひどいですね しかし、自分のコードを誰にも見せたことはなく、学校のパスカルしか教えていないので、自分の間違いを知りたいです。 そのため、一部の人々が嫌いなクリーンな構造的アプローチに従って、C ++でオリジナルバージョンを作成しました。



アイデアはこれです。 ボットはコンソールからフレーズを取得し、単語を分離し、「Memory.txt」ファイルにある辞書に従ってそれぞれを確認し、見つかった回答を各単語に返します。 単一の単語に対する答えが見つからない場合、合意されたフレーズを返します(基本的にではありません)。



「Memory.txt」ファイルの辞書は、最も単純な方法で構成されています。

単語=答え



例:

りんご=おいしいりんご



Bot.hはヘッダーファイルです。これについては後で説明します。 主な機能は、Bot.cppファイルにあります。



/**  , 2015   Cbot */ #include"Bot.h"
      
      







同じファイルで辞書の名前を定義します。



 ///  -   const char *const MemoryPath="Memory.txt";
      
      







ボットの「基本」は、1行から単語を抽出して文字列の配列に入れ、配列へのポインターを返す関数です。

const std :: string * const GetWords(const std :: string&Word)
  ///    - ,    ... int MaxIndex=0; ///     const std::string *const GetWords(const std::string &Word) { ///     256  std::string *const PtrWords=new std::string[256]; ///   MaxIndex=0; ///       bool Fix=false; /// - ,    for(int i=0; i<Word.size(); ++i) { ///-   if(Word[i]==' '||Word[i]=='.'||Word[i]==','||Word[i]=='!'||Word[i]=='?'||Word[i]=='='||Word[i]=='/') { ///  ,       Fix=true; continue; } ///     ,      if(Fix) { Fix=false; ++MaxIndex; } PtrWords[MaxIndex]+=Word[i]; } return PtrWords; }
      
      







次の関数は検索文字列を受け取り、辞書で検索します;答えが見つからない場合、空の文字列 ""を返します。 関連付けファイル内の別の単語の中に単語が見つかった場合、回答がカウントされるという事実に注意を喚起したいと思います。

const std :: string GetAssociation(const std :: string&Word)
 ///   const std::string GetAssociation(const std::string &Word) { std::ifstream Memory(MemoryPath, std::ios::in); if(!Memory) { std::ofstream NewMemory(MemoryPath); NewMemory.close(); Memory.open(MemoryPath); return ""; } while(!Memory.eof()) { std::string Buffer=""; std::getline(Memory, Buffer); if(Buffer.find(Word)!=-1) { std::string Result[2]; for(int i=0, Index=0; i<Buffer.size(); ++i) { if(Buffer[i]=='=') { ///    '=' -  if(Index==1) { break; } ++Index; continue; } Result[Index]+=Buffer[i]; } if(Result[0].find(Word)!=-1) { Memory.close(); return Result[1]; } } } Memory.close(); return ""; }
      
      







これで、オプションのささいなこと、ひどいトレーニングのパロディを考えることができます-「-」文字が行にあるときに新しい関連付けを追加します。

例:

悪は逆に良い

辞書が行く:

悪=良い逆

単語が別の単語の中にあるとき、答えがカウントされることを忘れないでください。その結果、興味深い結果が得られます。

void PutAssociation(const std :: string&Left、const std :: string&Right)
 ///   void PutAssociation(const std::string &Left, const std::string &Right) { std::ofstream Memory(MemoryPath, std::ios::app); Memory<<Left<<'='<<Right<<std::endl; Memory.close(); }
      
      







私の見解では、構造的なアプローチはカプセル化をキャンセルしないため、匿名の名前空間を追加します-これまでのすべての機能を含む一般的なカプセル化のために。



したがって、ヘッダーファイル「Bot.h」が含まれている場合、以前の関数は使用できなくなります。 達人を参照させてください:

これは、静的バインディングを使用して関数と変数を宣言することを避けるための最も先進的な方法です。

ユニット内でのみアクセスできます

翻訳(つまり、予備処理後に受信したファイル内)、

静的変数と同じ方法で、それらが配置されています。

スティーブンS.ダーチェスト、C ++。 神聖な知識



ここで、すべて一緒に:

名前空間
 namespace { ///  -   const char *const MemoryPath="Memory.txt"; ///     int MaxIndex=0; ///     const std::string *const GetWords(const std::string &Word) { ///   256  std::string *const PtrWords=new std::string[256]; ///   MaxIndex=0; ///       bool Fix=false; /// - ,    for(int i=0; i<Word.size(); ++i) { ///-   if(Word[i]==' '||Word[i]=='.'||Word[i]==','||Word[i]=='!'||Word[i]=='?'||Word[i]=='='||Word[i]=='/') { ///  ,       Fix=true; continue; } ///     ,      if(Fix) { Fix=false; ++MaxIndex; } PtrWords[MaxIndex]+=Word[i]; } return PtrWords; } ///   void PutAssociation(const std::string &Left, const std::string &Right) { std::ofstream Memory(MemoryPath, std::ios::app); Memory<<Left<<'='<<Right<<std::endl; Memory.close(); } }
      
      







そして最後に、もちろん、名前空間の外で、同じコンパイル単位で、外の世界と通信するための関数。 フレーズを取り、単語を分離し、それぞれの関連を取得し、キャラクターを見つけると、以前の機能を使用して同様に新しい関連を追加します。

const std :: string GetFullAssociation(const std :: string&Word)
 ///      const std::string GetFullAssociation(const std::string &Word) { const std::string *const Words=GetWords(Word); std::string Result=""; for(int i=0; i<=MaxIndex; ++i) { const std::string Buffer=GetAssociation(Words[i]); if(Buffer!="") { Result+=Buffer+' '; } } delete[] Words; if(Word.find('-')!=-1) { std::string NewAssociations[2]; for(int i=0, Index=0; i<Word.size(); ++i) { if(Word[i]=='-') { if(Index==1) { break; } ++Index; continue; } if(Word[i]=='=') { continue; } NewAssociations[Index]+=Word[i]; } PutAssociation(NewAssociations[0], NewAssociations[1]); } return Result; }
      
      









要約すると、「Bot.cpp」ファイルは完全に次のとおりです。



Bot.cpp
 /**  , 2015   Cbot */ #include"Bot.h" ///          namespace { ///  -   const char *const MemoryPath="Memory.txt"; ///     int MaxIndex=0; ///     const std::string *const GetWords(const std::string &Word) { ///   256  std::string *const PtrWords=new std::string[256]; ///   MaxIndex=0; ///       bool Fix=false; /// - ,    for(int i=0; i<Word.size(); ++i) { ///-   if(Word[i]==' '||Word[i]=='.'||Word[i]==','||Word[i]=='!'||Word[i]=='?'||Word[i]=='='||Word[i]=='/') { ///  ,       Fix=true; continue; } ///     ,      if(Fix) { Fix=false; ++MaxIndex; } PtrWords[MaxIndex]+=Word[i]; } return PtrWords; } ///   const std::string GetAssociation(const std::string &Word) { std::ifstream Memory(MemoryPath, std::ios::in); if(!Memory) { std::ofstream NewMemory(MemoryPath); NewMemory.close(); Memory.open(MemoryPath); return ""; } while(!Memory.eof()) { std::string Buffer=""; std::getline(Memory, Buffer); if(Buffer.find(Word)!=-1) { std::string Result[2]; for(int i=0, Index=0; i<Buffer.size(); ++i) { if(Buffer[i]=='=') { ///    '=' -  if(Index==1) { break; } ++Index; continue; } Result[Index]+=Buffer[i]; } if(Result[0].find(Word)!=-1) { Memory.close(); return Result[1]; } } } Memory.close(); return ""; } ///   void PutAssociation(const std::string &Left, const std::string &Right) { std::ofstream Memory(MemoryPath, std::ios::app); Memory<<Left<<'='<<Right<<std::endl; Memory.close(); } } ///      const std::string GetFullAssociation(const std::string &Word) { const std::string *const Words=GetWords(Word); std::string Result=""; for(int i=0; i<=MaxIndex; ++i) { const std::string Buffer=GetAssociation(Words[i]); if(Buffer!="") { Result+=Buffer+' '; } } delete[] Words; if(Word.find('-')!=-1) { std::string NewAssociations[2]; for(int i=0, Index=0; i<Word.size(); ++i) { if(Word[i]=='-') { if(Index==1) { break; } ++Index; continue; } if(Word[i]=='=') { continue; } NewAssociations[Index]+=Word[i]; } PutAssociation(NewAssociations[0], NewAssociations[1]); } return Result; }
      
      









これですべて「Bot.cpp」ファイルが完了し、ヘッダーファイル「Bot.h」の概要がすぐにわかります。



Bot.h
 #ifndef BOT #define BOT ///   ,    iostream #ifndef _GLIBCXX_IOSTREAM #include<iostream> #endif //_GLIBCXX_IOSTREAM /// fstream #ifndef _GLIBCXX_FSTREAM #include<fstream> #endif //_GLIBCXX_FSTREAM ///        extern const std::string GetFullAssociation(const std::string&); #endif //BOT
      
      









メイン部分はこれで完了です。メイン()関数が少しです。 Cbot.cppファイルに配置します。 Cbot-信じられないほど独創的ですね。



Cbot.cpp
 #include"Bot.h" int main() { ///  866 OEM (),  "Memory.txt"      setlocale(LC_ALL, ".866"); std::wcout<<"Cbot 2.0\n:  \nE-Mail: DDemidko1@gmail.com"<<std::endl; while(true) { std::wcout<<": "; std::string Buffer=""; std::getline(std::cin, Buffer); const std::string Association=GetFullAssociation(Buffer); /**   ? ,     - if(Association=="") { Association="Bot:    !"; } std::cout<<Association<<std::endl;     ,    866 OEM-        std::string      -   ( 866 OEM)     . */ if(Association=="") { std::wcout<<"Bot:    !"<<std::endl; } else { std::cout<<"Bot: "<<Association<<std::endl; } } }
      
      









ボットの準備が整ったので、まとめてCbot.exeを取得し、OEM 866エンコーディングでMemory.txtファイルを保存し、プログラムによって1つのディレクトリに配置します。 アセンブリへのリンク: space.ru/files/?r=main / view&Read=58688510



私は、コードの明らかなエラーを示す批判の建設的な流れを期待しています。



All Articles