public void NewDetector(string oldText) { string text = String.Join(" ", oldText, m_text[0]); m_text.RemoveAt(0); var languageDetection = new MappingService(MappingAvailableServices.LanguageDetection); using (MappingPropertyBag bag = languageDetection.RecognizeText(text, null)) { MappingDataRange[] ranges = bag.GetResultRanges(); if (ranges.First().FormatData(new StringArrayFormatter()).Length == 0) { return; } string lang = ranges.First(). FormatData(new StringArrayFormatter()).First(); int count = GetCountOfWords(text, lang); AddPointToLang(lang, count); //if (m_wordCount.ContainsKey(lang)) //{ // m_wordCount[lang] += count; //} //else //{ // m_wordCount.Add(lang, count); //} } }
public static void SDUsageSample() { try { MappingService scriptDetection = new MappingService( MappingAvailableServices.ScriptDetection); using (MappingPropertyBag bag = scriptDetection.RecognizeText("This is English. АБВГД.", null)) { MappingDataRange[] ranges = bag.GetResultRanges(); Console.WriteLine("Recognized {0} script ranges", ranges.Length); NullTerminatedStringFormatter formatter = new NullTerminatedStringFormatter(); foreach (MappingDataRange range in ranges) { Console.WriteLine("Range from {0} to {1}, script {2}", range.StartIndex, range.EndIndex, range.FormatData(formatter)); } } } catch (LinguisticException exc) { Console.WriteLine("Error calling ELS: {0}, HResult: {1}", exc.ResultState.ErrorMessage, exc.ResultState.HResult); } }
internal MappingActionAsyncResult( object callerData, AsyncCallback asyncCallback, MappingPropertyBag bag, int rangeIndex, string actionId) : base(callerData, asyncCallback) { base.SetResult(bag, new MappingResultState()); _rangeIndex = rangeIndex; _actionId = actionId; }
[InlineData("TransliterationCyrillicToLatin", "добро hello утро!", 1, 0, 16)] // BUG: possible bug here -- this should be 2 ranges public void RecognizeText( string service, string text, int expNumberOfDataRanges, int expStartIndexOfFirstDataRange, int expEndIndexOfFirstDataRange) { MappingService s = new MappingService(ServiceGuidFromServiceString(service)); MappingPropertyBag b = s.RecognizeText(text, null); MappingDataRange[] rs = b.GetResultRanges(); Assert.Equal <int>(expNumberOfDataRanges, rs.Length); Assert.Equal <int>(expStartIndexOfFirstDataRange, rs[0].StartIndex); Assert.Equal <int>(expEndIndexOfFirstDataRange, rs[0].EndIndex); Assert.Equal <string>("text/plain", rs[0].ContentType); // Win7 ELS services support only "text/plain" as content type }
public static void CyrlToLatinTransUsageSample1() { try { MappingService cyrlToLatin = new MappingService( MappingAvailableServices.TransliterationCyrillicToLatin); using (MappingPropertyBag bag = cyrlToLatin.RecognizeText("АБВГД.", null)) { string transliterated = bag.GetResultRanges()[0].FormatData(new StringFormatter()); Console.WriteLine("Transliterated text: {0}", transliterated); } } catch (LinguisticException exc) { Console.WriteLine("Error calling ELS: {0}, HResult: {1}", exc.ResultState.ErrorMessage, exc.ResultState.HResult); } }
public static void CyrlToLatinTransUsageSample2() { try { MappingEnumOptions enumOptions = new MappingEnumOptions(); enumOptions.InputScript = "Cyrl"; enumOptions.OutputScript = "Latn"; enumOptions.Category = "Transliteration"; MappingService[] cyrlToLatin = MappingService.GetServices(enumOptions); using (MappingPropertyBag bag = cyrlToLatin[0].RecognizeText("АБВГД.", null)) { string transliterated = bag.GetResultRanges()[0].FormatData(new StringFormatter()); Console.WriteLine("Transliterated text: {0}", transliterated); } } catch (LinguisticException exc) { Console.WriteLine("Error calling ELS: {0}, HResult: {1}", exc.ResultState.ErrorMessage, exc.ResultState.HResult); } }
private string LanguageConverter(Guid serviceGuid, string sourceContent) { string transliterated = null; if (!string.IsNullOrEmpty(sourceContent)) { try { MappingService mapService = new MappingService(serviceGuid); using (MappingPropertyBag bag = mapService.RecognizeText(sourceContent, null)) { transliterated = bag.GetResultRanges()[0].FormatData(new StringFormatter()); } } catch (LinguisticException exc) { MessageBox.Show(exc.Message); } } return(transliterated); }
private string LanguageConverter(Guid serviceGuid, string sourceContent) { string transliterated = null; if ((sourceContent != null) && (sourceContent.Length > 0)) { try { MappingService mapService = new MappingService(serviceGuid); using (MappingPropertyBag bag = mapService.RecognizeText(sourceContent, null)) { transliterated = bag.GetResultRanges()[0].FormatData(new StringFormatter()); } } catch (LinguisticException exc) { ShowErrorMessage(String.Format("Error calling ELS: {0}, HResult: {1}", exc.ResultState.ErrorMessage, exc.ResultState.HResult)); } } return(transliterated); }
public static void LADUsageSample() { try { MappingService languageDetection = new MappingService( MappingAvailableServices.LanguageDetection); using (MappingPropertyBag bag = languageDetection.RecognizeText("This is English", null)) { string[] languages = bag.GetResultRanges()[0].FormatData( new StringArrayFormatter()); foreach (string language in languages) { Console.WriteLine("Recognized language: {0}", language); } } } catch (LinguisticException exc) { Console.WriteLine("Error calling ELS: {0}, HResult: {1}", exc.ResultState.ErrorMessage, exc.ResultState.HResult); } }
/// <summary> /// Causes an ELS service to perform an action after text recognition has occurred. For example, /// a phone dialer service first must recognize phone numbers and then can perform the "action" /// of dialing a number. /// </summary> /// <param name="bag">A <see cref="MappingPropertyBag">MappingPropertyBag</see> object containing the results of a previous call to /// MappingService.MappingRecognizeText. This parameter cannot be set to null.</param> /// <param name="rangeIndex">A starting index inside the text recognition results for a recognized /// text range. This value should be between 0 and the range count.</param> /// <param name="actionId">The identifier of the action to perform. /// This parameter cannot be set to null.</param> /// <param name="asyncCallback">An application callback delegate to receive callbacks with the results from /// the action operation. Cannot be set to null.</param> /// <param name="callerData">Optional. Private application object passed to the callback function /// by a service after the action operation is complete. The application must set this parameter to null /// to indicate no private application data.</param> /// <returns>A <see cref="MappingActionAsyncResult">MappingActionAsyncResult</see> object describing the asynchronous operation.</returns> public MappingActionAsyncResult BeginDoAction(MappingPropertyBag bag, int rangeIndex, string actionId, AsyncCallback asyncCallback, object callerData) { MappingActionAsyncResult result = new MappingActionAsyncResult(callerData, asyncCallback, bag, rangeIndex, actionId); try { ThreadPool.QueueUserWorkItem(this.RunDoAction, result); return result; } catch { result.Dispose(); throw; } }
/// <summary> /// Causes an ELS service to perform an action after text recognition has occurred. For example, /// a phone dialer service first must recognize phone numbers and then can perform the "action" /// of dialing a number. /// </summary> /// <param name="bag">A <see cref="MappingPropertyBag">MappingPropertyBag</see> object containing the results of a previous call to /// MappingService.MappingRecognizeText. This parameter cannot be set to null.</param> /// <param name="rangeIndex">A starting index inside the text recognition results for a recognized /// text range. This value should be between 0 and the range count.</param> /// <param name="actionId">The identifier of the action to perform. /// This parameter cannot be set to null.</param> public static void DoAction(MappingPropertyBag bag, int rangeIndex, string actionId) { if (bag == null) { throw new ArgumentNullException("bag"); } if (rangeIndex < 0) { throw new LinguisticException(LinguisticException.InvalidArgs); } UInt32 hResult = Win32NativeMethods.MappingDoAction(ref bag._win32PropertyBag, (uint)rangeIndex, actionId); if (hResult != 0) { throw new LinguisticException(hResult); } }
/// <summary> /// Calls an ELS service to recognize text. For example, the Microsoft Language Detection service /// will attempt to recognize the language in which the input text is written. /// </summary> /// <param name="text">The text to recognize. The text must be UTF-16, but some services have additional /// requirements for the input format. This parameter cannot be set to null.</param> /// <param name="length">Length, in characters, of the text specified in text.</param> /// <param name="index">Index inside the specified text to be used by the service. This value should be /// between 0 and length-1. If the application wants to process the entire text, it should set this /// parameter to 0.</param> /// <param name="options">Optional. A <see cref="MappingOptions">MappingOptions</see> object containing options that affect the result and /// behavior of text recognition. The application does not have to specify values for all object members. /// This parameter can be set to null to use the default mapping options.</param> /// <returns>A <see cref="MappingPropertyBag">MappingPropertyBag</see> object in which the service has stored its results. The structure is filled /// with information produced by the service during text recognition.</returns> public MappingPropertyBag RecognizeText(string text, int length, int index, MappingOptions options) { if (text == null) { throw new ArgumentNullException("text"); } if (length > text.Length || length < 0) { throw new ArgumentOutOfRangeException("length"); } if (index < 0) { throw new ArgumentOutOfRangeException("index"); } UInt32 hResult = LinguisticException.Fail; MappingPropertyBag bag = new MappingPropertyBag(options, text); try { hResult = Win32NativeMethods.MappingRecognizeText( _service, bag._text.AddrOfPinnedObject(), (uint)length, (uint)index, bag._options, ref bag._win32PropertyBag); if (hResult != 0) { throw new LinguisticException(hResult); } return bag; } catch { bag.Dispose(); throw; } }
internal void SetResult(MappingPropertyBag bag, MappingResultState resultState) { _resultState = resultState; _bag = bag; }
/// <summary> /// Causes an ELS service to perform an action after text recognition has occurred. For example, /// a phone dialer service first must recognize phone numbers and then can perform the "action" /// of dialing a number. /// </summary> /// <param name="bag">A <see cref="MappingPropertyBag">MappingPropertyBag</see> object containing the results of a previous call to /// MappingService.MappingRecognizeText. This parameter cannot be set to null.</param> /// <param name="rangeIndex">A starting index inside the text recognition results for a recognized /// text range. This value should be between 0 and the range count.</param> /// <param name="actionId">The identifier of the action to perform. /// This parameter cannot be set to null.</param> public void DoAction(MappingPropertyBag bag, int rangeIndex, string actionId) { if (rangeIndex < 0) { throw new LinguisticException(LinguisticException.E_INVALIDARG); } UInt32 hResult = Win32Methods.MappingDoAction(ref bag._win32PropertyBag, (uint)rangeIndex, actionId); if (hResult != 0) { throw new LinguisticException(hResult); } }