simple_text_extract 2.0.0 → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f3d117bf20380fae2d755e2a258189520479785207c3d27115ab3e9c90e84e51
4
- data.tar.gz: 96b634b6b061520a25be360f62eac98dabde4b1ca3e723703b40bf1d7dbc11d2
3
+ metadata.gz: 411c821b9a0f6ab4f5b95d0104786fbca88c42ab97fa4bd41e8443baae7c80a3
4
+ data.tar.gz: e6034b96a08ed5bea7abc61e0fb0c762e4c16c44a1e4d54adbc7eff2d9a8bcab
5
5
  SHA512:
6
- metadata.gz: 171f01d876c6fc30abf68268c1dd69bd56d135af56cda361027efc3ee12482afcaeafeb376cf6ff3d0ad231c92a694a873e1162525f74ca405a404192858a78e
7
- data.tar.gz: 2fad0ef23c0036fb1b9257221f22bafd9f2b0760eba7407a115931705ebf7b2d42f2f09b0c0fa5ef028500e2bb4ac68e59a5f5873c97037ca2dccbe00a24cc62
6
+ metadata.gz: 87d96e6217062b72e104ac1165475d3af497937532fe7f03364128086f6ab61d490d650d60030813783ba32c418bbab564455dc0659882724e6711d9a612cc4d
7
+ data.tar.gz: 645af90b82616a06805603d2417498890f6fbb53c0d2661934b9ec40d686550c97a42b5bbcc9cf8fe90c049367e45a2e8dd76af8f29d375d8f501beed5be4b16
@@ -0,0 +1,30 @@
1
+ name: build
2
+ on: [push, pull_request]
3
+ jobs:
4
+ build:
5
+ strategy:
6
+ fail-fast: false
7
+ matrix:
8
+ include:
9
+ - ruby: 3.2
10
+ gemfile: Gemfile
11
+ - ruby: 3.1
12
+ gemfile: Gemfile
13
+ runs-on: ubuntu-latest
14
+ env:
15
+ BUNDLE_GEMFILE: ${{ matrix.gemfile }}
16
+ steps:
17
+ - uses: actions/checkout@v3
18
+
19
+ - name: Install poppler-utils
20
+ run: sudo apt-get install -y poppler-utils
21
+
22
+ - name: Install antiword
23
+ run: sudo apt-get install -y antiword
24
+
25
+ - uses: ruby/setup-ruby@v1
26
+ with:
27
+ ruby-version: ${{ matrix.ruby }}
28
+ bundler-cache: true
29
+
30
+ - run: bundle exec rake test
data/.rubocop.yml ADDED
@@ -0,0 +1,115 @@
1
+ AllCops:
2
+ NewCops: enable
3
+ TargetRubyVersion: 3.1
4
+ DisabledByDefault: true
5
+
6
+ Security:
7
+ Enabled: true
8
+
9
+ Bundler:
10
+ Enabled: true
11
+
12
+ Lint:
13
+ Enabled: true
14
+
15
+ Naming:
16
+ Enabled: true
17
+
18
+ Style:
19
+ Enabled: true
20
+
21
+ Layout:
22
+ Enabled: true
23
+
24
+ Metrics:
25
+ Enabled: true
26
+
27
+ Metrics/AbcSize:
28
+ Max: 26
29
+
30
+ Metrics/CyclomaticComplexity:
31
+ Max: 13
32
+
33
+ Metrics/PerceivedComplexity:
34
+ Max: 13
35
+
36
+ Metrics/MethodLength:
37
+ Enabled: false
38
+
39
+ Metrics/ClassLength:
40
+ Max: 125
41
+ Enabled: true
42
+ Exclude:
43
+ - 'test/**/*'
44
+
45
+ Metrics/BlockLength:
46
+ Max: 30
47
+ Exclude:
48
+ - 'test/**/*'
49
+
50
+ Style/ConditionalAssignment:
51
+ Enabled: false
52
+
53
+ Style/NumericLiterals:
54
+ Enabled: false
55
+
56
+ Style/SymbolProc:
57
+ Enabled: false
58
+
59
+ Style/DoubleNegation:
60
+ Enabled: false
61
+
62
+ Style/SymbolArray:
63
+ EnforcedStyle: brackets
64
+
65
+ Style/WordArray:
66
+ EnforcedStyle: brackets
67
+
68
+ Style/EmptyMethod:
69
+ EnforcedStyle: expanded
70
+
71
+ Style/ClassAndModuleChildren:
72
+ Enabled: false
73
+
74
+ Style/Documentation:
75
+ Enabled: false
76
+
77
+ Style/FrozenStringLiteralComment:
78
+ EnforcedStyle: always
79
+
80
+ Style/StringLiterals:
81
+ EnforcedStyle: double_quotes
82
+
83
+ Style/RedundantReturn:
84
+ AllowMultipleReturnValues: true
85
+
86
+ Style/Semicolon:
87
+ AllowAsExpressionSeparator: true
88
+
89
+ Style/ExponentialNotation:
90
+ Enabled: false
91
+
92
+ Style/RegexpLiteral:
93
+ Enabled: false
94
+
95
+ Layout/LineLength:
96
+ Enabled: false
97
+
98
+ Layout/EndAlignment:
99
+ EnforcedStyleAlignWith: variable
100
+ AutoCorrect: true
101
+
102
+ Layout/IndentationConsistency:
103
+ EnforcedStyle: indented_internal_methods
104
+
105
+ Layout/SpaceInsideBlockBraces:
106
+ EnforcedStyleForEmptyBraces: space
107
+
108
+ Lint/MissingSuper:
109
+ Enabled: false
110
+
111
+ Lint/ConstantDefinitionInBlock:
112
+ Enabled: false
113
+
114
+ Style/SingleArgumentDig:
115
+ Enabled: false
data/.ruby-version CHANGED
@@ -1 +1 @@
1
- 3.0.1
1
+ 3.1.2
data/CHANGELOG.md ADDED
@@ -0,0 +1,6 @@
1
+ ## 2.0.0 (2023-04-14)
2
+
3
+ - [BREAKING CHANGE] Improves format of text extracts to better support post processing into sentences/rows. This will only cause issues if you have tests/code that relies on the specific format of whitespace characters in the resulting extracts. For example, the docx extract will now preserve newline characters.
4
+ - Improves memory allocation for xlsx files using `Roo#each_row_streaming`
5
+ - Adds github actions as ci
6
+ - Simplifies private api away from individual "format extractors"
data/Gemfile CHANGED
@@ -5,3 +5,10 @@ source "https://rubygems.org"
5
5
  git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
6
6
 
7
7
  gemspec
8
+
9
+ gem "memory_profiler"
10
+ gem "minitest"
11
+ gem "mocha"
12
+ gem "pry"
13
+ gem "rake"
14
+ gem "rubocop"
data/Gemfile.lock CHANGED
@@ -1,38 +1,72 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- simple_text_extract (1.3.0)
5
- roo (~> 2.9.0)
4
+ simple_text_extract (3.0.0)
5
+ roo (~> 2.10.0)
6
6
  rubyzip (~> 2.3.2)
7
7
  spreadsheet (~> 1.3.0)
8
8
 
9
9
  GEM
10
10
  remote: https://rubygems.org/
11
11
  specs:
12
- mini_portile2 (2.8.0)
13
- minitest (5.15.0)
14
- mocha (1.13.0)
15
- nokogiri (1.13.3)
16
- mini_portile2 (~> 2.8.0)
12
+ ast (2.4.2)
13
+ coderay (1.1.3)
14
+ json (2.6.3)
15
+ memory_profiler (1.0.1)
16
+ method_source (1.0.0)
17
+ minitest (5.18.0)
18
+ mocha (2.0.2)
19
+ ruby2_keywords (>= 0.0.5)
20
+ nokogiri (1.14.3-arm64-darwin)
17
21
  racc (~> 1.4)
18
- racc (1.6.0)
22
+ nokogiri (1.14.3-x86_64-linux)
23
+ racc (~> 1.4)
24
+ parallel (1.22.1)
25
+ parser (3.2.2.0)
26
+ ast (~> 2.4.1)
27
+ pry (0.14.2)
28
+ coderay (~> 1.1)
29
+ method_source (~> 1.0)
30
+ racc (1.6.2)
31
+ rainbow (3.1.1)
19
32
  rake (13.0.6)
20
- roo (2.9.0)
33
+ regexp_parser (2.7.0)
34
+ rexml (3.2.5)
35
+ roo (2.10.0)
21
36
  nokogiri (~> 1)
22
37
  rubyzip (>= 1.3.0, < 3.0.0)
38
+ rubocop (1.50.1)
39
+ json (~> 2.3)
40
+ parallel (~> 1.10)
41
+ parser (>= 3.2.0.0)
42
+ rainbow (>= 2.2.2, < 4.0)
43
+ regexp_parser (>= 1.8, < 3.0)
44
+ rexml (>= 3.2.5, < 4.0)
45
+ rubocop-ast (>= 1.28.0, < 2.0)
46
+ ruby-progressbar (~> 1.7)
47
+ unicode-display_width (>= 2.4.0, < 3.0)
48
+ rubocop-ast (1.28.0)
49
+ parser (>= 3.2.1.0)
23
50
  ruby-ole (1.2.12.2)
51
+ ruby-progressbar (1.13.0)
52
+ ruby2_keywords (0.0.5)
24
53
  rubyzip (2.3.2)
25
54
  spreadsheet (1.3.0)
26
55
  ruby-ole
56
+ unicode-display_width (2.4.2)
27
57
 
28
58
  PLATFORMS
29
- ruby
59
+ arm64-darwin-21
60
+ x86_64-linux
30
61
 
31
62
  DEPENDENCIES
32
- minitest (~> 5.0)
63
+ memory_profiler
64
+ minitest
33
65
  mocha
34
- rake (~> 13.0)
66
+ pry
67
+ rake
68
+ rubocop
35
69
  simple_text_extract!
36
70
 
37
71
  BUNDLED WITH
38
- 2.2.15
72
+ 2.4.12
data/LICENSE.txt CHANGED
@@ -1,7 +1,5 @@
1
1
  The MIT License (MIT)
2
2
 
3
- Copyright (c) 2019 Nick Weiland
4
-
5
3
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
4
  of this software and associated documentation files (the "Software"), to deal
7
5
  in the Software without restriction, including without limitation the rights
data/README.md CHANGED
@@ -1,8 +1,8 @@
1
1
  # 📄 SimpleTextExtract
2
2
 
3
- SimpleTextExtract attempts extract text from various file types and is recommended as a guard before resorting to something more extreme like Apache Tika. It is built specifically with ActiveStorage in mind and originally built for the purpose of extracting text from attachments in order to index the text in ElasticSearch using [SearchKick](https://github.com/ankane/searchkick).
3
+ SimpleTextExtract attempts extract text from various file types and is recommended as a guard before resorting to something more extreme like Apache Tika. It is built specifically with ActiveStorage in mind and originally built for the purpose of extracting text from attachments in order to index the text in ElasticSearch.
4
4
 
5
- SimpleTextExtract handles parsing text from:
5
+ SimpleTextExtract andlhes parsing text from:
6
6
 
7
7
  - `.pdf`
8
8
  - `.docx`
@@ -12,7 +12,7 @@ SimpleTextExtract handles parsing text from:
12
12
  - `.csv`
13
13
  - `.txt` 😜
14
14
 
15
- If no text is parsed (for `pdf`), or a file format is not supported (like images), then `nil` is returned and you can move on to the heavy-duty tools like [Henkei](https://github.com/abrom/henkei) 💪.
15
+ If no text is parsed (for `pdf`), or a file format is not supported (like images), then `""` is returned and you can move on to try an OCR solution. For example, at [Govly](www.govly.com) use SimpleTextExtract before sending files to [AWS Textract](https://aws.amazon.com/textract/).
16
16
 
17
17
  ## Installation
18
18
 
@@ -35,66 +35,34 @@ Or install it yourself as:
35
35
  Text can be parsed from raw file content or files in the filesystem t by calling `SimpleTextExtract.extract`:
36
36
 
37
37
  ```ruby
38
- # using ActiveStorage >= 6
39
- extract = attachment.open { |tmp| SimpleTextExtract.extract(tempfile: tmp) }
40
- # raw file content or when ActiveStorage < 6
41
- extract = SimpleTextExtract.extract(filename: attachment.blob.filename, raw: attachment.download)
42
-
43
38
  # filesystem
44
39
  extract = SimpleTextExtract.extract(filepath: "path_to_file.pdf")
45
- ```
46
-
47
- ### Usage Dependencies
48
-
49
- You can choose to use SimpleTextExtract without the following dependencies, but it won't work for specific file types:
50
-
51
- `pdf` parsing requires `poppler-utils`
52
- - `brew install poppler`
53
-
54
- `doc` parsing requires `antiword` and `unzip`
55
- - `brew install antiword`
56
40
 
57
- `xlsx` and `xls` parsing requires `ssconvert` which is part of `gnumeric`
58
- - `brew install gnumeric`
41
+ # raw
42
+ extract = SimpleTextExtract.extract(filename: "some_file.raw", raw: "raw contents")
59
43
 
60
- ### Usage on Heroku
44
+ # tempfile
45
+ extract = SimpleTextExtract.extract(tempfile: temp_file)
61
46
 
62
- To use on Heroku you'll have to add some custom buildpacks.
63
47
 
64
48
 
65
- ##### heroku-buildpack-activestorage-preview
66
-
67
- If you're using ActiveStorage, you might already have the [heroku-buildpack-activestorage-preview](https://github.com/heroku/heroku-buildpack-activestorage-preview) added, which means you already have `poppler-utils` installed 🎉
68
-
69
- If not, you can either add that buildpack, or add `poppler-utils` to your `Aptfile` (see below).
70
-
71
- ##### heroku-buildpack-apt
72
-
73
- To add `antiword` and/or `gnumeric`* as a dependency on Heroku, install the [heroku-buildpack-apt](https://elements.heroku.com/buildpacks/heroku/heroku-buildpack-apt) buildpack and follow the install instructions.
49
+ # using ActiveStorage >= 6
50
+ extract = attachment.open { |tmp| SimpleTextExtract.extract(tempfile: tmp) }
74
51
 
75
- In your `Aptfile`, add:
76
- ```
77
- antiword
78
- gnumeric
79
- unzip
52
+ # raw file content or when ActiveStorage < 6
53
+ extract = SimpleTextExtract.extract(filename: attachment.blob.filename, raw: attachment.download)
80
54
  ```
81
55
 
82
- * There is currently an [issue](https://github.com/heroku/heroku-buildpack-google-chrome/issues/59) with the heroku-18 stack that requires additional dependencies added to the Aptfile to get `gnumeric` to work properly. You can reference the linked issue above to figure out those dependencies, or downgrade to heroku-16 until it is fixed.
83
-
84
- ## Benchmarks
56
+ ### Usage Dependencies
85
57
 
86
- *Benchmarks test extracting text from the same file 50 times (Macbook pro)*
58
+ You can choose to use SimpleTextExtract without the following dependencies, but it won't work for specific file types:
87
59
 
88
- | File format | SimpleTextExtract | Henkei (i.e. Yomu/Apache Tika) |
89
- |-------------|-------------------|--------------------------------|
90
- | .doc | 1.40s | 74.27s |
91
- | .docx | 0.78s | 71.44s |
92
- | .pdf* | 1.73s | 82.86s |
93
- | .xlsx | 1.16s | 51.89s |
94
- | .xls | 0.80s | 67.88s |
95
- | .txt | 0.04s | 39.25s |
60
+ `pdf` parsing requires `poppler-utils`
61
+ `doc` parsing requires `antiword`
96
62
 
97
- * SimpleTextExtract is limited in its text extraction from pdfs, as Tika can also perform OCR on pdfs with Tesseract
63
+ #### Install deps on MacOS
64
+ - `brew install poppler`
65
+ - `brew install antiword`
98
66
 
99
67
  ## Development
100
68
 
data/bin/console CHANGED
@@ -3,8 +3,5 @@
3
3
  require "bundler/setup"
4
4
  require "simple_text_extract"
5
5
 
6
- # You can add fixtures and/or initialization code here to make experimenting
7
- # with your gem easier. You can also use a different console, if you like.
8
-
9
- require "irb"
10
- IRB.start(__FILE__)
6
+ require "pry"
7
+ Pry.start
@@ -0,0 +1,154 @@
1
+ # frozen_string_literal: true
2
+
3
+ class SimpleTextExtract::Extract
4
+ def self.formatter(path)
5
+ case path
6
+ when /.zip$/i
7
+ :zip
8
+ when /(.txt$|.csv$)/i
9
+ :plain
10
+ when /.pdf$/i
11
+ :pdf
12
+ when /.docx$/i
13
+ :docx
14
+ when /.doc$/i
15
+ :doc
16
+ when /.xlsx$/i
17
+ :xlsx
18
+ when /.xls$/i
19
+ :xls
20
+ end
21
+ end
22
+
23
+ attr_reader :file, :formatter
24
+
25
+ def initialize(filename: nil, raw: nil, filepath: nil, tempfile: nil)
26
+ @file = get_file(filename:, raw:, filepath:, tempfile:)
27
+ @formatter = self.class.formatter(File.extname(file)) if file
28
+ end
29
+
30
+ def to_s
31
+ @to_s ||= extract.to_s.gsub(/[^\S\n]+/, " ").gsub(/\s?\n\s+/, "\n").strip
32
+ end
33
+
34
+ private
35
+
36
+ def get_file(filename:, raw:, filepath:, tempfile:)
37
+ if tempfile&.class == Tempfile
38
+ tempfile
39
+ elsif !filename.nil? && !raw.nil?
40
+ write_tempfile(filename: filename.to_s, raw:)
41
+ elsif !filepath.nil? && File.exist?(filepath)
42
+ File.new(filepath)
43
+ end
44
+ end
45
+
46
+ def extract
47
+ return unless file
48
+
49
+ begin
50
+ send("#{formatter}_extract") if formatter
51
+ rescue StandardError
52
+ nil
53
+ ensure
54
+ cleanup
55
+ end
56
+ end
57
+
58
+ def cleanup
59
+ return unless file.instance_of?(Tempfile)
60
+
61
+ file.close
62
+ file.unlink
63
+ end
64
+
65
+ def write_tempfile(filename:, raw:)
66
+ filename = filename.split(".").then { |parts| [parts[0], ".#{parts[1]}"] }
67
+ file = Tempfile.new(filename)
68
+ raw = String.new(raw, encoding: Encoding::UTF_8)
69
+
70
+ file.write(raw)
71
+ file.tap(&:rewind)
72
+ end
73
+
74
+ def plain_extract
75
+ file.read
76
+ end
77
+
78
+ def pdf_extract
79
+ return nil if SimpleTextExtract.missing_dependency?("pdftotext")
80
+
81
+ `pdftotext #{Shellwords.escape(file.path)} -`
82
+ end
83
+
84
+ def xlsx_extract
85
+ require "roo"
86
+
87
+ spreadsheet = Roo::Spreadsheet.open(file, only_visible_sheets: true)
88
+
89
+ text = []
90
+
91
+ spreadsheet.sheets.each do |name|
92
+ text << name
93
+
94
+ spreadsheet.sheet(name)&.each_row_streaming { |row| text << row.join(" ") }
95
+ end
96
+
97
+ text.join("\n")
98
+ end
99
+
100
+ def xls_extract
101
+ require "spreadsheet"
102
+
103
+ spreadsheet = Spreadsheet.open(file)
104
+ text = []
105
+ spreadsheet.worksheets.each do |sheet|
106
+ text << sheet.name
107
+ sheet.rows.each { |row| text << row.join(" ") }
108
+ end
109
+
110
+ text.join("\n")
111
+ end
112
+
113
+ def doc_extract
114
+ return nil if SimpleTextExtract.missing_dependency?("antiword")
115
+
116
+ `antiword #{Shellwords.escape(file.path)}`
117
+ end
118
+
119
+ def docx_extract
120
+ require "zip"
121
+ require "nokogiri"
122
+
123
+ result = []
124
+ Zip::File.open(file) do |zip_file|
125
+ document = zip_file.glob("word/document*.xml").first
126
+ return "" if document.nil?
127
+
128
+ document_xml = document.get_input_stream.read
129
+ doc = Nokogiri::XML(document_xml)
130
+ doc.xpath("//w:document//w:body/w:p").each do |node|
131
+ result << node.text
132
+ end
133
+ end
134
+
135
+ result.join("\n")
136
+ end
137
+
138
+ def zip_extract
139
+ require "zip"
140
+
141
+ result = []
142
+ Zip::File.open(file) do |zip_file|
143
+ zip_file.each do |entry|
144
+ result << entry.name
145
+ result << SimpleTextExtract.extract(
146
+ raw: entry.get_input_stream.read,
147
+ filename: entry.name
148
+ )
149
+ end
150
+ end
151
+
152
+ result.join("\n")
153
+ end
154
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SimpleTextExtract
4
- VERSION = "2.0.0"
4
+ VERSION = "3.0.0"
5
5
  end
@@ -1,8 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "simple_text_extract/version"
4
- require "simple_text_extract/text_extractor"
5
- require "simple_text_extract/format_extractor_factory"
4
+ require "simple_text_extract/extract"
6
5
 
7
6
  module SimpleTextExtract
8
7
  SUPPORTED_FILETYPES = ["xls", "xlsx", "doc", "docx", "txt", "pdf", "csv", "zip"].freeze
@@ -10,10 +9,15 @@ module SimpleTextExtract
10
9
  class Error < StandardError; end
11
10
 
12
11
  def self.extract(filename: nil, raw: nil, filepath: nil, tempfile: nil)
13
- TextExtractor.new(filename: filename, raw: raw, filepath: filepath, tempfile: tempfile).to_s
12
+ Extract.new(filename:, raw:, filepath:, tempfile:).to_s
14
13
  end
15
14
 
16
15
  def self.supports?(filename: nil)
17
16
  SUPPORTED_FILETYPES.include?(filename.split(".").last)
18
17
  end
18
+
19
+ def self.missing_dependency?(command)
20
+ dependency = `bash -c 'command -v #{command}'`
21
+ dependency.nil? || dependency.empty?
22
+ end
19
23
  end
@@ -9,30 +9,24 @@ Gem::Specification.new do |spec|
9
9
  spec.version = SimpleTextExtract::VERSION
10
10
  spec.authors = ["Nick Weiland"]
11
11
  spec.email = ["nickweiland@gmail.com"]
12
-
13
- spec.summary = "Attempts to quickly extract text from various file types before resorting to something more extreme like Apache Tika."
14
- spec.description = "Attempts to quickly extract text from various file types before resorting to something more extreme like Apache Tika. Built with ActiveStorage in mind."
12
+ spec.summary = "Extract text from various file types before resorting to an OCR solution."
13
+ spec.description = "Extract text from various file types before resorting to an OCR solution."
15
14
  spec.homepage = "https://github.com/weilandia/simple_text_extract"
16
15
  spec.license = "MIT"
16
+ spec.required_ruby_version = Gem::Requirement.new(">= 3.1")
17
17
 
18
- # Specify which files should be added to the gem when it is released.
19
- # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
20
18
  spec.files = Dir.chdir(File.expand_path(__dir__)) do
21
19
  `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
22
20
  end
21
+
23
22
  spec.bindir = "exe"
24
23
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
25
24
  spec.require_paths = ["lib"]
26
25
 
27
26
  spec.requirements << "antiword"
28
27
  spec.requirements << "pdftotext/poppler"
29
- spec.required_ruby_version = ">= 2.5"
30
-
31
- spec.add_runtime_dependency "roo", "~> 2.9.0"
32
- spec.add_runtime_dependency "spreadsheet", "~> 1.3.0"
33
- spec.add_runtime_dependency "rubyzip", "~> 2.3.2"
34
28
 
35
- spec.add_development_dependency "rake", "~> 13.0"
36
- spec.add_development_dependency "minitest", "~> 5.0"
37
- spec.add_development_dependency "mocha"
29
+ spec.add_dependency "roo", "~> 2.10.0"
30
+ spec.add_dependency "spreadsheet", "~> 1.3.0"
31
+ spec.add_dependency "rubyzip", "~> 2.3.2"
38
32
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple_text_extract
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0
4
+ version: 3.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nick Weiland
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-03-20 00:00:00.000000000 Z
11
+ date: 2023-04-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: roo
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: 2.9.0
19
+ version: 2.10.0
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: 2.9.0
26
+ version: 2.10.0
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: spreadsheet
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -52,60 +52,18 @@ dependencies:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
54
  version: 2.3.2
55
- - !ruby/object:Gem::Dependency
56
- name: rake
57
- requirement: !ruby/object:Gem::Requirement
58
- requirements:
59
- - - "~>"
60
- - !ruby/object:Gem::Version
61
- version: '13.0'
62
- type: :development
63
- prerelease: false
64
- version_requirements: !ruby/object:Gem::Requirement
65
- requirements:
66
- - - "~>"
67
- - !ruby/object:Gem::Version
68
- version: '13.0'
69
- - !ruby/object:Gem::Dependency
70
- name: minitest
71
- requirement: !ruby/object:Gem::Requirement
72
- requirements:
73
- - - "~>"
74
- - !ruby/object:Gem::Version
75
- version: '5.0'
76
- type: :development
77
- prerelease: false
78
- version_requirements: !ruby/object:Gem::Requirement
79
- requirements:
80
- - - "~>"
81
- - !ruby/object:Gem::Version
82
- version: '5.0'
83
- - !ruby/object:Gem::Dependency
84
- name: mocha
85
- requirement: !ruby/object:Gem::Requirement
86
- requirements:
87
- - - ">="
88
- - !ruby/object:Gem::Version
89
- version: '0'
90
- type: :development
91
- prerelease: false
92
- version_requirements: !ruby/object:Gem::Requirement
93
- requirements:
94
- - - ">="
95
- - !ruby/object:Gem::Version
96
- version: '0'
97
- description: Attempts to quickly extract text from various file types before resorting
98
- to something more extreme like Apache Tika. Built with ActiveStorage in mind.
55
+ description: Extract text from various file types before resorting to an OCR solution.
99
56
  email:
100
57
  - nickweiland@gmail.com
101
58
  executables: []
102
59
  extensions: []
103
60
  extra_rdoc_files: []
104
61
  files:
62
+ - ".github/workflows/build.yml"
105
63
  - ".gitignore"
64
+ - ".rubocop.yml"
106
65
  - ".ruby-version"
107
- - ".travis.yml"
108
- - CODE_OF_CONDUCT.md
66
+ - CHANGELOG.md
109
67
  - Gemfile
110
68
  - Gemfile.lock
111
69
  - LICENSE.txt
@@ -114,19 +72,9 @@ files:
114
72
  - bin/console
115
73
  - bin/setup
116
74
  - lib/simple_text_extract.rb
117
- - lib/simple_text_extract/format_extractor/base.rb
118
- - lib/simple_text_extract/format_extractor/doc.rb
119
- - lib/simple_text_extract/format_extractor/doc_x.rb
120
- - lib/simple_text_extract/format_extractor/pdf.rb
121
- - lib/simple_text_extract/format_extractor/plain_text.rb
122
- - lib/simple_text_extract/format_extractor/xls.rb
123
- - lib/simple_text_extract/format_extractor/xls_x.rb
124
- - lib/simple_text_extract/format_extractor/zip_extract.rb
125
- - lib/simple_text_extract/format_extractor_factory.rb
126
- - lib/simple_text_extract/text_extractor.rb
75
+ - lib/simple_text_extract/extract.rb
127
76
  - lib/simple_text_extract/version.rb
128
77
  - simple_text_extract.gemspec
129
- - tags
130
78
  homepage: https://github.com/weilandia/simple_text_extract
131
79
  licenses:
132
80
  - MIT
@@ -139,7 +87,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
139
87
  requirements:
140
88
  - - ">="
141
89
  - !ruby/object:Gem::Version
142
- version: '2.5'
90
+ version: '3.1'
143
91
  required_rubygems_version: !ruby/object:Gem::Requirement
144
92
  requirements:
145
93
  - - ">="
@@ -148,9 +96,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
148
96
  requirements:
149
97
  - antiword
150
98
  - pdftotext/poppler
151
- rubygems_version: 3.2.15
99
+ rubygems_version: 3.3.7
152
100
  signing_key:
153
101
  specification_version: 4
154
- summary: Attempts to quickly extract text from various file types before resorting
155
- to something more extreme like Apache Tika.
102
+ summary: Extract text from various file types before resorting to an OCR solution.
156
103
  test_files: []
data/.travis.yml DELETED
@@ -1,7 +0,0 @@
1
- ---
2
- sudo: false
3
- language: ruby
4
- cache: bundler
5
- rvm:
6
- - 2.6.0
7
- before_install: gem install bundler -v 1.17.2
data/CODE_OF_CONDUCT.md DELETED
@@ -1,74 +0,0 @@
1
- # Contributor Covenant Code of Conduct
2
-
3
- ## Our Pledge
4
-
5
- In the interest of fostering an open and welcoming environment, we as
6
- contributors and maintainers pledge to making participation in our project and
7
- our community a harassment-free experience for everyone, regardless of age, body
8
- size, disability, ethnicity, gender identity and expression, level of experience,
9
- nationality, personal appearance, race, religion, or sexual identity and
10
- orientation.
11
-
12
- ## Our Standards
13
-
14
- Examples of behavior that contributes to creating a positive environment
15
- include:
16
-
17
- * Using welcoming and inclusive language
18
- * Being respectful of differing viewpoints and experiences
19
- * Gracefully accepting constructive criticism
20
- * Focusing on what is best for the community
21
- * Showing empathy towards other community members
22
-
23
- Examples of unacceptable behavior by participants include:
24
-
25
- * The use of sexualized language or imagery and unwelcome sexual attention or
26
- advances
27
- * Trolling, insulting/derogatory comments, and personal or political attacks
28
- * Public or private harassment
29
- * Publishing others' private information, such as a physical or electronic
30
- address, without explicit permission
31
- * Other conduct which could reasonably be considered inappropriate in a
32
- professional setting
33
-
34
- ## Our Responsibilities
35
-
36
- Project maintainers are responsible for clarifying the standards of acceptable
37
- behavior and are expected to take appropriate and fair corrective action in
38
- response to any instances of unacceptable behavior.
39
-
40
- Project maintainers have the right and responsibility to remove, edit, or
41
- reject comments, commits, code, wiki edits, issues, and other contributions
42
- that are not aligned to this Code of Conduct, or to ban temporarily or
43
- permanently any contributor for other behaviors that they deem inappropriate,
44
- threatening, offensive, or harmful.
45
-
46
- ## Scope
47
-
48
- This Code of Conduct applies both within project spaces and in public spaces
49
- when an individual is representing the project or its community. Examples of
50
- representing a project or community include using an official project e-mail
51
- address, posting via an official social media account, or acting as an appointed
52
- representative at an online or offline event. Representation of a project may be
53
- further defined and clarified by project maintainers.
54
-
55
- ## Enforcement
56
-
57
- Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
- reported by contacting the project team at nickweiland@gmail.com. All
59
- complaints will be reviewed and investigated and will result in a response that
60
- is deemed necessary and appropriate to the circumstances. The project team is
61
- obligated to maintain confidentiality with regard to the reporter of an incident.
62
- Further details of specific enforcement policies may be posted separately.
63
-
64
- Project maintainers who do not follow or enforce the Code of Conduct in good
65
- faith may face temporary or permanent repercussions as determined by other
66
- members of the project's leadership.
67
-
68
- ## Attribution
69
-
70
- This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
- available at [http://contributor-covenant.org/version/1/4][version]
72
-
73
- [homepage]: http://contributor-covenant.org
74
- [version]: http://contributor-covenant.org/version/1/4/
@@ -1,23 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "shellwords"
4
-
5
- module SimpleTextExtract
6
- module FormatExtractor
7
- class Base
8
- attr_reader :file
9
-
10
- def initialize(file)
11
- @file = file
12
- end
13
-
14
- def extract
15
- end
16
-
17
- def missing_dependency?(command)
18
- dependency = `bash -c 'command -v #{command}'`
19
- dependency.nil? || dependency.empty?
20
- end
21
- end
22
- end
23
- end
@@ -1,13 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module SimpleTextExtract
4
- module FormatExtractor
5
- class Doc < Base
6
- def extract
7
- return nil if missing_dependency?("antiword")
8
-
9
- `antiword #{Shellwords.escape(file.path)}`
10
- end
11
- end
12
- end
13
- end
@@ -1,13 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module SimpleTextExtract
4
- module FormatExtractor
5
- class DocX < Base
6
- def extract
7
- return nil if missing_dependency?("unzip")
8
-
9
- `unzip -p #{Shellwords.escape(file.path)} | grep '<w:t' | sed 's/<[^<]*>//g' | grep -v '^[[:space:]]*$'`
10
- end
11
- end
12
- end
13
- end
@@ -1,13 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module SimpleTextExtract
4
- module FormatExtractor
5
- class PDF < Base
6
- def extract
7
- return nil if missing_dependency?("pdftotext")
8
-
9
- `pdftotext #{Shellwords.escape(file.path)} -`
10
- end
11
- end
12
- end
13
- end
@@ -1,11 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module SimpleTextExtract
4
- module FormatExtractor
5
- class PlainText < Base
6
- def extract
7
- file.read
8
- end
9
- end
10
- end
11
- end
@@ -1,20 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module SimpleTextExtract
4
- module FormatExtractor
5
- class Xls < Base
6
- def extract
7
- require "spreadsheet"
8
-
9
- spreadsheet = Spreadsheet.open(file)
10
- text = []
11
- spreadsheet.worksheets.each do |sheet|
12
- text << sheet.name
13
- text << sheet.rows
14
- end
15
-
16
- text.flatten.join(" ")
17
- end
18
- end
19
- end
20
- end
@@ -1,22 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module SimpleTextExtract
4
- module FormatExtractor
5
- class XlsX < Base
6
- def extract
7
- require "roo"
8
-
9
- spreadsheet = Roo::Spreadsheet.open(file, only_visible_sheets: true)
10
-
11
- text = []
12
-
13
- spreadsheet.each_with_pagename do |name, sheet|
14
- text << name
15
- 1.upto(sheet.last_row.to_i) { |row| text << sheet.row(row) }
16
- end
17
-
18
- text.flatten.join(" ")
19
- end
20
- end
21
- end
22
- end
@@ -1,24 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module SimpleTextExtract
4
- module FormatExtractor
5
- class ZipExtract < Base
6
- def extract
7
- require "zip"
8
-
9
- result = []
10
- Zip::File.open(file) do |zip_file|
11
- zip_file.each do |entry|
12
- result << entry.name
13
- result << SimpleTextExtract.extract(
14
- raw: entry.get_input_stream.read,
15
- filename: entry.name
16
- )
17
- end
18
- end
19
-
20
- result.join(" ")
21
- end
22
- end
23
- end
24
- end
@@ -1,35 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "simple_text_extract/format_extractor/base"
4
- require "simple_text_extract/format_extractor/plain_text"
5
- require "simple_text_extract/format_extractor/pdf"
6
- require "simple_text_extract/format_extractor/xls_x"
7
- require "simple_text_extract/format_extractor/xls"
8
- require "simple_text_extract/format_extractor/doc_x"
9
- require "simple_text_extract/format_extractor/doc"
10
- require "simple_text_extract/format_extractor/zip_extract"
11
-
12
- module SimpleTextExtract
13
- class FormatExtractorFactory
14
- def self.call(file)
15
- case file.path
16
- when /.zip$/i
17
- FormatExtractor::ZipExtract.new(file)
18
- when /(.txt$|.csv$)/i
19
- FormatExtractor::PlainText.new(file)
20
- when /.pdf$/i
21
- FormatExtractor::PDF.new(file)
22
- when /.docx$/i
23
- FormatExtractor::DocX.new(file)
24
- when /.doc$/i
25
- FormatExtractor::Doc.new(file)
26
- when /.xlsx$/i
27
- FormatExtractor::XlsX.new(file)
28
- when /.xls$/i
29
- FormatExtractor::Xls.new(file)
30
- else
31
- FormatExtractor::Base.new(file)
32
- end
33
- end
34
- end
35
- end
@@ -1,55 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module SimpleTextExtract
4
- class TextExtractor
5
- attr_reader :file
6
-
7
- def initialize(filename: nil, raw: nil, filepath: nil, tempfile: nil)
8
- @file = get_file(filename: filename, raw: raw, filepath: filepath, tempfile: tempfile)
9
- end
10
-
11
- def to_s
12
- @to_s ||= extract.to_s
13
- end
14
-
15
- private
16
-
17
- def get_file(filename:, raw:, filepath:, tempfile:)
18
- if tempfile&.class == Tempfile
19
- tempfile
20
- elsif !filename.nil? && !raw.nil?
21
- write_tempfile(filename: filename.to_s, raw: raw)
22
- elsif !filepath.nil? && File.exist?(filepath)
23
- File.new(filepath)
24
- end
25
- end
26
-
27
- def extract
28
- return unless file
29
-
30
- begin
31
- FormatExtractorFactory.call(file).extract
32
- rescue StandardError
33
- nil
34
- ensure
35
- cleanup
36
- end
37
- end
38
-
39
- def cleanup
40
- return unless file.instance_of?(Tempfile)
41
-
42
- file.close
43
- file.unlink
44
- end
45
-
46
- def write_tempfile(filename:, raw:)
47
- filename = filename.split(".").yield_self { |parts| [parts[0], ".#{parts[1]}"] }
48
- file = Tempfile.new(filename)
49
- raw = String.new(raw, encoding: Encoding::UTF_8)
50
-
51
- file.write(raw)
52
- file.tap(&:rewind)
53
- end
54
- end
55
- end
data/tags DELETED
@@ -1,17 +0,0 @@
1
- !_TAG_FILE_FORMAT 2 /extended format; --format=1 will not append ;" to lines/
2
- !_TAG_FILE_SORTED 1 /0=unsorted, 1=sorted, 2=foldcase/
3
- !_TAG_OUTPUT_MODE u-ctags /u-ctags or e-ctags/
4
- !_TAG_PROGRAM_AUTHOR Universal Ctags Team //
5
- !_TAG_PROGRAM_NAME Universal Ctags /Derived from Exuberant Ctags/
6
- !_TAG_PROGRAM_URL https://ctags.io/ /official site/
7
- !_TAG_PROGRAM_VERSION 0.0.0 /54afb0f/
8
- Error lib/simple_text_extract.rb /^ class Error < StandardError; end$/;" c module:SimpleTextExtract
9
- SimpleTextExtract lib/simple_text_extract.rb /^module SimpleTextExtract$/;" m
10
- SimpleTextExtract lib/simple_text_extract/version.rb /^module SimpleTextExtract$/;" m
11
- TextExtractor lib/simple_text_extract/text_extractor.rb /^class SimpleTextExtract::TextExtractor$/;" c module:SimpleTextExtract
12
- VERSION lib/simple_text_extract/version.rb /^ VERSION = "0.1.0"$/;" c
13
- call lib/simple_text_extract/text_extractor.rb /^ def self.call(filename:, raw_content:, filepath:)$/;" S class:TextExtractor
14
- extract lib/simple_text_extract.rb /^ def self.extract(filename: nil, raw_content: nil, filepath: nil)$/;" S module:SimpleTextExtract
15
- extract lib/simple_text_extract/text_extractor.rb /^ def extract$/;" f class:TextExtractor
16
- filepath lib/simple_text_extract/text_extractor.rb /^ attr_reader :filename, :raw_content, :filepath$/;" A
17
- initialize lib/simple_text_extract/text_extractor.rb /^ def initialize(filename:, raw_content:, filepath:)$/;" f class:TextExtractor