コード例 #1
0
            /// <summary>
            /// Services this thread's client by first sending the
            /// client a welcome message then repeatedly reading strings
            /// and sending back the capitalized version of the string.
            /// </summary>
            public override void Run()
            {
                PrintWriter @out = null;
                string      msg  = string.Empty;
                // Decorate the streams so we can send characters
                // and not just bytes.  Ensure output is flushed
                // after every newline.
                BufferedReader @in = null;

                try
                {
                    @in  = new BufferedReader(new InputStreamReader(socket.GetInputStream()));
                    @out = new PrintWriter(socket.GetOutputStream(), true);
                }
                catch (IOException e)
                {
                    try
                    {
                        socket.Close();
                    }
                    catch (IOException e1)
                    {
                        Sharpen.Runtime.PrintStackTrace(e1);
                    }
                    Sharpen.Runtime.PrintStackTrace(e);
                }
                // Send a welcome message to the client.
                @out.Println("The possible actions are " + Arrays.ToString(TextAnnotationPatternsInterface.Actions.Values()) + ".Enter a line with only a period to quit");
                TextAnnotationPatternsInterface.Actions nextlineAction = TextAnnotationPatternsInterface.Actions.None;
                // Get messages from the client, line by line; return them
                // capitalized
                while (true)
                {
                    try
                    {
                        string line = @in.ReadLine();
                        if (line == null || line.Equals("."))
                        {
                            break;
                        }
                        string[] toks = line.Split("###");
                        try
                        {
                            nextlineAction = TextAnnotationPatternsInterface.Actions.ValueOf(toks[0].Trim());
                        }
                        catch (ArgumentException)
                        {
                            System.Console.Out.WriteLine("read " + toks[0] + " and cannot understand");
                            msg = "Did not understand " + toks[0] + ". POSSIBLE ACTIONS ARE: " + Arrays.ToString(TextAnnotationPatternsInterface.Actions.Values());
                        }
                        string input = toks.Length == 2 ? toks[1] : null;
                        switch (nextlineAction)
                        {
                        case TextAnnotationPatternsInterface.Actions.Newphrases:
                        {
                            msg = annotate.DoNewPhrases(input);
                            break;
                        }

                        case TextAnnotationPatternsInterface.Actions.Removephrases:
                        {
                            msg = annotate.DoRemovePhrases(input);
                            break;
                        }

                        case TextAnnotationPatternsInterface.Actions.Newannotations:
                        {
                            msg = annotate.DoNewAnnotations(input);
                            break;
                        }

                        case TextAnnotationPatternsInterface.Actions.Processfile:
                        {
                            annotate.SetUpProperties(input, true, true, null);
                            msg = annotate.ProcessText(true);
                            break;
                        }

                        case TextAnnotationPatternsInterface.Actions.Removeannotations:
                        {
                            msg = annotate.DoRemoveAnnotations(input);
                            break;
                        }

                        case TextAnnotationPatternsInterface.Actions.Suggest:
                        {
                            msg = annotate.SuggestPhrases();
                            break;
                        }

                        case TextAnnotationPatternsInterface.Actions.Matchedtokensbyall:
                        {
                            msg = annotate.GetMatchedTokensByAllPhrases();
                            break;
                        }

                        case TextAnnotationPatternsInterface.Actions.Matchedtokensbyphrase:
                        {
                            msg = annotate.GetMatchedTokensByPhrase(input);
                            break;
                        }

                        case TextAnnotationPatternsInterface.Actions.Allannotations:
                        {
                            msg = annotate.GetAllAnnotations();
                            break;
                        }

                        case TextAnnotationPatternsInterface.Actions.Annotationsbysent:
                        {
                            msg = annotate.GetAllAnnotations(input);
                            break;
                        }

                        case TextAnnotationPatternsInterface.Actions.Summary:
                        {
                            msg = annotate.CurrentSummary();
                            break;
                        }

                        case TextAnnotationPatternsInterface.Actions.None:
                        {
                            break;
                        }

                        case TextAnnotationPatternsInterface.Actions.Close:
                        {
                            msg = "bye!";
                            break;
                        }
                        }
                        System.Console.Out.WriteLine("sending msg " + msg);
                    }
                    catch (Exception e)
                    {
                        msg            = "ERROR " + e.ToString().ReplaceAll("\n", "\t") + ". REDO.";
                        nextlineAction = TextAnnotationPatternsInterface.Actions.None;
                        Log("Error handling client# " + clientNumber);
                        Sharpen.Runtime.PrintStackTrace(e);
                    }
                    finally
                    {
                        @out.Println(msg);
                    }
                }
            }
コード例 #2
0
        /// <summary>Actually perform the GET request, given all the relevant information (already sanity checked).</summary>
        /// <remarks>
        /// Actually perform the GET request, given all the relevant information (already sanity checked).
        /// This is the meat of the servlet code.
        /// </remarks>
        /// <param name="out">The writer to write the output to.</param>
        /// <param name="q">The query string.</param>
        /// <exception cref="System.Exception"/>
        private void Run(PrintWriter @out, string q, string seedWords, string model)
        {
            // Clean the string a bit
            q = q.Trim();
            if (q.Length == 0)
            {
                return;
            }
            char lastChar = q[q.Length - 1];

            if (lastChar != '.' && lastChar != '!' && lastChar != '?')
            {
                q = q + ".";
            }
            TextAnnotationPatterns annotate = new TextAnnotationPatterns();
            string quotedString             = Quote(q);
            string jsonObject = "{\"input\":" + quotedString + ",\"seedWords\":{\"NAME\":[\"" + StringUtils.Join(seedWords.Split("[,;]"), "\",\"") + "\"]}}";
            bool   testmode   = true;

            if (Sharpen.Runtime.EqualsIgnoreCase(model, "new"))
            {
                testmode = false;
            }
            logger.Info("Testmode is " + testmode);
            logger.Info("model is " + model);
            string suggestions;

            // Collect results
            if (testmode)
            {
                Properties testProps = new Properties();
                if (testPropertiesFile != null && new File(testPropertiesFile).Exists())
                {
                    try
                    {
                        string props = IOUtils.StringFromFile(testPropertiesFile);
                        testProps.Load(new StringReader(props));
                    }
                    catch (IOException e)
                    {
                        WriteError(e, @out, "Cannot read test properties file");
                        return;
                    }
                }
                else
                {
                    WriteError(new Exception("test prop file not found"), @out, "Test properties file not found");
                    return;
                }
                string modelDir = GetServletContext().GetRealPath("/WEB-INF/data/" + modelNametoDirName[model]);
                testProps.SetProperty("patternsWordsDir", modelDir);
                logger.Info("Reading saved model from " + modelDir);
                string seedWordsFiles      = "NAME," + modelDir + "/NAME/seedwords.txt," + modelDir + "/NAME/phrases.txt";
                string modelPropertiesFile = modelDir + "/model.properties";
                logger.Info("Loading model properties from " + modelPropertiesFile);
                string stopWordsFile   = modelDir + "/stopwords.txt";
                bool   writeOutputFile = false;
                annotate.SetUpProperties(jsonObject, false, writeOutputFile, seedWordsFiles);
                suggestions = annotate.SuggestPhrasesTest(testProps, modelPropertiesFile, stopWordsFile);
            }
            else
            {
                bool writeOutputFile = false;
                annotate.SetUpProperties(jsonObject, false, writeOutputFile, null);
                annotate.ProcessText(writeOutputFile);
                suggestions = annotate.SuggestPhrases();
            }
            @out.Print(suggestions);
        }