Exemplo n.º 1
0
      /// <summary>Reads the entire language file</summary>
      /// <param name="path">Full path</param>
      /// <returns>New language file</returns>
      /// <exception cref="Logic::ComException">COM Error</exception>
      /// <exception cref="Logic::FileFormatException">Corrupt XML / Missing elements / missing attributes</exception>
      /// <exception cref="Logic::InvalidValueException">Invalid languageID or pageID</exception>
      /// <exception cref="Logic::IOException">An I/O error occurred</exception>
      LanguageFile LanguageFileReader::ReadFile(Path path)
      {
         try
         {
            LanguageFile file(path);

            // Parse document
            LoadDocument();

            // Get root (as node)
            XmlNodePtr languageNode(Document->documentElement);

            // Read fileID + language tag
            file.ID = LanguageFilenameReader(path.FileName).FileID;
            file.Language = ReadLanguageTag(languageNode);

            // Read pages
            for (int i = 0; i < languageNode->childNodes->length; i++)
            {
               XmlNodePtr n = languageNode->childNodes->item[i];

               if (n->nodeType == Xml::NODE_ELEMENT)
                  file.Pages.Add( ReadPage(n) );
            }

            return file;
         }
         catch (_com_error& ex) {
            throw ComException(HERE, ex);
         }
      }
bool LanguageActionAnalysis::mainNode()
{
    if(getline(file,svector))
    {
        cout << svector << endl;
        iCub->say("Any questions");
        iquestion = languageNode();
    }
    else{
        iCub->say("We have finish the interaction");
    }
    return 0;
}
int LanguageActionAnalysis::languageNode()
{
    //iquestion = 3;
    //return grammarNode();

    sCurrentNode = "nodeQuestion";
    sCurrentGrammarFile = nameGrammarNode;
    ostringstream osError;			// Error message
    osError << "Error in  LanguageActionAnalysis | "<< sCurrentNode << " :: ";
    cout << endl << "In " << sCurrentNode << endl << endl;

    Bottle bOutput;

    bool fGetaReply = false;
    Bottle bSpeechRecognized, //recceived FROM speech recog with transfer information (1/0 (bAnswer) ACK/NACK)
            bMessenger, //to be send TO speech recog
            bAnswer, //response from speech recog without transfer information, including raw sentence
            bSemantic; // semantic information of the content of the recognition

    bMessenger.addString("recogBottle");
    bMessenger.addString("grammarXML");
    bMessenger.addString(grammarToString(sCurrentGrammarFile).c_str());


    while (!fGetaReply)
    {
        bSpeechRecognized.clear();
        Port2SpeechRecog.write(bMessenger,bSpeechRecognized);

        //e.g. : Reply from Speech Recog : 1 ("I want you to produce language" (INFORMATION (type produce))) ACK
        cout << "Reply from Speech Recog : " << bSpeechRecognized.toString() << endl;

        if (bSpeechRecognized.toString() == "NACK" || bSpeechRecognized.size() != 3)
        {
            osError << "Check " << sCurrentGrammarFile;
            bOutput.addString(osError.str());
            cout << osError.str() << endl;
            //return bOutput;
        }

        if (bSpeechRecognized.get(0).toString() == "0")
        {
            osError << "Grammar not recognized";
            bOutput.addString(osError.str());
            cout << osError.str() << endl;
            //return bOutput;
        }

        // bAnswer is the result of the regognition system (first element is the raw sentence, 2nd is the list of semantic element)
        bAnswer = *bSpeechRecognized.get(1).asList();

        if (bAnswer.toString() != "" && !bAnswer.isNull())
        {
            fGetaReply = true;
        }
    }

    if (bAnswer.get(0).asString() == "stop")
    {
        iCurrentInstance = -1;
        osError.str("");
        osError << " | STOP called";
        bOutput.addString(osError.str());
        cout << osError.str() << endl;
        //return bOutput;
    }

    bAnswer = *bSpeechRecognized.get(1).asList();

    if (bAnswer.get(0).asString() == "next")
    {
        return mainNode();
    }

    if (bAnswer.get(0).asString() == "what happened")
    {
        iquestion = 1;
        return grammarNode();
    }
    else if (bAnswer.get(1).asList()->get(0).asString() == "whoagent")
    {
        cout << "bAnswer.get(0).asString() : " << bAnswer.get(1).asList()->get(0).asString() << endl;
        cout << "here    " << bAnswer.get(1).asString() << endl;
        iquestion = 2;
        bSemantic = *bAnswer.get(1).asList()->get(1).asList();
        sagent = bSemantic.check("agent", Value("none")).asString();
        return grammarNode();
    }
    else if (bAnswer.get(0).asString() == "how did that happen")
    {
        iquestion = 3;
        return grammarNode();
    }

    else if (bAnswer.get(0).asString() == "no")
    {
        iCub->say("go ahead");
    }
    return languageNode();
}
bool LanguageActionAnalysis::grammarNode()
{
        int id = svector.find(";");
        int idf = svector.size();
        string sforce = svector.substr(6, id -7); //  => sforce=   "push(Ag1,object)"
        string sresult = svector.substr(id +8,idf-1-(id +8)); // =>   sresult=   "move(object)"

        string sverb, sagent1, sagent2, sobject;

        //force<give(Ag1,Ag2,object)>;result<received(Ag2,object)>

        if(iquestion == 1)  // => Case "What happened"  => Result part !!
        {
            int i = sresult.find("(");
            int iend = sresult.size();
            sverb = sresult.substr(0,i); // => "move"
            cout << "sverb : " << sverb << endl;

            sresult = sresult.substr(i+1,iend-(i+1)-1); // =>  "object"
            cout << "sresult : " << sresult << endl;

            list<int> lposElements = nbCaracters(sresult);
            int nb = lposElements.size();


            if (nb == 0){            //  "object"
                sobject=sresult;
                sanswer = "The " + sobject + " " + sverb;
                iCub->say(sanswer,true);
                cout << sanswer << endl;
            }

            else if(nb == 1)   //  "Ag2,object"
            {
                list<int>::iterator it;

                for(it=lposElements.begin(); it!=lposElements.end(); ++it)
                {
                    sagent1=sresult.substr(0,*it);
                    cout << "sagent1 : " << sagent1 << endl;
                    sobject=sresult.substr(*it+1,lposElements.size()-(*it+1));
                    cout << "sobject : " << sobject << endl;
                }
                sanswer = sagent1 + " " + sverb + " the " + sobject;
                iCub->say(sanswer,true);
                cout << sanswer << endl;
            }
        }
        else if(iquestion == 2)  // "what did Anne do ?" => active form
        {
            int i = sforce.find("(");
            int iend = sforce.size();
            sverb = sforce.substr(0,i); // => "result"

            sforce = sforce.substr(i+1,iend-(i+1)-1); // =>  "Ag2,object"

            list<int> lposElements = nbCaracters(sforce);
            int nb = lposElements.size();

            if (nb == 0){            //  "object"
                sobject=sforce;
            }

            else if(nb == 1)   //  "Ag2,object"
            {
                list<int>::iterator it;
                int cpt=0;

                int tab[lposElements.size()];
                for(it=lposElements.begin(); it!=lposElements.end(); ++it)
                {
                    tab[cpt]=*it;
                    cpt++;
                }
                cout << tab << endl;
                sagent1=sforce.substr(0,tab[0]);
                sobject=sforce.substr(tab[0]+1,sforce.size()-sforce.size()-tab[0]+1);
                //sagent1 = sagent.c_str();
                sanswer = sagent1 + " " + sverb + "s the " + sobject;
                iCub->say(sanswer,true);
                cout << sanswer << endl;
            }
            else if(nb == 2)   //  "Ag1,Ag2,object"
            {
                list<int>::iterator it;
                int cpt=0;
                int tab[lposElements.size()];
                for(it=lposElements.begin(); it!=lposElements.end(); ++it)
                {
                    tab[cpt]=*it;
                    cpt++;
                    cout << tab << endl;
                }
                sagent1=sforce.substr(0,tab[0]);
                sagent2=sforce.substr(tab[0]+1,tab[1]-(tab[0]+1));
                sobject=sforce.substr(tab[1]+1,sforce.size()-tab[1]+1);
                //sagent1 = sagent.c_str();
                sanswer = sagent1 + " " + sverb + "s the " + sobject + " to " + sagent2;
                iCub->say(sanswer,true);
                cout << sanswer << endl;
            }
        }

        else if(iquestion == 3)  // "how did that happen" => passive form
        {
            int i = sforce.find("(");
            int iend = sforce.size();
            string sverb = sforce.substr(0,i); // => "result"

            sforce = sforce.substr(i+1,iend-(i+1)-1); // =>  "Ag2,object"

            list<int> lposElements = nbCaracters(sforce);
            int nb = lposElements.size();

            if (nb == 0){            //  "object"
                string object=sforce;
            }

            else if(nb == 1)   //  "Ag2,object"
            {
                list<int>::iterator it;
                int cpt=0;

                int tab[lposElements.size()];
                for(it=lposElements.begin(); it!=lposElements.end(); ++it)
                {
                    tab[cpt]=*it;
                    cpt++;
                }
                cout << tab << endl;
                sagent1=sforce.substr(0,tab[0]);
                sobject=sforce.substr(tab[0]+1,sforce.size()-sforce.size()-tab[0]+1);
                sanswer = "The " + sobject + " has been " + sverb + " by " + sagent1;
                cout << "sanswer : " << endl;
                iCub->say(sanswer,true);
                cout << sanswer << endl;
            }
            else if(nb == 2)   //  "Ag2,object"
            {
                list<int>::iterator it;
                int cpt=0;
                int tab[lposElements.size()];
                for(it=lposElements.begin(); it!=lposElements.end(); ++it)
                {
                    tab[cpt]=*it;
                    cpt++;
                    cout << tab << endl;
                }
                sagent1=sforce.substr(0,tab[0]);
                sagent2=sforce.substr(tab[0]+1,tab[1]-(tab[0]+1));
                sobject=sforce.substr(tab[1]+1,sforce.size()-tab[1]+1);
                sanswer = "The " + sobject + " has been " + sverb + "ed by " + sagent1 + " to " +sagent2;
                cout << "sanswer : " << endl;
                iCub->say(sanswer,true);
                cout << sanswer << endl;
            }
        }

    cout << "##############################################################" << endl;
    return languageNode();
}