Skip to content

Commit

Permalink
Merge branch 'master' of git@github.com:threedaymonk/text
Browse files Browse the repository at this point in the history
  • Loading branch information
threedaymonk committed Mar 3, 2010
2 parents 427509f + 84f598c commit 6256fbd
Show file tree
Hide file tree
Showing 11 changed files with 1,294 additions and 1,250 deletions.
10 changes: 9 additions & 1 deletion README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

A collection of text algorithms.


= Usage

require 'text'
Expand All @@ -22,6 +21,15 @@ A collection of text algorithms.

Text::PorterStemming.stem('abatements') # => 'abat'

= Ruby 1.9 Compatibility

Most parts of the library are now compatible including
tests. The big exception are the Figlet libraries which
allow you to quickly do text-rendering in ASCII.
On 1.9, Figlet isn't loaded and isn't tested.

Work to integrate in Ruby 1.9 compatibility was done by
Hampton Catlin (hcatlin)

= License

Expand Down
25 changes: 24 additions & 1 deletion lib/text.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,30 @@
require 'text/double_metaphone'
require 'text/figlet'
require 'text/levenshtein'
require 'text/metaphone'
require 'text/porter_stemming'
require 'text/soundex'
require 'text/version'

module Text
def self.is_19?
RUBY_VERSION[0, 3] == "1.9"
end

def self.encoding_of(string)
if is_19?
string.encoding.to_s
else
$KCODE
end
end

def self.raise_19_incompat
if is_19?
raise "Text::Figlet is not compatible with Ruby 1.9 at this time"
end
end
end

if !Text.is_19?
require 'text/figlet'
end
2 changes: 1 addition & 1 deletion lib/text/double_metaphone.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def double_metaphone(str)
secondary << b if b
current += c if c
end
primary, secondary = primary.to_s[0, 4], secondary.to_s[0, 4]
primary, secondary = primary.join("")[0, 4], secondary.join("")[0, 4]
return primary, (primary == secondary ? nil : secondary)
end

Expand Down
4 changes: 3 additions & 1 deletion lib/text/figlet/font.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ class UnknownFontFormat < StandardError

class Font
def initialize(filename, load_german = true)
Text.raise_19_incompat

file = File.open(filename, 'rb')

header = file.gets.strip.split(/ /)

raise UnknownFontFormat if 'flf2a' != header[0][0, 5]
Expand Down
1 change: 1 addition & 0 deletions lib/text/figlet/smusher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module Figlet
class Smusher

def initialize(font)
Text.raise_19_incompat
@font = font
end

Expand Down
1 change: 1 addition & 0 deletions lib/text/figlet/typesetter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module Figlet
class Typesetter

def initialize(font, options = nil)
Text.raise_19_incompat
@font = font
@options = options || {}
@smush = @options.has_key?(:smush) ? @options[:smush] : true
Expand Down
5 changes: 4 additions & 1 deletion lib/text/levenshtein.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,14 @@ module Levenshtein
# should be performed beforehand.
#
def distance(str1, str2)
if $KCODE =~ /^U/i
encoding = defined?(Encoding) ? str1.encoding.to_s : $KCODE

if Text.encoding_of(str1) =~ /^U/i
unpack_rule = 'U*'
else
unpack_rule = 'C*'
end

s = str1.unpack(unpack_rule)
t = str2.unpack(unpack_rule)
n = s.length
Expand Down
Loading

0 comments on commit 6256fbd

Please sign in to comment.