forematter 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/forematter.rb +3 -0
- data/lib/forematter/command_runner.rb +12 -3
- data/lib/forematter/commands/add.rb +2 -2
- data/lib/forematter/commands/classify.rb +17 -4
- data/lib/forematter/commands/cleanup.rb +6 -2
- data/lib/forematter/commands/list.rb +1 -1
- data/lib/forematter/commands/merge.rb +1 -1
- data/lib/forematter/commands/remove.rb +1 -1
- data/lib/forematter/file_wrapper.rb +1 -1
- data/lib/forematter/version.rb +1 -1
- metadata +1 -7
    
        data/lib/forematter.rb
    CHANGED
    
    
| @@ -4,16 +4,25 @@ module Forematter | |
| 4 4 | 
             
              class CommandRunner < ::Cri::CommandRunner
         | 
| 5 5 | 
             
                def call
         | 
| 6 6 | 
             
                  run
         | 
| 7 | 
            +
                  exit 1 if @has_error
         | 
| 7 8 | 
             
                rescue UsageException
         | 
| 8 | 
            -
                  path = [command.supercommand]
         | 
| 9 | 
            -
                  path.unshift(path[0].supercommand) until path[0].nil?
         | 
| 10 | 
            -
                  super_usage = path[1..-1].map { |c| c.name + ' ' }.join
         | 
| 11 9 | 
             
                  $stderr.puts "usage: #{super_usage}#{command.usage}"
         | 
| 12 10 | 
             
                  exit 1
         | 
| 13 11 | 
             
                end
         | 
| 14 12 |  | 
| 15 13 | 
             
                protected
         | 
| 16 14 |  | 
| 15 | 
            +
                def log_skip(file, msg)
         | 
| 16 | 
            +
                  $stderr.puts "#{super_usage}#{command.name}: #{file.filename}: #{msg}"
         | 
| 17 | 
            +
                  @has_error = 1
         | 
| 18 | 
            +
                end
         | 
| 19 | 
            +
             | 
| 20 | 
            +
                def super_usage
         | 
| 21 | 
            +
                  path = [command.supercommand]
         | 
| 22 | 
            +
                  path.unshift(path[0].supercommand) until path[0].nil?
         | 
| 23 | 
            +
                  path[1..-1].map { |c| c.name + ' ' }.join
         | 
| 24 | 
            +
                end
         | 
| 25 | 
            +
             | 
| 17 26 | 
             
                def field
         | 
| 18 27 | 
             
                  partition
         | 
| 19 28 | 
             
                  fail UsageException, 'Missing field name' unless @field
         | 
| @@ -18,8 +18,8 @@ module Forematter::Commands | |
| 18 18 | 
             
              class Add < Forematter::CommandRunner
         | 
| 19 19 | 
             
                def run
         | 
| 20 20 | 
             
                  files.each do |file|
         | 
| 21 | 
            -
                    old = file[field].to_ruby  | 
| 22 | 
            -
                     | 
| 21 | 
            +
                    old = file.key?(field) ? file[field].to_ruby : []
         | 
| 22 | 
            +
                    log_skip(file, "#{field} is not an array, unable to add") && next unless old.is_a?(Array)
         | 
| 23 23 | 
             
                    add = options[:'allow-dupes'] ? values : values.select { |v| !old.include?(v) }
         | 
| 24 24 | 
             
                    next if add.empty?
         | 
| 25 25 | 
             
                    add.each { |v| old << v }
         | 
| @@ -29,8 +29,14 @@ module Forematter::Commands | |
| 29 29 | 
             
                  load_classifier
         | 
| 30 30 |  | 
| 31 31 | 
             
                  puts 'Getting categories'
         | 
| 32 | 
            -
                  categories_for(files_with(field)).each  | 
| 33 | 
            -
             | 
| 32 | 
            +
                  categories_for(files_with(field)).each { |cat| bayes.add_category(cat) }
         | 
| 33 | 
            +
             | 
| 34 | 
            +
                  if bayes.categories.empty?
         | 
| 35 | 
            +
                    $stderr.puts "No categories found in #{field}, unable to classify"
         | 
| 36 | 
            +
                    exit 1
         | 
| 37 | 
            +
                  else
         | 
| 38 | 
            +
                    found = bayes.categories.length
         | 
| 39 | 
            +
                    puts "#{found} #{found == 1 ? 'category' : 'categories'} found"
         | 
| 34 40 | 
             
                  end
         | 
| 35 41 |  | 
| 36 42 | 
             
                  puts 'Training classifier'
         | 
| @@ -54,7 +60,11 @@ module Forematter::Commands | |
| 54 60 | 
             
                end
         | 
| 55 61 |  | 
| 56 62 | 
             
                def categories_for(files)
         | 
| 57 | 
            -
                  files | 
| 63 | 
            +
                  files
         | 
| 64 | 
            +
                    .map { |file| file[field].to_ruby }
         | 
| 65 | 
            +
                    .select { |f| f.is_a?(String) }
         | 
| 66 | 
            +
                    .uniq
         | 
| 67 | 
            +
                    .map(&:to_sym)
         | 
| 58 68 | 
             
                end
         | 
| 59 69 |  | 
| 60 70 | 
             
                def files_to_classify
         | 
| @@ -65,7 +75,10 @@ module Forematter::Commands | |
| 65 75 |  | 
| 66 76 | 
             
                def train(file)
         | 
| 67 77 | 
             
                  val = file[field].to_ruby
         | 
| 68 | 
            -
                   | 
| 78 | 
            +
                  unless val.is_a?(String)
         | 
| 79 | 
            +
                    skip file, "unable to train, #{field} is not a string"
         | 
| 80 | 
            +
                    return
         | 
| 81 | 
            +
                  end
         | 
| 69 82 | 
             
                  bayes.train(val.to_sym, file.content)
         | 
| 70 83 | 
             
                end
         | 
| 71 84 |  | 
| @@ -25,7 +25,11 @@ module Forematter::Commands | |
| 25 25 |  | 
| 26 26 | 
             
                  files_with(field).each do |file|
         | 
| 27 27 | 
             
                    old = file[field].to_ruby
         | 
| 28 | 
            -
                     | 
| 28 | 
            +
                    begin
         | 
| 29 | 
            +
                      val = cleanup(old)
         | 
| 30 | 
            +
                    rescue Forematter::UnexpectedValue => e
         | 
| 31 | 
            +
                      log_skip(file, e.message) && next
         | 
| 32 | 
            +
                    end
         | 
| 29 33 | 
             
                    unless val == old
         | 
| 30 34 | 
             
                      file[field] = val
         | 
| 31 35 | 
             
                      file.write
         | 
| @@ -47,7 +51,7 @@ module Forematter::Commands | |
| 47 51 | 
             
                def cleanup(val)
         | 
| 48 52 | 
             
                  val = val.dup
         | 
| 49 53 | 
             
                  return cleanup_array(val) if val.is_a?(Array)
         | 
| 50 | 
            -
                  fail  | 
| 54 | 
            +
                  fail Forematter::UnexpectedValue, "#{field} is not an array" if options[:sort]
         | 
| 51 55 | 
             
                  options.keys.each do |option|
         | 
| 52 56 | 
             
                    val = val.method(CLEANUP_MAP[option]).call if CLEANUP_MAP.key?(option) && options[option]
         | 
| 53 57 | 
             
                  end
         | 
| @@ -17,7 +17,7 @@ module Forematter::Commands | |
| 17 17 |  | 
| 18 18 | 
             
                  files_with(field).each do |file|
         | 
| 19 19 | 
             
                    old = file[field].to_ruby
         | 
| 20 | 
            -
                     | 
| 20 | 
            +
                    log_skip(file, "#{field} is not an array") && next unless old.is_a?(Array)
         | 
| 21 21 |  | 
| 22 22 | 
             
                    # Continue unless unless field had one of the values to remove
         | 
| 23 23 | 
             
                    next if (old & dups).empty?
         | 
| @@ -15,7 +15,7 @@ module Forematter::Commands | |
| 15 15 | 
             
                def run
         | 
| 16 16 | 
             
                  files_with(field).each do |file|
         | 
| 17 17 | 
             
                    old = file[field].to_ruby
         | 
| 18 | 
            -
                     | 
| 18 | 
            +
                    log_skip(file, "#{field} is not an array") && next unless old.is_a?(Array)
         | 
| 19 19 |  | 
| 20 20 | 
             
                    # Continue unless old contains elements of values
         | 
| 21 21 | 
             
                    next if (old & values).empty?
         | 
    
        data/lib/forematter/version.rb
    CHANGED
    
    
    
        metadata
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            --- !ruby/object:Gem::Specification
         | 
| 2 2 | 
             
            name: forematter
         | 
| 3 3 | 
             
            version: !ruby/object:Gem::Version
         | 
| 4 | 
            -
              version: 0.1. | 
| 4 | 
            +
              version: 0.1.1
         | 
| 5 5 | 
             
              prerelease: 
         | 
| 6 6 | 
             
            platform: ruby
         | 
| 7 7 | 
             
            authors:
         | 
| @@ -139,18 +139,12 @@ required_ruby_version: !ruby/object:Gem::Requirement | |
| 139 139 | 
             
              - - ! '>='
         | 
| 140 140 | 
             
                - !ruby/object:Gem::Version
         | 
| 141 141 | 
             
                  version: '0'
         | 
| 142 | 
            -
                  segments:
         | 
| 143 | 
            -
                  - 0
         | 
| 144 | 
            -
                  hash: 2379320938142761946
         | 
| 145 142 | 
             
            required_rubygems_version: !ruby/object:Gem::Requirement
         | 
| 146 143 | 
             
              none: false
         | 
| 147 144 | 
             
              requirements:
         | 
| 148 145 | 
             
              - - ! '>='
         | 
| 149 146 | 
             
                - !ruby/object:Gem::Version
         | 
| 150 147 | 
             
                  version: '0'
         | 
| 151 | 
            -
                  segments:
         | 
| 152 | 
            -
                  - 0
         | 
| 153 | 
            -
                  hash: 2379320938142761946
         | 
| 154 148 | 
             
            requirements: []
         | 
| 155 149 | 
             
            rubyforge_project: 
         | 
| 156 150 | 
             
            rubygems_version: 1.8.23
         |