/** * Create an inflected word element. InflectedWordElement represents a word * that already specifies the morphological and other features that it * should exhibit in a realisation. While normally, phrases are constructed * using <code>WordElement</code>s, and features are set on phrases, it is * sometimes desirable to set features directly on words (for example, when * one wants to elide a specific word, but not its parent phrase). * * <P> * If the object passed is already a <code>WordElement</code>, then a new * * <code>InflectedWordElement<code> is returned which wraps this <code>WordElement</code> * . If the object is a <code>String</code>, then the * <code>WordElement</code> representing this <code>String</code> is looked * up, and a new * <code>InflectedWordElement<code> wrapping this is returned. If no such <code>WordElement</code> * is found, the element returned is an <code>InflectedWordElement</code> * with the supplied string as baseform and no base <code>WordElement</code> * . If an <code>NLGElement</code> is passed, this is returned unchanged. * * @param word * the word * @param category * the category * @return an <code>InflectedWordElement</code>, or the original supplied * object if it is an <code>NLGElement</code>. */ public virtual NLGElement createInflectedWord(object word, LexicalCategory category) { // first get the word element NLGElement inflElement = null; if (word is WordElement) { inflElement = new InflectedWordElement((WordElement)word); } else if (word is string) { NLGElement baseword = createWord((string)word, category); if (baseword != null && baseword is WordElement) { inflElement = new InflectedWordElement((WordElement)baseword); } else { inflElement = new InflectedWordElement((string)word, category); } } else if (word is NLGElement) { inflElement = (NLGElement)word; } return(inflElement); }
/** * Creates a new element representing a word. If the word passed is already * an <code>NLGElement</code> then that is returned unchanged. If a * <code>String</code> is passed as the word then the factory will look up * the <code>Lexicon</code> if one exists and use the details found to * create a new <code>WordElement</code>. * * @param word * the base word for the new element. This can be a * <code>NLGElement</code>, which is returned unchanged, or a * <code>String</code>, which is used to construct a new * <code>WordElement</code>. * @param category * the <code>LexicalCategory</code> for the word. * * @return an <code>NLGElement</code> representing the word. */ public virtual NLGElement createWord(object word, LexicalCategory category) { NLGElement wordElement = null; if (word is NLGElement) { wordElement = (NLGElement)word; } else if (word is string && lexicon != null) { // AG: change: should create a WordElement, not an // InflectedWordElement // wordElement = new InflectedWordElement( // (String) word, category); // if (this.lexicon != null) { // doLexiconLookUp(category, (String) word, wordElement); // } // wordElement = lexicon.getWord((String) word, category); wordElement = lexicon.lookupWord((string)word, category); if (PRONOUNS.Contains((string)word)) { setPronounFeatures(wordElement, (string)word); } } return(wordElement); }
/** * create a WordElement with the specified baseForm, category, ID * * @param baseForm * - base form of WordElement * @param category * - category of WordElement * @param id * - ID of word in lexicon */ public WordElement(string baseForm, LexicalCategory category, string id) : base() { this.baseForm = baseForm; Category = category; this.id = id; inflVars = new Dictionary <Inflection, InflectionSet>(); }
/** * this method creates an NLGElement from an object If object is null, * return null If the object is already an NLGElement, it is returned * unchanged Exception: if it is an InflectedWordElement, return underlying * WordElement If it is a String which matches a lexicon entry or pronoun, * the relevant WordElement is returned If it is a different String, a * wordElement is created if the string is a single word Otherwise a * StringElement is returned Otherwise throw an exception * * @param element * - object to look up * @param category * - default lexical category of object * @return NLGelement */ public virtual NLGElement createNLGElement(object element, LexicalCategory category) { if (element == null) { return(null); } // InflectedWordElement - return underlying word else if (element is InflectedWordElement) { return(((InflectedWordElement)element).BaseWord); } // StringElement - look up in lexicon if it is a word // otherwise return element else if (element is StringElement) { if (stringIsWord(((StringElement)element).Realisation, category)) { return(createWord(((StringElement)element).Realisation, category)); } else { return((StringElement)element); } } // other NLGElement - return element else if (element is NLGElement) { return((NLGElement)element); } // String - look up in lexicon if a word, otherwise return StringElement else if (element is string) { if (stringIsWord((string)element, category)) { return(createWord(element, category)); } else { return(new StringElement((string)element)); } } throw new ArgumentException(element.ToString() + " is not a valid type"); }
/** * A helper method to look up the lexicon for the given word. * * @param category * the <code>LexicalCategory</code> of the word. * @param word * the base form of the word. * @param wordElement * the created element representing the word. */ private void doLexiconLookUp(LexicalCategory category, string word, NLGElement wordElement) { WordElement baseWord = null; if (category.GetLexicalCategory() == LexicalCategory.LexicalCategoryEnum.NOUN && lexicon.hasWord(word, new LexicalCategory(LexicalCategory.LexicalCategoryEnum.PRONOUN))) { baseWord = lexicon.lookupWord(word, new LexicalCategory(LexicalCategory.LexicalCategoryEnum.PRONOUN)); if (baseWord != null) { wordElement.setFeature(InternalFeature.BASE_WORD, baseWord); wordElement.Category = new LexicalCategory(LexicalCategory.LexicalCategoryEnum.PRONOUN); if (!PRONOUNS.Contains(word)) { wordElement.setFeature(InternalFeature.NON_MORPH, true); } } } else { baseWord = lexicon.lookupWord(word, category); wordElement.setFeature(InternalFeature.BASE_WORD, baseWord); } }
/** * Constructs a new inflected word using the giving word as the base form. * Constructing the word also requires a lexical category (such as noun, * verb). * * @param word * the base form for this inflected word. * @param category * the lexical category for the word. */ public InflectedWordElement(string word, LexicalCategory category) : base() { setFeature(LexicalFeature.BASE_FORM, word); Category = category; }
/** * return true if string is a word * * @param string * @param category * @return */ private bool stringIsWord(string str, LexicalCategory category) { return(lexicon != null && (lexicon.hasWord(str, category) || PRONOUNS.Contains(str) || (Regex.IsMatch(str, "^" + WORD_REGEX + "$")))); }
/** * create a WordElement with the specified baseForm and category * * @param baseForm * - base form of WordElement * @param category * - category of WordElement */ public WordElement(string baseForm, LexicalCategory category) : this(baseForm, category, null) { }