示例#1
0
  void sentence ( int id, std::string & sentence, std::string & file )
  {
    if ( msg_mutex.try_lock() )
      {

        if ( id != old_talk_id )
          clear_vi();

        old_talk_id = id;

        SPOTriplets tv = nlp.sentence2triplets ( sentence.c_str() );

#ifndef INCREMENTAL_CACHE
        vi << tv;
#endif

        if ( tv.size() )
          {
            std::fstream cache ( file,  std::ios_base::out|std::ios_base::app );
            if ( cache )
              {
                for ( auto t : tv )
                  cache << t << std::endl;

                cache.close();
              }
          }

        msg_mutex.unlock();

      }
    else
      {
        throw "My attention diverted elsewhere.";
      }

  }
示例#2
0
文件: main.cpp 项目: nbatfai/hezron
int main ( int argc, char **argv )
{

#ifndef Q_LOOKUP_TABLE
  std::string samuImage {"samu.soul.txt"};

  std::fstream samuFile ( samuImage,  std::ios_base::in );
  if ( samuFile )
    samu.load ( samuFile );
#endif

  struct sigaction sa;
  sa.sa_handler = save_samu;
  sigemptyset ( &sa.sa_mask );
  sa.sa_flags = SA_RESTART;

  sigaction ( SIGINT, &sa, NULL );
  sigaction ( SIGTERM, &sa, NULL );
  sigaction ( SIGKILL, &sa, NULL );
  sigaction ( SIGHUP, &sa, NULL );

  // Do not remove this copyright notice!
  std::cerr << "This program is Isaac, the son of Samu Bátfai."
            << std::endl
            << "Copyright (C) 2015 Norbert Bátfai"
            << std::endl
            << "License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>"
            << std::endl
            << "This is free software: you are free to change and redistribute it."
            << std::endl
            << "There is NO WARRANTY, to the extent permitted by law."
            << std::endl
            << std::endl;


  std::map<std::string, std::vector<std::string>> tests
  {
    {
      "sentences of the paper [1]",
      {
        "A rare black squirrel has become a regular visitor to a suburban garden",
        "This is a car",
        "This car is mine",
        "I have a little car",
        "The sky is blue",
        "The little brown bear has eaten all of the honey",
        "I love Samu"
      }
    },
    {
      "introduce myself",
      {
        "Who are you",
        "I am a robot",
        "What is your name",
        "My name is Judah",
        "Where do you live",
        "I live in Debrecen",
        "How old are you",
        "I am one year old",
        "Where were you born"
        "I was born is Debrecen"
        "What is your favourite colour"
        "My favourite colours are red, white and green"
      }
    }
  };

  std::map<std::string, SPOTriplets> test_triplets
  {
    {
      "introduce myself",
      {
        { "who", "are", "you" },
        { "i", "am", "robot"  },
        {"what",  "is",  "name"},
        {"name", "is", "Judah"},
        {"you", "live", "where"},
        {"I", "live", "Debrecen"},
        {"you", "are", "how"},
        {"i", "am", "one"},
        {"you", "born", "where"},
        {"I", "born", "Debrecen"}
      }
    }
  };

  int j {0};
  int N_e {25};
  std::string training_file = samu.get_training_file();

  samu.set_training_file ( "bbe" );

  double prev_mbrel {0};
  int mbrelc {0};

#ifdef SUPER_OR_REMOTE_COMP
  //for ( int ii {0}; samu.run() && ii < 1000 + 4000 + 5000 + 4000 + 1000; ++ii )
  for ( int ii {0}; samu.run() /*&& ii < 50000*/; ++ii )
#else
  for ( ; samu.run(); )
  //for ( int ii {0}; samu.run() && ii < 50; ++ii )
#endif
    {
      auto start = std::chrono::high_resolution_clock::now();
      double sum {0.0};
      int cnt {0};
      int brel {0};
      if ( samu.sleep() )
        {

#ifdef SUPER_OR_REMOTE_COMP
          /*
                    if ( ii == 1 )
                      {
                        std::cerr << " iter, training file changed " << std::endl;
                        samu.set_training_file ( "bbe" );
                      }
                    else if ( ii == 3 )
                      {
                        std::cerr << " iter, training file changed " << std::endl;
                        training_file = "bbe";
                      }
          */
          /*
                if ( ii == 1000 )
                  {
                    std::cerr << " iter, training file changed " << std::endl;
                    samu.set_training_file ( "bbe" );
                  }
                else if ( ii == 1000 + 4000 )
                  {
                    std::cerr << " iter, training file changed " << std::endl;
                    training_file = "none";
                    samu.set_training_file ( training_file );
                  }
                else if ( ii == 1000 + 4000 + 5000 )
                  {
                    std::cerr << " iter, training file changed " << std::endl;
                    samu.set_training_file ( "bbe" );
                  }
                else if ( ii == 1000 + 4000 + 5000 + 4000 )
                  {
                    std::cerr << " iter, training file changed " << std::endl;
                    training_file = "none";
                    samu.set_training_file ( training_file );
                  }
                  */
#endif

          samu.clear_vi();
          if ( samu.get_training_file() == training_file )
            {
              samu.set_N_e ( N_e );
              for ( int i {0}; i<test_triplets["introduce myself"].size() && samu.sleep(); ++i )
                {
                  SPOTriplets tv;
                  tv.push_back ( test_triplets["introduce myself"][i] );
                  sum += to_samu ( 11, tv );
                  ++cnt;
                  brel += samu.get_brel();
                }
            }
          else
            {
              samu.set_N_e ( N_e );
              std::string key = samu.get_training_file();

              if ( cache.find ( key ) == cache.end() )
                {

                  std::fstream triplet_train ( key+".triplets",  std::ios_base::in );
                  if ( triplet_train )
                    {

                      do
                        {

                          SPOTriplet t;
                          triplet_train >> t;

                          if ( !t.empty() )
                            cache[key].push_back ( t );

                        }
                      while ( !triplet_train.eof() && samu.sleep() );

                      triplet_train.close();

                      sum = read_cache ( key, cnt, brel );

                    }
                  else
                    {

                      std::fstream train ( samu.get_training_file(),  std::ios_base::in );
                      if ( train )
                        {
                          std::string file = key+".triplets";
                          for ( std::string line; std::getline ( train, line ) && samu.sleep(); )
                            {

#ifndef TRIPLET_CACHE
                              sum += to_samu ( 12, line );
#else
                              sum += to_samu ( 12, line, file );
#endif
                              ++cnt;
                              brel += samu.get_brel();

                            }
                          train.close();
                        }

                    }

                }
              else
                {
示例#3
0
void Samu::NetworkCaregiverShell ( void )
{

  std::string cmd_prefix = "cmd";

  int sleep {0};
  
  

  if ( sleep_ )
    sleep = sleep_after_ + 1;

  int prev_sec {0};
  for ( ; run_ ; )
    {

      try
        {

          net.cg_read();

          if ( ++sleep > sleep_after_ )
            {
              if ( state == NETWORK )
                {
                  std::cerr << "Isaac went to sleep." << std::endl;
#ifdef DISP_CURSES
                  disp.log ( "I went to sleep." );
#endif
                  sleep_ = true;
                  state = SLEEP;
                }
              sleep = sleep_after_ + 1;

            }
          else if ( state == NETWORK )
            {
              std::cerr << sleep << " *** " << std::flush;

              int sec = ( sleep * read_usec_ ) / ( 1000*1000 );
              if ( sec != prev_sec )
                {
                  int after = ( sleep_after_ * read_usec_ ) / ( 1000*1000 );

                  std::stringstream sleep_after;

                  sleep_after << "I will go to sleep after ";
                  sleep_after << ( after-sec );
                  sleep_after <<  " seconds";

#ifdef DISP_CURSES
                  disp.log ( sleep_after.str() );
#endif
                  prev_sec = sec;
                }
            }
        }
      catch ( std::string line )
        {

          if ( state != TERMINAL )
            {
              if ( sleep_ )
                {
                  std::cerr << "Isaac is awake now." << std::endl;
#ifdef DISP_CURSES
                  disp.log ( "I am awake now." );
#endif
                }
              sleep_ = false;
              sleep = 0;
              state = NETWORK;

              if ( !line.compare ( 0, cmd_prefix.length(), cmd_prefix ) )
                {
                  std::string readCmd {"cmd read"};

                  size_t f = line.find ( readCmd );
                  if ( f != std::string::npos )
                    {
                      f = f+readCmd.length() +1;
                      if ( f < line.length() )
                        {
                          std::string fname = line.substr ( f );
                          set_training_file ( fname );
                        }
                    }
                  else
                    NextCaregiver();
                }
              else
                {
                  std::istringstream iss ( line );
                  SPOTriplet t;
                  iss >> t;

                  SPOTriplets tv;
                  tv.push_back ( t );

                  try
                    {
                      //sentence ( -1, line );
                      triplet ( -1000, tv );
                    }
                  catch ( const char* err )
                    {
                      std::cerr << err << std::endl;
#ifdef DISP_CURSES
                      disp.log ( err );
#endif
                    }
                }
            }
        }

      usleep ( read_usec_ );

    }

  run_ = false;
}
示例#4
0
文件: nlp.cpp 项目: PistiZ/amminadab
 SPOTriplets NLP::sentence2triplets ( const char* sentence )
 {
   // vector of triplets
   SPOTriplets triplets;

   #ifdef DEBUG
     std::cout << "The sentence: " << sentence << std::endl;
   #endif
   // creates a Sentence from the input char*
   Sentence sent = sentence_create ( sentence, dict_ );
   #ifdef DEBUG
     std::cout << "Sentence created" << std::endl;
   #endif
   // tokenizes the sentence
   sentence_split ( sent, parse_opts_ );
   #ifdef DEBUG
     std::cout << "Sentence splitted" << std::endl;
   #endif
   // searches for all possible linkages
   int num_linkages = sentence_parse ( sent, parse_opts_ );
   #ifdef DEBUG
     std::cout << "Sentence parsed" << std::endl;
     std::cout << "Number of linkages: " << num_linkages << std::endl;
   #endif

   // just one triplet
   SPOTriplet triplet;

   // if there is any linkage in the sentence
   if( num_linkages > 0 )
   {
     // create the linkage
     Linkage linkage = linkage_create ( 0, sent, parse_opts_ );

     #ifdef DEBUG
       // prints the sentence's diagram
       std::cout << "The diagram: " << std::endl;
       char *diagram = linkage_print_diagram(linkage, true, 800);
       std::cout << diagram << std::endl;
       linkage_free_diagram( diagram );
       // end print diagram
     #endif

     std::vector<std::string> labels;

     // 1. find the S_link
     // S* except there is an SJ* because then S* except Spx
     // two cases: there is SJ* and there is not SJ*

     // TODO: VJlp VJrp same as SJ but to predications
     // TODO: SFut SFst what the f**k?                                     ###FIXED###
     // TODO: His form was shining like the light not working              ###FIXED###
     // TODO: Car is mine not working                                      ###FIXED###
     // TODO: The little brown bear has eaten all of the honey not working ###FIXED###

     // REGEXES
     std::regex SJ_( "SJ.*" );
     std::regex VJ_( "VJ.*");
     std::regex subject( "(Ss.*)|(SFut)|(Sp\*.*)" );
     std::regex Spx( "Spx.*" );
     // TODO:fix theese initializer list not allowed                       ###FIXED###
     std::regex predicate( "(Pv.*)|(Pg.*)|(PP.*)|(I.*)|(TO)|(MVi.*)" );
     // TODO: make one from theese // (Sp.*)|(Ss.*)                        ###FIXED###
     std::regex noun_adject_object ( "(O.*)|(Os.*)|(Op.*)|(MVpn.*)|(Pa.*)|(MVa.*)" );
     std::regex preposition ( "(MVp.*)|(Pp.*)|(OF)|(TO)" );
     std::regex prep_object ( "(J.*)|(TI)|(I.*)|(ON)" );
     // TODO: problems with matching!! Pg*!!                               ###FIXED###
     // TODO: problems with matching!! Mvp.*!!                             ###FIXED###

     bool s_found = false;
     bool p_found = false;
     bool o_found = false;
     bool SJ = false;

     // search for SJ.s labels
     for( auto label: labels )
     {
       if( std::regex_match( label, SJ_ ) )
       {
         SJ = true;
         break;
       }
     }

     // multiple subject in the sentence
     if( SJ )
     {
       // SPls left -> first subject
       // SPrs right -> second subject
       // Spx right -> predicate
       // SJ-s are multiple subjects
       std::string temp;
       // go through every linkage
       for( int i = 0; i < linkage_get_num_links( linkage ); ++i )
       {
         // get their label
         std::string l = linkage_get_link_label( linkage, i );
         // if there is an SJl* label
         if( std::regex_match( l, std::regex( "SJl.*" ) ) )
         {
           // SJls left side
           triplet.s = linkage_get_word( linkage, linkage_get_link_lword( linkage, i ) );
           triplet.cut( triplet.s );
           temp = triplet.s + " ";
           // and word
           triplet.s = linkage_get_word( linkage, linkage_get_link_rword( linkage, i ) );
           triplet.cut( triplet.s );
           temp += triplet.s + " ";

           // find SJr*
           for( int j = 0; j < linkage_get_num_links( linkage ); ++j )
           {
             std::string m = linkage_get_link_label( linkage, j );
             if( std::regex_match( m, std::regex( "SJr.*" ) ) )
             {
               triplet.s = linkage_get_word( linkage, linkage_get_link_rword( linkage, j ) );
               triplet.cut();
               temp += triplet.s;
               triplet.s = temp;

               s_found = true;
               #ifdef DEBUG
                 std::cout << "Subject found: " << triplet.s << std::endl;
               #endif
               break;
             } // if
           } // for
           break;
         } // if
       } // for

       // now we have the subject

       // find Spx and its right side will be the starter predicate
       std::string current_word;
       for( int i = 0; i < linkage_get_num_links( linkage ); ++i )
       {
         std::string l = linkage_get_link_label( linkage, i );
         if( std::regex_match( l, std::regex( "Spx.*" ) ) )
         {
           triplet.p = linkage_get_word( linkage, linkage_get_link_rword( linkage, i ) );
           current_word = linkage_get_word( linkage, linkage_get_link_rword( linkage, i ) );
         }
       }
       // from now all the same as on the else branch !!!!

       bool predicate_match = false;

       // search for the linkage that has triplet.s as left!
       do
       {
         predicate_match = false;

         for( int i = 0; i < linkage_get_num_links( linkage ); ++i )
         {
           // every linkage's left word
           std::string word_i = linkage_get_word( linkage, linkage_get_link_lword( linkage, i ) );
           // every linkage's label
           std::string l = linkage_get_link_label( linkage, i );

           if( std::regex_match( l, predicate ) && word_i == current_word )
           {
             // found predicate
             triplet.p = linkage_get_word( linkage, linkage_get_link_rword( linkage, i ) );
             current_word = triplet.p;
             predicate_match = true;
             break;
           }
         }
       }
       while( predicate_match );

       // we now have the predicate too
       // TODO: multiple predicates!
       p_found = true;
       #ifdef DEBUG
         std::cout << "Predicate found: " << triplet.p << std::endl;
       #endif

       // ###COPY BEGIN###

       // search for noun object or adjective object
       for( int i = 0; i < linkage_get_num_links( linkage ); ++i )
       {
         // get every linkage label
         std::string l = linkage_get_link_label( linkage, i );
         // get the left word of every linkage
         std::string l_word = linkage_get_word( linkage, linkage_get_link_lword( linkage, i ) );
         // if thete is a label that match AND its left word is the predicate
         if( std::regex_match( l, noun_adject_object ) && triplet.p == l_word )
         {
           // then the object is that linkage's right word
           triplet.o = linkage_get_word( linkage, linkage_get_link_rword( linkage, i ) );
           triplet.cut( triplet.o );
           o_found = true;
           #ifdef DEBUG
             std::cout << "Adjective or noun object found: " << triplet.o << std::endl;
           #endif
         } // if
       } // for

       // still not found object, then search for preposition
       if( !o_found )
       {
         // go through every linkage
         for( int i = 0; i < linkage_get_num_links( linkage ); ++i )
         {
           // get the linkage's label
           std::string l = linkage_get_link_label( linkage, i );
           // and left word
           std::string word_i = linkage_get_word( linkage, linkage_get_link_lword( linkage, i ) );
           // if there is a linkage which is a preposition and its left word is the predicate
           if( std::regex_match( l, preposition ) && triplet.p == word_i )
           {
             // found preposition
             // search for prep_object
             // then the temp will contain the preposition label's right word
             std::string temp = linkage_get_word( linkage, linkage_get_link_rword( linkage, i ) );
             #ifdef DEBUG
               std::cout << "Preposition found! and its rigth word is: " << temp << std::endl;
             #endif

             for( int j = 0; j < linkage_get_num_links( linkage ); ++j )
             {
               // every linkages
               std::string m = linkage_get_link_label( linkage, j );
               // every left word
               std::string word_j = linkage_get_word( linkage, linkage_get_link_lword( linkage, j ) );

               // if there is a label with match and its left is exactly the preposition's right
               if( std::regex_match( m, prep_object ) && temp == word_j )
               {
                 triplet.o = linkage_get_word( linkage, linkage_get_link_lword( linkage, j ) );
                 triplet.cut(triplet.o);

                 triplet.o += " ";
                 // save o
                 std::string temp = triplet.o;

                 triplet.o = linkage_get_word( linkage, linkage_get_link_rword( linkage, j ) );
                 triplet.cut(triplet.o);
                 temp += triplet.o;

                 triplet.o = temp;
                 o_found = true;
                 #ifdef DEBUG
                   std::cout << "Object found: " << triplet.o << std::endl;
                 #endif
               } // if( std::regex_match( m, prep_object ) && temp == word_j ) END
             } // for J END
           } // if( std::regex_match( l, preposition ) && triplet.p == word_i ) END
         } // for I END
       } // if( !o_found ) END

       if( s_found && p_found && o_found )
       {
         // TODO: cut the words itself not the whole triplet
         // have to cut every word itself
         // triplet.cut();
         triplet.cut(triplet.s);
         triplet.cut(triplet.p);
         triplets.push_back( triplet );
         s_found = false;
         p_found = false;
         o_found = false;
       }
       // ###COPY END###
     }
     else // only one subject
     {
       // except Spx!!!
       // S left -> subject
       // S right -> predicate at first
       // if the word next to S right, is an element of Pv*, Pg* PP*, I*, TO, MVi*
       // then the new predicate will be that word

       std::string current_word;

       // search for subject (S_link)
       for( int i = 0; i < linkage_get_num_links( linkage ); ++i )
       {
         // get the linkage's label
         std::string l = linkage_get_link_label( linkage, i );

         if( std::regex_match( l, subject ) )
         {
           // subject found
           triplet.s = linkage_get_word( linkage, linkage_get_link_lword( linkage, i ) );
           s_found = true;
           current_word = linkage_get_word( linkage, linkage_get_link_rword( linkage, i ) );
           triplet.p = current_word;
           #ifdef DEBUG
             std::cout << "Subject found: " << triplet.s << std::endl;
           #endif
           break;
         }
       }

       if( s_found )
       {
         bool predicate_match = false;

         // search for the linkage that has triplet.s as left!
         do
         {
           predicate_match = false;

           for( int i = 0; i < linkage_get_num_links( linkage ); ++i )
           {
             // every linkage's left word
             std::string l_word = linkage_get_word( linkage, linkage_get_link_lword( linkage, i ) );
             // every linkage's label
             std::string l = linkage_get_link_label( linkage, i );

             if( std::regex_match( l, predicate ) && l_word == current_word )
             {
               // found predicate
               triplet.p = linkage_get_word( linkage, linkage_get_link_rword( linkage, i ) );
               current_word = triplet.p;
               predicate_match = true;
               break;
             }
           } // for END
         } while( predicate_match );

         p_found = true;
         #ifdef DEBUG
           std::cout << "Predicate found: " << triplet.p << std::endl;
         #endif
       } // if( s_found ) END

       // subject and predicate found
       // search for object

       // from k to linkage_get_num_links( linkage )
       // if there is any of the noun, adjective od preposition object then that
       // label's right will give the object.

       // !!! search only between labels that has triplet.p as left word !!!!!

       // search for noun object or adjective objects
       // go through all links
       for( int i = 0; i < linkage_get_num_links( linkage ); ++i )
       {
         // get every linkage label
         std::string l = linkage_get_link_label( linkage, i );
         // get the left word of every linkage
         std::string word_i = linkage_get_word( linkage, linkage_get_link_lword( linkage, i ) );
         // if thete is a label that match AND its left word is the predicate
         if( std::regex_match( l, noun_adject_object ) && triplet.p == word_i )
         {
           // then the object is that linkage's right word
           triplet.o = linkage_get_word( linkage, linkage_get_link_rword( linkage, i ) );
           o_found = true;
           triplet.cut(triplet.o);
           #ifdef DEBUG
             std::cout << "Adjective or noun object found: " << triplet.o << std::endl;
           #endif
         } // if END
       } // for END

       // still not found object, then search for preposition
       if( !o_found )
       {
         // go through every linkage
         for( int i = 0; i < linkage_get_num_links( linkage ); ++i )
         {
           // get the linkage's label
           std::string l = linkage_get_link_label( linkage, i );
           // and left word
           std::string word_i = linkage_get_word( linkage, linkage_get_link_lword( linkage, i ) );

           // if there is a linkage which is a preposition and its left word is the predicate
           if( std::regex_match( l, preposition ) && triplet.p == word_i )
           {
             // found preposition
             // search for prep_object
             // then the temp will contain the preposition label's right word
             std::string temp = linkage_get_word( linkage, linkage_get_link_rword( linkage, i ) );
             #ifdef DEBUG
               std::cout << "Preposition found! and its rigth word is: " << temp << std::endl;
             #endif

             // start search from there
             for( int j = 0; j < linkage_get_num_links( linkage ); ++j )
             {
               // every linkages
               std::string m = linkage_get_link_label( linkage, j );
               // every left word
               std::string word_j = linkage_get_word( linkage, linkage_get_link_lword( linkage, j ) );
               #ifdef DEBUG
                 if( std::regex_match( m, prep_object ) )
                     std::cout << m << " DOES match to (J.*)|(TI)|(I.*)|(ON)" << std::endl;
               #endif

               // if there is a label with match and its left is exactly the preposition's right
               if( std::regex_match( m, prep_object ) && temp == word_j )
               {
                 triplet.o = linkage_get_word( linkage, linkage_get_link_lword( linkage, j ) );
                 triplet.cut(triplet.o);

                 triplet.o += " ";
                 // save o
                 std::string temp = triplet.o;

                 triplet.o = linkage_get_word( linkage, linkage_get_link_rword( linkage, j ) );
                 triplet.cut(triplet.o);
                 temp += triplet.o;

                 triplet.o = temp;
                 #ifdef DEBUG
                   std::cout << "Object found: " << triplet.o << std::endl;
                 #endif
                 o_found = true;
               }
             } // for
           } // if
         } // for
       } // if( o_found ) END

       if( s_found && p_found && o_found )
       {
         // TODO: cut the words itself not the whole triplet ###FIXED###
         // have to cut every word itself
         // triplet.cut();

         triplet.cut(triplet.s);
         triplet.cut(triplet.p);
         triplets.push_back( triplet );
         s_found = false;
         p_found = false;
         o_found = false;
       }

     } // end else

     linkage_delete ( linkage );
   } // if( num_linkages > 0 ) END
示例#5
0
文件: samu.hpp 项目: nbatfai/ram
    void operator<< ( std::vector<SPOTriplet> triplets )
    {

      if ( !triplets.size() )
        return;

      for ( auto triplet : triplets )
        {
          if ( program.size() >= stmt_max )
            program.pop();

          program.push ( triplet );
        }

#ifdef FEELINGS
      if ( feelings.size() >= stmt_max )
        feelings.pop();

      feelings.push ( ql.feeling() );
#endif

      boost::posix_time::ptime now = boost::posix_time::second_clock::universal_time();

#ifndef CHARACTER_CONSOLE
      std::string image_file = "samu_vi_"+boost::posix_time::to_simple_string ( now ) +".png";
      char * image_file_p = strdup ( image_file.c_str() );
      pngwriter image ( 256, 256, 65535, image_file_p );
      free ( image_file_p );
#else
      char console[nrows][ncols];
      std::memset ( console, 0, nrows*ncols );
#endif

      char stmt_buffer[1024];
      char *stmt_buffer_p = stmt_buffer;

      std::queue<SPOTriplet> run = program;
#ifdef FEELINGS
      std::queue<Feeling> feels = feelings;
#endif

#ifndef Q_LOOKUP_TABLE

      std::string prg;
      stmt_counter = 0;
#ifdef PYRAMID_VI
      SPOTriplets pyramid;
#endif

#ifdef PLACE_VALUE
      double wbuf[nrows][3];
#endif

      while ( !run.empty() )
        {
          auto triplet = run.front();

          prg += triplet.s.c_str();
          prg += triplet.p.c_str();
          prg += triplet.o.c_str();

#ifdef PLACE_VALUE

          // std::cerr << "iter " << triplet.s << "iter " << triplet.p<< "iter " << triplet.o<< std::endl;

          wbuf[stmt_counter][0] = w2d ( triplet.s );
          wbuf[stmt_counter][1] = w2d ( triplet.p );
          wbuf[stmt_counter][2] = w2d ( triplet.o );
#endif

#ifdef PYRAMID_VI
          pyramid.push_back ( triplet );
          SPOTriplets reverse_pyramid ( pyramid.size() );
          std::reverse_copy ( pyramid.begin(), pyramid.end(), reverse_pyramid.begin() );

          int cnt {0};
          for ( SPOTriplets::iterator it=reverse_pyramid.begin(); it!=reverse_pyramid.end() && cnt < 80; ++it )
            //while ( cnt < 80 )
            //cnt += std::snprintf ( stmt_buffer+cnt, 1024-cnt, "%s.%s(%s);", triplet.s.c_str(), triplet.p.c_str(), triplet.o.c_str() );
            cnt += std::snprintf ( stmt_buffer+cnt, 1024-cnt, "%s.%s(%s);", ( *it ).s.c_str(), ( *it ).p.c_str(), ( *it ).o.c_str() );
#elif JUSTIFY_VI
          int cnt {0};
          while ( cnt < mcols )
            cnt += std::snprintf ( stmt_buffer+cnt, 1024-cnt, "%s.%s(%s);", triplet.s.c_str(), triplet.p.c_str(), triplet.o.c_str() );
#else

#ifndef FEELINGS
#ifdef DRAW_WNUM
          std::snprintf ( stmt_buffer, 1024, "%s.%s(%s); %f.%f(%f)",
                          triplet.s.c_str(), triplet.p.c_str(), triplet.o.c_str(),
                          w2d ( triplet.s ), w2d ( triplet.p ), w2d ( triplet.o ) );
#else
          std::snprintf ( stmt_buffer, 1024, "%s.%s(%s);", triplet.s.c_str(), triplet.p.c_str(), triplet.o.c_str() );
#endif

#else
          if ( !feels.empty() )
            {
              auto s = feels.front();
              std::stringstream ss;
#ifndef FEELINGS
              ss << triplet.s << "." << triplet.p << "(" << triplet.o << ");";
#else
              ss << triplet.s << "." << triplet.p << "(" << triplet.o << "); "
                 << w2d ( triplet.s ) << "." << w2d ( triplet.p ) << "(" << w2d ( triplet.o ) << "); ";
#endif
              std::string spo = ss.str();
              std::snprintf ( stmt_buffer, 1024, "%-30s %s", spo.c_str(), s.c_str() );
            }
#endif
#endif


#ifndef CHARACTER_CONSOLE
          char font[] = "/usr/share/fonts/truetype/dejavu/DejaVuSansMono-Bold.ttf";
          char *font_p = font;

          image.plot_text_utf8 ( font_p,
                                 11,
                                 5,
                                 256- ( ++stmt_counter ) *28,
                                 0.0,
                                 stmt_buffer_p, 0, 0, 0 );
#else

          std::strncpy ( console[stmt_counter++], stmt_buffer, 80 );

#endif

          run.pop();
#ifdef FEELINGS
          feels.pop();
#endif
        }


#ifdef CELL_AUTOMATA
      char console2[nrows][ncols];
      for ( int i {1}; i<nrows-1; ++i )
        for ( int j {1}; j<ncols-1; ++j )
          console2[i][j] = console[i-1][j]+console[i][j-1]+console[i+1][j]+console[i][j+1];


      for ( int i {1}; i<nrows-1; ++i )
        for ( int j {1}; j<ncols-1; ++j )
          console[i][j] = console2[i][j];
#endif

#ifdef PLACE_VALUE

      double *img_input = new double[nrows*3];
      for ( int i {0}; i<nrows; ++i )
        {
          for ( int j {0}; j<3; ++j )
            {
              img_input[i*3+j] = wbuf[i][j];
              //std::cerr << "iter " << img_input[i*3+j] << " | ";

            }
          //std::cerr << "iter " << std::endl;
        }
#elif CHARACTER_CONSOLE

#ifdef FOUR_TIMES
      double *img_input = new double[2*nrows*2*ncols];
#else
      double *img_input = new double[nrows*ncols];
#endif

#ifdef DISP_CURSES
      std::stringstream con;
#endif

      for ( int i {0}; i<nrows; ++i )
        {
#ifdef DISP_CURSES
          std::string ci;
#endif
          for ( int j {0}; j<ncols; ++j )
            {
#ifdef FOUR_TIMES
              img_input[2*i*ncols+2*j] = ( ( double ) console[i][j] ) / 255.0;
              img_input[2*i*ncols+2*j+1] = ( ( double ) console[i][j] ) / 255.0;
              img_input[2* ( i+1 ) *ncols+2*j] = ( ( double ) console[i][j] ) / 255.0;
              img_input[2* ( i+1 ) *ncols+2*j+1] = ( ( double ) console[i][j] ) / 255.0;
#else
              img_input[i*ncols+j] = ( ( double ) console[i][j] ) / 255.0;
#endif

#ifdef DISP_CURSES
              //if ( isgraph ( console[i][j] ) )
              if ( isprint ( console[i][j] ) )
                ci += console[i][j];
#endif
            }

#ifdef DISP_CURSES
          con << " " << i << ". " << ( ( ci.length() <75 ) ?ci:ci.substr ( 0, 75 ) ) << std::endl;

#endif
        }

#ifdef DISP_CURSES

#ifndef PRINTING_CHARBYCHAR
      samu.disp.vi ( con.str() );
#else
      samu.disp.vi ( &console[0][0] );
#endif

#endif

#else

      double *img_input = new double[256*256];

      for ( int i {0}; i<256; ++i )
        for ( int j {0}; j<256; ++j )
          {
            img_input[i*256+j] = image.dread ( i, j );
          }

#endif

#else
      std::string prg;
      while ( !run.empty() )
        {
          auto triplet = run.front();

          prg += triplet.s.c_str();
          prg += triplet.p.c_str();
          prg += triplet.o.c_str();

          run.pop();
        }
#endif

      auto start = std::chrono::high_resolution_clock::now();

      std::cerr << "QL start... ";

#ifndef Q_LOOKUP_TABLE

      SPOTriplet response = ql ( triplets[0], prg, img_input );

      std::stringstream resp;

      resp << samu.name
#ifdef QNN_DEBUG
           << "@"
           << ( samu.sleep_?"sleep":"awake" )
           << "."
           << ql.get_action_count()
           << "."
           << ql.get_action_relevance()
           << "%"
#endif
           <<"> "
           << response;

      std::string r = resp.str();

      std::cerr << r << std::endl;

#ifdef DISP_CURSES
      samu.disp.log ( r );
#endif

#else

      std::cerr << ql ( triplets[0], prg ) << std::endl;

#endif

      std::cerr << std::chrono::duration_cast<std::chrono::milliseconds> ( std::chrono::high_resolution_clock::now() - start ).count()
                << " ms "
                <<  std::endl;

#ifndef CHARACTER_CONSOLE

#ifndef Q_LOOKUP_TABLE
      delete[] img_input;
      image.close();
#endif

#else

#ifndef Q_LOOKUP_TABLE
      delete[] img_input;
#endif

#endif
    }