spellr 0.5.2 → 0.5.3

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ffa5cd50771c2305c4e4f3e14d6905b40be8b337c6ebbddc2b2d8a82649cfb55
4
- data.tar.gz: 51d07400d834a8c5e4e4432a5ecb95017bd4fb9896addb39753a7490bb4793f5
3
+ metadata.gz: 6ab2e491fac49775163c74ab18787f8abed0b46f96de0bbcb2fa00d02c3ebe89
4
+ data.tar.gz: b6fcd62fb1a090e9f82e6d7b591eb44156f0a000ef1f7e74417a6d0cef9b8e8f
5
5
  SHA512:
6
- metadata.gz: 0fb135cb1f8368c65372e10af566b5a8553f926aa0ac6443f047be7042fd1c2d07a7ae0e451995430048b22241c266d40826306b58f5aa9665dd2ec624ca0ca7
7
- data.tar.gz: e18d571c9269875bfede005247266a9c39dff4611f76a3327adf19dda08e400b9c9066adf6cdc05580e7282f39a23549a3d0b436616ecc868c763bc6a4979a2a
6
+ metadata.gz: be574430d9bd37fb857670e4e555e5c90559a62749e788af6e844c24a56f4c08aaeee5b7498f10c3a7edce39cf0393927e9439871ae59569e46e84f719569467
7
+ data.tar.gz: b711c144f99e37d96bf789caa3dcabb36273218f6b02c02f368c2164575a41e99a0b32c2382c66679f3583ffc6ca85f1600e4aff59690de42af7f858f0aea929
data/CHANGELOG.md CHANGED
@@ -1,3 +1,7 @@
1
+ # v0.5.3
2
+ - update fast_ignore requirement. it's slightly faster.
3
+ - misc other performance improvements
4
+
1
5
  # v0.5.2
2
6
  - require Parallel dependency in gemspec (oops)
3
7
 
data/README.md CHANGED
@@ -1,5 +1,6 @@
1
1
  # Spellr
2
2
 
3
+ [![Gem Version](https://badge.fury.io/rb/spellr.svg)](https://rubygems.org/gems/spellr)
3
4
  [![Build Status](https://travis-ci.org/robotdana/spellr.svg?branch=master)](https://travis-ci.org/robotdana/spellr)
4
5
 
5
6
  Spell check your source code for fun and occasionally finding bugs
data/lib/.spellr.yml CHANGED
@@ -29,6 +29,8 @@ excludes: # this list is parsed with the .gitignore format
29
29
  languages:
30
30
  english:
31
31
  locale: US # options US, CA, AU, GBs (GB with -ise endings), GBz (GB with -ize endings)
32
+ spellr:
33
+ addable: false
32
34
  ruby:
33
35
  includes: # Filtered using gitignore format
34
36
  - '*.rb'
@@ -12,6 +12,10 @@ class Array
12
12
  end
13
13
  end
14
14
 
15
+ class Regexp
16
+ alias_method :match?, :match unless RUBY_VERSION >= '2.4'
17
+ end
18
+
15
19
  class String
16
20
  alias_method :match?, :match unless RUBY_VERSION >= '2.4'
17
21
  end
data/lib/spellr/check.rb CHANGED
@@ -6,6 +6,7 @@ require_relative 'token'
6
6
  require_relative 'column_location'
7
7
  require_relative 'line_location'
8
8
  require_relative 'output_stubbed'
9
+ require_relative 'backports'
9
10
 
10
11
  require 'parallel'
11
12
 
data/lib/spellr/config.rb CHANGED
@@ -61,7 +61,7 @@ module Spellr
61
61
 
62
62
  def languages
63
63
  @languages ||= @config[:languages].map do |key, args|
64
- Spellr::Language.new(key, args)
64
+ Spellr::Language.new(key, **args)
65
65
  end
66
66
  end
67
67
 
@@ -89,14 +89,18 @@ module Spellr
89
89
  private
90
90
 
91
91
  def only_has_one_key_per_language
92
- conflicting_languages = languages.group_by(&:key).values.select { |g| g.length > 1 }
93
-
94
- conflicting_languages.each do |conflicts|
92
+ languages_with_conflicting_keys.each do |conflicts|
95
93
  errors << "Error: #{conflicts.map(&:name).join(' & ')} share the same language key "\
96
94
  "(#{conflicts.first.key}). Please define one to be different with `key:`"
97
95
  end
98
96
  end
99
97
 
98
+ def languages_with_conflicting_keys
99
+ languages.select(&:addable?).group_by(&:key).values.select do |g|
100
+ g.length > 1
101
+ end
102
+ end
103
+
100
104
  def keys_are_single_characters
101
105
  bad_languages = languages.select { |l| l.key.length > 1 }
102
106
 
@@ -14,6 +14,8 @@ module Spellr
14
14
 
15
15
  def each
16
16
  fast_ignore.each do |file|
17
+ next unless cli_patterns_ignore.allowed?(file)
18
+
17
19
  file = Spellr::File.new(file)
18
20
 
19
21
  yield(file)
@@ -41,9 +43,13 @@ module Spellr
41
43
  def fast_ignore
42
44
  FastIgnore.new(
43
45
  ignore_rules: Spellr.config.excludes,
44
- include_rules: Spellr.config.includes + cli_patterns,
46
+ include_rules: Spellr.config.includes,
45
47
  gitignore: gitignore_path
46
48
  )
47
49
  end
50
+
51
+ def cli_patterns_ignore
52
+ @cli_patterns_ignore ||= FastIgnore.new(include_rules: cli_patterns, gitignore: false)
53
+ end
48
54
  end
49
55
  end
@@ -19,14 +19,18 @@ module Spellr
19
19
  @languages ||= Spellr.config.languages_for(token.location.file.to_path)
20
20
  end
21
21
 
22
+ def addable_languages
23
+ languages.select(&:addable?)
24
+ end
25
+
22
26
  def language_keys
23
- @language_keys ||= @languages.map(&:key)
27
+ @language_keys ||= addable_languages.map(&:key)
24
28
  end
25
29
 
26
30
  def ask_wordlist
27
31
  puts "Add #{red(token)} to wordlist:"
28
32
 
29
- languages.each do |language|
33
+ addable_languages.each do |language|
30
34
  puts "[#{language.key}] #{language.name}"
31
35
  end
32
36
 
@@ -50,7 +54,7 @@ module Spellr
50
54
  end
51
55
 
52
56
  def add_to_wordlist(choice)
53
- wordlist = languages.find { |w| w.key == choice }.project_wordlist
57
+ wordlist = addable_languages.find { |w| w.key == choice }.project_wordlist
54
58
  wordlist << token
55
59
  reporter.increment(:total_added)
56
60
  puts "Added #{red(token)} to #{wordlist.name} wordlist"
@@ -11,10 +11,10 @@ class NaiveBayes
11
11
 
12
12
  def initialize(path = YAML_PATH)
13
13
  load_from_yaml(path) if File.exist?(path)
14
+ @key = {}
14
15
  end
15
16
 
16
17
  def key?(string)
17
- @key ||= {}
18
18
  @key.fetch(string) do
19
19
  @key[string] = classify(PossibleKey.new(string).features).start_with?('key')
20
20
  end
@@ -92,7 +92,7 @@ class NaiveBayes
92
92
  # value: the value of the feature for which we are finding the probability
93
93
  # class_name: name of the class in consideration
94
94
  def feature_probability(feature, value, class_name)
95
- Stats.gaussian_probability(value, feature_set[class_name][feature])
95
+ Stats.gaussian_probability(value, **feature_set[class_name][feature])
96
96
  end
97
97
 
98
98
  # multiply together the feature probabilities for all of the
@@ -103,12 +103,16 @@ class NaiveBayes
103
103
  end
104
104
  end
105
105
 
106
+ def heuristic_weight
107
+ @heuristic_weight ||= 10**Spellr.config.key_heuristic_weight
108
+ end
109
+
106
110
  # this is where we compute the final naive Bayesian probability
107
111
  # for a given set of features being a part of a given class.
108
112
  def class_probability(features, class_name)
109
113
  class_fraction = 1.0 / num_classes
110
114
  feature_bayes = feature_multiplication(features, class_name)
111
- feature_bayes *= (10**Spellr.config.key_heuristic_weight) if class_name.start_with?('key_')
115
+ feature_bayes *= heuristic_weight if class_name.start_with?('key_')
112
116
  feature_bayes * class_fraction
113
117
  end
114
118
 
@@ -7,12 +7,17 @@ module Spellr
7
7
  attr_reader :name
8
8
  attr_reader :key
9
9
 
10
- def initialize(name, key: name[0], includes: [], hashbangs: [], locale: [])
10
+ def initialize(name, key: name[0], includes: [], hashbangs: [], locale: [], addable: true) # rubocop:disable Metrics/ParameterLists, Metrics/MethodLength
11
11
  @name = name
12
12
  @key = key
13
13
  @includes = includes
14
14
  @hashbangs = hashbangs
15
15
  @locales = Array(locale)
16
+ @addable = addable
17
+ end
18
+
19
+ def addable?
20
+ @addable
16
21
  end
17
22
 
18
23
  def matches?(file)
data/lib/spellr/token.rb CHANGED
@@ -4,8 +4,9 @@ require_relative 'column_location'
4
4
  require_relative 'string_format'
5
5
 
6
6
  class String
7
+ @@spellr_normalize = {} # rubocop:disable Style/ClassVars # I want to share this with subclasses
8
+
7
9
  def spellr_normalize
8
- @@spellr_normalize ||= {} # rubocop:disable Style/ClassVars # I want to share this with subclasses
9
10
  @@spellr_normalize.fetch(to_s) do |term|
10
11
  @@spellr_normalize[term] = "#{term.strip.downcase.unicode_normalize.tr('’', "'")}\n"
11
12
  end
@@ -19,7 +19,7 @@ module Spellr
19
19
  @file = file.is_a?(StringIO) || file.is_a?(IO) ? file : ::File.new(file)
20
20
  @file.pos = @start_at.line_location.byte_offset
21
21
 
22
- @line_tokenizer = LineTokenizer.new(skip_key: skip_key)
22
+ @line_tokenizer = LineTokenizer.new('', skip_key: skip_key)
23
23
  end
24
24
 
25
25
  def terms
@@ -34,6 +34,8 @@ module Spellr
34
34
  file.each_line do |line|
35
35
  prepare_tokenizer_for_line(line).each_term(&block)
36
36
  end
37
+ ensure
38
+ file.close
37
39
  end
38
40
 
39
41
  def each_token(skip_term_proc: nil) # rubocop:disable Metrics/MethodLength
@@ -64,6 +66,8 @@ module Spellr
64
66
  char_offset += line.length
65
67
  byte_offset += line.bytesize
66
68
  end
69
+ ensure
70
+ file.close
67
71
  end
68
72
 
69
73
  def normalized_terms
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Spellr
4
- VERSION = '0.5.2'
4
+ VERSION = '0.5.3'
5
5
  end
@@ -14,6 +14,7 @@ module Spellr
14
14
  path = @file = file
15
15
  @path = Pathname.pwd.join('.spellr_wordlists').join(path).expand_path
16
16
  @name = name
17
+ @include = {}
17
18
  end
18
19
 
19
20
  def each(&block)
@@ -29,20 +30,24 @@ module Spellr
29
30
  # significantly faster than default Enumerable#include?
30
31
  # requires terms to have been sorted
31
32
  def include?(term)
32
- include_cache[term.spellr_normalize]
33
+ term = term.spellr_normalize
34
+ @include.fetch(term) do
35
+ @include[term] = words.bsearch { |value| term <=> value }
36
+ end
33
37
  end
34
38
 
35
39
  def <<(term)
36
40
  term = term.spellr_normalize
37
41
  touch
38
- include_cache[term] = true
42
+ @include[term] = true
39
43
  insert_sorted(term)
40
- @path.write(to_a.join) # we don't need to clear the cache
44
+ @path.write(words.join) # we don't need to clear the cache
41
45
  end
42
46
 
43
- def to_a
44
- @to_a ||= super
47
+ def words
48
+ @words ||= (exist? ? @path.readlines : [])
45
49
  end
50
+ alias_method :to_a, :words
46
51
 
47
52
  def clean(file = @path)
48
53
  require_relative 'tokenizer'
@@ -78,21 +83,13 @@ module Spellr
78
83
  private
79
84
 
80
85
  def insert_sorted(term)
81
- insert_at = to_a.bsearch_index { |value| value >= term }
82
- insert_at ? to_a.insert(insert_at, term) : to_a.push(term)
83
- end
84
-
85
- def include_cache
86
- @include_cache ||= Hash.new do |cache, term|
87
- cache[term] = to_a.bsearch do |value|
88
- term <=> value
89
- end
90
- end
86
+ insert_at = words.bsearch_index { |value| value >= term }
87
+ insert_at ? words.insert(insert_at, term) : words.push(term)
91
88
  end
92
89
 
93
90
  def clear_cache
94
- @to_a = nil
95
- @include = nil
91
+ @words = nil
92
+ @include = {}
96
93
  remove_instance_variable(:@exist) if defined?(@exist)
97
94
  end
98
95
 
data/spellr.gemspec CHANGED
@@ -34,6 +34,6 @@ Gem::Specification.new do |spec|
34
34
  spec.add_development_dependency 'rubocop-rspec'
35
35
  spec.add_development_dependency 'terminal-table'
36
36
  spec.add_development_dependency 'tty_string'
37
- spec.add_dependency 'fast_ignore', '~> 0.4.0'
37
+ spec.add_dependency 'fast_ignore', '~> 0.5.1'
38
38
  spec.add_dependency 'parallel', '~> 1.0'
39
39
  end
@@ -0,0 +1,17 @@
1
+ disable
2
+ enable
3
+ excludes
4
+ generated
5
+ hashbangs
6
+ heuristic
7
+ includes
8
+ key
9
+ languages
10
+ length
11
+ locale
12
+ minimum
13
+ robotdana
14
+ spellr
15
+ weight
16
+ word
17
+ wordlists
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: spellr
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.2
4
+ version: 0.5.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dana Sherson
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-10-30 00:00:00.000000000 Z
11
+ date: 2020-02-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -142,14 +142,14 @@ dependencies:
142
142
  requirements:
143
143
  - - "~>"
144
144
  - !ruby/object:Gem::Version
145
- version: 0.4.0
145
+ version: 0.5.1
146
146
  type: :runtime
147
147
  prerelease: false
148
148
  version_requirements: !ruby/object:Gem::Requirement
149
149
  requirements:
150
150
  - - "~>"
151
151
  - !ruby/object:Gem::Version
152
- version: 0.4.0
152
+ version: 0.5.1
153
153
  - !ruby/object:Gem::Dependency
154
154
  name: parallel
155
155
  requirement: !ruby/object:Gem::Requirement
@@ -228,6 +228,7 @@ files:
228
228
  - wordlists/javascript.txt
229
229
  - wordlists/ruby.txt
230
230
  - wordlists/shell.txt
231
+ - wordlists/spellr.txt
231
232
  homepage: http://github.com/robotdana/spellr
232
233
  licenses:
233
234
  - MIT