Exemplo n.º 1
0
 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);
     }
 }
Exemplo n.º 2
0
        public void ConstructorWithValidServiceGuidSucceeds(string service)
        {
            Guid guid = ServiceGuidFromServiceString(service);
            MappingService s = new MappingService(guid);

            Assert.Equal<Guid>(s.Guid, guid);
        }
Exemplo n.º 3
0
        public void ConstructorWithInvalidServiceGuidThrowsLinguisticException(string service)
        {
            Guid guid = ServiceGuidFromServiceString(service);

            Assert.Throws<LinguisticException>(() =>
            {
                MappingService s = new MappingService(guid);
            });
        }
Exemplo n.º 4
0
 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);
     }
 }
Exemplo n.º 5
0
        /// <summary>
        /// Retrieves a list of available ELS platform-supported services, along with associated
        /// information, according to application-specified criteria.
        /// </summary>
        /// <param name="options">Optional. A <see cref="MappingEnumOptions">MappingEnumOptions</see> object containing criteria to use during
        /// enumeration of services. The application specifies null for this parameter to retrieve all
        /// installed services.</param>
        /// <returns>An array of <see cref="MappingService">MappingService</see> objects matching the criteria supplied in the options
        /// parameter.</returns>
        public static MappingService[] GetServices(MappingEnumOptions options)
        {
            ThrowIfNotWin7();

            IntPtr servicePointer = IntPtr.Zero;
            UInt32 serviceCount = 0;
            UInt32 hresult = 0;
            IntPtr guidPointer = IntPtr.Zero;
            try
            {
                if (options != null)
                {
                    Win32EnumOptions enumOptions = options._win32EnumOption;
                    Nullable<Guid> pGuid = options._guid;
                    if (pGuid != null)
                    {
                        Guid guid = (Guid)pGuid;
                        guidPointer = Marshal.AllocHGlobal(InteropTools.SizeOfGuid);
                        Marshal.StructureToPtr(guid, guidPointer, false);
                        enumOptions._pGuid = guidPointer;
                    }
                    hresult = Win32NativeMethods.MappingGetServices(ref enumOptions, ref servicePointer, ref serviceCount);
                }
                else
                {
                    hresult = Win32NativeMethods.MappingGetServices(IntPtr.Zero, ref servicePointer, ref serviceCount);
                }

                if (hresult != 0)
                {
                    throw new LinguisticException(hresult);
                }
                if ((servicePointer == IntPtr.Zero) != (serviceCount == 0))
                {
                    throw new InvalidOperationException();
                }

                IntPtr[] services = new IntPtr[serviceCount];
                ServiceCache.Instance.RegisterServices(ref servicePointer, services);
                MappingService[] result = new MappingService[serviceCount];
                for (int i = 0; i < serviceCount; ++i)
                {
                    result[i] = new MappingService(services[i]);
                }
                return result;
            }
            finally
            {
                if (servicePointer != IntPtr.Zero)
                {
                    // Ignore the result if an exception is being thrown.
                    Win32NativeMethods.MappingFreeServicesVoid(servicePointer);
                }
                if (guidPointer != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(guidPointer);
                }
            }
        }
Exemplo n.º 6
0
        /// <summary>
        /// Retrieves a list of available ELS platform-supported services, along with associated
        /// information, according to application-specified criteria.
        /// </summary>
        /// <param name="options">Optional. A <see cref="MappingEnumOptions">MappingEnumOptions</see> object containing criteria to use during
        /// enumeration of services. The application specifies null for this parameter to retrieve all
        /// installed services.</param>
        /// <returns>An array of <see cref="MappingService">MappingService</see> objects matching the criteria supplied in the options
        /// parameter.</returns>
        public static MappingService[] GetServices(MappingEnumOptions options)
        {
            // Throw PlatformNotSupportedException if not running on Win7 or greater
            ThrowIfNotWin7();

            IntPtr pService = IntPtr.Zero;
            UInt32 dwServiceCount = 0;
            UInt32 hResult = 0;
            IntPtr guidPtr = IntPtr.Zero;
            try
            {
                if (options != null)
                {
                    Win32EnumOptions enumOptions = options._win32EnumOption;
                    Nullable<Guid> pGuid = options._guid;
                    if (pGuid != null)
                    {
                        Guid guid = (Guid)pGuid;
                        guidPtr = Marshal.AllocHGlobal(InteropTools.SizeOfGuid);
                        Marshal.StructureToPtr(guid, guidPtr, false);
                        enumOptions._pGuid = guidPtr;
                    }
                    hResult = Win32Methods.MappingGetServices(ref enumOptions, ref pService, ref dwServiceCount);
                }
                else
                {
                    hResult = Win32Methods.MappingGetServices(IntPtr.Zero, ref pService, ref dwServiceCount);
                }
                if (hResult != 0)
                {
                    throw new LinguisticException(hResult);
                }
                if ((pService == IntPtr.Zero) != (dwServiceCount == 0))
                {
                    throw new InvalidOperationException();
                }
                IntPtr[] services = new IntPtr[dwServiceCount];
                ServiceCache.Instance.RegisterServices(ref pService, services);
                MappingService[] result = new MappingService[dwServiceCount];
                for (int i = 0; i < dwServiceCount; ++i)
                {
                    result[i] = new MappingService(services[i]);
                }
                return result;
            }
            finally
            {
                if (pService != IntPtr.Zero)
                {
                    // Ignore the result if an exception is being thrown.
                    Win32Methods.MappingFreeServices(pService);
                }
                if (guidPtr != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(guidPtr);
                }
            }
        }
Exemplo n.º 7
0
 public static void SDUsageSampleAsync()
 {
     try
     {
         MappingService scriptDetection = new MappingService(
             MappingAvailableServices.ScriptDetection);
         MappingRecognizeAsyncResult asyncResult = scriptDetection.BeginRecognizeText(
             "This is English. АБВГД.", null, SDSampleCallback, null);
         MappingService.EndRecognizeText(asyncResult);
     }
     catch (LinguisticException exc)
     {
         Console.WriteLine("Error calling ELS: {0}, HResult: {1}",
             exc.ResultState.ErrorMessage, exc.ResultState.HResult);
     }
 }
Exemplo n.º 8
0
 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);
     }
 }
 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;
 }
Exemplo n.º 10
0
        [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
        }