/// <summary> /// Queries the dictionary for translation of <c>token</c>, into opposite <c>side</c> /// </summary> /// <param name="token">The token to translate, in language of the <c>side</c> specified</param> /// <param name="scope">The scope of search, the matching rule</param> /// <param name="side">Side of the dictionary that the search <c>token</c> is from</param> /// <returns></returns> public apertiumDictionaryResult query(String token, apertiumDictQueryScope scope = apertiumDictQueryScope.startingWith, apertiumDictNeedleSide side = apertiumDictNeedleSide.native) { apertiumDictionaryResult output = new apertiumDictionaryResult(); String query = makeQuery(token, scope, side); fileTextSearchResult res = dictionaryOperator.Search(query, true, 50, RegexOptions.IgnoreCase); foreach (var pair in res) { output.addLine(pair.Value); } return(output); }
/// <summary> /// Builds a term model out from Word input /// </summary> /// <param name="word">The word.</param> /// <param name="response">The response.</param> /// <returns></returns> public static termExploreModel exploreWithUnitex(string word, ILogBuilder response, bool wordIsLemma = false) { termExploreModel output = new termExploreModel(); output.modelSource = termExploreModelSource.fromToken; output.inputForm = word; string lemma = word; var tls = semanticLexiconManager.manager.resolve(word); if (tls != null) { if (Enumerable.Count(tls) > 0) { if (response != null) { response.AppendLine("#1 Lemma already defined in the triplestore [" + word + "] "); } output = semanticLexiconManager.manager.constructor.getTermModel(Enumerable.First(tls)); return(output); } } if (!wordIsLemma) { if (response != null) { response.AppendLine("#1 Finding Lemma for [" + word + "] "); } string query = string.Format(posConverter.REGEX_UNITEX_InstanceToLemmaFormat, word); fileTextSearchResult reslt = languageManagerUnitex.manager.operatorDelaf.Search(query, true, 1, RegexOptions.IgnoreCase); if (response != null) { reslt.ToString(response, true); } Regex instanceToLemmaReg = new Regex(query); if (reslt.Count() > 0) { var lnp = reslt.First(); Match mch = instanceToLemmaReg.Match(lnp.Value); lemma = mch.Groups[1].Value; } } else { if (response != null) { response.AppendLine("#1 The word is trusted to be a lemma [" + word + "] - skipping search"); } } // <------------------------------------------------------------------- preparing chache --------------- var cache = languageManagerUnitex.manager.operatorDelaf.Search(lemma, false, 300); if (response != null) { response.AppendLine("Cached definitions [" + cache.Count() + "] "); } // <------------------------------------------------------------ 2. finding lemma definition output.lemmaForm = lemma; output.lemma = new termExploreItem(lemma); if (response != null) { response.AppendLine("#2 Finding Lemma definition [" + lemma + "] "); } string lemmaQuery = string.Format(posConverter.REGEX_UNITEX_DeclarationForLemma, lemma); Regex lemmaQueryRegex = new Regex(lemmaQuery); fileTextSearchResult lemmaResult = languageManagerUnitex.manager.operatorDelaf.Search(cache, lemmaQuery, true, 5, RegexOptions.IgnoreCase); if (response != null) { lemmaResult.ToString(response, true); } if (lemmaResult.Count() == 0) { if (response != null) { response.consoleAltColorToggle(); response.AppendLine("Failed to find lemma definition for [" + word + "]. Aborting exploration."); response.consoleAltColorToggle(); } output.wasExploreFailed = true; return(output); } foreach (var lr_lnp in lemmaResult) { Match lmch = lemmaQueryRegex.Match(lr_lnp.Value); if (lmch.Success) { output.lemma.gramSet.Add(lmch.Groups[1].Value); } } if (response != null) { output.lemma.ToString(response); } // <------------------------------------------------------------ 3. getting all instances for the lemma if (response != null) { response.AppendLine("#3 Extracting all instances for the Lemma [" + lemma + "] "); } string instanceQuery = string.Format(posConverter.REGEX_UNITEX_LemmaToInstanceFormat, lemma); string instanceUnitexQuery = "," + lemma + "."; Regex instanceQueryRegex = new Regex(instanceQuery); fileTextSearchResult instanceResult = languageManagerUnitex.manager.operatorDelaf.Search(cache, instanceUnitexQuery, false, 100, RegexOptions.IgnoreCase); if (response != null) { instanceResult.ToString(response, true); } foreach (var lr_lnp in instanceResult) { Match lmch = instanceQueryRegex.Match(lr_lnp.Value); output.instances.Add(lmch.Groups[1].Value, lmch.Groups[2].Value); } // <------------------------------------------------------------ 4. Resulting term model if (response != null) { response.AppendLine("#4 Resulting term model [" + lemma + "] "); output.ToString(response); } return(output); }