Exemplo n.º 1
0
 public virtual void PrintSentences(IEnumerable <IList <IHasWord> > sentences, string filename)
 {
     try
     {
         PrintWriter pw = IOUtils.GetPrintWriter(filename);
         foreach (IList <IHasWord> sentence in sentences)
         {
             pw.Print("<s> ");
             // Note: Use <s sentence-id > to identify sentences
             string sentString = SentenceUtils.ListToString(sentence);
             if (sentence.Count > maxSentenceLength)
             {
                 logger.Warning("Sentence length=" + sentence.Count + " is longer than maximum set length " + maxSentenceLength);
                 logger.Warning("Long Sentence: " + sentString);
             }
             pw.Print(sentString);
             pw.Println(" </s>");
         }
         pw.Close();
     }
     catch (IOException ex)
     {
         throw new Exception(ex);
     }
 }
        /*for(int i = 0; i < data.length; i++) {
         * for(int j = 0; j < data[i].length; j++) {
         * System.out.println(data[i][j]);
         * }
         * }*/
        /// <summary>prints the full feature matrix in tab-delimited form.</summary>
        /// <remarks>
        /// prints the full feature matrix in tab-delimited form. These can be BIG
        /// matrices, so be careful! [Can also use printFullFeatureMatrixWithValues]
        /// </remarks>
        public virtual void PrintFullFeatureMatrix(PrintWriter pw)
        {
            string sep = "\t";

            for (int i = 0; i < featureIndex.Size(); i++)
            {
                pw.Print(sep + featureIndex.Get(i));
            }
            pw.Println();
            for (int i_1 = 0; i_1 < labels.Length; i_1++)
            {
                pw.Print(labelIndex.Get(i_1));
                ICollection <int> feats = Generics.NewHashSet();
                for (int j = 0; j < data[i_1].Length; j++)
                {
                    int feature = data[i_1][j];
                    feats.Add(int.Parse(feature));
                }
                for (int j_1 = 0; j_1 < featureIndex.Size(); j_1++)
                {
                    if (feats.Contains(int.Parse(j_1)))
                    {
                        pw.Print(sep + "1");
                    }
                    else
                    {
                        pw.Print(sep + "0");
                    }
                }
                pw.Println();
            }
        }
Exemplo n.º 3
0
        /// <exception cref="System.IO.IOException"/>
        private void AddResults(IHttpServletRequest request, IHttpServletResponse response)
        {
            string input = request.GetParameter("input");

            if (input == null)
            {
                return;
            }
            input = input.Trim();
            if (input.IsEmpty())
            {
                return;
            }
            PrintWriter @out = response.GetWriter();

            if (input.Length > MaximumQueryLength)
            {
                @out.Print("This query is too long.  If you want to run very long queries, please download and use our <a href=\"http://nlp.stanford.edu/software/CRF-NER.html\">publicly released distribution</a>.");
                return;
            }
            string outputFormat = request.GetParameter("outputFormat");

            if (outputFormat == null || outputFormat.Trim().IsEmpty())
            {
                outputFormat = this.format;
            }
            bool   preserveSpacing;
            string preserveSpacingStr = request.GetParameter("preserveSpacing");

            if (preserveSpacingStr == null || preserveSpacingStr.Trim().IsEmpty())
            {
                preserveSpacing = this.spacing;
            }
            else
            {
                preserveSpacingStr = preserveSpacingStr.Trim();
                preserveSpacing    = bool.ValueOf(preserveSpacingStr);
            }
            string classifier = request.GetParameter("classifier");

            if (classifier == null || classifier.Trim().IsEmpty())
            {
                classifier = this.defaultClassifier;
            }
            response.AddHeader("classifier", classifier);
            response.AddHeader("outputFormat", outputFormat);
            response.AddHeader("preserveSpacing", preserveSpacing.ToString());
            if (outputFormat.Equals("highlighted"))
            {
                OutputHighlighting(@out, ners[classifier], input);
            }
            else
            {
                @out.Print(StringEscapeUtils.EscapeHtml4(ners[classifier].ClassifyToString(input, outputFormat, preserveSpacing)));
            }
        }
 private static void PrintAnswersTokenizedTextTsv <In>(IList <In> l, PrintWriter @out)
     where In : ICoreMap
 {
     foreach (IN wi in l)
     {
         @out.Print(StringUtils.GetNotNullString(wi.Get(typeof(CoreAnnotations.TextAnnotation))));
         @out.Print('\t');
         @out.Println(StringUtils.GetNotNullString(wi.Get(typeof(CoreAnnotations.AnswerAnnotation))));
     }
     @out.Println();
 }
        public virtual void PrintAnswers(IList <CoreLabel> doc, PrintWriter pw)
        {
            string prevAnswer = "O";
            string prevClass  = string.Empty;
            string afterLast  = string.Empty;

            foreach (CoreLabel word in doc)
            {
                if (!prevAnswer.Equals("O") && !prevAnswer.Equals(word.Get(typeof(CoreAnnotations.AnswerAnnotation))))
                {
                    pw.Print("</" + prevClass + ">");
                    prevClass = string.Empty;
                }
                pw.Print(word.Get(typeof(CoreAnnotations.BeforeAnnotation)));
                if (!word.Get(typeof(CoreAnnotations.AnswerAnnotation)).Equals("O") && !word.Get(typeof(CoreAnnotations.AnswerAnnotation)).Equals(prevAnswer))
                {
                    if (Sharpen.Runtime.EqualsIgnoreCase(word.Get(typeof(CoreAnnotations.AnswerAnnotation)), "PERSON") || Sharpen.Runtime.EqualsIgnoreCase(word.Get(typeof(CoreAnnotations.AnswerAnnotation)), "ORGANIZATION") || Sharpen.Runtime.EqualsIgnoreCase(word
                                                                                                                                                                                                                                                                   .Get(typeof(CoreAnnotations.AnswerAnnotation)), "LOCATION"))
                    {
                        prevClass = "ENAMEX";
                    }
                    else
                    {
                        if (Sharpen.Runtime.EqualsIgnoreCase(word.Get(typeof(CoreAnnotations.AnswerAnnotation)), "DATE") || Sharpen.Runtime.EqualsIgnoreCase(word.Get(typeof(CoreAnnotations.AnswerAnnotation)), "TIME"))
                        {
                            prevClass = "TIMEX";
                        }
                        else
                        {
                            if (Sharpen.Runtime.EqualsIgnoreCase(word.Get(typeof(CoreAnnotations.AnswerAnnotation)), "PERCENT") || Sharpen.Runtime.EqualsIgnoreCase(word.Get(typeof(CoreAnnotations.AnswerAnnotation)), "MONEY"))
                            {
                                prevClass = "NUMEX";
                            }
                            else
                            {
                                log.Info("unknown type: " + word.Get(typeof(CoreAnnotations.AnswerAnnotation)));
                                System.Environment.Exit(0);
                            }
                        }
                    }
                    pw.Print("<" + prevClass + " TYPE=\"" + word.Get(typeof(CoreAnnotations.AnswerAnnotation)) + "\">");
                }
                pw.Print(word.Get(typeof(CoreAnnotations.OriginalTextAnnotation)));
                afterLast  = word.Get(typeof(CoreAnnotations.AfterAnnotation));
                prevAnswer = word.Get(typeof(CoreAnnotations.AnswerAnnotation));
            }
            if (!prevAnswer.Equals("O"))
            {
                pw.Print("</" + prevClass + ">");
                prevClass = string.Empty;
            }
            pw.Println(afterLast);
        }
 // put a single newline at the end [added 20091024].
 private static void PrintAnswersAsIsText <In>(IList <In> l, PrintWriter @out)
     where In : ICoreMap
 {
     foreach (IN wi in l)
     {
         @out.Print(StringUtils.GetNotNullString(wi.Get(typeof(CoreAnnotations.BeforeAnnotation))));
         @out.Print(StringUtils.GetNotNullString(wi.Get(typeof(CoreAnnotations.TextAnnotation))));
         @out.Print('/');
         @out.Print(StringUtils.GetNotNullString(wi.Get(typeof(CoreAnnotations.AnswerAnnotation))));
         @out.Print(StringUtils.GetNotNullString(wi.Get(typeof(CoreAnnotations.AfterAnnotation))));
     }
 }
        /// <summary>
        /// <inheritDoc/>
        ///
        /// </summary>
        public override void PrintSparseFeatureMatrix(PrintWriter pw)
        {
            string sep = "\t";

            for (int i = 0; i < size; i++)
            {
                pw.Print(labelIndex.Get(labels[i]));
                int[] datum = data[i];
                foreach (int j in datum)
                {
                    pw.Print(sep + featureIndex.Get(j));
                }
                pw.Println();
            }
        }
        //logger.info(lwi.get(i));
        public virtual void PrintAnswers(IList <CoreLabel> doc, PrintWriter pw)
        {
            string ansStr = ChineseStringUtils.CombineSegmentedSentence(doc, flags);

            pw.Print(ansStr);
            pw.Println();
        }
Exemplo n.º 9
0
 public void SendToPrinter(string val)
 {
     try
     {
         Socket socket = new Socket("192.168.8.105", 1100);
         // Exmp : Socket socket = new Socket("192.168.0.101", 80);
         PrintWriter oStream = new PrintWriter(socket.OutputStream);
         oStream.Print("\t\t Text to The Printer");
         oStream.Print("\n\n\n");
         oStream.Close();
         socket.Close();
     }
     catch (Exception e)
     {
     }
 }
Exemplo n.º 10
0
        /// <summary><inheritDoc/></summary>
        /// <exception cref="Javax.Servlet.ServletException"/>
        /// <exception cref="System.IO.IOException"/>
        protected override void DoGet(IHttpServletRequest request, IHttpServletResponse response)
        {
            logger.Info("GET SPIED query from " + request.GetRemoteAddr());
            if (request.GetCharacterEncoding() == null)
            {
                request.SetCharacterEncoding("utf-8");
            }
            response.SetContentType("text/json; charset=UTF-8");
            PrintWriter @out = response.GetWriter();

            try
            {
                string raw       = request.GetParameter("q");
                string seedwords = request.GetParameter("seedwords");
                string model     = request.GetParameter("model");
                if (raw == null || string.Empty.Equals(raw))
                {
                    @out.Print("{\"okay\":false,\"reason\":\"No data provided\"}");
                }
                else
                {
                    Run(@out, raw, seedwords, model);
                }
            }
            catch (Exception t)
            {
                WriteError(t, @out, request.ToString());
            }
            @out.Close();
        }
 /// <summary>Prints a sparse feature-value output of the Dataset.</summary>
 /// <remarks>
 /// Prints a sparse feature-value output of the Dataset. Prints the actual
 /// <see cref="object.ToString()"/>
 /// representations of features. This is probably
 /// what you want for RVFDataset since the above two methods seem useless and
 /// unused.
 /// </remarks>
 public virtual void PrintSparseFeatureValues(int datumNo, PrintWriter pw)
 {
     pw.Print(labelIndex.Get(labels[datumNo]));
     pw.Print('\t');
     pw.Println("LABEL");
     int[]    datum = data[datumNo];
     double[] vals  = values[datumNo];
     System.Diagnostics.Debug.Assert(datum.Length == vals.Length);
     for (int i = 0; i < datum.Length; i++)
     {
         pw.Print(featureIndex.Get(datum[i]));
         pw.Print('\t');
         pw.Println(vals[i]);
     }
     pw.Println();
 }
Exemplo n.º 12
0
            public virtual void RecordScore(IKBestViterbiParser parser, PrintWriter pw)
            {
                double score = parser.GetBestScore();

                totScore += score;
                n++;
                if (pw != null)
                {
                    pw.Print(str + " score: " + nf.Format(score));
                    if (runningAverages)
                    {
                        pw.Print(" average score: " + nf.Format(totScore / n));
                    }
                    pw.Println();
                }
            }
        /// <exception cref="System.IO.IOException"/>
        private static void Print(Annotation annotation, PrintWriter pw, AnnotationOutputter.Options options)
        {
            IList <ICoreMap> sentences = annotation.Get(typeof(CoreAnnotations.SentencesAnnotation));

            if (sentences != null)
            {
                for (int i = 0; i < sentences.Count; i++)
                {
                    ICoreMap      sentence        = sentences[i];
                    StringBuilder sentenceToWrite = new StringBuilder();
                    foreach (CoreLabel token in sentence.Get(typeof(CoreAnnotations.TokensAnnotation)))
                    {
                        sentenceToWrite.Append(" ");
                        sentenceToWrite.Append(token.Lemma().ToLower());
                        if (token.Get(typeof(CoreAnnotations.PartOfSpeechAnnotation)).StartsWith("V"))
                        {
                            //verb
                            sentenceToWrite.Append("_V");
                        }
                        else
                        {
                            if (token.Get(typeof(CoreAnnotations.PartOfSpeechAnnotation)).StartsWith("N"))
                            {
                                //noun
                                sentenceToWrite.Append("_N");
                            }
                        }
                    }
                    pw.Print(sentenceToWrite.ToString());
                }
            }
        }
Exemplo n.º 14
0
        private static void CountTaggings(Treebank tb, PrintWriter pw)
        {
            TwoDimensionalCounter <string, string> wtc = new TwoDimensionalCounter <string, string>();

            tb.Apply(null);
            foreach (string key in wtc.FirstKeySet())
            {
                pw.Print(key);
                pw.Print('\t');
                ICounter <string> ctr = wtc.GetCounter(key);
                foreach (string k2 in ctr.KeySet())
                {
                    pw.Print(k2 + '\t' + ctr.GetCount(k2) + '\t');
                }
                pw.Println();
            }
        }
        private void PrintAnswersTokenizedTextTabbed(IList <In> l, PrintWriter @out)
        {
            string background     = flags.backgroundSymbol;
            string lastEntityType = null;

            foreach (IN wi in l)
            {
                string entityType = wi.Get(typeof(CoreAnnotations.AnswerAnnotation));
                string token      = StringUtils.GetNotNullString(wi.Get(typeof(CoreAnnotations.TextAnnotation)));
                if (entityType.Equals(lastEntityType))
                {
                    // continue the same entity in column 1 or 3
                    @out.Print(' ');
                    @out.Print(token);
                }
                else
                {
                    if (lastEntityType != null && !background.Equals(lastEntityType))
                    {
                        // different entity type.  If previous not background/start, write in column 2
                        @out.Print('\t');
                        @out.Print(lastEntityType);
                    }
                    if (background.Equals(entityType))
                    {
                        // we'll print it in column 3. Normally, we're in column 2, unless we were at the start of doc
                        if (lastEntityType == null)
                        {
                            @out.Print('\t');
                        }
                        @out.Print('\t');
                    }
                    else
                    {
                        // otherwise we're printing in column 1 again
                        @out.Println();
                    }
                    @out.Print(token);
                    lastEntityType = entityType;
                }
            }
            // if we're in the middle of printing an entity, then we should print its type
            if (lastEntityType != null && !background.Equals(lastEntityType))
            {
                @out.Print('\t');
                @out.Print(lastEntityType);
            }
            // finish line then add blank line
            @out.Println();
            @out.Println();
        }
Exemplo n.º 16
0
        /// <exception cref="System.IO.IOException"/>
        public override void Print(Annotation doc, OutputStream target, AnnotationOutputter.Options options)
        {
            PrintWriter      writer    = new PrintWriter(IOUtils.EncodedOutputStreamWriter(target, options.encoding));
            IList <ICoreMap> sentences = doc.Get(typeof(CoreAnnotations.SentencesAnnotation));

            foreach (ICoreMap sentence in sentences)
            {
                SemanticGraph sg = sentence.Get(typeof(SemanticGraphCoreAnnotations.BasicDependenciesAnnotation));
                if (sg != null)
                {
                    writer.Print(conllUWriter.PrintSemanticGraph(sg));
                }
                else
                {
                    writer.Print(conllUWriter.PrintPOSAnnotations(sentence));
                }
            }
            writer.Flush();
        }
Exemplo n.º 17
0
        internal virtual void WriteError(Exception t, PrintWriter @out, string input)
        {
            StringWriter sw = new StringWriter();
            PrintWriter  pw = new PrintWriter(sw);

            Sharpen.Runtime.PrintStackTrace(t, pw);
            logger.Info("input is " + input);
            logger.Info(sw.ToString());
            @out.Print("{\"okay\":false, \"reason\":\"Something bad happened. Contact the author.\"}");
        }
Exemplo n.º 18
0
        public virtual void Evaluate(Tree guess, Tree gold, PrintWriter pw, double weight)
        {
            ICollection <object> dep1 = MakeObjects(guess);
            ICollection <object> dep2 = MakeObjects(gold);
            double curPrecision       = Precision(dep1, dep2);
            double curRecall          = Precision(dep2, dep1);

            curF1       = (curPrecision > 0.0 && curRecall > 0.0 ? 2.0 / (1.0 / curPrecision + 1.0 / curRecall) : 0.0);
            precision  += curPrecision * weight;
            recall     += curRecall * weight;
            f1         += curF1 * weight;
            num        += weight;
            precision2 += dep1.Count * curPrecision * weight;
            pnum2      += dep1.Count * weight;
            recall2    += dep2.Count * curRecall * weight;
            rnum2      += dep2.Count * weight;
            if (curF1 > 0.9999)
            {
                exact += 1.0;
            }
            if (pw != null)
            {
                pw.Print(" P: " + ((int)(curPrecision * 10000)) / 100.0);
                if (runningAverages)
                {
                    pw.Println(" (sent ave " + ((int)(precision * 10000 / num)) / 100.0 + ") (evalb " + ((int)(precision2 * 10000 / pnum2)) / 100.0 + ")");
                }
                pw.Print(" R: " + ((int)(curRecall * 10000)) / 100.0);
                if (runningAverages)
                {
                    pw.Print(" (sent ave " + ((int)(recall * 10000 / num)) / 100.0 + ") (evalb " + ((int)(recall2 * 10000 / rnum2)) / 100.0 + ")");
                }
                pw.Println();
                double cF1 = 2.0 / (rnum2 / recall2 + pnum2 / precision2);
                pw.Print(str + " F1: " + ((int)(curF1 * 10000)) / 100.0);
                if (runningAverages)
                {
                    pw.Print(" (sent ave " + ((int)(10000 * f1 / num)) / 100.0 + ", evalb " + ((int)(10000 * cF1)) / 100.0 + ")   Exact: " + ((int)(10000 * exact / num)) / 100.0);
                }
                //      pw.println(" N: " + getNum());
                pw.Println(" N: " + num);
            }
        }
Exemplo n.º 19
0
 private static void EmitSortedTrees(PriorityQueue <Triple <double, Tree, Tree> > queue, int worstKTreesToEmit, string filePrefix)
 {
     if (queue == null)
     {
         log.Info("Queue was not initialized properly");
     }
     try
     {
         PrintWriter         guessPw    = new PrintWriter(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filePrefix + ".kworst.guess"), "UTF-8")));
         PrintWriter         goldPw     = new PrintWriter(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filePrefix + ".kworst.gold"), "UTF-8")));
         IConstituentFactory cFact      = new LabeledScoredConstituentFactory();
         PrintWriter         guessDepPw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filePrefix + ".kworst.guess.deps"), "UTF-8")));
         PrintWriter         goldDepPw  = new PrintWriter(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filePrefix + ".kworst.gold.deps"), "UTF-8")));
         System.Console.Out.Printf("F1s of %d worst trees:\n", worstKTreesToEmit);
         for (int i = 0; queue.Peek() != null && i < worstKTreesToEmit; i++)
         {
             Triple <double, Tree, Tree> trees = queue.Poll();
             System.Console.Out.WriteLine(trees.First());
             //Output the trees
             goldPw.Println(trees.Second().ToString());
             guessPw.Println(trees.Third().ToString());
             //Output the set differences
             ICollection <Constituent> goldDeps = Generics.NewHashSet();
             Sharpen.Collections.AddAll(goldDeps, trees.Second().Constituents(cFact));
             goldDeps.RemoveAll(trees.Third().Constituents(cFact));
             foreach (Constituent c in goldDeps)
             {
                 goldDepPw.Print(c.ToString() + "  ");
             }
             goldDepPw.Println();
             ICollection <Constituent> guessDeps = Generics.NewHashSet();
             Sharpen.Collections.AddAll(guessDeps, trees.Third().Constituents(cFact));
             guessDeps.RemoveAll(trees.Second().Constituents(cFact));
             foreach (Constituent c_1 in guessDeps)
             {
                 guessDepPw.Print(c_1.ToString() + "  ");
             }
             guessDepPw.Println();
         }
         guessPw.Close();
         goldPw.Close();
         goldDepPw.Close();
         guessDepPw.Close();
     }
     catch (UnsupportedEncodingException e)
     {
         Sharpen.Runtime.PrintStackTrace(e);
     }
     catch (FileNotFoundException e)
     {
         Sharpen.Runtime.PrintStackTrace(e);
     }
 }
Exemplo n.º 20
0
        /// <summary>Print an Annotation to an output stream.</summary>
        /// <remarks>
        /// Print an Annotation to an output stream.
        /// The target OutputStream is assumed to already by buffered.
        /// </remarks>
        /// <param name="doc"/>
        /// <param name="target"/>
        /// <param name="options"/>
        /// <exception cref="System.IO.IOException"/>
        public override void Print(Annotation doc, OutputStream target, AnnotationOutputter.Options options)
        {
            PrintWriter writer = new PrintWriter(IOUtils.EncodedOutputStreamWriter(target, options.encoding));

            // vv A bunch of nonsense to get tokens vv
            if (doc.Get(typeof(CoreAnnotations.SentencesAnnotation)) != null)
            {
                foreach (ICoreMap sentence in doc.Get(typeof(CoreAnnotations.SentencesAnnotation)))
                {
                    if (sentence.Get(typeof(CoreAnnotations.TokensAnnotation)) != null)
                    {
                        IList <CoreLabel> tokens  = sentence.Get(typeof(CoreAnnotations.TokensAnnotation));
                        SemanticGraph     depTree = sentence.Get(typeof(SemanticGraphCoreAnnotations.BasicDependenciesAnnotation));
                        for (int i = 0; i < tokens.Count; ++i)
                        {
                            // ^^ end nonsense to get tokens ^^
                            // Try to get the incoming dependency edge
                            int    head   = -1;
                            string deprel = null;
                            if (depTree != null)
                            {
                                ICollection <int> rootSet = depTree.GetRoots().Stream().Map(null).Collect(Collectors.ToSet());
                                IndexedWord       node    = depTree.GetNodeByIndexSafe(i + 1);
                                if (node != null)
                                {
                                    IList <SemanticGraphEdge> edgeList = depTree.GetIncomingEdgesSorted(node);
                                    if (!edgeList.IsEmpty())
                                    {
                                        System.Diagnostics.Debug.Assert(edgeList.Count == 1);
                                        head   = edgeList[0].GetGovernor().Index();
                                        deprel = edgeList[0].GetRelation().ToString();
                                    }
                                    else
                                    {
                                        if (rootSet.Contains(i + 1))
                                        {
                                            head   = 0;
                                            deprel = "ROOT";
                                        }
                                    }
                                }
                            }
                            // Write the token
                            writer.Print(Line(i + 1, tokens[i], head, deprel));
                            writer.Println();
                        }
                    }
                    writer.Println();
                }
            }
            // extra blank line at end of sentence
            writer.Flush();
        }
Exemplo n.º 21
0
 private void PrettyPrint(PrintWriter pw, int indent)
 {
     for (int i = 0; i < indent; i++)
     {
         pw.Print("   ");
     }
     pw.Println(LocalString());
     foreach (Edu.Stanford.Nlp.Semgraph.Semgrex.SemgrexPattern child in GetChildren())
     {
         child.PrettyPrint(pw, indent + 1);
     }
 }
        public virtual void PrintAnswers(IList <CoreLabel> doc, PrintWriter @out)
        {
            IList <string> sentence = new List <string>();
            int            wrong    = 0;

            foreach (CoreLabel wi in doc)
            {
                StringBuilder sb = new StringBuilder();
                if (!wi.Get(typeof(CoreAnnotations.AnswerAnnotation)).Equals(wi.Get(typeof(CoreAnnotations.GoldAnswerAnnotation))))
                {
                    wrong++;
                }
                if (!ThreeClasses && wi.Get(typeof(CoreAnnotations.AnswerAnnotation)).Equals("UPPER"))
                {
                    sb.Append(wi.Word().ToUpper());
                }
                else
                {
                    if (wi.Get(typeof(CoreAnnotations.AnswerAnnotation)).Equals("LOWER"))
                    {
                        sb.Append(wi.Word().ToLower());
                    }
                    else
                    {
                        if (wi.Get(typeof(CoreAnnotations.AnswerAnnotation)).Equals("INIT_UPPER"))
                        {
                            sb.Append(Sharpen.Runtime.Substring(wi.Word(), 0, 1).ToUpper()).Append(Sharpen.Runtime.Substring(wi.Word(), 1));
                        }
                        else
                        {
                            if (wi.Get(typeof(CoreAnnotations.AnswerAnnotation)).Equals("O"))
                            {
                                // in this case, if it contains a-z at all, then append "MIX" at the end
                                sb.Append(wi.Word());
                                Matcher alphaMatcher = alphabet.Matcher(wi.Word());
                                if (alphaMatcher.Matches())
                                {
                                    sb.Append("/MIX");
                                }
                            }
                        }
                    }
                }
                if (verboseForTrueCasing)
                {
                    sb.Append("/GOLD-").Append(wi.Get(typeof(CoreAnnotations.GoldAnswerAnnotation))).Append("/GUESS-").Append(wi.Get(typeof(CoreAnnotations.AnswerAnnotation)));
                }
                sentence.Add(sb.ToString());
            }
            @out.Print(StringUtils.Join(sentence, " "));
            System.Console.Error.Printf("> wrong = %d ; total = %d%n", wrong, doc.Count);
            @out.Println();
        }
Exemplo n.º 23
0
        private static void OutputHighlighting(PrintWriter @out, CRFClassifier <ICoreMap> classifier, string input)
        {
            ICollection <string> labels               = classifier.Labels();
            string background                         = classifier.BackgroundSymbol();
            IList <IList <ICoreMap> >   sentences     = classifier.Classify(input);
            IDictionary <string, Color> tagToColorMap = NERGUI.MakeTagToColorMap(labels, background);
            StringBuilder result                      = new StringBuilder();
            int           lastEndOffset               = 0;

            foreach (IList <ICoreMap> sentence in sentences)
            {
                foreach (ICoreMap word in sentence)
                {
                    int    beginOffset = word.Get(typeof(CoreAnnotations.CharacterOffsetBeginAnnotation));
                    int    endOffset   = word.Get(typeof(CoreAnnotations.CharacterOffsetEndAnnotation));
                    string answer      = word.Get(typeof(CoreAnnotations.AnswerAnnotation));
                    if (beginOffset > lastEndOffset)
                    {
                        result.Append(StringEscapeUtils.EscapeHtml4(Sharpen.Runtime.Substring(input, lastEndOffset, beginOffset)));
                    }
                    // Add a color bar for any tagged words
                    if (!background.Equals(answer))
                    {
                        Color color = tagToColorMap[answer];
                        result.Append("<span style=\"color:#ffffff;background:" + NERGUI.ColorToHTML(color) + "\">");
                    }
                    result.Append(StringEscapeUtils.EscapeHtml4(Sharpen.Runtime.Substring(input, beginOffset, endOffset)));
                    // Turn off the color bar
                    if (!background.Equals(answer))
                    {
                        result.Append("</span>");
                    }
                    lastEndOffset = endOffset;
                }
            }
            if (lastEndOffset < input.Length)
            {
                result.Append(StringEscapeUtils.EscapeHtml4(Sharpen.Runtime.Substring(input, lastEndOffset)));
            }
            result.Append("<br><br>");
            result.Append("Potential tags:");
            foreach (KeyValuePair <string, Color> stringColorEntry in tagToColorMap)
            {
                result.Append("<br>&nbsp;&nbsp;");
                Color color = stringColorEntry.Value;
                result.Append("<span style=\"color:#ffffff;background:" + NERGUI.ColorToHTML(color) + "\">");
                result.Append(StringEscapeUtils.EscapeHtml4(stringColorEntry.Key));
                result.Append("</span>");
            }
            @out.Print(result);
        }
        /// <summary>Writes out a lot of redundant data from this Object to the Writer w.</summary>
        /// <param name="w">Data is written to this Writer</param>
        public virtual void WriteAllData(TextWriter w)
        {
            int         numStates = index.Size();
            PrintWriter @out      = new PrintWriter(w);

            // all lines have one rule per line
            @out.Println("Unary ruleIterator");
            for (IEnumerator <UnaryRule> rI = RuleIterator(); rI.MoveNext();)
            {
                @out.Println(rI.Current.ToString(index));
            }
            @out.Println("Unary closedRuleIterator");
            for (IEnumerator <UnaryRule> rI_1 = ClosedRuleIterator(); rI_1.MoveNext();)
            {
                @out.Println(rI_1.Current.ToString(index));
            }
            @out.Println("Unary rulesWithParentIterator");
            for (int i = 0; i < numStates; i++)
            {
                @out.Println(index.Get(i));
                for (IEnumerator <UnaryRule> rI_2 = RuleIteratorByParent(i); rI_2.MoveNext();)
                {
                    @out.Print("  ");
                    @out.Println(rI_2.Current.ToString(index));
                }
            }
            @out.Println("Unary closedRulesWithParentIterator");
            for (int i_1 = 0; i_1 < numStates; i_1++)
            {
                @out.Println(index.Get(i_1));
                for (IEnumerator <UnaryRule> rI_2 = ClosedRuleIteratorByParent(i_1); rI_2.MoveNext();)
                {
                    @out.Print("  ");
                    @out.Println(rI_2.Current.ToString(index));
                }
            }
            @out.Flush();
        }
        public virtual string ToSummaryString()
        {
            StringWriter sw = new StringWriter();
            PrintWriter  pw = new PrintWriter(sw);

            pw.Println("Number of data points: " + Size());
            pw.Print("Number of labels: " + labelIndex.Size() + " [");
            IEnumerator <L> iter = labelIndex.GetEnumerator();

            while (iter.MoveNext())
            {
                pw.Print(iter.Current);
                if (iter.MoveNext())
                {
                    pw.Print(", ");
                }
            }
            pw.Println("]");
            pw.Println("Number of features (Phi(X) types): " + featureIndex.Size());
            pw.Println("Number of active feature types: " + NumFeatureTypes());
            pw.Println("Number of active feature tokens: " + NumFeatureTokens());
            return(sw.ToString());
        }
        /// <exception cref="System.IO.IOException"/>
        private static void OutputByWriter(IConsumer <StringWriter> printer, PrintWriter @out)
        {
            StringWriter output = new StringWriter();

            printer.Accept(output);
            output.Flush();
            string escapedXml = StringEscapeUtils.EscapeHtml4(output.ToString());

            string[] lines = escapedXml.Split("\n");
            @out.Print("<div><pre>");
            foreach (string line in lines)
            {
                int numSpaces = 0;
                while (numSpaces < line.Length && line[numSpaces] == ' ')
                {
                    @out.Print("&nbsp;");
                    ++numSpaces;
                }
                @out.Print(Sharpen.Runtime.Substring(line, numSpaces));
                @out.Print("\n");
            }
            @out.Print("</pre></div>");
        }
 public virtual void WriteSVMLightFormat(PrintWriter writer)
 {
     foreach (RVFDatum <L, F> datum in this)
     {
         writer.Print(this.labelIndex.IndexOf(datum.Label()));
         ICounter <F> features = datum.AsFeaturesCounter();
         foreach (F feature in features.KeySet())
         {
             double count = features.GetCount(feature);
             writer.Format(Locale.English, " %s:%f", this.featureIndex.IndexOf(feature), count);
         }
         writer.Println();
     }
 }
        /// <summary>
        /// Modification of printFullFeatureMatrix to correct bugs and print values
        /// (Rajat).
        /// </summary>
        /// <remarks>
        /// Modification of printFullFeatureMatrix to correct bugs and print values
        /// (Rajat). Prints the full feature matrix in tab-delimited form. These can be
        /// BIG matrices, so be careful!
        /// </remarks>
        public virtual void PrintFullFeatureMatrixWithValues(PrintWriter pw)
        {
            string sep = "\t";

            for (int i = 0; i < featureIndex.Size(); i++)
            {
                pw.Print(sep + featureIndex.Get(i));
            }
            pw.Println();
            for (int i_1 = 0; i_1 < size; i_1++)
            {
                // changed labels.length to size
                pw.Print(labelIndex.Get(labels[i_1]));
                // changed i to labels[i]
                IDictionary <int, double> feats = Generics.NewHashMap();
                for (int j = 0; j < data[i_1].Length; j++)
                {
                    int    feature = data[i_1][j];
                    double val     = values[i_1][j];
                    feats[int.Parse(feature)] = val;
                }
                for (int j_1 = 0; j_1 < featureIndex.Size(); j_1++)
                {
                    if (feats.Contains(int.Parse(j_1)))
                    {
                        pw.Print(sep + feats[int.Parse(j_1)]);
                    }
                    else
                    {
                        pw.Print(sep);
                        pw.Print(' ');
                    }
                }
                pw.Println();
            }
            pw.Flush();
        }
        private static void PrintAnswersXML <In>(IList <In> doc, PrintWriter @out)
            where In : ICoreMap
        {
            int num = 0;

            foreach (IN wi in doc)
            {
                string prev = StringUtils.GetNotNullString(wi.Get(typeof(CoreAnnotations.BeforeAnnotation)));
                @out.Print(prev);
                @out.Print("<wi num=\"");
                // tag.append(wi.get("position"));
                @out.Print(num++);
                @out.Print("\" entity=\"");
                @out.Print(StringUtils.GetNotNullString(wi.Get(typeof(CoreAnnotations.AnswerAnnotation))));
                @out.Print("\">");
                @out.Print(XMLUtils.EscapeXML(StringUtils.GetNotNullString(wi.Get(typeof(CoreAnnotations.TextAnnotation)))));
                @out.Print("</wi>");
                string after = StringUtils.GetNotNullString(wi.Get(typeof(CoreAnnotations.AfterAnnotation)));
                @out.Print(after);
            }
        }
Exemplo n.º 30
0
 public static bool WriteFile(TransducerGraph graph, string dir, string name)
 {
     try
     {
         File baseDir = new File(dir);
         if (baseDir.Exists())
         {
             if (!baseDir.IsDirectory())
             {
                 return(false);
             }
         }
         else
         {
             if (!baseDir.Mkdirs())
             {
                 return(false);
             }
         }
         File        file = new File(baseDir, name + ".dot");
         PrintWriter w;
         try
         {
             w = new PrintWriter(new FileWriter(file));
             string dotString = graph.AsDOTString();
             w.Print(dotString);
             w.Flush();
             w.Close();
         }
         catch (FileNotFoundException)
         {
             log.Info("Failed to open file in writeToDOTfile: " + file);
             return(false);
         }
         catch (IOException)
         {
             log.Info("Failed to open file in writeToDOTfile: " + file);
             return(false);
         }
         return(true);
     }
     catch (Exception e)
     {
         Sharpen.Runtime.PrintStackTrace(e);
         return(false);
     }
 }