token_estimator 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: c4f8fd7ec54ffb7145d793b94eada2a34aa331a562cbb7b52eee0f74fcd6c181
4
+ data.tar.gz: b61a2e7c7ce96b60267bcde6d67fedfe5330ad9f4d021b2d9a4b30fbccbaf6e9
5
+ SHA512:
6
+ metadata.gz: 19333d9dce63923d2490c525b723a1585de5107eb16d5021c05ea1f461d0ad7659435b0d07df92d8e4b209f79501b41386132b604158dc20c60b2db2f571680d
7
+ data.tar.gz: 2790194ec3c05d191dbbdc0037e01567a9e80236dc872eaf335c93738d010b5ec414bb04872d0fa22cc2f1df11cfa2c49888d002d0692460c766e8d34c48abaa
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.standard.yml ADDED
@@ -0,0 +1,3 @@
1
+ # For available configuration options, see:
2
+ # https://github.com/standardrb/standard
3
+ ruby_version: 3.0
data/CHANGELOG.md ADDED
@@ -0,0 +1,10 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2024-07-11
4
+
5
+ - Initial release
6
+
7
+ ### Added
8
+ - Functionality to count tokens in PDF, Markdown (.md), CSV, Excel (XLSX), JSON, Plain Text, and HTML files.
9
+ - Initial documentation in the README.md.
10
+ - Detailed roadmap in the README.md.
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at aemabit@gmail.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024 TODO: Write your name
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,36 @@
1
+ # TokenEstimator
2
+ TokenEstimator is a Rails gem that provides functionality to count tokens in various file formats and text inputs using different tokenizers.
3
+
4
+ ## Installation
5
+ Add this line to your application's Gemfile:
6
+
7
+ ```ruby
8
+ gem "token_counter"
9
+ ```
10
+
11
+ And then execute:
12
+ ```bash
13
+ bundle install
14
+ ```
15
+
16
+ ## Roadmap
17
+ Here is a checklist of the formats we currently support for token counting and those we plan to support in the future:
18
+
19
+ - [x] PDF
20
+ - [x] Markdown (.md)
21
+ - [x] CSV
22
+ - [x] Excel (XLSX)
23
+ - [x] JSON
24
+ - [x] Plain Text
25
+ - [x] HTML
26
+ - [ ] DOCX (Word Documents)
27
+ - [ ] XML
28
+ - [ ] RTF (Rich Text Format)
29
+ - [ ] PNG
30
+ - [ ] JPG
31
+
32
+ ## Contributing
33
+ Contribution directions go here. You can fork the repository, create a new branch, and submit a pull request for review. Please make sure to write tests for your contributions and follow the coding standards set in the project.
34
+
35
+ ## License
36
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "standard/rake"
9
+
10
+ task default: %i[spec standard]
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module TokenEstimator
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,144 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "token_estimator/version"
4
+ require "roo"
5
+ require "pdf-reader"
6
+ require "tokenizers"
7
+ require "csv"
8
+ require "kramdown"
9
+ require "nokogiri"
10
+ require "json"
11
+
12
+ module TokenEstimator
13
+ class UnsupportedFileTypeError < StandardError; end
14
+
15
+ class Estimator
16
+ def initialize(tokenizer_name)
17
+ @tokenizer = Tokenizers.from_pretrained(tokenizer_name)
18
+ end
19
+
20
+ def count_tokens_from_file(file_path)
21
+ extension = File.extname(file_path)
22
+ case extension
23
+ when ".txt"
24
+ count_tokens_from_txt_file(file_path)
25
+ when ".csv"
26
+ count_tokens_from_csv_file(file_path)
27
+ when ".pdf"
28
+ count_tokens_from_pdf_file(file_path)
29
+ when ".json"
30
+ count_tokens_from_json_file(file_path)
31
+ when ".md"
32
+ count_tokens_from_markdown_file(file_path)
33
+ when ".html"
34
+ count_tokens_from_html_file(file_path)
35
+ when ".xlsx"
36
+ count_tokens_from_excel_file(file_path)
37
+ else
38
+ raise UnsupportedFileTypeError, "File type \"#{extension}\" is not supported"
39
+ end
40
+ end
41
+
42
+ def count_tokens_from_excel_file(file_path)
43
+ xlsx = Roo::Excelx.new(file_path)
44
+ text = extract_text_from_excel(xlsx)
45
+ count_tokens_from_text(text)
46
+ end
47
+
48
+ def count_tokens_from_csv_file(file_path)
49
+ text = extract_text_from_csv(file_path)
50
+ count_tokens_from_text(text)
51
+ end
52
+
53
+ def count_tokens_from_pdf_file(file_path)
54
+ reader = PDF::Reader.new(file_path)
55
+ text = extract_text_from_pdf(reader)
56
+ count_tokens_from_text(text)
57
+ end
58
+
59
+ def count_tokens_from_txt_file(file_path)
60
+ text = File.read(file_path)
61
+ count_tokens_from_text(text)
62
+ end
63
+
64
+ def count_tokens_from_markdown_file(file_path)
65
+ markdown = File.read(file_path)
66
+ html_content = Kramdown::Document.new(markdown).to_html
67
+ count_tokens_from_html(html_content)
68
+ end
69
+
70
+ def count_tokens_from_json_file(file_path)
71
+ json_content = File.read(file_path)
72
+ json_data = JSON.parse(json_content)
73
+ count_tokens_from_json(json_data)
74
+ end
75
+
76
+ def count_tokens_from_html_file(file_path)
77
+ html_content = File.read(file_path)
78
+ count_tokens_from_html(html_content)
79
+ end
80
+
81
+ # util
82
+
83
+ def count_tokens_from_json(json_data)
84
+ text = extract_text_from_json(json_data)
85
+ count_tokens_from_text(text)
86
+ end
87
+
88
+ def count_tokens_from_html(html_content)
89
+ text = extract_text_from_html(html_content)
90
+ count_tokens_from_text(text)
91
+ end
92
+
93
+ def count_tokens_from_text(text)
94
+ tokens = @tokenizer.encode(text).tokens
95
+ tokens.count
96
+ end
97
+
98
+ private
99
+
100
+ def extract_text_from_excel(xlsx)
101
+ text = ""
102
+ xlsx.each_row_streaming do |row|
103
+ row.each do |cell|
104
+ text += cell.value.to_s + " "
105
+ end
106
+ end
107
+ text.strip
108
+ end
109
+
110
+ def extract_text_from_csv(file_path)
111
+ text = ""
112
+ CSV.foreach(file_path) do |row|
113
+ text += row.join(" ") + " "
114
+ end
115
+ text.strip
116
+ end
117
+
118
+ def extract_text_from_pdf(reader)
119
+ text = ""
120
+ reader.pages.each do |page|
121
+ text += page.text + " "
122
+ end
123
+ text.strip
124
+ end
125
+
126
+ def extract_text_from_html(html)
127
+ Nokogiri::HTML(html).text
128
+ end
129
+
130
+ def extract_text_from_json(json_data)
131
+ text = ""
132
+ case json_data
133
+ when Hash
134
+ json_data.each_value { |value| text += extract_text_from_json(value) + " " }
135
+ when Array
136
+ json_data.each { |value| text += extract_text_from_json(value) + " " }
137
+ else
138
+ text += json_data.to_s
139
+ end
140
+ text.strip
141
+ end
142
+ end
143
+
144
+ end
@@ -0,0 +1,4 @@
1
+ module TokenEstimator
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,168 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: token_estimator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - aemabit
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2024-07-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 7.2.0.beta2
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 7.2.0.beta2
27
+ - !ruby/object:Gem::Dependency
28
+ name: roo
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.10'
34
+ - - ">="
35
+ - !ruby/object:Gem::Version
36
+ version: 2.10.1
37
+ type: :runtime
38
+ prerelease: false
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - "~>"
42
+ - !ruby/object:Gem::Version
43
+ version: '2.10'
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: 2.10.1
47
+ - !ruby/object:Gem::Dependency
48
+ name: pdf-reader
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '2.12'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: '2.12'
61
+ - !ruby/object:Gem::Dependency
62
+ name: tokenizers
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: 0.5.0
68
+ type: :runtime
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - "~>"
73
+ - !ruby/object:Gem::Version
74
+ version: 0.5.0
75
+ - !ruby/object:Gem::Dependency
76
+ name: kramdown
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - "~>"
80
+ - !ruby/object:Gem::Version
81
+ version: '2.4'
82
+ type: :runtime
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - "~>"
87
+ - !ruby/object:Gem::Version
88
+ version: '2.4'
89
+ - !ruby/object:Gem::Dependency
90
+ name: nokogiri
91
+ requirement: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - "~>"
94
+ - !ruby/object:Gem::Version
95
+ version: '1.16'
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ version: 1.16.6
99
+ type: :runtime
100
+ prerelease: false
101
+ version_requirements: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - "~>"
104
+ - !ruby/object:Gem::Version
105
+ version: '1.16'
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: 1.16.6
109
+ - !ruby/object:Gem::Dependency
110
+ name: csv
111
+ requirement: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - "~>"
114
+ - !ruby/object:Gem::Version
115
+ version: '3.3'
116
+ type: :runtime
117
+ prerelease: false
118
+ version_requirements: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - "~>"
121
+ - !ruby/object:Gem::Version
122
+ version: '3.3'
123
+ description: TokenEstimator is a Rails gem that allows you to count tokens in Excel,
124
+ CSV, PDF, TXT, Markdown, and input text files using different tokenizers.
125
+ email:
126
+ - am@connectnodes.io
127
+ executables: []
128
+ extensions: []
129
+ extra_rdoc_files: []
130
+ files:
131
+ - ".rspec"
132
+ - ".standard.yml"
133
+ - CHANGELOG.md
134
+ - CODE_OF_CONDUCT.md
135
+ - LICENSE.txt
136
+ - README.md
137
+ - Rakefile
138
+ - lib/token_estimator.rb
139
+ - lib/token_estimator/version.rb
140
+ - sig/token_estimator.rbs
141
+ homepage: https://github.com/aemabit/token_estimator
142
+ licenses:
143
+ - MIT
144
+ metadata:
145
+ allowed_push_host: https://rubygems.org
146
+ homepage_uri: https://github.com/aemabit/token_estimator
147
+ source_code_uri: https://github.com/aemabit/token_estimator
148
+ changelog_uri: https://github.com/aemabit/token_estimator/blob/main/CHANGELOG.md
149
+ post_install_message:
150
+ rdoc_options: []
151
+ require_paths:
152
+ - lib
153
+ required_ruby_version: !ruby/object:Gem::Requirement
154
+ requirements:
155
+ - - ">="
156
+ - !ruby/object:Gem::Version
157
+ version: 3.0.0
158
+ required_rubygems_version: !ruby/object:Gem::Requirement
159
+ requirements:
160
+ - - ">="
161
+ - !ruby/object:Gem::Version
162
+ version: '0'
163
+ requirements: []
164
+ rubygems_version: 3.5.9
165
+ signing_key:
166
+ specification_version: 4
167
+ summary: A Rails engine for counting tokens in various types of files and text.
168
+ test_files: []