guessmethod 0.0.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/History.txt +4 -0
- data/License.txt +20 -0
- data/Manifest.txt +17 -0
- data/README.txt +26 -0
- data/Rakefile +138 -0
- data/lib/guessmethod/version.rb +9 -0
- data/lib/guessmethod.rb +97 -0
- data/lib/string/levenshtein.rb +39 -0
- data/scripts/txt2html +67 -0
- data/setup.rb +1585 -0
- data/spec/guessmethod_spec.rb +11 -0
- data/spec/spec_helper.rb +1 -0
- data/website/index.html +111 -0
- data/website/index.txt +56 -0
- data/website/javascripts/rounded_corners_lite.inc.js +285 -0
- data/website/stylesheets/screen.css +138 -0
- data/website/template.rhtml +48 -0
- metadata +67 -0
data/History.txt
ADDED
data/License.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2007 Chris Shea
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Manifest.txt
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
History.txt
|
2
|
+
License.txt
|
3
|
+
Manifest.txt
|
4
|
+
README.txt
|
5
|
+
Rakefile
|
6
|
+
lib/guessmethod.rb
|
7
|
+
lib/guessmethod/version.rb
|
8
|
+
lib/string/levenshtein.rb
|
9
|
+
scripts/txt2html
|
10
|
+
setup.rb
|
11
|
+
spec/guessmethod_spec.rb
|
12
|
+
spec/spec_helper.rb
|
13
|
+
website/index.html
|
14
|
+
website/index.txt
|
15
|
+
website/javascripts/rounded_corners_lite.inc.js
|
16
|
+
website/stylesheets/screen.css
|
17
|
+
website/template.rhtml
|
data/README.txt
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
README for guessmethod
|
2
|
+
======================
|
3
|
+
|
4
|
+
= Usage
|
5
|
+
|
6
|
+
<tt>require 'guessmethod'</tt>
|
7
|
+
|
8
|
+
<tt>class String; include GuessMethod; end</tt>
|
9
|
+
|
10
|
+
Now, you can accidentally call <tt>'1'.toi</tt> and Guess Method will step in and call
|
11
|
+
<tt>'1'.to_i</tt> like you meant to.
|
12
|
+
|
13
|
+
== Options
|
14
|
+
|
15
|
+
<tt>GuessMethod::Options</tt> is a hash with configuration values. The keys :insert_weight,
|
16
|
+
:delete_weight, and :substitution_weight change the behavior of the levenshtein calculation.
|
17
|
+
:threshold determines how close a method name needs to be to be counted as a 'match'. Values
|
18
|
+
over 2 can give you unpredictable results, especially for short method names.
|
19
|
+
|
20
|
+
= Warning
|
21
|
+
|
22
|
+
This is *not* intended for production.
|
23
|
+
|
24
|
+
= Contact
|
25
|
+
|
26
|
+
Chris Shea: chris at tie-rack . org
|
data/Rakefile
ADDED
@@ -0,0 +1,138 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
require 'rake/clean'
|
4
|
+
require 'rake/testtask'
|
5
|
+
require 'rake/packagetask'
|
6
|
+
require 'rake/gempackagetask'
|
7
|
+
require 'rake/rdoctask'
|
8
|
+
require 'rake/contrib/rubyforgepublisher'
|
9
|
+
require 'fileutils'
|
10
|
+
require 'hoe'
|
11
|
+
begin
|
12
|
+
require 'spec/rake/spectask'
|
13
|
+
rescue LoadError
|
14
|
+
puts 'To use rspec for testing you must install rspec gem:'
|
15
|
+
puts '$ sudo gem install rspec'
|
16
|
+
exit
|
17
|
+
end
|
18
|
+
|
19
|
+
include FileUtils
|
20
|
+
require File.join(File.dirname(__FILE__), 'lib', 'guessmethod', 'version')
|
21
|
+
|
22
|
+
AUTHOR = 'Chris Shea' # can also be an array of Authors
|
23
|
+
EMAIL = "chris@tie-rack.org"
|
24
|
+
DESCRIPTION = "hijacks method_missing to act as a spellchecker"
|
25
|
+
GEM_NAME = 'guessmethod' # what ppl will type to install your gem
|
26
|
+
|
27
|
+
@config_file = "~/.rubyforge/user-config.yml"
|
28
|
+
@config = nil
|
29
|
+
def rubyforge_username
|
30
|
+
unless @config
|
31
|
+
begin
|
32
|
+
@config = YAML.load(File.read(File.expand_path(@config_file)))
|
33
|
+
rescue
|
34
|
+
puts <<-EOS
|
35
|
+
ERROR: No rubyforge config file found: #{@config_file}"
|
36
|
+
Run 'rubyforge setup' to prepare your env for access to Rubyforge
|
37
|
+
- See http://newgem.rubyforge.org/rubyforge.html for more details
|
38
|
+
EOS
|
39
|
+
exit
|
40
|
+
end
|
41
|
+
end
|
42
|
+
@rubyforge_username ||= @config["username"]
|
43
|
+
end
|
44
|
+
|
45
|
+
RUBYFORGE_PROJECT = 'guessmethod' # The unix name for your project
|
46
|
+
HOMEPATH = "http://#{RUBYFORGE_PROJECT}.rubyforge.org"
|
47
|
+
DOWNLOAD_PATH = "http://rubyforge.org/projects/#{RUBYFORGE_PROJECT}"
|
48
|
+
|
49
|
+
NAME = "guessmethod"
|
50
|
+
REV = nil
|
51
|
+
# UNCOMMENT IF REQUIRED:
|
52
|
+
# REV = `svn info`.each {|line| if line =~ /^Revision:/ then k,v = line.split(': '); break v.chomp; else next; end} rescue nil
|
53
|
+
VERS = GuessMethod::VERSION::STRING + (REV ? ".#{REV}" : "")
|
54
|
+
CLEAN.include ['**/.*.sw?', '*.gem', '.config', '**/.DS_Store']
|
55
|
+
RDOC_OPTS = ['--quiet', '--title', 'guessmethod documentation',
|
56
|
+
"--opname", "index.html",
|
57
|
+
"--line-numbers",
|
58
|
+
"--main", "README",
|
59
|
+
"--inline-source"]
|
60
|
+
|
61
|
+
class Hoe
|
62
|
+
def extra_deps
|
63
|
+
@extra_deps.reject { |x| Array(x).first == 'hoe' }
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
# Generate all the Rake tasks
|
68
|
+
# Run 'rake -T' to see list of generated tasks (from gem root directory)
|
69
|
+
hoe = Hoe.new(GEM_NAME, VERS) do |p|
|
70
|
+
p.author = AUTHOR
|
71
|
+
p.description = DESCRIPTION
|
72
|
+
p.email = EMAIL
|
73
|
+
p.summary = DESCRIPTION
|
74
|
+
p.url = HOMEPATH
|
75
|
+
p.rubyforge_name = RUBYFORGE_PROJECT if RUBYFORGE_PROJECT
|
76
|
+
p.test_globs = ["test/**/test_*.rb"]
|
77
|
+
p.clean_globs |= CLEAN #An array of file patterns to delete on clean.
|
78
|
+
|
79
|
+
# == Optional
|
80
|
+
p.changes = p.paragraphs_of("History.txt", 0..1).join("\n\n")
|
81
|
+
#p.extra_deps = [] # An array of rubygem dependencies [name, version], e.g. [ ['active_support', '>= 1.3.1'] ]
|
82
|
+
#p.spec_extras = {} # A hash of extra values to set in the gemspec.
|
83
|
+
end
|
84
|
+
|
85
|
+
CHANGES = hoe.paragraphs_of('History.txt', 0..1).join("\n\n")
|
86
|
+
PATH = (RUBYFORGE_PROJECT == GEM_NAME) ? RUBYFORGE_PROJECT : "#{RUBYFORGE_PROJECT}/#{GEM_NAME}"
|
87
|
+
hoe.remote_rdoc_dir = File.join(PATH.gsub(/^#{RUBYFORGE_PROJECT}\/?/,''), 'rdoc')
|
88
|
+
|
89
|
+
desc 'Generate website files'
|
90
|
+
task :website_generate do
|
91
|
+
Dir['website/**/*.txt'].each do |txt|
|
92
|
+
sh %{ ruby scripts/txt2html #{txt} > #{txt.gsub(/txt$/,'html')} }
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
desc 'Upload website files to rubyforge'
|
97
|
+
task :website_upload do
|
98
|
+
host = "#{rubyforge_username}@rubyforge.org"
|
99
|
+
remote_dir = "/var/www/gforge-projects/#{PATH}/"
|
100
|
+
local_dir = 'website'
|
101
|
+
sh %{rsync -aCv #{local_dir}/ #{host}:#{remote_dir}}
|
102
|
+
end
|
103
|
+
|
104
|
+
desc 'Generate and upload website files'
|
105
|
+
task :website => [:website_generate, :website_upload, :publish_docs]
|
106
|
+
|
107
|
+
desc 'Release the website and new gem version'
|
108
|
+
task :deploy => [:check_version, :website, :release] do
|
109
|
+
puts "Remember to create SVN tag:"
|
110
|
+
puts "svn copy svn+ssh://#{rubyforge_username}@rubyforge.org/var/svn/#{PATH}/trunk " +
|
111
|
+
"svn+ssh://#{rubyforge_username}@rubyforge.org/var/svn/#{PATH}/tags/REL-#{VERS} "
|
112
|
+
puts "Suggested comment:"
|
113
|
+
puts "Tagging release #{CHANGES}"
|
114
|
+
end
|
115
|
+
|
116
|
+
desc 'Runs tasks website_generate and install_gem as a local deployment of the gem'
|
117
|
+
task :local_deploy => [:website_generate, :install_gem]
|
118
|
+
|
119
|
+
task :check_version do
|
120
|
+
unless ENV['VERSION']
|
121
|
+
puts 'Must pass a VERSION=x.y.z release version'
|
122
|
+
exit
|
123
|
+
end
|
124
|
+
unless ENV['VERSION'] == VERS
|
125
|
+
puts "Please update your version.rb to match the release version, currently #{VERS}"
|
126
|
+
exit
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
desc "Run the specs under spec/models"
|
131
|
+
Spec::Rake::SpecTask.new do |t|
|
132
|
+
t.spec_opts = ['--options', "spec/spec.opts"]
|
133
|
+
t.spec_files = FileList['spec/*_spec.rb']
|
134
|
+
end
|
135
|
+
|
136
|
+
desc "Default task is to run specs"
|
137
|
+
task :default => :spec
|
138
|
+
|
data/lib/guessmethod.rb
ADDED
@@ -0,0 +1,97 @@
|
|
1
|
+
require 'string/levenshtein'
|
2
|
+
require 'highline'
|
3
|
+
|
4
|
+
# The GuessMethod module aliases out method_missing and
|
5
|
+
# replaces it with its own, which attempts to find a
|
6
|
+
# similarly named method callable on the object at hand.
|
7
|
+
# If it finds one, it calls that instead, and prints
|
8
|
+
# a message to stderr explaining as much. If it doesn't
|
9
|
+
# find a suitable method, the orignial method_missing
|
10
|
+
# gets called.
|
11
|
+
module GuessMethod
|
12
|
+
|
13
|
+
Options = {
|
14
|
+
:insert_weight => 1,
|
15
|
+
:delete_weight => 1,
|
16
|
+
:substitution_weight => 1,
|
17
|
+
:threshold => 2
|
18
|
+
}
|
19
|
+
|
20
|
+
def self.included(base)
|
21
|
+
base.class_eval do
|
22
|
+
unless method_defined? :method_missing
|
23
|
+
def method_missing(meth, *args, &block); super; end
|
24
|
+
end
|
25
|
+
alias_method :old_method_missing, :method_missing
|
26
|
+
alias_method :method_missing, :guess_method_missing
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def guess_method_missing(meth, *args, &block)
|
31
|
+
minimum_distance = self.methods.map {|m| m.levenshtein(meth.to_s,
|
32
|
+
Options[:insert_weight], Options[:delete_weight],
|
33
|
+
Options[:substitution_weight])}.min
|
34
|
+
if minimum_distance <= Options[:threshold]
|
35
|
+
closest_methods = self.methods.find_all {|m| m.levenshtein(meth.to_s,
|
36
|
+
Options[:insert_weight], Options[:delete_weight],
|
37
|
+
Options[:substitution_weight]) == minimum_distance}
|
38
|
+
if closest_methods.size == 1
|
39
|
+
call_method = closest_methods.first
|
40
|
+
$stderr.puts Outputter.replacing_method(meth, call_method, self)
|
41
|
+
self.send(call_method, *args, &block)
|
42
|
+
else
|
43
|
+
$stderr.puts Outputter.ambiguous_method(meth, closest_methods, self)
|
44
|
+
old_method_missing(meth, *args, &block)
|
45
|
+
end
|
46
|
+
else
|
47
|
+
$stderr.puts Outputter.no_method_in_threshold(meth, self)
|
48
|
+
old_method_missing(meth, *args, &block)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
# GuessMethod::Outputter uses HighLine to format
|
53
|
+
# alert messages generated by the GuessMethod method_missing
|
54
|
+
class Outputter
|
55
|
+
|
56
|
+
Formatter = HighLine.new
|
57
|
+
|
58
|
+
def self.object(obj) #:nodoc:
|
59
|
+
message = Formatter.color(obj.inspect, :green, :bold)
|
60
|
+
message << ':'
|
61
|
+
message << Formatter.color(obj.class, :green)
|
62
|
+
end
|
63
|
+
|
64
|
+
def self.ambiguous_method(meth, possibilities, obj) #:nodoc:
|
65
|
+
message = Formatter.color('ambigious method:', :red)
|
66
|
+
message << ' '
|
67
|
+
message << Formatter.color(meth, :green)
|
68
|
+
message << ' possible matches '
|
69
|
+
message << possibilities.map {|m| Formatter.color(m, :cyan)}.join(', ')
|
70
|
+
message << ' for '
|
71
|
+
message << Outputter.object(obj)
|
72
|
+
end
|
73
|
+
|
74
|
+
def self.no_method_in_threshold(meth, obj) #:nodoc:
|
75
|
+
message = Formatter.color('no method in threshold:', :red)
|
76
|
+
message << ' for '
|
77
|
+
message << Formatter.color(meth, :green)
|
78
|
+
message << ', sending to '
|
79
|
+
message << Outputter.object(obj)
|
80
|
+
message << "'s method_missing"
|
81
|
+
end
|
82
|
+
|
83
|
+
def self.replacing_method(meth, call_method, obj) #:nodoc:
|
84
|
+
message = Formatter.color('attention:', :red)
|
85
|
+
message << ' sending '
|
86
|
+
message << Formatter.color(call_method, :cyan)
|
87
|
+
message << ' instead of '
|
88
|
+
message << Formatter.color(meth, :cyan)
|
89
|
+
message << ' to '
|
90
|
+
message << Outputter.object(obj)
|
91
|
+
end
|
92
|
+
|
93
|
+
end
|
94
|
+
|
95
|
+
end
|
96
|
+
|
97
|
+
require 'guessmethod/version'
|
@@ -0,0 +1,39 @@
|
|
1
|
+
class String
|
2
|
+
|
3
|
+
# Calculates the levenshtein distance between strings. This method was taken from
|
4
|
+
# The Ruby Way, 2nd Edition, Hal Fulton, pp. 97-98
|
5
|
+
def levenshtein(other, ins=2, del=2, sub=1)
|
6
|
+
|
7
|
+
# ins, del, sub are weighted costs
|
8
|
+
return nil if self.nil?
|
9
|
+
return nil if other.nil?
|
10
|
+
dm = [] # distance matrix
|
11
|
+
|
12
|
+
# Initialize first row values
|
13
|
+
dm[0] = (0..self.length).collect { |i| i * ins }
|
14
|
+
fill = [0] * (self.length - 1)
|
15
|
+
|
16
|
+
# Initialize first column values
|
17
|
+
for i in 1..other.length
|
18
|
+
dm[i] = [i * del, fill.flatten]
|
19
|
+
end
|
20
|
+
|
21
|
+
# populate matrix
|
22
|
+
for i in 1..other.length
|
23
|
+
for j in 1..self.length
|
24
|
+
# critical comparison
|
25
|
+
dm[i][j] = [
|
26
|
+
dm[i-1][j-1] +
|
27
|
+
(self[j-1] == other[i-1] ? 0 : sub),
|
28
|
+
dm[i][j-1] + ins,
|
29
|
+
dm[i-1][j] + del
|
30
|
+
].min
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
# The last value in matrix is the
|
35
|
+
# Levenshtein distance between the strings
|
36
|
+
dm[other.length][self.length]
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
data/scripts/txt2html
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'redcloth'
|
5
|
+
require 'syntax/convertors/html'
|
6
|
+
require 'erb'
|
7
|
+
require File.dirname(__FILE__) + '/../lib/guessmethod/version.rb'
|
8
|
+
|
9
|
+
version = GuessMethod::VERSION::STRING
|
10
|
+
download = 'http://rubyforge.org/projects/guessmethod'
|
11
|
+
|
12
|
+
class Fixnum
|
13
|
+
def ordinal
|
14
|
+
# teens
|
15
|
+
return 'th' if (10..19).include?(self % 100)
|
16
|
+
# others
|
17
|
+
case self % 10
|
18
|
+
when 1: return 'st'
|
19
|
+
when 2: return 'nd'
|
20
|
+
when 3: return 'rd'
|
21
|
+
else return 'th'
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
class Time
|
27
|
+
def pretty
|
28
|
+
return "#{mday}#{mday.ordinal} #{strftime('%B')} #{year}"
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def convert_syntax(syntax, source)
|
33
|
+
return Syntax::Convertors::HTML.for_syntax(syntax).convert(source).gsub(%r!^<pre>|</pre>$!,'')
|
34
|
+
end
|
35
|
+
|
36
|
+
if ARGV.length >= 1
|
37
|
+
src, template = ARGV
|
38
|
+
template ||= File.dirname(__FILE__) + '/../website/template.rhtml'
|
39
|
+
|
40
|
+
else
|
41
|
+
puts("Usage: #{File.split($0).last} source.txt [template.rhtml] > output.html")
|
42
|
+
exit!
|
43
|
+
end
|
44
|
+
|
45
|
+
template = ERB.new(File.open(template).read)
|
46
|
+
|
47
|
+
title = nil
|
48
|
+
body = nil
|
49
|
+
File.open(src) do |fsrc|
|
50
|
+
title_text = fsrc.readline
|
51
|
+
body_text = fsrc.read
|
52
|
+
syntax_items = []
|
53
|
+
body_text.gsub!(%r!<(pre|code)[^>]*?syntax=['"]([^'"]+)[^>]*>(.*?)</>!m){
|
54
|
+
ident = syntax_items.length
|
55
|
+
element, syntax, source = $1, $2, $3
|
56
|
+
syntax_items << "<#{element} class='syntax'>#{convert_syntax(syntax, source)}</#{element}>"
|
57
|
+
"syntax-temp-#{ident}"
|
58
|
+
}
|
59
|
+
title = RedCloth.new(title_text).to_html.gsub(%r!<.*?>!,'').strip
|
60
|
+
body = RedCloth.new(body_text).to_html
|
61
|
+
body.gsub!(%r!(?:<pre><code>)?syntax-temp-(d+)(?:</code></pre>)?!){ syntax_items[$1.to_i] }
|
62
|
+
end
|
63
|
+
stat = File.stat(src)
|
64
|
+
created = stat.ctime
|
65
|
+
modified = stat.mtime
|
66
|
+
|
67
|
+
$stdout << template.result(binding)
|