ispell 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +3 -0
- data/Manifest.txt +6 -0
- data/README.txt +85 -0
- data/Rakefile +14 -0
- data/lib/ispell.rb +127 -0
- data/test/test_ispell.rb +27 -0
- metadata +71 -0
data/History.txt
ADDED
data/Manifest.txt
ADDED
data/README.txt
ADDED
@@ -0,0 +1,85 @@
|
|
1
|
+
= ispell
|
2
|
+
|
3
|
+
* http://rubzilla.rubyforge.org/ispell
|
4
|
+
|
5
|
+
== DESCRIPTION:
|
6
|
+
|
7
|
+
This is a ruby interface to the once popular Ispell package.
|
8
|
+
Please keep in mind, that every instance forks an ispell process.
|
9
|
+
It was since then mostly superseeded by Aspell, but still remains quite
|
10
|
+
useful. Especially it has a good support for Russian using ru-ispell
|
11
|
+
dictionaries.
|
12
|
+
|
13
|
+
Ispell is a fast screen-oriented spelling checker that shows you your
|
14
|
+
errors in the context of the original file, and suggests possible
|
15
|
+
corrections when it can figure them out. Compared to UNIX spell, it
|
16
|
+
is faster and much easier to use. Ispell can also handle languages
|
17
|
+
other than English.
|
18
|
+
|
19
|
+
== FEATURES/PROBLEMS:
|
20
|
+
|
21
|
+
This library was modelled after the Perl ispell module.
|
22
|
+
However, no support for adding words or modifying the dictionary in any
|
23
|
+
way is provided at that stage.
|
24
|
+
|
25
|
+
|
26
|
+
== SYNOPSIS:
|
27
|
+
|
28
|
+
require 'ispell'
|
29
|
+
|
30
|
+
speller = Ispell.new('/usr/bin/ispell', 'english')
|
31
|
+
|
32
|
+
speller.suggest('mantisa') # => 'mantissa'
|
33
|
+
speller.suggest('cake') # => 'cake'
|
34
|
+
|
35
|
+
results = speller.spellcheck('hello wonderfull wordl nice 42 trouting')
|
36
|
+
puts "Misspelled words: #{results.collect {|w| w.original}.join(',')}"
|
37
|
+
results.each do |res|
|
38
|
+
case res.type
|
39
|
+
when :miss, :guess
|
40
|
+
puts "#{res.original} => #{res.guesses.join(',')}"
|
41
|
+
when :none
|
42
|
+
puts "#{res.original} wasn't found"
|
43
|
+
else
|
44
|
+
puts "#{res.type.to_s} type of result"
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
speller.destroy!
|
49
|
+
|
50
|
+
== REQUIREMENTS:
|
51
|
+
|
52
|
+
* ispell binary
|
53
|
+
|
54
|
+
You can get it from http://fmg-www.cs.ucla.edu/geoff/ispell.html
|
55
|
+
For Russian dictionaries visit
|
56
|
+
http://scon155.phys.msu.su/~swan/orthography.html
|
57
|
+
|
58
|
+
== INSTALL:
|
59
|
+
|
60
|
+
* sudo gem install ispell
|
61
|
+
|
62
|
+
== LICENSE:
|
63
|
+
|
64
|
+
(The MIT License)
|
65
|
+
|
66
|
+
Copyright (c) 2008 Roman Shterenzon
|
67
|
+
|
68
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
69
|
+
a copy of this software and associated documentation files (the
|
70
|
+
'Software'), to deal in the Software without restriction, including
|
71
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
72
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
73
|
+
permit persons to whom the Software is furnished to do so, subject to
|
74
|
+
the following conditions:
|
75
|
+
|
76
|
+
The above copyright notice and this permission notice shall be
|
77
|
+
included in all copies or substantial portions of the Software.
|
78
|
+
|
79
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
80
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
81
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
82
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
83
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
84
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
85
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
# -*- ruby -*-
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'hoe'
|
5
|
+
require './lib/ispell.rb'
|
6
|
+
|
7
|
+
Hoe.new('ispell', Ispell::VERSION) do |p|
|
8
|
+
p.developer('Roman Shterenzon', 'romanbsd@yahoo.com')
|
9
|
+
p.rubyforge_name = 'rubzilla'
|
10
|
+
p.remote_rdoc_dir = 'ispell'
|
11
|
+
p.name = 'ispell'
|
12
|
+
end
|
13
|
+
|
14
|
+
# vim: syntax=Ruby
|
data/lib/ispell.rb
ADDED
@@ -0,0 +1,127 @@
|
|
1
|
+
class Ispell
|
2
|
+
VERSION = '0.1.0'
|
3
|
+
|
4
|
+
class Result
|
5
|
+
TYPES = { '*' => :ok, '+' => :root, '&' => :miss,
|
6
|
+
'?' => :guess, '-' => :compound, '#' => :none }
|
7
|
+
attr_accessor :type, :root, :original, :offset, :count, :guesses
|
8
|
+
|
9
|
+
# Freezes all instance variables
|
10
|
+
def freeze!
|
11
|
+
instance_variables.each {|v| eval("#{v}.freeze")}
|
12
|
+
self
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
# Create new speller instance, optionally can specify the ispell binary
|
17
|
+
# and the desired language.
|
18
|
+
def initialize(ispell='/usr/bin/ispell', lang='english')
|
19
|
+
@ispell_bin, @lang = ispell, lang
|
20
|
+
@options = ['-a','-S',"-d #{lang}"]
|
21
|
+
@ispell = nil
|
22
|
+
start!
|
23
|
+
end
|
24
|
+
|
25
|
+
# Consider run-together words as legal compounds.
|
26
|
+
def allow_compounds!(bool = true)
|
27
|
+
if bool
|
28
|
+
@options.delete('-B')
|
29
|
+
@options.unshift('-C')
|
30
|
+
else
|
31
|
+
@options.delete('-C')
|
32
|
+
@options.unshift('-B')
|
33
|
+
end
|
34
|
+
restart!
|
35
|
+
end
|
36
|
+
|
37
|
+
# Make possible root/affix combinations that aren't in the dictionary.
|
38
|
+
def make_wild_guesses!(bool = true)
|
39
|
+
if bool
|
40
|
+
@options.delete('-P')
|
41
|
+
@options.unshift('-m')
|
42
|
+
else
|
43
|
+
@options.delete('-m')
|
44
|
+
@options.unshift('-P')
|
45
|
+
end
|
46
|
+
restart!
|
47
|
+
end
|
48
|
+
|
49
|
+
# Spell check the provided line, returns an +Array+ of +Ispell::Result+
|
50
|
+
def spellcheck(line)
|
51
|
+
line.gsub!(/\r/,'')
|
52
|
+
line.gsub!(/\s+/, ' ')
|
53
|
+
results = to_ispell(line)
|
54
|
+
results.collect do |line|
|
55
|
+
tokens = line.split(/,?:?\s+/)
|
56
|
+
res = Result.new
|
57
|
+
res.type = Result::TYPES[tokens.shift]
|
58
|
+
case res.type
|
59
|
+
when :root
|
60
|
+
res.root = tokens.shift
|
61
|
+
when :none
|
62
|
+
res.original = tokens.shift
|
63
|
+
res.offset = tokens.shift
|
64
|
+
when :miss, :guess
|
65
|
+
res.original = tokens.shift
|
66
|
+
res.count = tokens.shift
|
67
|
+
res.offset = tokens.shift
|
68
|
+
res.guesses = tokens
|
69
|
+
end
|
70
|
+
res.freeze!
|
71
|
+
end
|
72
|
+
end
|
73
|
+
alias :check :spellcheck
|
74
|
+
|
75
|
+
# Suggest a replacement for a given word, or the word itself if it's ok.
|
76
|
+
# Picks the first word from the suggestions list.
|
77
|
+
# If the word wasn't found, for leave_erroneous=true the original word
|
78
|
+
# is returned, for leave_erroneous=false an empty string is returned.
|
79
|
+
def suggest(word, leave_erroneous=false)
|
80
|
+
res = spellcheck(word).at(0)
|
81
|
+
return word unless res
|
82
|
+
case res.type
|
83
|
+
when :ok
|
84
|
+
return word
|
85
|
+
when :miss, :guess
|
86
|
+
return res.guesses.at(0)
|
87
|
+
when :none
|
88
|
+
return leave_erroneous ? word : ''
|
89
|
+
else
|
90
|
+
return ''
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
# Stop the ispell
|
95
|
+
def destroy!
|
96
|
+
@ispell.close
|
97
|
+
@ispell = nil
|
98
|
+
end
|
99
|
+
|
100
|
+
# Start the ispell
|
101
|
+
def start!
|
102
|
+
return if @ispell
|
103
|
+
@ispell = IO.popen("#{@ispell_bin} #{@options.join(' ')}", 'r+')
|
104
|
+
# Swallow the greeting line
|
105
|
+
@ispell.gets
|
106
|
+
# Turn on the terse mode
|
107
|
+
@ispell.puts('!')
|
108
|
+
end
|
109
|
+
|
110
|
+
# Restart the ispell
|
111
|
+
def restart!
|
112
|
+
destroy!
|
113
|
+
start!
|
114
|
+
end
|
115
|
+
|
116
|
+
private
|
117
|
+
# Send a line to ispell and return the result as array
|
118
|
+
def to_ispell(line)
|
119
|
+
raise "Ispell not running" unless @ispell
|
120
|
+
@ispell.puts(line)
|
121
|
+
lines = []
|
122
|
+
while ! (line = @ispell.gets).eql?("\n")
|
123
|
+
lines.push(line)
|
124
|
+
end
|
125
|
+
lines
|
126
|
+
end
|
127
|
+
end
|
data/test/test_ispell.rb
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require 'test/unit'
|
3
|
+
require 'ispell'
|
4
|
+
|
5
|
+
class TestIspell < Test::Unit::TestCase
|
6
|
+
def setup
|
7
|
+
@speller = Ispell.new
|
8
|
+
@domain = {
|
9
|
+
'cake' => 'cake',
|
10
|
+
'caskade' => 'cascade',
|
11
|
+
'mantisa' => 'mantissa',
|
12
|
+
'phisical' => 'physical'
|
13
|
+
}
|
14
|
+
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_suggest
|
18
|
+
@domain.each do |pair|
|
19
|
+
assert_equal(pair[1], @speller.suggest(pair[0]))
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_spellcheck
|
24
|
+
assert_equal(:miss, @speller.check('mantisa').at(0).type)
|
25
|
+
assert_nil(@speller.check('cake').at(0))
|
26
|
+
end
|
27
|
+
end
|
metadata
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ispell
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Roman Shterenzon
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2008-11-13 00:00:00 +02:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: hoe
|
17
|
+
type: :development
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 1.8.2
|
24
|
+
version:
|
25
|
+
description: This is a ruby interface to the once popular Ispell package. Please keep in mind, that every instance forks an ispell process. It was since then mostly superseeded by Aspell, but still remains quite useful. Especially it has a good support for Russian using ru-ispell dictionaries. Ispell is a fast screen-oriented spelling checker that shows you your errors in the context of the original file, and suggests possible corrections when it can figure them out. Compared to UNIX spell, it is faster and much easier to use. Ispell can also handle languages other than English.
|
26
|
+
email:
|
27
|
+
- romanbsd@yahoo.com
|
28
|
+
executables: []
|
29
|
+
|
30
|
+
extensions: []
|
31
|
+
|
32
|
+
extra_rdoc_files:
|
33
|
+
- History.txt
|
34
|
+
- Manifest.txt
|
35
|
+
- README.txt
|
36
|
+
files:
|
37
|
+
- History.txt
|
38
|
+
- Manifest.txt
|
39
|
+
- README.txt
|
40
|
+
- Rakefile
|
41
|
+
- lib/ispell.rb
|
42
|
+
- test/test_ispell.rb
|
43
|
+
has_rdoc: true
|
44
|
+
homepage: http://rubzilla.rubyforge.org/ispell
|
45
|
+
post_install_message:
|
46
|
+
rdoc_options:
|
47
|
+
- --main
|
48
|
+
- README.txt
|
49
|
+
require_paths:
|
50
|
+
- lib
|
51
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: "0"
|
56
|
+
version:
|
57
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: "0"
|
62
|
+
version:
|
63
|
+
requirements: []
|
64
|
+
|
65
|
+
rubyforge_project: rubzilla
|
66
|
+
rubygems_version: 1.2.0
|
67
|
+
signing_key:
|
68
|
+
specification_version: 2
|
69
|
+
summary: This is a ruby interface to the once popular Ispell package
|
70
|
+
test_files:
|
71
|
+
- test/test_ispell.rb
|