Пример #1
0
        /**
         * create a simplenlg WordElement from a Word node in a lexicon XML file
         *
         * @param wordNode
         * @return
         * @throws XPathUtilException
         */
        private WordElement convertNodeToWord(XmlNode wordNode)
        {
            // if this isn't a Word node, ignore it
            if (!wordNode.Name.Equals(XML_WORD, StringComparison.CurrentCultureIgnoreCase))
            {
                return(null);
            }

            // if there is no base, flag an error and return null
            // String base = XPathUtil.extractValue(wordNode, Constants.XML_BASE);
            // if (base == null) {
            // System.out.println("Error in loading XML lexicon: Word with no base");
            // return null;
            // }

            // create word
            WordElement        word        = new WordElement();
            IList <Inflection> inflections = new List <Inflection>();

            // now copy features
            XmlNodeList nodes = wordNode.ChildNodes;

            for (int i = 0; i < nodes.Count; i++)
            {
                XmlNode featureNode = nodes.Item(i);

                if (featureNode.NodeType == XmlNodeType.Element)
                {
                    string feature = featureNode.Name.Trim();
                    string value   = featureNode.InnerText;

                    if (!ReferenceEquals(value, null))
                    {
                        value = value.Trim();
                    }

                    if (ReferenceEquals(feature, null))
                    {
                        Console.Error.WriteLine("Error in XML lexicon node for " + word.ToString());
                        break;
                    }

                    if (feature.Equals(XML_BASE, StringComparison.OrdinalIgnoreCase))
                    {
                        word.BaseForm = value;
                    }
                    else if (feature.Equals(XML_CATEGORY, StringComparison.OrdinalIgnoreCase))
                    {
                        Enum.TryParse(value.ToUpper(), out LexicalCategory.LexicalCategoryEnum lexcat);
                        word.Category = new LexicalCategory(lexcat);
                    }
                    else if (feature.Equals(XML_ID, StringComparison.OrdinalIgnoreCase))
                    {
                        word.Id = value;
                    }

                    else if (ReferenceEquals(value, null) || value.Equals(""))
                    {
                        // if this is an infl code, add it to inflections
                        Inflection?infl = Inflection.REGULAR.getInflCode(feature);

                        if (infl != null)
                        {
                            inflections.Add((Inflection)infl);
                        }
                        else
                        {
                            // otherwise assume it's a boolean feature
                            word.setFeature(feature, true);
                        }
                    }
                    else
                    {
                        word.setFeature(feature, value);
                    }
                }
            }

            // if no infl specified, assume regular
            if (inflections.Count == 0)
            {
                inflections.Add(Inflection.REGULAR);
            }

            // default inflection code is "reg" if we have it, else random pick form infl codes available
            Inflection defaultInfl = inflections.Contains(Inflection.REGULAR) ? Inflection.REGULAR : inflections[0];

            word.setFeature(LexicalFeature.DEFAULT_INFL, defaultInfl);
            word.setDefaultInflectionalVariant(defaultInfl);

            foreach (Inflection infl in inflections)
            {
                word.addInflectionalVariant(infl);
            }

            // done, return word
            return(word);
        }