public static string Get(LexicalCategory category, string word) { switch (category) // requests the method for the corresponding category { case LexicalCategory.AllTypes: return(Request("", word)); case LexicalCategory.Adjective: return(Request("adjective", word)); case LexicalCategory.Adverb: return(Request("adverb", word)); case LexicalCategory.Noun: return(Request("noun", word)); case LexicalCategory.Idiomatic: return(Request("idiomatic", word)); case LexicalCategory.Verb: return(Request("verb", word)); case LexicalCategory.Residual: return(Request("residual", word)); case LexicalCategory.Interjection: return(Request("interjection", word)); default: return("Couldn't find the specified lexical category!"); } }
/** * check if this record has a standard (regular) inflection * * @param record * @param simplenlg * syntactic category * @return true if standard (regular) inflection */ private bool standardInflections(LexRecord record, LexicalCategory category) { List <string> variants = null; switch (category.GetLexicalCategory()) { case LexicalCategory.LexicalCategoryEnum.NOUN: variants = record.GetCatEntry().GetNounEntry().GetVariants(); break; case LexicalCategory.LexicalCategoryEnum.ADJECTIVE: variants = record.GetCatEntry().GetAdjEntry().GetVariants(); break; case LexicalCategory.LexicalCategoryEnum.ADVERB: variants = record.GetCatEntry().GetAdvEntry().GetVariants(); break; case LexicalCategory.LexicalCategoryEnum.MODAL: variants = record.GetCatEntry().GetModalEntry().GetVariant(); break; case LexicalCategory.LexicalCategoryEnum.VERB: if (record.GetCatEntry().GetVerbEntry() != null) // aux verbs (eg be) won't have verb entries { variants = record.GetCatEntry().GetVerbEntry().GetVariants(); } break; } return(variants != null && variants.Contains("reg")); }
/** * check if this record has a standard (regular) inflection * * @param record * @param simplenlg * syntactic category * @return true if standard (regular) inflection */ private bool standardInflections(LexRecord record, LexicalCategory category) { List <string> variants = null; switch (((LexicalCategory)category).lexType) { case LexicalCategoryEnum.NOUN: variants = record.GetCatEntry().GetNounEntry().GetVariants(); break; case LexicalCategoryEnum.ADJECTIVE: variants = record.GetCatEntry().GetAdjEntry().GetVariants(); break; case LexicalCategoryEnum.ADVERB: variants = record.GetCatEntry().GetAdvEntry().GetVariants(); break; case LexicalCategoryEnum.MODAL: variants = record.GetCatEntry().GetModalEntry().GetVariant(); break; case LexicalCategoryEnum.VERB: if (record.GetCatEntry().GetVerbEntry() != null) // aux verbs (eg // be) won't // have verb // entries { variants = record.GetCatEntry().GetVerbEntry().GetVariants(); } break; } return(notEmpty(variants) && variants.contains("reg")); }
/** * get matching keys from an index map * * @param indexKey * @param category * @param indexMap * @return */ private IList <WordElement> getWordsFromIndex(string indexKey, LexicalCategory category, IDictionary <string, IList <WordElement> > indexMap) { IList <WordElement> result = new List <WordElement>(); // case 1: unknown, return empty list if (!indexMap.ContainsKey(indexKey)) { return(result); } // case 2: category is ANY, return everything if (category.GetLexicalCategory() == LexicalCategory.LexicalCategoryEnum.ANY) { foreach (WordElement word in indexMap[indexKey]) { result.Add(new WordElement(word)); } return(result); } else { // case 3: other category, search for match foreach (WordElement word in indexMap[indexKey]) { if (word.Category == category) { result.Add(new WordElement(word)); } } } return(result); }
/** * Unwrap word element. * * @param wordElement * the word element * @return the nLG element */ private NLGElement UnwrapWordElement(XmlWordElement wordElement) { NLGElement word = null; if (wordElement != null) { if (true.Equals(wordElement.Canned)) { word = factory.createStringElement(wordElement.Base); } else { LexicalCategory lexCat = new LexicalCategory(LexicalCategory.LexicalCategoryEnum.ANY); ElementCategory cat = UnwrapCategory(wordElement.Cat); if (cat != null && cat is LexicalCategory) { lexCat = (LexicalCategory)cat; } // String baseForm = getBaseWord(wordElement); string baseForm = wordElement.Base; if (!ReferenceEquals(baseForm, null)) { word = factory.createWord(baseForm, lexCat); if (word is InflectedWordElement && ((InflectedWordElement)word).BaseWord.BaseForm.Length == 0) { word = null; // cch TESTING } else if (word is WordElement) { WordElement we = (WordElement)word; // Inflection if (wordElement.Var != null) { Enum.TryParse(wordElement.Var.ToString(), out Inflection defaultInflection); we.setDefaultInflectionalVariant(defaultInflection); } // Spelling variant may have been given as base form in xml. // If so, use that variant. if (!Regex.IsMatch(baseForm, "^" + we.BaseForm + "$")) { we.DefaultSpellingVariant = baseForm; } } } } } return(word); }
/** * returns a WordElement which has the specified inflected form and/or * spelling variant that matches the specified variant, of the specified * category * * @param variant * - base form, inflected form, or spelling variant of word * @param category * - syntactic category of word (ANY for unknown) * @return a matching WordElement (if found), otherwise a new word is * created using thie variant as the base form */ public virtual WordElement getWordFromVariant(string variant, LexicalCategory category) { IList <WordElement> wordElements = getWordsFromVariant(variant, category); if (wordElements.Count == 0) { return(createWord(variant, category)); // return default WordElement using variant as baseform } else { return(selectMatchingWord(wordElements, variant)); } }
/** * Realises the key word of the interrogative. For example, <em>who</em>, * <em>what</em> * * @param keyWord * the key word of the interrogative. * @param cat * the category (usually pronoun, but not in the case of * "how many") * @param parent * the parent <code>SyntaxProcessor</code> that will do the * realisation of the complementiser. * @param realisedElement * the current realisation of the clause. * @param phraseFactory * the phrase factory to be used. */ private static void realiseInterrogativeKeyWord(string keyWord, LexicalCategory cat, SyntaxProcessor parent, ListElement realisedElement, NLGFactory phraseFactory) { if (!ReferenceEquals(keyWord, null)) { NLGElement question = phraseFactory.createWord(keyWord, cat); NLGElement currentElement = parent.realise(question); if (currentElement != null) { realisedElement.addComponent(currentElement); } } }
/** * get a WordElement which has the specified base form and category * * @param baseForm * - base form of word, eg "be" or "dog" (not "is" or "dogs") * @param category * - syntactic category of word (ANY for unknown) * @return if Lexicon contains such a WordElement, it is returned (the first * match is returned if there are several matches). If the Lexicon * does not contain such a WordElement, a new WordElement is created * and returned */ public virtual WordElement getWord(string baseForm, LexicalCategory category) { // convenience method derived from other methods IList <WordElement> wordElements = getWords(baseForm, category); if (wordElements.Count == 0) { return(createWord(baseForm, category)); // return default WordElement of this baseFOrm, category } else { return(selectMatchingWord(wordElements, baseForm)); } }
/** * return list of WordElement from LexAccessApiResult * * @param category * - desired category (eg, NOUN) (this filters list) * @param lexResult * - the LexAccessApiResult * @return list of WordElement */ private IList <WordElement> getWordsFromLexResult(LexicalCategory category, LexAccessApiResult lexResult) { List <LexRecord> recordList = lexResult.GetJavaObjs(); // set up array of words to return IList <WordElement> wordElements = new List <WordElement>(); // iterate through result records, adding to words as appropriate foreach (LexRecord record in recordList) { if (category.GetLexicalCategory() == LexicalCategory.LexicalCategoryEnum.ANY || category == getSimplenlgCategory(record)) { wordElements.Add(makeWord(record)); } } return(wordElements); }
/* * (non-Javadoc) * * @see simplenlg.lexicon.Lexicon#getWordsFromVariant(java.lang.string, * simplenlg.features.LexicalCategory) */ public List <WordElement> getWordsFromVariant(string variant, LexicalCategory category) { // get words from DB try { LexAccessApiResult lexResult = lexdb.GetLexRecords(variant); return(getWordsFromLexResult(category, lexResult)); } catch (SQLException ex) { Console.WriteLine("Lexical DB error: " + ex.ToString()); // probably should thrown an exception } return(null); }
/* (non-Javadoc) * @see simplenlg.lexicon.Lexicon#getWordsFromVariant(java.lang.String, simplenlg.features.LexicalCategory) */ public override IList <WordElement> getWordsFromVariant(string variant, LexicalCategory category) { IList <WordElement> result = new List <WordElement>(); foreach (Lexicon lex in lexiconList) { IList <WordElement> lexResult = lex.getWordsFromVariant(variant, category); if (lexResult != null && lexResult.Any()) { ((List <WordElement>)result).AddRange(lexResult); if (!alwaysSearchAll) { return(result); } } } return(result); }
/****************************************************************************/ // core methods to retrieve words from DB /****************************************************************************/ /* * (non-Javadoc) * * @see simplenlg.lexicon.Lexicon#getWords(java.lang.String, * simplenlg.features.LexicalCategory) */ public override IList <WordElement> getWords(string baseForm, LexicalCategory category) { lock (this) { // get words from DB try { LexAccessApiResult lexResult = lexdb.GetLexRecordsByBase(baseForm, LexAccessApi.B_EXACT); return(getWordsFromLexResult(category, lexResult)); } catch (Exception ex) { Console.WriteLine("Lexical DB error: " + ex.ToString()); // probably should thrown an exception } return(null); } }
/***************************************************************************/ // default methods for looking up words // These try the following (in this order) // 1) word with matching base // 2) word with matching variant // 3) word with matching ID // 4) create a new workd /***************************************************************************/ /** * General word lookup method, tries base form, variant, ID (in this order) * Creates new word if can't find existing word * * @param baseForm * @param category * @return word */ public virtual WordElement lookupWord(string baseForm, LexicalCategory category) { if (hasWord(baseForm, category)) { return(getWord(baseForm, category)); } else if (hasWordFromVariant(baseForm, category)) { return(getWordFromVariant(baseForm, category)); } else if (hasWordByID(baseForm)) { return(getWordByID(baseForm)); } else { return(createWord(baseForm, category)); } }
/** * return <code>true</code> if the lexicon contains a WordElement which has * the specified base form and category * * @param baseForm * - base form of word, eg "be" or "dog" (not "is" or "dogs") * @param category * - syntactic category of word (ANY for unknown) * @return <code>true</code> if Lexicon contains such a WordElement */ public virtual bool hasWord(string baseForm, LexicalCategory category) { // convenience method derived from other methods return(getWords(baseForm, category).Any()); }
/** * make a WordElement from a lexical record. Currently just specifies basic * params and inflections Should do more in the future! * * @param record * @return */ private WordElement makeWord(LexRecord record) // LexRecord { // get basic data String baseForm = record.GetBase(); LexicalCategory category = record.GetSimpleNLGCategory(record); String id = record.GetEui(); // create word class WordElement wordElement = new WordElement(baseForm, category, id); // now add type information switch (category.GetLexicalCategory()) { case LexicalCategory.LexicalCategoryEnum.ADJECTIVE: addAdjectiveInfo(wordElement, record.GetCatEntry().GetAdjEntry()); break; case LexicalCategory.LexicalCategoryEnum.ADVERB: addAdverbInfo(wordElement, record.GetCatEntry().GetAdvEntry()); break; case LexicalCategory.LexicalCategoryEnum.NOUN: addNounInfo(wordElement, record.GetCatEntry().GetNounEntry()); break; case LexicalCategory.LexicalCategoryEnum.VERB: addVerbInfo(wordElement, record.GetCatEntry().GetVerbEntry()); break; // ignore closed class words } Inflection?defaultInfl = wordElement.getDefaultInflectionalVariant(); // now add inflected forms // if (keepStandardInflections || !standardInflections(record, // category)) { foreach (InflVar inflection in record.GetInflVarsAndAgreements().GetInflValues()) { String simplenlgInflection = getSimplenlgInflection(inflection .GetInflection()); if (simplenlgInflection != null) { String inflectedForm = inflection.GetVar(); Inflection?inflType = Inflection.REGULAR.getInflCode(inflection.GetType()); // store all inflectional variants, except for regular ones // unless explicitly set if (inflType != null && !(Inflection.REGULAR == inflType && !keepStandardInflections)) { wordElement.addInflectionalVariant((Inflection)inflType, simplenlgInflection, inflectedForm); } // if the infl variant is the default, also set this feature on // the word if (defaultInfl == null || (defaultInfl.Equals(inflType) && !(Inflection.REGULAR.Equals(inflType) && !keepStandardInflections))) { wordElement.setFeature(simplenlgInflection, inflectedForm); } // wordElement // .setFeature(simplenlgInflection, inflection.GetVar()); } } // } // add acronym info addAcronymInfo(wordElement, record); // now add spelling variants addSpellingVariants(wordElement, record); return(wordElement); }
/****************************************************************************/ // get words by variant - try to return a WordElement given an inflectional // or spelling // variant. For the moment, acronyms are considered as separate words, not // variants // (this may change in the future) // fundamental version is getWordsFromVariant(String baseForm, Category // category), // this must be defined by subclasses. Other versions are convenience // methods. These may be overriden for efficiency, but this is not required. /****************************************************************************/ /** * returns Words which have an inflected form and/or spelling variant that * matches the specified variant, and are in the specified category. <br> * <I>Note:</I> the returned word list may not be complete, it depends on * how it is implemented by the underlying lexicon * * @param variant * - base form, inflected form, or spelling variant of word * @param category * - syntactic category of word (ANY for unknown) * @return list of all matching Words (empty list if no matching WordElement * found) */ public abstract IList <WordElement> getWordsFromVariant(string variant, LexicalCategory category);
/** * return <code>true</code> if the lexicon contains a WordElement which * matches the specified variant form and category * * @param variant * - base form, inflected form, or spelling variant of word * @param category * - syntactic category of word (ANY for unknown) * @return <code>true</code> if Lexicon contains such a WordElement */ public virtual bool hasWordFromVariant(string variant, LexicalCategory category) { // convenience method derived from other methods return(getWordsFromVariant(variant, category).Any()); }
public Terminal(String word, LexicalCategory category) { Word = word; Category = category; }
/* * (non-Javadoc) * * @see simplenlg.lexicon.Lexicon#getWordsFromVariant(java.lang.String, * simplenlg.features.LexicalCategory) */ public override IList <WordElement> getWordsFromVariant(string variant, LexicalCategory category) { return(getWordsFromIndex(variant, category, indexByVariant)); }
/****************************************************************************/ // get words by baseform and category // fundamental version is getWords(String baseForm, Category category), // this must be defined by subclasses. Other versions are convenience // methods. These may be overriden for efficiency, but this is not required. /****************************************************************************/ /** * returns all Words which have the specified base form and category * * @param baseForm * - base form of word, eg "be" or "dog" (not "is" or "dogs") * @param category * - syntactic category of word (ANY for unknown) * @return collection of all matching Words (may be empty) */ public abstract IList <WordElement> getWords(string baseForm, LexicalCategory category);
/******************************************************************************************/ // main methods to get data from lexicon /******************************************************************************************/ /* * (non-Javadoc) * * @see simplenlg.lexicon.Lexicon#getWords(java.lang.String, * simplenlg.features.LexicalCategory) */ public override IList <WordElement> getWords(string baseForm, LexicalCategory category) { return(getWordsFromIndex(baseForm, category, indexByBase)); }
/** * create a default WordElement. May be overridden by specific types of * lexicon * * @param baseForm * - base form of word * @param category * - category of word * @return WordElement entry for specified info */ protected internal virtual WordElement createWord(string baseForm, LexicalCategory category) { return(new WordElement(baseForm, category)); // return default // WordElement of this baseForm, category }
public Tag(string token, LexicalCategory lexicalCategory) { Token = token; LexicalCategory = lexicalCategory; }
/// <summary> /// Initializes a new instance of the <see cref="NinjaEntry"/> class. /// </summary> /// <param name="category">The category the term falls under.</param> /// <param name="definition">The text that defines the word.</param> /// <param name="example">An example for the word.</param> public NinjaEntry(LexicalCategory category, string definition, string example) { Category = category; Definition = definition; Example = example; }