Esempio n. 1
0
        private IList <CoreLabel> SegmentStringToIOB(string line)
        {
            IList <CoreLabel> tokenList;

            if (tf == null)
            {
                // Whitespace tokenization.
                tokenList = IOBUtils.StringToIOB(line);
            }
            else
            {
                IList <CoreLabel> tokens = tf.GetTokenizer(new StringReader(line)).Tokenize();
                tokenList = IOBUtils.StringToIOB(tokens, null, false, tf, line);
            }
            IOBUtils.LabelDomain(tokenList, domain);
            tokenList = classifier.Classify(tokenList);
            return(tokenList);
        }
Esempio n. 2
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);
        }