Beispiel #1
0
        /// <summary>
        /// Appends all the annotations present in the annotMan to the annotations currently loaded in this annotation Manager.
        /// </summary>
        /// <param name="annotMan"></param>
        public void appendAnnotations(AnnotationManager annotMan)
        {
            if (annotMan == null)
            {
                return;
            }

            foreach (var ann in annotMan.GetAnnotationSubjects())
            {
                appendAnnotations(ann.Key, ann.Value, annotMan.GetAnnotations(ann.Key));
            }
        }
Beispiel #2
0
        /// <summary>
        /// Compare the annotations loaded in this annotation manager to the annotations loaded in the annotNew.
        /// </summary>
        /// <param name="annotNew">Manager to which you want to compare the current annotations</param>
        /// <returns>An annotations manager diff structure which contains the difference between the two annotation managers</returns>
        public AnnotationManagerDiff Compare(AnnotationManager annotNew)
        {
            AnnotationManagerDiff diffMan = new AnnotationManagerDiff();
            // get the difference between the keys of the two managers.
            var keysInRef = new HashSet <string>(this.GetAnnotationSubjects().Select(x => x.Key).ToList());
            var keysInNew = new HashSet <string>(annotNew.GetAnnotationSubjects().Select(x => x.Key).ToList());
            // all keys present in annotReference and not in annotNew should be added in removedAnnotations
            var keysInRefNotInNew = keysInRef.Except(keysInNew);

            foreach (var annKey in keysInRefNotInNew)
            {
                diffMan.RemovedAnnotations.Add(new Tuple <string, string>(annKey, this.subjectType[annKey]), this.GetAnnotations(annKey));
            }
            // all keys present in annotNew and not in annotReference should be added in addedAnnotations
            var keysInNewNotInRef = keysInNew.Except(keysInRef);

            foreach (var annKey in keysInNewNotInRef)
            {
                diffMan.AddedAnnotations.Add(new Tuple <string, string>(annKey, annotNew.subjectType[annKey]), annotNew.GetAnnotations(annKey));
            }

            // if they have both the same key --> check if each annotation is the same
            var keysInBoth = keysInRef.Intersect(keysInNew);

            foreach (var annKey in keysInBoth)
            {
                var annInRef         = new HashSet <W3CAnnotation>(this.GetAnnotations(annKey));
                var annInNew         = new HashSet <W3CAnnotation>(annotNew.GetAnnotations(annKey));
                var annInRefNotInNew = annInRef.Except(annInNew, new W3CAnnotation(true)).ToList();
                if (annInRefNotInNew.Count > 0)
                {
                    diffMan.RemovedAnnotations.Add(new Tuple <string, string>(annKey, this.subjectType[annKey]), annInRefNotInNew);
                }
                var annInNewNotInRef = annInNew.Except(annInRef, new W3CAnnotation(true)).ToList();
                if (annInNewNotInRef.Count > 0)
                {
                    diffMan.AddedAnnotations.Add(new Tuple <string, string>(annKey, annotNew.subjectType[annKey]), annInNewNotInRef);
                }
            }

            return(diffMan);
        }
Beispiel #3
0
        /// <summary>
        /// Loads the W3CAnnotations found in the line. By default the annotationManager is using DL internally if the text you are giving is not in DL, use the inputTranslator argument
        /// If the subject of an annotation is a Statement, then the Statement is expected to be written like: "statement where quote is ''"
        /// </summary>
        /// <param name="line"></param>
        /// <param name="append">Decides if the text should be appended to the annotationManager or not</param>
        /// <param name="inputTranslator">Used internally to translate for CNL to DL. Use it if the input is not in DL. From ENCNL, the translator is: x => Ontorion.CNL.EN.ENNameingConvention.ToDL(new Ontorion.CNL.EN.EnName() { id = x }, Ontorion.CNL.EN.endict.WordKind.NormalForm).id </param>
        /// <returns></returns>
        public void loadW3CAnnotationsFromText(string line, bool append = false, Func <string, string> inputTranslator = null)
        {
            if (String.IsNullOrEmpty(line) || String.IsNullOrWhiteSpace(line) || !line.Contains(":"))
            {
                return;
            }

            var localAnnotationManager = new AnnotationManager();

            var dp = 0;

            if (line.Contains(ANNOTATION_START))
            {
                dp = line.IndexOf(':') + 1;
            }

            bool newAnnotationSubjects = false;
            var  refs = w3cAnnotRg.Matches(line.Substring(dp).Trim());

            foreach (Match match in refs)
            {
                string annotated = match.Groups["annotated"].Value;
                string type      = match.Groups["type"].Value;
                string val       = match.Groups["value"].Value;
                string kind      = match.Groups["annotatedKind"].Value;
                // if one of these values are null this means that something went wrong during parsing... someone changed it manually?
                // in this case we skip this line.
                if (String.IsNullOrWhiteSpace(annotated) || String.IsNullOrWhiteSpace(type) || String.IsNullOrWhiteSpace(val) || String.IsNullOrWhiteSpace(kind))
                {
                    continue;
                }

                var res = ParseSubjectKind(kind);
                annotated = annotated.Trim();
                type      = type.Trim();

                if (inputTranslator != null)
                {
                    if (res != ARS.EntityKind.Statement && !CNLTools.isSurelyDLEntity(annotated, res))
                    {
                        annotated = inputTranslator(annotated);
                    }

                    if (!CNLTools.isSurelyDLEntity(type, ARS.EntityKind.Role))
                    {
                        type = inputTranslator(type);
                    }
                }

                // if the subject is a statement, then the annotation manager keeps it internally as: statement with quotes inside (no quotes around!)
                if (res == ARS.EntityKind.Statement && annotated.StartsWith("\"") && annotated.EndsWith("\""))
                {
                    annotated = annotated.Substring(1, annotated.Length - 2).Replace("\''", "\"");
                }
                else if (res == ARS.EntityKind.Statement && !annotated.StartsWith("\"") && !annotated.EndsWith("\"")) //statement should be quoted! If not, continue.
                {
                    continue;
                }

                if (!localAnnotationManager.ContainsAnnotationSubject(annotated.Trim()))
                {
                    newAnnotationSubjects = true;
                }
                localAnnotationManager.appendAnnotations(annotated.Trim(), kind.Trim(), new List <W3CAnnotation>()
                {
                    new W3CAnnotation(true)
                    {
                        Type = type.Trim(), Value = val.Trim(), Language = match.Groups["language"].Value
                    }
                });
            }

            if (newAnnotationSubjects && NewAnnotationSubject != null && FireNewSubjectEvent)
            {
                NewAnnotationSubject(this);
            }

            if (!append)
            {
                this.clearAnnotations();
            }

            appendAnnotations(localAnnotationManager);

            if (!append)
            {
                this.AssumeNotModifiedNow();
            }
        }