Esempio n. 1
0
 public Segments(CharacterDefinitionTable table, string representation, Shape shape)
 {
     _representation = representation;
     _table = table;
     _shape = shape;
     _shape.Freeze();
 }
		public SegmentDefinition(string strRep, CharacterDefinitionTable charDefTable, FeatureBundle fb)
		{
			m_strRep = strRep;
			m_charDefTable = charDefTable;
			m_synthFeatures = fb;
			m_antiFeatures = new FeatureBundle(m_synthFeatures, true);
			m_analysisFeatures = new FeatureBundle(m_synthFeatures, false);
		}
Esempio n. 3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="Stratum"/> class.
        /// </summary>
        /// <param name="charDefTable"></param>
        public Stratum(CharacterDefinitionTable charDefTable)
        {
            Depth = -1;
            _charDefTable = charDefTable;

            _mrules = new ObservableCollection<IMorphologicalRule>();
            _mrules.CollectionChanged += MorphologicalRulesChanged;
            _prules = new List<IPhonologicalRule>();

            _templates = new ObservableCollection<AffixTemplate>();
            _templates.CollectionChanged += TemplatesChanged;

            _entries = new ObservableCollection<LexEntry>();
            _entries.CollectionChanged += EntriesChanged;
        }
        /// <summary>
        /// Converts the specified phonetic shape to a valid regular expression string. Regular expressions
        /// formatted for display purposes are NOT guaranteed to compile.
        /// </summary>
        /// <param name="shape">The phonetic shape.</param>
        /// <param name="table">The symbol table.</param>
        /// <param name="displayFormat">if <c>true</c> the result will be formatted for display, otherwise
        /// it will be formatted for compilation.</param>
        /// <returns>The regular expression string.</returns>
        public static string ToRegexString(this Shape shape, CharacterDefinitionTable table, bool displayFormat)
        {
            var sb = new StringBuilder();
            if (!displayFormat)
                sb.Append("^");
            foreach (ShapeNode node in shape)
            {
                if (node.IsDeleted())
                    continue;

                string[] strReps = table.GetMatchingStrReps(node).ToArray();
                int strRepCount = strReps.Length;
                if (strRepCount > 0)
                {
                    if (strRepCount > 1)
                        sb.Append(displayFormat ? "[" : "(");
                    int i = 0;
                    foreach (string strRep in strReps)
                    {
                        if (strRep.Length > 1)
                            sb.Append("(");

                        sb.Append(displayFormat ? strRep : Regex.Escape(strRep));

                        if (strRep.Length > 1)
                            sb.Append(")");
                        if (i < strRepCount - 1 && !displayFormat)
                            sb.Append("|");
                        i++;
                    }
                    if (strReps.Length > 1)
                        sb.Append(displayFormat ? "]" : ")");

                    if (node.Annotation.Optional)
                        sb.Append("?");
                }
            }
            if (!displayFormat)
                sb.Append("$");
            return sb.ToString();
        }
Esempio n. 5
0
        private bool TryLoadRealizationalRule(XElement realRuleElem, CharacterDefinitionTable defaultTable, out IMorphologicalRule mrule)
        {
            var realRuleID = (string) realRuleElem.Attribute("id");
            var realRule = new RealizationalAffixProcessRule
            {
                Name = (string) realRuleElem.Element("Name"),
                Gloss = (string) realRuleElem.Element("Gloss"),
                Blockable = (bool?) realRuleElem.Attribute("blockable") ?? true
            };

            var fs = new FeatureStruct();
            XElement requiredHeadFeatElem = realRuleElem.Element("RequiredHeadFeatures");
            if (requiredHeadFeatElem != null)
                fs.AddValue(_headFeature, LoadFeatureStruct(requiredHeadFeatElem, _language.SyntacticFeatureSystem));
            XElement requiredFootFeatElem = realRuleElem.Element("RequiredFootFeatures");
            if (requiredFootFeatElem != null)
                fs.AddValue(_footFeature, LoadFeatureStruct(requiredFootFeatElem, _language.SyntacticFeatureSystem));
            fs.Freeze();
            realRule.RequiredSyntacticFeatureStruct = fs;

            XElement realFeatElem = realRuleElem.Element("RealizationalFeatures");
            if (realFeatElem != null)
                realRule.RealizationalFeatureStruct = FeatureStruct.New().Feature(_headFeature).EqualTo(LoadFeatureStruct(realFeatElem, _language.SyntacticFeatureSystem)).Value;
            LoadProperties(realRuleElem.Element("Properties"), realRule.Properties);

            foreach (XElement subruleElem in realRuleElem.Elements("MorphologicalSubrules").Elements("MorphologicalSubrule").Where(IsActive))
            {
                try
                {
                    AffixProcessAllomorph allomorph = LoadAffixProcessAllomorph(subruleElem, defaultTable);
                    realRule.Allomorphs.Add(allomorph);
                    _allomorphs[(string) subruleElem.Attribute("id")] = allomorph;
                }
                catch (Exception e)
                {
                    if (_errorHandler != null)
                        _errorHandler(e, (string) realRuleElem.Attribute("id"));
                    else
                        throw;
                }
            }

            if (realRule.Allomorphs.Count > 0)
            {
                _morphemes[realRuleID] = realRule;
                mrule = realRule;
                return true;
            }

            mrule = null;
            return false;
        }
Esempio n. 6
0
 public Segments(CharacterDefinitionTable table, string representation)
     : this(table, representation, table.Segment(representation))
 {
 }
Esempio n. 7
0
        private bool TryLoadAffixProcessRule(XElement mruleElem, CharacterDefinitionTable defaultTable, out IMorphologicalRule mrule)
        {
            var id = (string) mruleElem.Attribute("id");
            var affixProcessRule = new AffixProcessRule
            {
                Name = (string) mruleElem.Element("Name"),
                Gloss = (string) mruleElem.Element("Gloss"),
                Blockable = (bool?) mruleElem.Attribute("blockable") ?? true,
                IsPartial = (bool?) mruleElem.Attribute("partial") ?? false
            };
            var multApp = (string) mruleElem.Attribute("multipleApplication");
            if (!string.IsNullOrEmpty(multApp))
                affixProcessRule.MaxApplicationCount = int.Parse(multApp);

            var fs = new FeatureStruct();
            var requiredPos = (string) mruleElem.Attribute("requiredPartsOfSpeech");
            if (!string.IsNullOrEmpty(requiredPos))
                fs.AddValue(_posFeature, ParsePartsOfSpeech(requiredPos));
            XElement requiredHeadFeatElem = mruleElem.Element("RequiredHeadFeatures");
            if (requiredHeadFeatElem != null)
                fs.AddValue(_headFeature, LoadFeatureStruct(requiredHeadFeatElem, _language.SyntacticFeatureSystem));
            XElement requiredFootFeatElem = mruleElem.Element("RequiredFootFeatures");
            if (requiredFootFeatElem != null)
                fs.AddValue(_footFeature, LoadFeatureStruct(requiredFootFeatElem, _language.SyntacticFeatureSystem));
            fs.Freeze();
            affixProcessRule.RequiredSyntacticFeatureStruct = fs;

            fs = new FeatureStruct();
            var outPos = (string) mruleElem.Attribute("outputPartOfSpeech");
            if (!string.IsNullOrEmpty(outPos))
                fs.AddValue(_posFeature, ParsePartsOfSpeech(outPos));
            XElement outHeadFeatElem = mruleElem.Element("OutputHeadFeatures");
            if (outHeadFeatElem != null)
                fs.AddValue(_headFeature, LoadFeatureStruct(outHeadFeatElem, _language.SyntacticFeatureSystem));
            XElement outFootFeatElem = mruleElem.Element("OutputFootFeatures");
            if (outFootFeatElem != null)
                fs.AddValue(_footFeature, LoadFeatureStruct(outFootFeatElem, _language.SyntacticFeatureSystem));
            fs.Freeze();
            affixProcessRule.OutSyntacticFeatureStruct = fs;

            var obligHeadIDsStr = (string) mruleElem.Attribute("outputObligatoryFeatures");
            if (!string.IsNullOrEmpty(obligHeadIDsStr))
            {
                foreach (string obligHeadID in obligHeadIDsStr.Split(' '))
                    affixProcessRule.ObligatorySyntacticFeatures.Add(_language.SyntacticFeatureSystem.GetFeature(obligHeadID));
            }

            var stemNameIDStr = (string) mruleElem.Attribute("requiredStemName");
            if (!string.IsNullOrEmpty(stemNameIDStr))
                affixProcessRule.RequiredStemName = _stemNames[stemNameIDStr];

            LoadProperties(mruleElem.Element("Properties"), affixProcessRule.Properties);

            foreach (XElement subruleElem in mruleElem.Elements("MorphologicalSubrules").Elements("MorphologicalSubrule").Where(IsActive))
            {
                try
                {
                    AffixProcessAllomorph allomorph = LoadAffixProcessAllomorph(subruleElem, defaultTable);
                    affixProcessRule.Allomorphs.Add(allomorph);
                    _allomorphs[(string) subruleElem.Attribute("id")] = allomorph;
                }
                catch (Exception e)
                {
                    if (_errorHandler != null)
                        _errorHandler(e, id);
                    else
                        throw;
                }
            }

            if (affixProcessRule.Allomorphs.Count > 0)
            {
                _morphemes[id] = affixProcessRule;
                mrule = affixProcessRule;
                return true;
            }

            mrule = null;
            return false;
        }
Esempio n. 8
0
        private RootAllomorph LoadRootAllomorph(XElement alloElem, CharacterDefinitionTable table)
        {
            var shapeStr = (string) alloElem.Element("PhoneticShape");
            Segments segments = new Segments(table, shapeStr);
            if (segments.Shape.All(n => n.Type() == HCFeatureSystem.Boundary))
                throw new InvalidShapeException(shapeStr, 0);
            var allomorph = new RootAllomorph(segments)
            {
                IsBound = (bool?) alloElem.Attribute("isBound") ?? false
            };

            allomorph.Environments.AddRange(LoadAllomorphEnvironments(alloElem.Element("RequiredEnvironments"), ConstraintType.Require, table));
            allomorph.Environments.AddRange(LoadAllomorphEnvironments(alloElem.Element("ExcludedEnvironments"), ConstraintType.Exclude, table));

            var stemNameIDStr = (string) alloElem.Attribute("stemName");
            if (!string.IsNullOrEmpty(stemNameIDStr))
                allomorph.StemName = _stemNames[stemNameIDStr];

            LoadProperties(alloElem.Element("Properties"), allomorph.Properties);

            return allomorph;
        }
Esempio n. 9
0
        private bool TryLoadLexEntry(XElement entryElem, CharacterDefinitionTable table, out LexEntry entry)
        {
            var id = (string) entryElem.Attribute("id");
            entry = new LexEntry {Gloss = (string) entryElem.Element("Gloss")};

            var fs = new FeatureStruct();
            var pos = (string) entryElem.Attribute("partOfSpeech");
            if (!string.IsNullOrEmpty(pos))
                fs.AddValue(_posFeature, ParsePartsOfSpeech(pos));

            XElement headFeatElem = entryElem.Element("AssignedHeadFeatures");
            if (headFeatElem != null)
                fs.AddValue(_headFeature, LoadFeatureStruct(headFeatElem, _language.SyntacticFeatureSystem));
            XElement footFeatElem = entryElem.Element("AssignedFootFeatures");
            if (footFeatElem != null)
                fs.AddValue(_footFeature, LoadFeatureStruct(footFeatElem, _language.SyntacticFeatureSystem));
            fs.Freeze();
            entry.SyntacticFeatureStruct = fs;

            entry.MprFeatures.UnionWith(LoadMprFeatures((string) entryElem.Attribute("ruleFeatures")));

            var familyID = (string) entryElem.Attribute("family");
            if (!string.IsNullOrEmpty(familyID))
            {
                LexFamily family = _families[familyID];
                family.Entries.Add(entry);
            }

            entry.IsPartial = (bool?) entryElem.Attribute("partial") ?? false;

            LoadProperties(entryElem.Element("Properties"), entry.Properties);

            foreach (XElement alloElem in entryElem.Elements("Allomorphs").Elements("Allomorph").Where(IsActive))
            {
                try
                {
                    RootAllomorph allomorph = LoadRootAllomorph(alloElem, table);
                    entry.Allomorphs.Add(allomorph);
                    _allomorphs[(string) alloElem.Attribute("id")] = allomorph;
                }
                catch (Exception e)
                {
                    if (_errorHandler != null)
                        _errorHandler(e, id);
                    else
                        throw;
                }
            }

            if (entry.Allomorphs.Count > 0)
            {
                _morphemes[id] = entry;
                return true;
            }

            entry = null;
            return false;
        }
Esempio n. 10
0
        private AffixProcessAllomorph LoadAffixProcessAllomorph(XElement msubruleElem, CharacterDefinitionTable defaultTable)
        {
            var allomorph = new AffixProcessAllomorph();

            allomorph.Environments.AddRange(LoadAllomorphEnvironments(msubruleElem.Element("RequiredEnvironments"), ConstraintType.Require, defaultTable));
            allomorph.Environments.AddRange(LoadAllomorphEnvironments(msubruleElem.Element("ExcludedEnvironments"), ConstraintType.Exclude, defaultTable));

            var fs = new FeatureStruct();
            XElement requiredHeadFeatElem = msubruleElem.Element("RequiredHeadFeatures");
            if (requiredHeadFeatElem != null)
                fs.AddValue(_headFeature, LoadFeatureStruct(requiredHeadFeatElem, _language.SyntacticFeatureSystem));
            XElement requiredFootFeatElem = msubruleElem.Element("RequiredFootFeatures");
            if (requiredFootFeatElem != null)
                fs.AddValue(_footFeature, LoadFeatureStruct(requiredFootFeatElem, _language.SyntacticFeatureSystem));
            fs.Freeze();
            allomorph.RequiredSyntacticFeatureStruct = fs;

            LoadProperties(msubruleElem.Element("Properties"), allomorph.Properties);

            Dictionary<string, Tuple<string, SymbolicFeature>> variables = LoadVariables(msubruleElem.Element("VariableFeatures"));

            XElement inputElem = msubruleElem.Element("MorphologicalInput");
            Debug.Assert(inputElem != null);

            allomorph.RequiredMprFeatures.UnionWith(LoadMprFeatures((string) inputElem.Attribute("requiredMPRFeatures")));
            allomorph.ExcludedMprFeatures.UnionWith(LoadMprFeatures((string) inputElem.Attribute("excludedMPRFeatures")));

            var partNames = new Dictionary<string, string>();
            LoadMorphologicalLhs(inputElem, variables, partNames, allomorph.Lhs, defaultTable);

            XElement outputElem = msubruleElem.Element("MorphologicalOutput");
            Debug.Assert(outputElem != null);

            allomorph.OutMprFeatures.UnionWith(LoadMprFeatures((string) outputElem.Attribute("MPRFeatures")));
            allomorph.ReduplicationHint = GetReduplicationHint((string) outputElem.Attribute("redupMorphType"));
            LoadMorphologicalRhs(outputElem, variables, partNames, allomorph.Rhs, defaultTable);

            return allomorph;
        }
		public BoundaryDefinition(string strRep, CharacterDefinitionTable charDefTable)
		{
			m_strRep = strRep;
			m_charDefTable = charDefTable;
		}
Esempio n. 12
0
        private void LoadMorphologicalRhs(XElement phonOutputElem, Dictionary<string, Tuple<string, SymbolicFeature>> variables,
			Dictionary<string, string> partNames, IList<MorphologicalOutputAction> rhs, CharacterDefinitionTable defaultTable)
        {
            foreach (XElement partElem in phonOutputElem.Elements())
            {
                switch (partElem.Name.LocalName)
                {
                    case "CopyFromInput":
                        rhs.Add(new CopyFromInput(partNames[(string) partElem.Attribute("index")]));
                        break;

                    case "InsertSimpleContext":
                        rhs.Add(new InsertSimpleContext(LoadSimpleContext(partElem.Element("SimpleContext"), variables)));
                        break;

                    case "ModifyFromInput":
                        rhs.Add(new ModifyFromInput(partNames[(string) partElem.Attribute("index")], LoadSimpleContext(partElem.Element("SimpleContext"), variables)));
                        break;

                    case "InsertSegments":
                        string tableId = (string) partElem.Attribute("characterDefinitionTable");
                        CharacterDefinitionTable table = tableId == null ? defaultTable : _tables[tableId];
                        var shapeStr = (string) partElem.Element("PhoneticShape");
                        rhs.Add(new InsertSegments(new Segments(table, shapeStr)));
                        break;
                }
            }
        }
Esempio n. 13
0
        private void LoadMorphologicalLhs(XElement reqPhonInputElem, Dictionary<string, Tuple<string, SymbolicFeature>> variables,
			Dictionary<string, string> partNames, IList<Pattern<Word, ShapeNode>> lhs, CharacterDefinitionTable defaultTable, string partNamePrefix = null)
        {
            int i = 1;
            foreach (XElement pseqElem in reqPhonInputElem.Elements("PhoneticSequence"))
            {
                var id = (string) pseqElem.Attribute("id");
                string name = null;
                if (!string.IsNullOrEmpty(id))
                {
                    name = (partNamePrefix ?? "") + i;
                    partNames[id] = name;
                }
                lhs.Add(LoadPhoneticSequence(pseqElem, variables, defaultTable, name));
                i++;
            }
        }
Esempio n. 14
0
        private CompoundingSubrule LoadCompoundingSubrule(XElement compSubruleElem, CharacterDefinitionTable defaultTable)
        {
            var subrule = new CompoundingSubrule();

            Dictionary<string, Tuple<string, SymbolicFeature>> variables = LoadVariables(compSubruleElem.Element("VariableFeatures"));

            XElement headElem = compSubruleElem.Element("HeadMorphologicalInput");
            Debug.Assert(headElem != null);

            subrule.RequiredMprFeatures.UnionWith(LoadMprFeatures((string) headElem.Attribute("requiredMPRFeatures")));
            subrule.ExcludedMprFeatures.UnionWith(LoadMprFeatures((string) headElem.Attribute("excludedMPRFeatures")));

            var partNames = new Dictionary<string, string>();
            LoadMorphologicalLhs(headElem, variables, partNames, subrule.HeadLhs, defaultTable, "head_");

            XElement nonHeadElem = compSubruleElem.Element("NonHeadMorphologicalInput");
            Debug.Assert(nonHeadElem != null);

            LoadMorphologicalLhs(nonHeadElem, variables, partNames, subrule.NonHeadLhs, defaultTable, "nonhead_");

            XElement outputElem = compSubruleElem.Element("MorphologicalOutput");
            Debug.Assert(outputElem != null);

            subrule.OutMprFeatures.UnionWith(LoadMprFeatures((string) outputElem.Attribute("MPRFeatures")));

            LoadMorphologicalRhs(outputElem, variables, partNames, subrule.Rhs, defaultTable);

            return subrule;
        }
Esempio n. 15
0
        private void LoadCharacterDefinitionTable(XElement charDefTableElem)
        {
            var table = new CharacterDefinitionTable(_spanFactory) { Name = (string) charDefTableElem.Element("Name") };
            foreach (XElement segDefElem in charDefTableElem.Elements("SegmentDefinitions").Elements("SegmentDefinition").Where(IsActive))
            {
                IEnumerable<string> reps = segDefElem.Elements("Representations").Elements("Representation").Select(e => (string) e);
                FeatureStruct fs = null;
                if (_language.PhonologicalFeatureSystem.Count > 0)
                    fs = LoadFeatureStruct(segDefElem, _language.PhonologicalFeatureSystem);
                CharacterDefinition cd = table.AddSegment(reps, fs);
                _charDefs[(string) segDefElem.Attribute("id")] = cd;
            }

            foreach (XElement bdryDefElem in charDefTableElem.Elements("BoundaryDefinitions").Elements("BoundaryDefinition").Where(IsActive))
            {
                IEnumerable<string> reps = bdryDefElem.Elements("Representations").Elements("Representation").Select(e => (string) e);
                CharacterDefinition cd = table.AddBoundary(reps);
                _charDefs[(string) bdryDefElem.Attribute("id")] = cd;
            }

            _language.CharacterDefinitionTables.Add(table);
            _tables[(string) charDefTableElem.Attribute("id")] = table;
        }
Esempio n. 16
0
        private IEnumerable<AllomorphEnvironment> LoadAllomorphEnvironments(XElement envsElem, ConstraintType type, CharacterDefinitionTable defaultTable)
        {
            if (envsElem == null)
                yield break;

            foreach (XElement envElem in envsElem.Elements("Environment"))
                yield return LoadAllomorphEnvironment(envElem, type, defaultTable);
        }
Esempio n. 17
0
        private AllomorphEnvironment LoadAllomorphEnvironment(XElement envElem, ConstraintType type, CharacterDefinitionTable defaultTable)
        {
            var variables = new Dictionary<string, Tuple<string, SymbolicFeature>>();

            Pattern<Word, ShapeNode> leftEnv = LoadPhoneticTemplate(envElem.Elements("LeftEnvironment").Elements("PhoneticTemplate").SingleOrDefault(), variables, defaultTable);
            Pattern<Word, ShapeNode> rightEnv = LoadPhoneticTemplate(envElem.Elements("RightEnvironment").Elements("PhoneticTemplate").SingleOrDefault(), variables, defaultTable);
            return new AllomorphEnvironment(_spanFactory, type, leftEnv, rightEnv);
        }
Esempio n. 18
0
		void LoadCharDefTable(XmlElement charDefTableNode)
		{
			string id = charDefTableNode.GetAttribute("id");
			string name = charDefTableNode.SelectSingleNode("Name").InnerText;
			CharacterDefinitionTable charDefTable = null;

#if IPA_CHAR_DEF_TABLE
			if (id == "ipa")
				charDefTable = new IPACharacterDefinitionTable(id, name, m_curMorpher);
			else
				charDefTable = new CharacterDefinitionTable(id, name, m_curMorpher);
#else
			charDefTable = new CharacterDefinitionTable(id, name, m_curMorpher);
#endif

			charDefTable.Encoding = charDefTableNode.SelectSingleNode("Encoding").InnerText;

			XmlNodeList segDefList = charDefTableNode.SelectNodes("SegmentDefinitions/SegmentDefinition[@isActive='yes']");
			foreach (XmlNode segDefNode in segDefList)
			{
				XmlElement segDefElem = segDefNode as XmlElement;
				XmlElement repElem = segDefElem.SelectSingleNode("Representation") as XmlElement;
				string strRep = repElem.InnerText;
				charDefTable.AddSegmentDefinition(strRep, LoadFeatValues(segDefElem));
				m_repIds[repElem.GetAttribute("id")] = strRep;
			}

			XmlNodeList bdryDefList = charDefTableNode.SelectNodes("BoundaryDefinitions/BoundarySymbol");
			foreach (XmlNode bdryDefNode in bdryDefList)
			{
				XmlElement bdryDefElem = bdryDefNode as XmlElement;
				string strRep = bdryDefElem.InnerText;
				charDefTable.AddBoundaryDefinition(strRep);
				m_repIds[bdryDefElem.GetAttribute("id")] = strRep;
			}

			m_curMorpher.AddCharacterDefinitionTable(charDefTable);
		}
Esempio n. 19
0
		/// <summary>
		/// Adds the character definition table.
		/// </summary>
		/// <param name="charDefTable">The character definition table.</param>
		public void AddCharacterDefinitionTable(CharacterDefinitionTable charDefTable)
		{
			m_charDefTables.Add(charDefTable);
		}
		public SegmentDefinition(string strRep, CharacterDefinitionTable charDefTable, IEnumerable<FeatureValue> featVals,
			FeatureSystem featSys) : this(strRep, charDefTable, new FeatureBundle(featVals, featSys))
		{
		}
Esempio n. 21
0
        private IEnumerable<PatternNode<Word, ShapeNode>> LoadPatternNodes(XElement pseqElem, Dictionary<string, Tuple<string, SymbolicFeature>> variables,
			CharacterDefinitionTable defaultTable, Dictionary<string, string> groupNames)
        {
            foreach (XElement recElem in pseqElem.Elements())
            {
                PatternNode<Word, ShapeNode> node = null;
                switch (recElem.Name.LocalName)
                {
                    case "SimpleContext":
                        SimpleContext simpleCtxt = LoadSimpleContext(recElem, variables);
                        node = new Constraint<Word, ShapeNode>(simpleCtxt.FeatureStruct) {Tag = simpleCtxt};
                        break;

                    case "Segment":
                    case "BoundaryMarker":
                        CharacterDefinition cd = _charDefs[(string) recElem.Attribute(recElem.Name.LocalName == "Segment" ? "segment" : "boundary")];
                        node = new Constraint<Word, ShapeNode>(cd.FeatureStruct) {Tag = cd};
                        break;

                    case "OptionalSegmentSequence":
                        var minStr = (string) recElem.Attribute("min");
                        int min = string.IsNullOrEmpty(minStr) ? 0 : int.Parse(minStr);
                        var maxStr = (string) recElem.Attribute("max");
                        int max = string.IsNullOrEmpty(maxStr) ? -1 : int.Parse(maxStr);
                        node = new Quantifier<Word, ShapeNode>(min, max, new Group<Word, ShapeNode>(LoadPatternNodes(recElem, variables, defaultTable, groupNames)));
                        break;

                    case "Segments":
                        CharacterDefinitionTable segsTable = GetTable(recElem, defaultTable);
                        var shapeStr = (string) recElem.Element("PhoneticShape");
                        var segments = new Segments(segsTable, shapeStr);
                        node = new Group<Word, ShapeNode>(segments.Shape.Select(n => new Constraint<Word, ShapeNode>(n.Annotation.FeatureStruct))) {Tag = segments};
                        break;
                }

                Debug.Assert(node != null);
                var id = (string) recElem.Attribute("id");
                string groupName;
                if (groupNames == null || string.IsNullOrEmpty(id) || !groupNames.TryGetValue(id, out groupName))
                    yield return node;
                else
                    yield return new Group<Word, ShapeNode>(groupName, node);
            }
        }
Esempio n. 22
0
 private CharacterDefinitionTable GetTable(XElement elem, CharacterDefinitionTable defaultTable)
 {
     string tableId = (string) elem.Attribute("characterDefinitionTable");
     return tableId == null ? defaultTable : _tables[tableId];
 }
Esempio n. 23
0
        public static string ToString(this IEnumerable<ShapeNode> nodes, CharacterDefinitionTable table, bool includeBdry)
        {
            var sb = new StringBuilder();
            foreach (ShapeNode node in nodes)
            {
                if ((!includeBdry && node.Annotation.Type() == HCFeatureSystem.Boundary) || node.IsDeleted())
                    continue;

                IEnumerable<string> strReps = table.GetMatchingStrReps(node);
                string strRep = strReps.FirstOrDefault();
                if (strRep != null)
                    sb.Append(strRep);
            }
            return sb.ToString();
        }
Esempio n. 24
0
        private Pattern<Word, ShapeNode> LoadPhoneticSequence(XElement pseqElem, Dictionary<string, Tuple<string, SymbolicFeature>> variables,
			CharacterDefinitionTable defaultTable = null, string name = null)
        {
            if (pseqElem == null)
                return Pattern<Word, ShapeNode>.New().Value;
            var pattern = new Pattern<Word, ShapeNode>(name, LoadPatternNodes(pseqElem, variables, defaultTable, null));
            pattern.Freeze();
            return pattern;
        }
Esempio n. 25
0
        private Pattern<Word, ShapeNode> LoadPhoneticTemplate(XElement ptempElem, Dictionary<string, Tuple<string, SymbolicFeature>> variables,
			CharacterDefinitionTable defaultTable = null, Dictionary<string, string> groupNames = null)
        {
            var pattern = new Pattern<Word, ShapeNode>();
            if (ptempElem != null)
            {
                if ((string) ptempElem.Attribute("initialBoundaryCondition") == "true")
                    pattern.Children.Add(new Constraint<Word, ShapeNode>(HCFeatureSystem.LeftSideAnchor));
                foreach (PatternNode<Word, ShapeNode> node in LoadPatternNodes(ptempElem.Element("PhoneticSequence"), variables, defaultTable, groupNames))
                    pattern.Children.Add(node);
                if ((string) ptempElem.Attribute("finalBoundaryCondition") == "true")
                    pattern.Children.Add(new Constraint<Word, ShapeNode>(HCFeatureSystem.RightSideAnchor));
            }
            pattern.Freeze();
            return pattern;
        }
Esempio n. 26
0
        private bool TryLoadCompoundingRule(XElement compRuleElem, CharacterDefinitionTable defaultTable, out IMorphologicalRule mrule)
        {
            var compRuleID = (string) compRuleElem.Attribute("id");
            var compRule = new CompoundingRule
            {
                Name = (string) compRuleElem.Element("Name"),
                Blockable = (bool?) compRuleElem.Attribute("blockable") ?? true
            };
            var multApp = (string) compRuleElem.Attribute("multipleApplication");
            if (!string.IsNullOrEmpty(multApp))
                compRule.MaxApplicationCount = int.Parse(multApp);

            var fs = new FeatureStruct();
            var headRequiredPos = (string) compRuleElem.Attribute("headPartsOfSpeech");
            if (!string.IsNullOrEmpty(headRequiredPos))
                fs.AddValue(_posFeature, ParsePartsOfSpeech(headRequiredPos));
            XElement headRequiredHeadFeatElem = compRuleElem.Element("HeadRequiredHeadFeatures");
            if (headRequiredHeadFeatElem != null)
                fs.AddValue(_headFeature, LoadFeatureStruct(headRequiredHeadFeatElem, _language.SyntacticFeatureSystem));
            XElement headRequiredFootFeatElem = compRuleElem.Element("HeadRequiredFootFeatures");
            if (headRequiredFootFeatElem != null)
                fs.AddValue(_footFeature, LoadFeatureStruct(headRequiredFootFeatElem, _language.SyntacticFeatureSystem));
            fs.Freeze();
            compRule.HeadRequiredSyntacticFeatureStruct = fs;

            fs = new FeatureStruct();
            var nonHeadRequiredPos = (string) compRuleElem.Attribute("nonHeadPartsOfSpeech");
            if (!string.IsNullOrEmpty(nonHeadRequiredPos))
                fs.AddValue(_posFeature, ParsePartsOfSpeech(nonHeadRequiredPos));
            XElement nonHeadRequiredHeadFeatElem = compRuleElem.Element("NonHeadRequiredHeadFeatures");
            if (nonHeadRequiredHeadFeatElem != null)
                fs.AddValue(_headFeature, LoadFeatureStruct(nonHeadRequiredHeadFeatElem, _language.SyntacticFeatureSystem));
            XElement nonHeadRequiredFootFeatElem = compRuleElem.Element("NonHeadRequiredFootFeatures");
            if (nonHeadRequiredFootFeatElem != null)
                fs.AddValue(_footFeature, LoadFeatureStruct(nonHeadRequiredFootFeatElem, _language.SyntacticFeatureSystem));
            fs.Freeze();
            compRule.NonHeadRequiredSyntacticFeatureStruct = fs;

            fs = new FeatureStruct();
            var outPos = (string) compRuleElem.Attribute("outputPartOfSpeech");
            if (!string.IsNullOrEmpty(outPos))
                fs.AddValue(_posFeature, ParsePartsOfSpeech(outPos));
            XElement outHeadFeatElem = compRuleElem.Element("OutputHeadFeatures");
            if (outHeadFeatElem != null)
                fs.AddValue(_headFeature, LoadFeatureStruct(outHeadFeatElem, _language.SyntacticFeatureSystem));
            XElement outFootFeatElem = compRuleElem.Element("OutputFootFeatures");
            if (outFootFeatElem != null)
                fs.AddValue(_footFeature, LoadFeatureStruct(outFootFeatElem, _language.SyntacticFeatureSystem));
            fs.Freeze();
            compRule.OutSyntacticFeatureStruct = fs;

            var obligHeadIDsStr = (string) compRuleElem.Attribute("outputObligatoryFeatures");
            if (!string.IsNullOrEmpty(obligHeadIDsStr))
            {
                foreach (string obligHeadID in obligHeadIDsStr.Split(' '))
                    compRule.ObligatorySyntacticFeatures.Add(_language.SyntacticFeatureSystem.GetFeature(obligHeadID));
            }

            foreach (XElement subruleElem in compRuleElem.Elements("CompoundingSubrules").Elements("CompoundingSubrule").Where(IsActive))
            {
                try
                {
                    compRule.Subrules.Add(LoadCompoundingSubrule(subruleElem, defaultTable));
                }
                catch (Exception e)
                {
                    if (_errorHandler != null)
                        _errorHandler(e, compRuleID);
                    else
                        throw;
                }
            }

            if (compRule.Subrules.Count > 0)
            {
                mrule = compRule;
                return true;
            }

            mrule = null;
            return false;
        }