Пример #1
0
        public string OpenFile(string location)
        {
            List <object> annotationParts = new List <object>();

            // First, read sentiment file.  It is ok for it to not exist
            try
            {
                var      sentimentLocation = Path.ChangeExtension(location, ".snt");
                string[] sentimentLines    = File.ReadAllLines(sentimentLocation);
                string   sentiment         = sentimentLines[0];
                annotationParts.Add(sentiment);
            } catch (Exception e)
            {
                annotationParts.Add("UNK");
            }

            // Second read the ann file
            //===========================================
            // Read configuration for entity types first
            List <EntityType> entityTypes = fetchEntityTypesFromConfiguration();

            try {
                string[] allLines      = File.ReadAllLines(location);
                string   formatLine    = allLines[1];
                var      formatPattern = @"###FORMAT: ([A-Za-z]+) ###";
                var      match         = Regex.Match(formatLine, formatPattern);
                string   format        = match.Groups[1].Value;
                if (format == "default")
                {
                    List <Annotation> annotations = new List <Annotation>();
                    for (int index = 2; index < allLines.Length; index++)
                    {
                        EntityMention em  = EntityMention.FromString(allLines[index]);
                        Annotation    ann = new Annotation();
                        ann.begin = em.begin;
                        ann.end   = em.end;
                        ann.type  = em.type;
                        ann.color = colorDict[ann.type];
                        annotations.Add(ann);
                    }
                    annotationParts.Add(annotations);
                    return(JsonConvert.SerializeObject(annotationParts));
                }
                else if (format == "xml")
                {
                    List <Annotation> annotations = new List <Annotation>();
                    string            fulltext    = String.Concat(new List <string>(allLines).GetRange(2, allLines.Length).ToArray());
                    var   inlinePattern           = BuildEntityTypePattern(entityTypes);
                    Regex inlineRegex             = new Regex(inlinePattern);
                    int   xmlJunkOffset           = 0;
                    foreach (Match inlineMatch in inlineRegex.Matches(fulltext))
                    {
                        string     text  = match.Value;
                        int        begin = match.Index;
                        int        end   = inlineMatch.Length - begin;
                        Annotation ann   = new Annotation();
                        ann.begin = begin - xmlJunkOffset;
                        ann.end   = end - xmlJunkOffset;
                        SetAnnotationType(ann, entityTypes, text);
                        ann.color = colorDict[ann.type];
                    }
                    annotationParts.Add(annotations);
                    return(JsonConvert.SerializeObject(annotationParts));
                }
                else
                {
                    return(null);
                }
            } catch (Exception e)
            {
                return(null);
            }
        }