public void Compile()
		{
			m_featSys = new FeatureSystem
				{
					new SymbolicFeature("type",
						new FeatureSymbol("bdry", "Boundary"),
						new FeatureSymbol("word", "Word"),
						new FeatureSymbol("morph", "Morph"),
						new FeatureSymbol("ttag", "Text Tag")) {Description = "Type"},
					new SymbolicFeature("anchorType",
						new FeatureSymbol("paraBdry", "Paragraph"),
						new FeatureSymbol("segBdry", "Segment"),
						new FeatureSymbol("wordBdry", "Word"))
				};
			foreach (IWritingSystem ws in m_cache.ServiceLocator.WritingSystems.CurrentVernacularWritingSystems)
			{
				m_featSys.Add(new StringFeature(string.Format("entry-{0}", ws.Handle)) {Description = string.Format("Entry-{0}", ws.Abbreviation)});
				m_featSys.Add(new StringFeature(string.Format("form-{0}", ws.Handle)) {Description = string.Format("Form-{0}", ws.Abbreviation)});
			}

			foreach (IWritingSystem ws in m_cache.ServiceLocator.WritingSystems.CurrentAnalysisWritingSystems)
				m_featSys.Add(new StringFeature(string.Format("gloss-{0}", ws.Handle)) {Description = string.Format("Gloss-{0}", ws.Abbreviation)});

			m_featSys.Add(new SymbolicFeature("cat", m_cache.ServiceLocator.GetInstance<IPartOfSpeechRepository>().AllInstances()
				.Select(pos => new FeatureSymbol(pos.Hvo.ToString(CultureInfo.InvariantCulture), pos.Abbreviation.BestAnalysisAlternative.Text)))
				{
					Description = "Category"
				});

			m_featSys.Add(new SymbolicFeature("tag", m_cache.LangProject.TextMarkupTagsOA.PossibilitiesOS
				.SelectMany(poss => poss.SubPossibilitiesOS, (category, tag) => new FeatureSymbol(tag.Hvo.ToString(CultureInfo.InvariantCulture), tag.Abbreviation.BestAnalysisAlternative.Text)))
				{
					Description = "Tag"
				});

			m_featSys.Add(new ComplexFeature("infl") {Description = "Infl", DefaultValue = FeatureStruct.New().Value});
			foreach (IFsFeatDefn feature in m_cache.LangProject.MsFeatureSystemOA.FeaturesOC)
			{
				var complexFeat = feature as IFsComplexFeature;
				if (complexFeat != null)
				{
					m_featSys.Add(new ComplexFeature(complexFeat.Hvo.ToString(CultureInfo.InvariantCulture)) {Description = complexFeat.Abbreviation.BestAnalysisAlternative.Text, DefaultValue = FeatureStruct.New().Value});
				}
				else
				{
					var closedFeat = (IFsClosedFeature) feature;
					m_featSys.Add(new SymbolicFeature(closedFeat.Hvo.ToString(CultureInfo.InvariantCulture), closedFeat.ValuesOC.Select(sym =>
						new FeatureSymbol(sym.Hvo.ToString(CultureInfo.InvariantCulture), sym.Abbreviation.BestAnalysisAlternative.Text))
						.Concat(new FeatureSymbol(closedFeat.Hvo.ToString(CultureInfo.InvariantCulture) + "_us", "unspecified")))
						{
							Description = closedFeat.Abbreviation.BestAnalysisAlternative.Text,
							DefaultSymbolID = closedFeat.Hvo.ToString(CultureInfo.InvariantCulture) + "_us"
						});
				}
			}

			var pattern = new Pattern<ComplexConcParagraphData, ShapeNode>();
			pattern.Children.Add(m_root.GeneratePattern(m_featSys));
			m_matcher = new Matcher<ComplexConcParagraphData, ShapeNode>(m_spanFactory, pattern, new MatcherSettings<ShapeNode> {UseDefaults = true});
		}
        public void Compile()
        {
            m_featSys = new FeatureSystem
            {
                new SymbolicFeature("type",
                                    new FeatureSymbol("bdry", "Boundary"),
                                    new FeatureSymbol("word", "Word"),
                                    new FeatureSymbol("morph", "Morph"),
                                    new FeatureSymbol("ttag", "Text Tag"))
                {
                    Description = "Type"
                },
                new SymbolicFeature("anchorType",
                                    new FeatureSymbol("paraBdry", "Paragraph"),
                                    new FeatureSymbol("segBdry", "Segment"),
                                    new FeatureSymbol("wordBdry", "Word"))
            };
            foreach (IWritingSystem ws in m_cache.ServiceLocator.WritingSystems.CurrentVernacularWritingSystems)
            {
                m_featSys.Add(new StringFeature(string.Format("entry-{0}", ws.Handle))
                {
                    Description = string.Format("Entry-{0}", ws.Abbreviation)
                });
                m_featSys.Add(new StringFeature(string.Format("form-{0}", ws.Handle))
                {
                    Description = string.Format("Form-{0}", ws.Abbreviation)
                });
            }

            foreach (IWritingSystem ws in m_cache.ServiceLocator.WritingSystems.CurrentAnalysisWritingSystems)
            {
                m_featSys.Add(new StringFeature(string.Format("gloss-{0}", ws.Handle))
                {
                    Description = string.Format("Gloss-{0}", ws.Abbreviation)
                });
            }

            m_featSys.Add(new SymbolicFeature("cat", m_cache.ServiceLocator.GetInstance <IPartOfSpeechRepository>().AllInstances()
                                              .Select(pos => new FeatureSymbol(pos.Hvo.ToString(CultureInfo.InvariantCulture), pos.Abbreviation.BestAnalysisAlternative.Text)))
            {
                Description = "Category"
            });

            m_featSys.Add(new SymbolicFeature("tag", m_cache.LangProject.TextMarkupTagsOA.PossibilitiesOS
                                              .SelectMany(poss => poss.SubPossibilitiesOS, (category, tag) => new FeatureSymbol(tag.Hvo.ToString(CultureInfo.InvariantCulture), tag.Abbreviation.BestAnalysisAlternative.Text)))
            {
                Description = "Tag"
            });

            m_featSys.Add(new ComplexFeature("infl")
            {
                Description = "Infl", DefaultValue = FeatureStruct.New().Value
            });
            foreach (IFsFeatDefn feature in m_cache.LangProject.MsFeatureSystemOA.FeaturesOC)
            {
                var complexFeat = feature as IFsComplexFeature;
                if (complexFeat != null)
                {
                    m_featSys.Add(new ComplexFeature(complexFeat.Hvo.ToString(CultureInfo.InvariantCulture))
                    {
                        Description = complexFeat.Abbreviation.BestAnalysisAlternative.Text, DefaultValue = FeatureStruct.New().Value
                    });
                }
                else
                {
                    var closedFeat = (IFsClosedFeature)feature;
                    m_featSys.Add(new SymbolicFeature(closedFeat.Hvo.ToString(CultureInfo.InvariantCulture), closedFeat.ValuesOC.Select(sym =>
                                                                                                                                        new FeatureSymbol(sym.Hvo.ToString(CultureInfo.InvariantCulture), sym.Abbreviation.BestAnalysisAlternative.Text))
                                                      .Concat(new FeatureSymbol(closedFeat.Hvo.ToString(CultureInfo.InvariantCulture) + "_us", "unspecified")))
                    {
                        Description     = closedFeat.Abbreviation.BestAnalysisAlternative.Text,
                        DefaultSymbolID = closedFeat.Hvo.ToString(CultureInfo.InvariantCulture) + "_us"
                    });
                }
            }

            var pattern = new Pattern <ComplexConcParagraphData, ShapeNode>();

            pattern.Children.Add(m_root.GeneratePattern(m_featSys));
            m_matcher = new Matcher <ComplexConcParagraphData, ShapeNode>(m_spanFactory, pattern, new MatcherSettings <ShapeNode> {
                UseDefaults = true
            });
        }
示例#3
0
        private static bool LoadProject(SegmentPool segmentPool, XDocument doc, out CogProject project)
        {
            project = new CogProject();

            XElement root = doc.Root;

            Debug.Assert(root != null);

            bool migrated = false;

            while (root.GetDefaultNamespace() != Cog)
            {
                IConfigMigration cm;
                if (ConfigMigrations.TryGetValue(root.GetDefaultNamespace(), out cm))
                {
                    cm.Migrate(doc);
                    migrated = true;
                }
                else
                {
                    throw new ConfigException("The specified file is not a valid Cog config file");
                }
            }

            if (!Platform.IsMono)              // Bypass bug in Mono XSD validation. 2015-08 RM
            {
                doc.Validate(Schema, (sender, args) =>
                {
                    switch (args.Severity)
                    {
                    case XmlSeverityType.Error:
                        throw new ConfigException("The specified file is not a valid Cog config file", args.Exception);
                    }
                });
            }
            segmentPool.Reset();
            project.Version = (int)root.Attribute("version");
            var      featSys     = new FeatureSystem();
            XElement featSysElem = root.Element(Cog + "FeatureSystem");

            Debug.Assert(featSysElem != null);
            foreach (XElement featureElem in featSysElem.Elements(Cog + "Feature"))
            {
                var feat = new SymbolicFeature((string)featureElem.Attribute("id"), featureElem.Elements(Cog + "Value")
                                               .Select(e => new FeatureSymbol((string)e.Attribute("id"), (string)e.Attribute("name"))))
                {
                    Description = (string)featureElem.Attribute("name")
                };
                featSys.Add(feat);
            }
            project.FeatureSystem = featSys;

            XElement segmentationElem = root.Element(Cog + "Segmentation");

            Debug.Assert(segmentationElem != null);
            XElement vowelsElem = segmentationElem.Element(Cog + "Vowels");

            if (vowelsElem != null)
            {
                XAttribute maxLenAttr = vowelsElem.Attribute("maxLength");
                if (maxLenAttr != null)
                {
                    project.Segmenter.MaxVowelLength = (int)maxLenAttr;
                }

                ParseSymbols(project.FeatureSystem, vowelsElem, project.Segmenter.Vowels);
            }

            XElement consElem = segmentationElem.Element(Cog + "Consonants");

            if (consElem != null)
            {
                XAttribute maxLenAttr = consElem.Attribute("maxLength");
                if (maxLenAttr != null)
                {
                    project.Segmenter.MaxConsonantLength = (int)maxLenAttr;
                }

                ParseSymbols(project.FeatureSystem, consElem, project.Segmenter.Consonants);
            }

            XElement modsElem = segmentationElem.Element(Cog + "Modifiers");

            if (modsElem != null)
            {
                ParseSymbols(project.FeatureSystem, modsElem, project.Segmenter.Modifiers);
            }

            XElement bdrysElem = segmentationElem.Element(Cog + "Boundaries");

            if (bdrysElem != null)
            {
                ParseSymbols(project.FeatureSystem, bdrysElem, project.Segmenter.Boundaries);
            }

            XElement tonesElem = segmentationElem.Element(Cog + "ToneLetters");

            if (tonesElem != null)
            {
                ParseSymbols(project.FeatureSystem, tonesElem, project.Segmenter.ToneLetters);
            }

            XElement joinersElem = segmentationElem.Element(Cog + "Joiners");

            if (joinersElem != null)
            {
                ParseSymbols(project.FeatureSystem, joinersElem, project.Segmenter.Joiners);
            }

            XElement alignersElem = root.Element(Cog + "WordAligners");

            Debug.Assert(alignersElem != null);
            foreach (XElement alignerElem in alignersElem.Elements(Cog + "WordAligner"))
            {
                LoadComponent(segmentPool, project, alignerElem, project.WordAligners);
            }

            XElement cognateIdentifiersElem = root.Element(Cog + "CognateIdentifiers");

            Debug.Assert(cognateIdentifiersElem != null);
            foreach (XElement cognateIdentifierElem in cognateIdentifiersElem.Elements(Cog + "CognateIdentifier"))
            {
                LoadComponent(segmentPool, project, cognateIdentifierElem, project.CognateIdentifiers);
            }

            XElement meaningsElem = root.Element(Cog + "Meanings");

            Debug.Assert(meaningsElem != null);
            foreach (XElement meaningElem in meaningsElem.Elements(Cog + "Meaning"))
            {
                var meaning = new Meaning((string)meaningElem.Attribute("gloss"), (string)meaningElem.Attribute("category"));
                project.Meanings.Add(meaning);
            }

            XElement varietiesElem = root.Element(Cog + "Varieties");

            Debug.Assert(varietiesElem != null);
            foreach (XElement varietyElem in varietiesElem.Elements(Cog + "Variety"))
            {
                var      variety   = new Variety((string)varietyElem.Attribute("name"));
                XElement wordsElem = varietyElem.Element(Cog + "Words");
                if (wordsElem != null)
                {
                    foreach (XElement wordElem in wordsElem.Elements(Cog + "Word"))
                    {
                        Meaning meaning   = project.Meanings[(string)wordElem.Attribute("meaning")];
                        var     strRep    = ((string)wordElem).Trim();
                        var     stemIndex = (int?)wordElem.Attribute("stemIndex") ?? 0;
                        var     stemLen   = (int?)wordElem.Attribute("stemLength") ?? strRep.Length - stemIndex;
                        variety.Words.Add(new Word(strRep, stemIndex, stemLen, meaning));
                    }
                }
                XElement affixesElem = varietyElem.Element(Cog + "Affixes");
                if (affixesElem != null)
                {
                    foreach (XElement affixElem in affixesElem.Elements(Cog + "Affix"))
                    {
                        var type = AffixType.Prefix;
                        switch ((string)affixElem.Attribute("type"))
                        {
                        case "prefix":
                            type = AffixType.Prefix;
                            break;

                        case "suffix":
                            type = AffixType.Suffix;
                            break;
                        }

                        var affixStr = ((string)affixElem).Trim();
                        variety.Affixes.Add(new Affix(affixStr, type, (string)affixElem.Attribute("category")));
                    }
                }
                XElement regionsElem = varietyElem.Element(Cog + "Regions");
                if (regionsElem != null)
                {
                    foreach (XElement regionElem in regionsElem.Elements(Cog + "Region"))
                    {
                        var region = new GeographicRegion {
                            Description = (string)regionElem.Element(Cog + "Description")
                        };
                        foreach (XElement coordinateElem in regionElem.Elements(Cog + "Coordinates").Elements(Cog + "Coordinate"))
                        {
                            var latitude  = (double)coordinateElem.Element(Cog + "Latitude");
                            var longitude = (double)coordinateElem.Element(Cog + "Longitude");
                            region.Coordinates.Add(new GeographicCoordinate(latitude, longitude));
                        }
                        variety.Regions.Add(region);
                    }
                }
                project.Varieties.Add(variety);
            }

            XElement cognacyDecisionsElem = root.Element(Cog + "CognacyDecisions");

            Debug.Assert(cognacyDecisionsElem != null);
            foreach (XElement cognacyDecisionElem in cognacyDecisionsElem.Elements(Cog + "CognacyDecision"))
            {
                Variety variety1;
                if (!project.Varieties.TryGet((string)cognacyDecisionElem.Attribute("variety1"), out variety1))
                {
                    continue;
                }
                Variety variety2;
                if (!project.Varieties.TryGet((string)cognacyDecisionElem.Attribute("variety2"), out variety2))
                {
                    continue;
                }
                Meaning meaning;
                if (!project.Meanings.TryGet((string)cognacyDecisionElem.Attribute("meaning"), out meaning))
                {
                    continue;
                }

                project.CognacyDecisions.Add(new CognacyDecision(variety1, variety2, meaning,
                                                                 (bool)cognacyDecisionElem.Attribute("cognacy")));
            }

            XElement projectProcessorsElem = root.Element(Cog + "ProjectProcessors");

            Debug.Assert(projectProcessorsElem != null);
            foreach (XElement projectProcessorElem in projectProcessorsElem.Elements(Cog + "ProjectProcessor"))
            {
                LoadComponent(segmentPool, project, projectProcessorElem, project.ProjectProcessors);
            }

            XElement varietyProcessorsElem = root.Element(Cog + "VarietyProcessors");

            Debug.Assert(varietyProcessorsElem != null);
            foreach (XElement varietyProcessorElem in varietyProcessorsElem.Elements(Cog + "VarietyProcessor"))
            {
                LoadComponent(segmentPool, project, varietyProcessorElem, project.VarietyProcessors);
            }

            XElement varietyPairProcessorsElem = root.Element(Cog + "VarietyPairProcessors");

            Debug.Assert(varietyPairProcessorsElem != null);
            foreach (XElement varietyPairProcessorElem in varietyPairProcessorsElem.Elements(Cog + "VarietyPairProcessor"))
            {
                LoadComponent(segmentPool, project, varietyPairProcessorElem, project.VarietyPairProcessors);
            }

            return(migrated);
        }