From be544812dcde797f02ff56c8a3f9513ab94affa8 Mon Sep 17 00:00:00 2001 From: Geremia Taglialatela Date: Fri, 27 Sep 2024 09:25:55 +0200 Subject: [PATCH] Add support for specifying OCR language in Tika This commit addresses issue #302, which required Tika to specify a language for OCR other than English. The changes ensure that the application can specify the language configuration for Tesseract OCR. Close #302 --- config/app.yml | 3 + lib/colore.rb | 1 + lib/config.rb | 4 ++ lib/heathen/processor_methods/libreoffice.rb | 1 + lib/heathen/processor_methods/pdftotext.rb | 1 + lib/tika_config.rb | 61 ++++++++++++++++++ spec/fixtures/heathen/quickfox.ar.jpg | Bin 0 -> 6461 bytes spec/fixtures/heathen/quickfox.ar.pdf | Bin 0 -> 10639 bytes spec/fixtures/heathen/quickfox.ar.txt | 1 + .../processor_methods/pdftotext_spec.rb | 15 ++++- spec/lib/tika_config_spec.rb | 59 +++++++++++++++++ 11 files changed, 145 insertions(+), 1 deletion(-) create mode 100644 lib/tika_config.rb create mode 100644 spec/fixtures/heathen/quickfox.ar.jpg create mode 100644 spec/fixtures/heathen/quickfox.ar.pdf create mode 100644 spec/fixtures/heathen/quickfox.ar.txt create mode 100644 spec/lib/tika_config_spec.rb diff --git a/config/app.yml b/config/app.yml index 0471152..8ad03fc 100644 --- a/config/app.yml +++ b/config/app.yml @@ -29,4 +29,7 @@ libreoffice_path: <%= ENV['LIBREOFFICE_PATH'] %> tesseract_path: <%= ENV['TESSERACT_PATH'] %> tika_path: <%= ENV['TIKA_PATH'] %> wkhtmltopdf_path: <%= ENV['WKHTMLTOPDF_PATH'] %> + +# Other settings +tika_config_directory: <%= ENV['TIKA_CONFIG_DIRECTORY'] %> wkhtmltopdf_params: '-d 100 --encoding UTF-8' diff --git a/lib/colore.rb b/lib/colore.rb index 9b50db9..ae1b295 100644 --- a/lib/colore.rb +++ b/lib/colore.rb @@ -9,3 +9,4 @@ require_relative 'document' require_relative 'heathen' require_relative 'sidekiq_workers' +require_relative 'tika_config' diff --git a/lib/config.rb b/lib/config.rb index c034cca..e94d90b 100644 --- a/lib/config.rb +++ b/lib/config.rb @@ -41,6 +41,8 @@ class C_ attr_accessor :tika_path # @return [String] Path to the wkhtmltopdf binary. Defaults to `"wkhtmltopdf"` attr_accessor :wkhtmltopdf_path + # @return [String] Relative path to the writable tika config directory. Defaults to `"../tmp/tika"` + attr_accessor :tika_config_directory # @return [String] Params for wkhtmltopdf attr_accessor :wkhtmltopdf_params @@ -65,6 +67,8 @@ def self.config c.tesseract_path = yaml['tesseract_path'] || 'tesseract' c.tika_path = yaml['tika_path'] || 'tika' c.wkhtmltopdf_path = yaml['wkhtmltopdf_path'] || 'wkhtmltopdf' + + c.tika_config_directory = yaml['tika_config_directory'] || '../tmp/tika' c.wkhtmltopdf_params = yaml['wkhtmltopdf_params'] || '' c diff --git a/lib/heathen/processor_methods/libreoffice.rb b/lib/heathen/processor_methods/libreoffice.rb index f76f997..a983f59 100644 --- a/lib/heathen/processor_methods/libreoffice.rb +++ b/lib/heathen/processor_methods/libreoffice.rb @@ -55,6 +55,7 @@ def libreoffice(format:) if to_suffix == 'txt' executioner.execute( Colore::C_.tika_path, + "--config=#{Colore::TikaConfig.path_for(job.language)}", '--text', job.content_file, binary: true diff --git a/lib/heathen/processor_methods/pdftotext.rb b/lib/heathen/processor_methods/pdftotext.rb index d1dca7b..54a7c8c 100644 --- a/lib/heathen/processor_methods/pdftotext.rb +++ b/lib/heathen/processor_methods/pdftotext.rb @@ -7,6 +7,7 @@ def pdftotext executioner.execute( Colore::C_.tika_path, + "--config=#{Colore::TikaConfig.path_for(job.language)}", '--text', job.content_file, binary: true diff --git a/lib/tika_config.rb b/lib/tika_config.rb new file mode 100644 index 0000000..f6d2156 --- /dev/null +++ b/lib/tika_config.rb @@ -0,0 +1,61 @@ +# frozen_string_literal: true + +require 'fileutils' +require 'pathname' + +module Colore + # The Colore Tika is a module to help with Tika-related configuration files. + module TikaConfig + # The configuration template version + VERSION = 'v1' + + # The default language to use when the language has not been found + DEFAULT_LANGUAGE = 'eng' + + # Config template + TEMPLATE = <<~XML + + + + + + + %s + + + + + XML + + class << self + private + + def tika_config_path + Pathname.new File.expand_path(Colore::C_.tika_config_directory, __dir__) + end + + def path_for!(language_alpha3) + file = tika_config_path.join('ocr', VERSION, "tika.#{language_alpha3}.xml") + return file if file.file? + + FileUtils.mkdir_p(tika_config_path.join('ocr', VERSION)) + File.write(file, format(TEMPLATE, language_alpha3: language_alpha3)) + file + end + end + + # Returns the file path of the Tika configuration for performing OCR + # detection in a specified language. + # + # @param [String] language The language code in either ISO 639-1 (two-letter) or ISO 639-2 (three-letter) format. + # Supported languages are those with corresponding Tika configuration files. + # + # @return [Pathname] The path to the Tika configuration file for the specified language or + # the configuration file for DEFAULT_LANGUAGE if the language is not found. + def self.path_for(language) + language_alpha3 = Colore::Utils.language_alpha3(language) || DEFAULT_LANGUAGE + + path_for!(language_alpha3) + end + end +end diff --git a/spec/fixtures/heathen/quickfox.ar.jpg b/spec/fixtures/heathen/quickfox.ar.jpg new file mode 100644 index 0000000000000000000000000000000000000000..bb69b2408895477326867eb0862c07d547d8d267 GIT binary patch literal 6461 zcmb_gWmFtZm+leV-QC@T1PIPBxDB2F0fGgB#c}t+A-KDSpdq-25S$=Eh7dFXf&?3u zyx+Uu?)iS~?%Ca^>zwYYTUA}%_x98GshjzmRe%_-rlAIaKmY)`J%F14=z2jJTR z0fWI1DC8DUOz0nAVqyLTtiOT%7jXUn@1OWH3FyyA2n6G{;9+B8|I_wAzPRZDNU(u4 z&^ri-6#$cfAS9rhK7jFdkzfb_`Lp1^fsKQKi3Nt>LjTyw|C8Zw`^^G?54jyh3?aUK z$j+{RANRjJgZf|E;dQ54f^_@KzOf#g35W^dZ(8&1)e+e{4JNL3EQwZXZeGj8Ez#Fn zODbU}wPJDdEi6*;)(McTVvlnxN5o-loVGzt-P%7%+QPFXWm$HXnBbI}inZMXgF3z};9QunWI|8Mm_&f_5l zZleYS0b^kNJue0(6bph420+B5%;XeQ!U1FwcO><0J&JwnXAlPXN+QY!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f8H{J@9a4>OI35DPzb3kz!G?n&NQS`#)tXawEW#qI%M0A2#CD}-zXd?m-{zSb#@3p7KaooJwcjpE6Xuj;&fnd qu#gw14xP-8b?pZ*_M%^s1QstXo5G(>^!TxKelgAj&>Q|YU;YhRqOqC) literal 0 HcmV?d00001 diff --git a/spec/fixtures/heathen/quickfox.ar.pdf b/spec/fixtures/heathen/quickfox.ar.pdf new file mode 100644 index 0000000000000000000000000000000000000000..9c613b03cd718abec03e4e91dd00ef3295e62eba GIT binary patch literal 10639 zcmb_?1z23m^5+Z;?(Q(S!@%GHf_rcX8gy`%Km-l$uEE_kXmAe+gy0@DXaa;l@Evk< z@4N55{oa4S-7U_?Ij6e2y1S|$Du(P=x zx~M3aTh7+O1Lg+5JD7REWMCG~mM}243e3sc!v@U52L+3Zqq}>!!OR@dy@5J0b=bWS zT3jUX&TxfO$bTQ9Vn9La|-%o39p;SgIg8m3um$A4;&^J)Old<`bL**@+rcb6ezhn%@_+??x7-`y_pJA__ga_vxB)G+gieH$Ojb!b1T4Xt!+HOJp4THwZM1l=Hh&BZm@@&Ck*}X zYx-j?zxu#6#;p#6E6T;g)64X7f?pV@$f$p{{U1Nv^#brv0ja<@h(J030v-?%4|vxPpoT9J z0TF3;rtAl`I569xgwGvIycNpZHzF}hlr^S;QmBcoC`_!`{2OL+7hu5A z_id*6q>@Ml5gTm&kFkDDZyOkc1ENM|*G!)in>UW~pUvui(jx@`&=ro_tFg7dna7{u zYt@i+;eBuA<#dtF))#lRWWLt_+N*xu()=MUmn+4f_GqA?zaQ zFh^3V(8?h<9OiUcGKib$^>^eMf-s-k76s5Vo9Tm+C8ouoXg@(t{y<|y$d6rJ_b}WdGizcx@ z?$**(EjZhxb1GPwOR)p#+M+9Sz1Nb;RCnqVzgJ2I#gdKB^V`&8&m}81I^6=HjgA9( zg7@d-ViM6$!VP2T1d;(aktJFv0FG*+I2Rn1H#dz6AC8xyfeLTq>!WtcG(HbLZt9iI z>zmsr!Q7k>VvwEGu377(F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pmK=QV@!T9}&)L!eL#A*h*v pxe!Fi3My!2E@UZ={y%s5!ztZ8;Ku!>XdVbW|3YVElvR^M|6iKfq>cap literal 0 HcmV?d00001 diff --git a/spec/fixtures/heathen/quickfox.ar.txt b/spec/fixtures/heathen/quickfox.ar.txt new file mode 100644 index 0000000..36b2c79 --- /dev/null +++ b/spec/fixtures/heathen/quickfox.ar.txt @@ -0,0 +1 @@ +الثعلب البني السريع مفتون بالكلاب الكسولة diff --git a/spec/heathen/processor_methods/pdftotext_spec.rb b/spec/heathen/processor_methods/pdftotext_spec.rb index a48484d..8895141 100644 --- a/spec/heathen/processor_methods/pdftotext_spec.rb +++ b/spec/heathen/processor_methods/pdftotext_spec.rb @@ -4,7 +4,8 @@ RSpec.describe Heathen::Processor do let(:content) { fixture('heathen/quickfox.pdf').read } - let(:job) { Heathen::Job.new 'foo', content, 'en' } + let(:job) { Heathen::Job.new 'foo', content, language } + let(:language) { 'en' } let(:processor) { described_class.new job: job, logger: spec_logger } after do @@ -14,7 +15,19 @@ describe '#pdftotext' do it 'converts PDF to TXT' do processor.pdftotext + expect(job.content).to eq 'The quick brown fox jumps lazily over the dog' expect(job.content.mime_type).to eq 'text/plain; charset=us-ascii' end + + context 'with Arabic files' do + let(:content) { fixture('heathen/quickfox.ar.pdf').read } + let(:language) { 'ar' } + + it 'extracts Arabic text from images' do + processor.pdftotext + expect(job.content).to eq fixture('heathen/quickfox.ar.txt').read.strip.force_encoding(Encoding::ASCII_8BIT) + expect(job.content.mime_type).to eq 'text/plain; charset=utf-8' + end + end end end diff --git a/spec/lib/tika_config_spec.rb b/spec/lib/tika_config_spec.rb new file mode 100644 index 0000000..e2bf234 --- /dev/null +++ b/spec/lib/tika_config_spec.rb @@ -0,0 +1,59 @@ +# frozen_string_literal: true + +require 'spec_helper' +require 'fileutils' +require 'pathname' + +RSpec.describe Colore::TikaConfig do + let(:tika_config_directory) { '../tmp/tika-test' } + let(:tika_test_config_path) { Pathname.new(File.expand_path('../../tmp/tika-test', __dir__)) } + + before do + allow(Colore::C_.config).to receive(:tika_config_directory).and_return tika_config_directory + FileUtils.mkdir_p tika_test_config_path + FileUtils.rm_rf tika_test_config_path + end + + after do + FileUtils.rm_rf tika_test_config_path + end + + describe '.path_for' do + subject(:path_for) { described_class.path_for(language) } + + context 'when the language is found' do + let(:language) { 'fr' } + + before do + allow(Colore::Utils).to receive(:language_alpha3).with('fr').and_return('fra') + end + + it 'returns the correct configuration file path' do + expect(path_for).to eq tika_test_config_path.join('ocr', described_class::VERSION, 'tika.fra.xml') + end + end + + context 'when the language is not found' do + let(:language) { 'unknown' } + + it 'returns the default configuration file path' do + expect(path_for).to eq tika_test_config_path.join('ocr', described_class::VERSION, "tika.#{described_class::DEFAULT_LANGUAGE}.xml") + end + end + + context 'when the configuration file is already present' do + let(:language) { 'en' } + + before do + allow(File).to receive(:write) + .with(tika_test_config_path.join('ocr', described_class::VERSION, 'tika.eng.xml'), an_instance_of(String)) + .and_call_original + end + + it 'does not overwrite it' do + 2.times { described_class.path_for(language) } + expect(File).to have_received(:write).once + end + end + end +end