Esempio n. 1
0
        public void TestParse()
        {
            var jParser = CreateJavaParser(modelFile);
            var sParser = CreateSharpParser(modelFile);


            var sentences = new[] {
                "Let all your things have their places; let each part of your business have its time.",
                "It has become appallingly obvious that our technology has exceeded our humanity.",
                "The real problem is not whether machines think but whether men do.",
                "The worst form of inequality is to try to make unequal things equal.",
                "People won't have time for you if you are always angry or complaining.",
                "To keep the body in good health is a duty... otherwise we shall not be able to keep our mind strong and clear.",
                "You, yourself, as much as anybody in the entire universe, deserve your love and affection."
            };


            foreach (var sentence in sentences)
            {
                var jParses = opennlp.tools.cmdline.parser.ParserTool.parseLine(sentence, jParser, 1);
                var sParses = ParserTool.ParseLine(sentence, sParser, 1);

                var jsb = new java.lang.StringBuffer();

                jParses[0].show(jsb);

                Assert.AreEqual(jsb.toString(), sParses[0].ToString());

                jParses[0].show(jsb);
            }
        }
Esempio n. 2
0
        //[Test]
        public void TestParse()
        {
            // This test can't be done becouse a bug in the OpenNLP IKVM version
            // https://issues.apache.org/jira/browse/OPENNLP-727

            //const string sentence = "The quick brown fox jumps over the lazy dog .";

            const string sentence = "How much fruit do animals eat ?";

            var jParser = CreateJavaParser(modelFile);
            var sParser = CreateSharpParser(modelFile);

            var jParses = opennlp.tools.cmdline.parser.ParserTool.parseLine(sentence, jParser, 1);
            var sParses = ParserTool.ParseLine(sentence, sParser, 1);

            var jsb = new java.lang.StringBuffer();

            jParses[0].show(jsb);

            Assert.AreEqual("(TOP (SBAR (WHADJP (WRB How)) (S (NP (JJ much) (NN fruit)) (VP (VBP do) (S (NP (NNS animals)) (VP (VB eat))))) (. ?)))",
                            jsb.toString());

            Assert.AreEqual("(TOP (SBAR (WHADJP (WRB How)) (S (NP (JJ much) (NN fruit)) (VP (VBP do) (S (NP (NNS animals)) (VP (VB eat))))) (. ?)))",
                            sParses[0].ToString());

            Assert.NotNull(sParses);

            //CheckParseChild(jParses, sParses);
        }
Esempio n. 3
0
        /**
         * Processes messages associated with a Conversation.
         * Processes <em>only</em> messages of type:
         * <ul>
         *   <li>STARTED_LIVESESSION&#8212;lists details of in-coming and out-going calls</li>
         *   <li>POSTED_VOICE_MESSAGE&#8212;lists details of in-coming and out-going voicemails</li>
         *   <li>REQUESTED_AUTH&#8212;lists Contact authorization requests</li>
         *   <li>GRANTED_AUTH&#8212;lists Contacts granted authorization</li>
         * </ul>
         *
         * @param mySession
         *	Populated session object
         * @param myMessages
         *	Array of message strings to process.
         *
         * @since 1.0
         */
        static void doRenderHistory(MySession mySession, Message[] myMessages)
        {
            ParsePosition s_pos;
            ParsePosition e_pos;

            int msgTimeStamp;
            Date dateTimeStamp;
            DateFormat dateFmt = DateFormat.getDateTimeInstance();

            String author;
            String bodyXml;
            int bodyXmlLength;

            /*
             *         myXmlStrMgr.setVerboseDebug(true);
             */

            int msgCount = myMessages.Length;
            Message currMsg;
            Message.Type currMsgType;
            MySession.myConsole.printf("%d ...%n", msgCount);
            for (int i = 0; i < msgCount; i++)
            {
                currMsg = myMessages[i];
                currMsgType = currMsg.getType();

                if (currMsgType == Message.Type.STARTED_LIVE_SESSION)
                {
                    MySession.myConsole.printf("%nProcessing message of type %s%n", currMsgType.toString());
                    // Message.Property.P_AUTHOR tells who initiated the call.
                    author = currMsg.getAuthor();

                    // For duration we unfortunately have to parse the XML
                    // The duration we're interested in is
                    // <part identity="&me">%n...<duration>x</duration>...
                    //
                    // Real implementation should use a proper XML-parser here!
                    java.lang.StringBuffer partTag = new java.lang.StringBuffer("<part identity=\"");
                    partTag.append(mySession.myAccountName + "\">");
                    s_pos = myXmlStrMgr.getXmlSubStrPos(currMsg.getBodyXml(),
                                                        partTag.toString(), ZERO_POS);
                    if (s_pos == null)
                    {
                        MySession.myConsole.printf("%s: Could not find \"%s\" in xmlDoc%n%s%n%nSkipping...%n%n",
                                MY_CLASS_TAG, partTag.toString(),
                                currMsg.getBodyXml());
                        break;
                    }

                    int duration =
                        myXmlStrMgr.getXmlValueNum(currMsg.getBodyXml(),
                                                    "<duration>", s_pos);

                    // Ditto for counting how many parts the body has...
                    int num_parts = 0;
                    s_pos.setIndex(0);
                    do
                    {
                        e_pos = myXmlStrMgr.getXmlSubStrPos(currMsg.getBodyXml(),
                                                            "<part ", s_pos);
                        if (e_pos != null)
                        {
                            num_parts++;
                            s_pos.setIndex(e_pos.getIndex());
                        }
                    }
                    while (e_pos != null);

                    // Get timestamp -- it's in seconds, and the Date constructor needs milliseconds!
                    msgTimeStamp = currMsg.getTimestamp();
                    dateTimeStamp = new Date((msgTimeStamp * 1000L));
                    MySession.myConsole.printf("[%s] ", dateFmt.format(dateTimeStamp));
                    // Last part is to fetch message reason
                    String reason = currMsg.getReason();

                    if (author.Equals(mySession.myAccountName))
                    {
                        // I initiated the call
                        MySession.myConsole.println("outgoing call to ");
                    }
                    else
                    {
                        // Somebody else called me
                        MySession.myConsole.println("incoming call from ");
                    }

                    // List identities
                    doListIdentities(currMsg);

                    if (duration >= 0)
                    {
                        MySession.myConsole.printf("duration %d seconds", duration);
                    }
                    else if (num_parts > 1)
                    {
                        if (reason.Equals("manual"))
                        {
                            MySession.myConsole.printf("refused");
                        }
                        else
                        {
                            MySession.myConsole.printf("failed (%s)", reason);
                        }
                    }
                    else
                    {
                        MySession.myConsole.printf("missed");
                    }

                    MySession.myConsole.printf(" (%d parts).%n", num_parts);
                }
                else if (currMsgType == Message.Type.POSTED_VOICE_MESSAGE)
                {
                    MySession.myConsole.printf("%nProcessing message of type %s%n", currMsgType.toString());
                    author = currMsg.getAuthor();
                    // XML parsing again...
                    bodyXml = currMsg.getBodyXml();
                    bodyXmlLength = myXmlStrMgr.getXmlValueNum(bodyXml, "<length>", ZERO_POS);
                    // Get timestamp -- it's in seconds, and the Date constructor needs milliseconds!
                    msgTimeStamp = currMsg.getTimestamp();
                    dateTimeStamp = new Date((msgTimeStamp * 1000L));
                    MySession.myConsole.printf("[%s] ", dateFmt.format(dateTimeStamp));
                    if (author.Equals(mySession.myAccountName))
                    {
                        // I initiated the call
                        MySession.myConsole.println("Sent voicemail to ");
                    }
                    else
                    {
                        // Somebody else called me
                        MySession.myConsole.println("Got voicemail from ");
                    }
                    // List identities
                    doListIdentities(currMsg);
                    MySession.myConsole.printf("duration %d%n", bodyXmlLength);
                }
                else if (currMsgType == Message.Type.REQUESTED_AUTH)
                {
                    MySession.myConsole.printf("%nProcessing message of type %s%n", currMsgType.toString());
                    // Please note that REQUESTED_AUTH is not used to request authorization
                    // ALERT is used for that. REQUESTED_AUTH is used only for history
                    author = currMsg.getAuthor();
                    // Get timestamp -- it's in seconds, and the Date constructor needs milliseconds!
                    msgTimeStamp = currMsg.getTimestamp();
                    dateTimeStamp = new Date((msgTimeStamp * 1000L));
                    MySession.myConsole.printf("[%s] ", dateFmt.format(dateTimeStamp));
                    MySession.myConsole.printf("Authorization request from %s to ", author);
                    // List identities
                    doListIdentities(currMsg);
                    MySession.myConsole.println("");
                }
                else if (currMsgType == Message.Type.GRANTED_AUTH)
                {
                    MySession.myConsole.printf("%nProcessing message of type %s%n", currMsgType.toString());
                    author = currMsg.getAuthor();
                    // Get timestamp -- it's in seconds, and the Date constructor needs milliseconds!
                    msgTimeStamp = currMsg.getTimestamp();
                    dateTimeStamp = new Date((msgTimeStamp * 1000L));
                    MySession.myConsole.printf("[%s] ", dateFmt.format(dateTimeStamp));
                    MySession.myConsole.printf("%s granted authorization to ", author);
                    // List identities
                    doListIdentities(currMsg);
                    MySession.myConsole.println("");
                }

                else
                    MySession.myConsole.printf("%nIgnoring message of type %s%n", currMsgType.toString());
            }
        }
Esempio n. 4
0
        /**
         * Processes messages associated with a Conversation.
         * Processes <em>only</em> messages of type:
         * <ul>
         *   <li>STARTED_LIVESESSION&#8212;lists details of in-coming and out-going calls</li>
         *   <li>POSTED_VOICE_MESSAGE&#8212;lists details of in-coming and out-going voicemails</li>
         *   <li>REQUESTED_AUTH&#8212;lists Contact authorization requests</li>
         *   <li>GRANTED_AUTH&#8212;lists Contacts granted authorization</li>
         * </ul>
         *
         * @param mySession
         *	Populated session object
         * @param myMessages
         *	Array of message strings to process.
         *
         * @since 1.0
         */
        static void doRenderHistory(MySession mySession, Message[] myMessages)
        {
            ParsePosition s_pos;
            ParsePosition e_pos;

            int        msgTimeStamp;
            Date       dateTimeStamp;
            DateFormat dateFmt = DateFormat.getDateTimeInstance();

            String author;
            String bodyXml;
            int    bodyXmlLength;

            /*
             *         myXmlStrMgr.setVerboseDebug(true);
             */

            int     msgCount = myMessages.Length;
            Message currMsg;

            Message.Type currMsgType;
            MySession.myConsole.printf("%d ...%n", msgCount);
            for (int i = 0; i < msgCount; i++)
            {
                currMsg     = myMessages[i];
                currMsgType = currMsg.getType();

                if (currMsgType == Message.Type.STARTED_LIVE_SESSION)
                {
                    MySession.myConsole.printf("%nProcessing message of type %s%n", currMsgType.toString());
                    // Message.Property.P_AUTHOR tells who initiated the call.
                    author = currMsg.getAuthor();

                    // For duration we unfortunately have to parse the XML
                    // The duration we're interested in is
                    // <part identity="&me">%n...<duration>x</duration>...
                    //
                    // Real implementation should use a proper XML-parser here!
                    java.lang.StringBuffer partTag = new java.lang.StringBuffer("<part identity=\"");
                    partTag.append(mySession.myAccountName + "\">");
                    s_pos = myXmlStrMgr.getXmlSubStrPos(currMsg.getBodyXml(),
                                                        partTag.toString(), ZERO_POS);
                    if (s_pos == null)
                    {
                        MySession.myConsole.printf("%s: Could not find \"%s\" in xmlDoc%n%s%n%nSkipping...%n%n",
                                                   MY_CLASS_TAG, partTag.toString(),
                                                   currMsg.getBodyXml());
                        break;
                    }

                    int duration =
                        myXmlStrMgr.getXmlValueNum(currMsg.getBodyXml(),
                                                   "<duration>", s_pos);

                    // Ditto for counting how many parts the body has...
                    int num_parts = 0;
                    s_pos.setIndex(0);
                    do
                    {
                        e_pos = myXmlStrMgr.getXmlSubStrPos(currMsg.getBodyXml(),
                                                            "<part ", s_pos);
                        if (e_pos != null)
                        {
                            num_parts++;
                            s_pos.setIndex(e_pos.getIndex());
                        }
                    }while (e_pos != null);

                    // Get timestamp -- it's in seconds, and the Date constructor needs milliseconds!
                    msgTimeStamp  = currMsg.getTimestamp();
                    dateTimeStamp = new Date((msgTimeStamp * 1000L));
                    MySession.myConsole.printf("[%s] ", dateFmt.format(dateTimeStamp));
                    // Last part is to fetch message reason
                    String reason = currMsg.getReason();

                    if (author.Equals(mySession.myAccountName))
                    {
                        // I initiated the call
                        MySession.myConsole.println("outgoing call to ");
                    }
                    else
                    {
                        // Somebody else called me
                        MySession.myConsole.println("incoming call from ");
                    }

                    // List identities
                    doListIdentities(currMsg);

                    if (duration >= 0)
                    {
                        MySession.myConsole.printf("duration %d seconds", duration);
                    }
                    else if (num_parts > 1)
                    {
                        if (reason.Equals("manual"))
                        {
                            MySession.myConsole.printf("refused");
                        }
                        else
                        {
                            MySession.myConsole.printf("failed (%s)", reason);
                        }
                    }
                    else
                    {
                        MySession.myConsole.printf("missed");
                    }

                    MySession.myConsole.printf(" (%d parts).%n", num_parts);
                }
                else if (currMsgType == Message.Type.POSTED_VOICE_MESSAGE)
                {
                    MySession.myConsole.printf("%nProcessing message of type %s%n", currMsgType.toString());
                    author = currMsg.getAuthor();
                    // XML parsing again...
                    bodyXml       = currMsg.getBodyXml();
                    bodyXmlLength = myXmlStrMgr.getXmlValueNum(bodyXml, "<length>", ZERO_POS);
                    // Get timestamp -- it's in seconds, and the Date constructor needs milliseconds!
                    msgTimeStamp  = currMsg.getTimestamp();
                    dateTimeStamp = new Date((msgTimeStamp * 1000L));
                    MySession.myConsole.printf("[%s] ", dateFmt.format(dateTimeStamp));
                    if (author.Equals(mySession.myAccountName))
                    {
                        // I initiated the call
                        MySession.myConsole.println("Sent voicemail to ");
                    }
                    else
                    {
                        // Somebody else called me
                        MySession.myConsole.println("Got voicemail from ");
                    }
                    // List identities
                    doListIdentities(currMsg);
                    MySession.myConsole.printf("duration %d%n", bodyXmlLength);
                }
                else if (currMsgType == Message.Type.REQUESTED_AUTH)
                {
                    MySession.myConsole.printf("%nProcessing message of type %s%n", currMsgType.toString());
                    // Please note that REQUESTED_AUTH is not used to request authorization
                    // ALERT is used for that. REQUESTED_AUTH is used only for history
                    author = currMsg.getAuthor();
                    // Get timestamp -- it's in seconds, and the Date constructor needs milliseconds!
                    msgTimeStamp  = currMsg.getTimestamp();
                    dateTimeStamp = new Date((msgTimeStamp * 1000L));
                    MySession.myConsole.printf("[%s] ", dateFmt.format(dateTimeStamp));
                    MySession.myConsole.printf("Authorization request from %s to ", author);
                    // List identities
                    doListIdentities(currMsg);
                    MySession.myConsole.println("");
                }
                else if (currMsgType == Message.Type.GRANTED_AUTH)
                {
                    MySession.myConsole.printf("%nProcessing message of type %s%n", currMsgType.toString());
                    author = currMsg.getAuthor();
                    // Get timestamp -- it's in seconds, and the Date constructor needs milliseconds!
                    msgTimeStamp  = currMsg.getTimestamp();
                    dateTimeStamp = new Date((msgTimeStamp * 1000L));
                    MySession.myConsole.printf("[%s] ", dateFmt.format(dateTimeStamp));
                    MySession.myConsole.printf("%s granted authorization to ", author);
                    // List identities
                    doListIdentities(currMsg);
                    MySession.myConsole.println("");
                }

                else
                {
                    MySession.myConsole.printf("%nIgnoring message of type %s%n", currMsgType.toString());
                }
            }
        }
Esempio n. 5
0
        public void TestParse() {


            var jParser = CreateJavaParser(modelFile);
            var sParser = CreateSharpParser(modelFile);


            var sentences = new[] {
                "Let all your things have their places; let each part of your business have its time.",
                "It has become appallingly obvious that our technology has exceeded our humanity.",
                "The real problem is not whether machines think but whether men do.",  
                "The worst form of inequality is to try to make unequal things equal.",
                "People won't have time for you if you are always angry or complaining.",
                "To keep the body in good health is a duty... otherwise we shall not be able to keep our mind strong and clear.",
                "You, yourself, as much as anybody in the entire universe, deserve your love and affection."
            };


            foreach (var sentence in sentences) {

                var jParses = opennlp.tools.cmdline.parser.ParserTool.parseLine(sentence, jParser, 1);
                var sParses = ParserTool.ParseLine(sentence, sParser, 1);

                var jsb = new java.lang.StringBuffer();

                jParses[0].show(jsb);

                Assert.AreEqual(jsb.toString(), sParses[0].ToString());

                jParses[0].show(jsb);
            }
        }