コード例 #1
0
        private String applyRules(String st, SuffixTree <SavoyRule> rules)
        {
            int length = st.Length - 1;

            if (length < rules.Properties["size"])              //If the word is smaller than the minimum stemming size of this step, ignore it
            {
                return(st);
            }

            List <Pair <String, SavoyRule> > res = rules.getLongestSuffixesAndValues(st);

            for (int i = res.Count - 1; i >= 0; i--)
            {
                Pair <String, SavoyRule> r = res[i];
                String    suffix           = r.First;
                SavoyRule rule             = r.Second;
                if (length > rule.size)
                {
                    return(st.Substring(0, st.Length - suffix.Length) + rule.replacement);
                }
            }
            return(st);
        }
コード例 #2
0
ファイル: SavoyStemmer.cs プロジェクト: ebreti/ptstemmer
        public void readRulesFromXML()
        {
            XmlDocument doc = new XmlDocument();
            try{
                doc.Load(System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("SavoyStemmerRules.xml"));
            }catch(Exception e) {
                throw new PTStemmerException("Problem while parsing Savoy's XML stemming rules file.",e);}

            XmlElement root = doc.DocumentElement;
            XmlAttribute val,val2,val3;

            foreach (XmlNode step in root.ChildNodes)
            {
                val = step.Attributes["name"];
                if(val == null)
                    throw new PTStemmerException("Problem while parsing Savoy's XML stemming rules file: Invalid step.");
                String stepName = val.Value;
                SuffixTree<SavoyRule> suffixes = new SuffixTree<SavoyRule>();
                setProperty(suffixes,"size",0,step);

                foreach (XmlNode rule in step.ChildNodes)
                {
                    val = rule.Attributes["suffix"];
                    val2 = rule.Attributes["replacement"];
                    val3 = rule.Attributes["size"];

                    if(val == null || val2 == null || val3 == null)
                        throw new PTStemmerException("Problem while parsing Savoy's XML stemming rules file: Invalid rule in "+stepName+".");

                    String suffix = val.Value;
                    String replacement = val2.Value;
                    int size = 0;
                    try{
                        size = Convert.ToInt32(val3.Value);
                    }catch(Exception e) {throw new PTStemmerException("Problem while parsing Savoy's XML stemming rules file: Missing or invalid rules properties on step "+stepName+".", e);}

                    SavoyRule r = new SavoyRule(size,replacement);
                    suffixes.addSuffix(suffix,r);
                }

                if(stepName.Equals("pluralreduction"))
                    pluralreductionrules = suffixes;
                else if(stepName.Equals("femininereduction"))
                    femininereductionrules = suffixes;
                else if(stepName.Equals("finalvowel"))
                    finalvowel = suffixes;

            }

            if(pluralreductionrules == null || femininereductionrules == null || finalvowel == null)
                throw new PTStemmerException("Problem while parsing Savoy's XML stemming rules file: Missing steps.");
        }
コード例 #3
0
        public void readRulesFromXML()
        {
            XmlDocument doc = new XmlDocument();

            try{
                doc.Load(System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("SavoyStemmerRules.xml"));
            }catch (Exception e) {
                throw new PTStemmerException("Problem while parsing Savoy's XML stemming rules file.", e);
            }

            XmlElement   root = doc.DocumentElement;
            XmlAttribute val, val2, val3;

            foreach (XmlNode step in root.ChildNodes)
            {
                val = step.Attributes["name"];
                if (val == null)
                {
                    throw new PTStemmerException("Problem while parsing Savoy's XML stemming rules file: Invalid step.");
                }
                String stepName = val.Value;
                SuffixTree <SavoyRule> suffixes = new SuffixTree <SavoyRule>();
                setProperty(suffixes, "size", 0, step);

                foreach (XmlNode rule in step.ChildNodes)
                {
                    val  = rule.Attributes["suffix"];
                    val2 = rule.Attributes["replacement"];
                    val3 = rule.Attributes["size"];

                    if (val == null || val2 == null || val3 == null)
                    {
                        throw new PTStemmerException("Problem while parsing Savoy's XML stemming rules file: Invalid rule in " + stepName + ".");
                    }

                    String suffix      = val.Value;
                    String replacement = val2.Value;
                    int    size        = 0;
                    try{
                        size = Convert.ToInt32(val3.Value);
                    }catch (Exception e) { throw new PTStemmerException("Problem while parsing Savoy's XML stemming rules file: Missing or invalid rules properties on step " + stepName + ".", e); }

                    SavoyRule r = new SavoyRule(size, replacement);
                    suffixes.addSuffix(suffix, r);
                }

                if (stepName.Equals("pluralreduction"))
                {
                    pluralreductionrules = suffixes;
                }
                else if (stepName.Equals("femininereduction"))
                {
                    femininereductionrules = suffixes;
                }
                else if (stepName.Equals("finalvowel"))
                {
                    finalvowel = suffixes;
                }
            }

            if (pluralreductionrules == null || femininereductionrules == null || finalvowel == null)
            {
                throw new PTStemmerException("Problem while parsing Savoy's XML stemming rules file: Missing steps.");
            }
        }