Henkei is a library for extracting text and metadata from files and documents using the Apache Tika content analysis toolkit.
The library was forked from Yomu as it is no longer maintained.
Here are some of the formats supported:
For the complete list of supported formats, please visit the Apache Tika Supported Document Formats page.
Apache Tika v2.x brings with it some changes. One key change is that the Tika client and server applications have been split up. To keep the gem size down Henkei will only include the client app. That is to say, each time you call to Henkei, a new Java process will be started, run your command, then terminate.
Another change is the metadata keys. A lot of duplicate keys have been removed in favour of a more standards based approach. A list of the old vs new key names can be found here
Text, metadata and MIME type information can be extracted by calling Henkei.read
directly:
require 'henkei'
data = File.read 'sample.pages'
text = Henkei.read :text, data
metadata = Henkei.read :metadata, data
mimetype = Henkei.read :mimetype, data
Henkei is backward compatible with Yomu
text = Yomu.read :text, data
Create a new instance of Henkei and pass a filename.
henkei = Henkei.new 'sample.pages'
text = henkei.text
This is useful for reading remote files, like documents hosted on Amazon S3.
henkei = Henkei.new 'http://svn.apache.org/repos/asf/poi/trunk/test-data/document/sample.docx'
text = henkei.text
Henkei can also read from a stream or any object that responds to read
, including file uploads from Ruby on Rails or Sinatra.
post '/:name/:filename' do
henkei = Henkei.new params[:data][:tempfile]
henkei.text
end
You can enable OCR by specifying the optional include_ocr: true
when calling to the text
or html
instance methods,
as well as the read
class method. Note that Tika does indicate this will greatly increase processing time.
henkei = Henkei.new 'sample.pages'
text_with_ocr = henkei.text(include_ocr: true)
html_with_ocr = henkei.html(include_ocr: true)
data = File.read 'sample.pages'
text_with_ocr = Henkei.read :text, data, include_ocr: true
Metadata is returned as a hash.
henkei = Henkei.new 'sample.pages'
henkei.metadata['Content-Type'] #=> "application/vnd.apple.pages"
MIME type is returned as a MIME::Type object.
henkei = Henkei.new 'sample.docx'
henkei.mimetype.content_type #=> "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
henkei.mimetype.extensions #=> ['docx']
You can specify the output character encoding by passing in the optional encoding
argument when calling to the
text
or html
instance methods, as well as the read
class method.
henkei = Henkei.new 'sample.pages'
utf_8_text = henkei.text(encoding: 'UTF-8')
utf_16_html = henkei.html(encoding: 'UTF-16')
data = File.read 'sample.pages'
utf_32_text = Henkei.read :text, data, encoding: 'UTF-32'
Henkei packages the Apache Tika application jar and requires a working JRE for it to work.
Check that you either have the JAVA_HOME
environment variable set, or that java
is in your path.
Add this line to your application's Gemfile:
gem 'henkei'
And then execute:
$ bundle
Or install it yourself as:
$ gem install henkei
Add the JVM Buildpack to your Heroku project:
$ heroku buildpacks:add heroku/jvm --index 1 -a YOUR_APP_NAME
git checkout -b my-new-feature
)rake test
)git commit -am 'Added some feature'
)git push origin my-new-feature
)