Beispiel #1
0
        public void Extracting_text_returns_valid_result()
        {
            // Arrange
            var er = new ExtractionResult(int.MaxValue);

            er.Append("Test");
            var worker = new Mock <IAbbyyWorker>();

            worker.Setup(s => s.ExtractTextFromDocument(It.IsAny <string>(), It.IsAny <ITextExtractorSettings>()))
            .Returns(er);
            var sut = new AbbyyTextExtractor(worker.Object);

            sut.PathToAbbyyFrEngineDll = this.GetType().Assembly.Location;  // Just pass an existing file

            // Act
            var result = sut.ExtractText(new Doc(new FileInfo(Path.GetTempFileName()), "id"), new DefaultTextExtractorSettings("Dummy"));

            // Test
            result.HasError.Should().BeFalse();
            result.ErrorMessage.Should().BeNullOrEmpty();
            result.ToString().Should().Be("Test\r\n");
        }
Beispiel #2
0
        public ExtractionResult ExtractTextFromDocument(string inputFile, ITextExtractorSettings settings)
        {
            var retVal     = new ExtractionResult(settings.MaxExtractionSize);
            var outputFile = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString("N") + ".txt");

            var  engine            = enginesPool.GetEngine();
            bool isRecycleRequired = false;

            try
            {
                IFRDocument fineReaderDocument = null;
                try
                {
                    fineReaderDocument = LoadFineReaderDocument(inputFile, settings.TextExtractionProfile, engine, settings.Context);
                    SubscribeExtractionEvents((FRDocument)fineReaderDocument);
                    fineReaderDocument.Process();

                    // Leerseiten überspringen bei einseitigen Dokumenten.
                    if (fineReaderDocument.Pages.Count == 1)
                    {
                        // IsEmpty nutzt die Einstellungen die über das Profil festgelegt wurden
                        if (fineReaderDocument.Pages[0].IsEmpty())
                        {
                            Log.Information("The page {inputFile} was detected as empty.", inputFile);
                            return(retVal);
                        }
                    }

                    fineReaderDocument.Export(outputFile, FileExportFormatEnum.FEF_TextUnicodeDefaults, null);

                    // Read the contents of the exported file
                    using (var sr = new StreamReader(outputFile))
                    {
                        while (sr.Peek() >= 0)
                        {
                            retVal.Append(sr.ReadLine());
                            if (retVal.LimitExceeded)
                            {
                                break;
                            }
                        }
                    }

                    // Alles OK
                    return(retVal);
                }
                finally
                {
                    if (fineReaderDocument != null)
                    {
                        UnsubscribeExtractionEvents((FRDocument)fineReaderDocument);
                        fineReaderDocument.Close();
                        if (Marshal.IsComObject(fineReaderDocument))
                        {
                            Marshal.ReleaseComObject(fineReaderDocument);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex, $"Abbyy Textextraction failed: {ex.Message}");
                isRecycleRequired   = enginesPool.ShouldRestartEngine(ex);
                retVal.HasError     = true;
                retVal.ErrorMessage = ex.Message;

                // Push an error message to indicate that the item has failed
                bus.Publish <AbbyyProgressEvent>(new
                {
                    __TimeToLive = TimeSpan.FromSeconds(3),
                    File         = sourceFile,
                    Process      = ProcessType.TextExtraction,
                    EventType    = AbbyyEventType.AbbyyOnProgressEvent,
                    HasFailed    = true
                });
            }
            finally
            {
                // Push the final message to indicate that the item is finished
                bus.Publish <AbbyyProgressEvent>(new
                {
                    __TimeToLive = TimeSpan.FromSeconds(3),
                    File         = sourceFile,
                    IsComplete   = true,
                    Percentage   = 100,
                    Process      = ProcessType.TextExtraction,
                    EventType    = AbbyyEventType.AbbyyOnProgressEvent
                });

                enginesPool.ReleaseEngine(engine, isRecycleRequired);
                if (File.Exists(outputFile))
                {
                    File.Delete(outputFile);
                }
            }

            return(retVal);
        }
Beispiel #3
0
        public ExtractionResult ExtractTextFromDocument(string inputFile, ITextExtractorSettings settings)
        {
            var retVal     = new ExtractionResult(settings.MaxExtractionSize);
            var outputFile = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString("N") + ".txt");

            var  engine            = enginesPool.GetEngine();
            bool isRecycleRequired = false;


            try
            {
                IFRDocument fineReaderDocument = null;
                try
                {
                    fineReaderDocument = LoadFineReaderDocument(inputFile, settings.TextExtractionProfile, engine);
                    var documentProcessingParams = CreateProcessingParams(engine);
                    fineReaderDocument.Process(documentProcessingParams);

                    // Leerseiten überspringen bei einseitigen Dokumenten.
                    if (fineReaderDocument.Pages.Count == 1)
                    {
                        // IsEmpty nutzt die Einstellungen die über das Profil festgelegt wurden
                        if (fineReaderDocument.Pages[0].IsEmpty())
                        {
                            return(retVal);
                        }
                    }

                    fineReaderDocument.Export(outputFile, FileExportFormatEnum.FEF_TextUnicodeDefaults, null);

                    // Read the contents of the exported file
                    using (var sr = new StreamReader(outputFile))
                    {
                        while (sr.Peek() >= 0)
                        {
                            retVal.Append(sr.ReadLine());
                            if (retVal.LimitExceeded)
                            {
                                break;
                            }
                        }
                    }

                    // Alles OK
                    return(retVal);
                }
                finally
                {
                    if (fineReaderDocument != null)
                    {
                        fineReaderDocument.Close();
                        if (Marshal.IsComObject(fineReaderDocument))
                        {
                            Marshal.ReleaseComObject(fineReaderDocument);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex, $"Abbyy Textextraction failed: {ex.Message}");
                isRecycleRequired   = enginesPool.ShouldRestartEngine(ex);
                retVal.HasError     = true;
                retVal.ErrorMessage = ex.Message;
            }
            finally
            {
                enginesPool.ReleaseEngine(engine, isRecycleRequired);
                if (File.Exists(outputFile))
                {
                    File.Delete(outputFile);
                }
            }

            return(retVal);
        }