public GuessResult Guess(string phrase, NetworkSetting networkSetting) { RegisterState(null); GuessResult result = GuessResultsCache.GetCachedGuessResult(this, phrase); lock (result) { if (result.IsHasData()) { return(result); } long start = DateTime.Now.Ticks; try { string error; if (CheckPhrase(phrase, out error)) { DoGuess(phrase, result, networkSetting); } else { result.Error = new TranslationException(error); } } catch (System.Exception e) { result.Error = e; } result.QueryTicks = DateTime.Now.Ticks - start; } return(result); }
static void AddToCache(GuessResult result) { int minimalCacheLength = TranslateOptions.Instance.GuessingOptions.MinimalTextLengthForSwitchByLanguage; if (result.Phrase.Length < minimalCacheLength || !result.IsReliable) { return; } lock (cache) { if (cache.Count > 0 && cache[0] == result) { return; } cache.Remove(result); cache.Insert(0, result); if (cache.Count > 10) { cache.RemoveAt(10); } } }
protected abstract void DoGuess(string phrase, GuessResult result, NetworkSetting networkSetting);
public static GuessResult GetCachedGuessResult(LanguageGuesser serviceItem, string phrase) { if (!useCache) { return(new GuessResult(serviceItem, phrase)); } string key = phrase.Trim().ToLowerInvariant(); if (key.Length > 300) { key = key.Substring(0, 300); } GuessResultsHashtable collection; bool collection_exists = true; lock (cache) { if (!cache.TryGetValue(key, out collection)) { collection = new GuessResultsHashtable(); cache.Add(key, collection); collection_exists = false; } } int hash = serviceItem.FullName.GetHashCode(); bool needed_new_result = !collection_exists; GuessResult res = null; lock (collection) { if (!needed_new_result) { if (!collection.TryGetValue(hash, out res)) { needed_new_result = true; } else { needed_new_result = (res.Error != null && !res.ResultNotFound) || res.Phrase != phrase; } } if (needed_new_result) { res = new GuessResult(serviceItem, phrase); collection[hash] = res; lock (results_history) { results_history.Add(res); } } else { res.LastUsed = DateTime.Now; lock (results_history) { results_history.Remove(res); results_history.Add(res); } } } return(res); }
protected override void DoGuess(string phrase, GuessResult result, NetworkSetting networkSetting) { string query_base = "http://ajax.googleapis.com/ajax/services/language/detect?" + "v=1.0&hl=en&q="; int allowed_length = 2070 - query_base.Length; string query = SplitQuery(phrase, allowed_length); if (query.Length > 500) { throw new InvalidOperationException("The length of string is greater of 500 characters." + " string : " + query + ", full phrase : " + phrase ); } string real_query = query_base + HttpUtility.UrlEncode(query); if (real_query.Length > 2070) { throw new InvalidOperationException("The length of query is greater of 2070 characters." + " query : " + real_query + " full phrase : " + phrase ); } WebRequestHelper helper = new WebRequestHelper(result, new Uri(real_query), networkSetting, WebRequestContentType.UrlEncodedGet, true); helper.Referer = "http://translate-net.appspot.com/"; string responseFromServer = helper.GetResponse(); if (responseFromServer.Contains(", \"responseStatus\": 200}")) { string languageString = StringParser.Parse("\"language\":\"", "\"", responseFromServer); string isReliableString = StringParser.Parse("\"isReliable\":", ",", responseFromServer); string confidenceString = StringParser.Parse("\"confidence\":", "}", responseFromServer); Language language; if (languageString == "pt-PT") { language = Language.Portuguese; } else { if (!keyToLang.TryGetValue(languageString, out language)) { throw new TranslationException("Language : " + languageString + " not supported"); } } bool isReliable = isReliableString == "true"; //if(phrase.Length < 30) // isReliable = false; NumberFormatInfo num = NumberFormatInfo.InvariantInfo; confidenceString = confidenceString.Replace(",", num.NumberDecimalSeparator); confidenceString = confidenceString.Replace(".", num.NumberDecimalSeparator); double confidence; if (!double.TryParse(confidenceString, NumberStyles.Number | NumberStyles.AllowExponent, num, out confidence)) { throw new TranslationException("Can't parse string : " + confidenceString + " to float"); } result.AddScore(language, confidence * 100, isReliable); } else { string error = StringParser.Parse("\"responseDetails\": \"", "\"", responseFromServer); string code = StringParser.Parse("\"responseStatus\":", "}", responseFromServer); throw new TranslationException(error + ", error code : " + code); } }
static GuessResult GetFromCache(string phrase) { int minimalCacheLength = TranslateOptions.Instance.GuessingOptions.MinimalTextLengthForSwitchByLanguage; if (phrase.Length < minimalCacheLength) { return(null); } lock (cache) { GuessResultsCollection to_delete = null; GuessResult result = null; foreach (GuessResult r in cache) { if (r.Phrase.Length < minimalCacheLength) { if (to_delete == null) { to_delete = new GuessResultsCollection(); } to_delete.Add(r); } else { if (phrase.Length > r.Phrase.Length) { if (phrase.StartsWith(r.Phrase)) { result = r; } } else { if (r.Phrase.StartsWith(phrase)) { result = r; } } if (result != null) { break; } } } if (result != null) { cache.Remove(result); } if (to_delete != null && to_delete.Count > 0) { foreach (GuessResult r in to_delete) { cache.Remove(r); } } if (result != null) { cache.Insert(0, result); } return(result); } }