-
Notifications
You must be signed in to change notification settings - Fork 126
feat: add keywords CLI tool for text vectorization (#122) #163
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| #!/usr/bin/env ruby | ||
| # frozen_string_literal: true | ||
|
|
||
| # Force UTF-8 encoding for proper handling of model data and user input | ||
| Encoding.default_external = Encoding::UTF_8 | ||
| Encoding.default_internal = Encoding::UTF_8 | ||
|
|
||
| require 'classifier/keywords/cli' | ||
|
|
||
| result = Classifier::Keywords::CLI.new(ARGV).run | ||
|
Yegorov marked this conversation as resolved.
|
||
|
|
||
| warn result[:error] unless result[:error].empty? | ||
| puts result[:output] unless result[:output].empty? | ||
|
|
||
| exit result[:exit_code] | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -33,6 +33,12 @@ def clean_word_hash(min_word_length = 3) | |
| word_hash_for_words(gsub(/[^\w\s]/, '').split, min_word_length) | ||
| end | ||
|
|
||
| # Builds a mapping between stemmed roots and their most frequent original words. | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Metrics/LineLength: Line is too long. [82/80] |
||
| # @rbs (?Integer) -> Hash[Symbol, String] | ||
| def stem_to_word_hash(min_word_length = 3) | ||
| mapping_stem_to_word_for_words(gsub(/[^\w\s]/, '').split, min_word_length) | ||
| end | ||
|
|
||
| private | ||
|
|
||
| # @rbs (Array[String], Integer) -> Hash[Symbol, Integer] | ||
|
|
@@ -54,6 +60,20 @@ def word_hash_for_symbols(words) | |
| d | ||
| end | ||
|
|
||
| # @rbs (Array[String], Integer) -> Hash[Symbol, Integer] | ||
| def mapping_stem_to_word_for_words(words, min_word_length) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Metrics/AbcSize: Assignment Branch Condition size for mapping_stem_to_word_for_words is too high. [17.97/15] |
||
| h = {} | ||
| words.map { _1.tap(&:downcase!) }.tally.each do |word, count| | ||
| next unless !CORPUS_SKIP_WORDS.include?(word) && word.length >= min_word_length | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Metrics/LineLength: Line is too long. [85/80] |
||
|
|
||
| stem = word.stem.intern | ||
| h[stem] ||= [word, count] | ||
| h[stem] = [word, count] if h.dig(stem, 1) < count | ||
| end | ||
| h.each_key { |k| h[k] = h[k].first } | ||
| h | ||
| end | ||
|
|
||
| CORPUS_SKIP_WORDS = ::Set.new(%w[ | ||
| a | ||
| again | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.