similar_text 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,6 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
5
+ tmp/*
6
+ *.so
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in similar_text.gemspec
4
+ gemspec
@@ -0,0 +1,24 @@
1
+ Ruby similar_text
2
+ =================
3
+
4
+ Port of PHP similar_text function to Ruby, built as a native extension.
5
+
6
+ INSTALL
7
+ =======
8
+
9
+ You can install this extension via gems:
10
+
11
+ $ sudo gem install similar_text
12
+
13
+ USAGE
14
+ =====
15
+
16
+ Load extension:
17
+
18
+ require 'similar_text'
19
+
20
+ And use it by calling one of two methods (similar or similar?):
21
+
22
+ "Hello, World!".similar("Hello World!") # 96.0
23
+ "Hello, World!".similar?(Hello World!") # false
24
+
@@ -0,0 +1,27 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rubygems'
3
+ require 'rake'
4
+ require 'rake/extensiontask'
5
+ require 'rake/testtask'
6
+
7
+ task :default => [:test]
8
+
9
+ desc "Run tests"
10
+ Rake::TestTask.new("test") do |t|
11
+ t.pattern = 'test/*_test.rb'
12
+ t.libs << "test"
13
+ t.verbose = true
14
+ t.warning = true
15
+ end
16
+
17
+ Rake::ExtensionTask.new("similar_text") do |extension|
18
+ extension.lib_dir = "lib/similar_text"
19
+ end
20
+
21
+ task :chmod do
22
+ File.chmod(0775, 'lib/similar_text/similar_text.so')
23
+ end
24
+ task :build => [:clean, :compile, :chmod]
25
+
26
+ Bundler::GemHelper.install_tasks
27
+
@@ -0,0 +1,5 @@
1
+ require 'mkmf'
2
+
3
+ dir_config("similar_text")
4
+
5
+ create_makefile("similar_text")
@@ -0,0 +1,87 @@
1
+ #include <ruby.h>
2
+ #include <string.h>
3
+
4
+ static void similar_text_similar_str(const char *txt1, int len1, const char *txt2, int len2, int *pos1, int *pos2, int *max)
5
+ {
6
+ char *p, *q;
7
+ char *end1 = (char *) txt1 + len1;
8
+ char *end2 = (char *) txt2 + len2;
9
+ int l;
10
+
11
+ *max = 0;
12
+ for (p = (char *) txt1; p < end1; p++) {
13
+ for (q = (char *) txt2; q < end2; q++) {
14
+ for (l = 0; (p + l < end1) && (q + l < end2) && (p[l] == q[l]); l++);
15
+ if (l > *max) {
16
+ *max = l;
17
+ *pos1 = p - txt1;
18
+ *pos2 = q - txt2;
19
+ }
20
+ }
21
+ }
22
+ }
23
+
24
+ static int similar_text_similar_char(const char *txt1, int len1, const char *txt2, int len2)
25
+ {
26
+ int sum;
27
+ int pos1, pos2, max;
28
+
29
+ similar_text_similar_str(txt1, len1, txt2, len2, &pos1, &pos2, &max);
30
+ if ((sum = max)) {
31
+ if (pos1 && pos2) {
32
+ sum += similar_text_similar_char(txt1, pos1,
33
+ txt2, pos2);
34
+ }
35
+ if ((pos1 + max < len1) && (pos2 + max < len2)) {
36
+ sum += similar_text_similar_char(txt1 + pos1 + max, len1 - pos1 - max,
37
+ txt2 + pos2 + max, len2 - pos2 - max);
38
+ }
39
+ }
40
+
41
+ return sum;
42
+ }
43
+
44
+ int similar_text(const char *txt1, const char *txt2, double *percent)
45
+ {
46
+ size_t t1_len, t2_len;
47
+ int sim;
48
+
49
+ t1_len = strlen(txt1);
50
+ t2_len = strlen(txt2);
51
+
52
+ sim = similar_text_similar_char(txt1, t1_len, txt2, t2_len);
53
+ *percent = sim * 200.0 / (t1_len + t2_len);
54
+
55
+ return sim;
56
+ }
57
+
58
+ static VALUE t_similar(VALUE str1, VALUE str2)
59
+ {
60
+ double percent;
61
+
62
+ similar_text(StringValueCStr(str1), StringValueCStr(str2), &percent);
63
+
64
+ return rb_float_new(percent);
65
+ }
66
+
67
+ static VALUE t_similar_bool(VALUE str1, VALUE str2)
68
+ {
69
+ double percent;
70
+
71
+ similar_text(StringValueCStr(str1), StringValueCStr(str2), &percent);
72
+
73
+ if (percent == 100.0) {
74
+ return Qtrue;
75
+ }
76
+ else {
77
+ return Qfalse;
78
+ }
79
+ }
80
+
81
+ void Init_similar_text()
82
+ {
83
+ rb_cString = rb_define_class("String", rb_cObject);
84
+ rb_define_method(rb_cString, "similar", t_similar, 1);
85
+ rb_define_method(rb_cString, "similar?", t_similar_bool, 1);
86
+ }
87
+
@@ -0,0 +1,8 @@
1
+ $:.unshift File.dirname(__FILE__) + "/../lib/similar_text"
2
+ require "similar_text/version"
3
+ require 'similar_text.so'
4
+
5
+ module SimilarText
6
+ # Your code goes here...
7
+ end
8
+
@@ -0,0 +1,3 @@
1
+ module SimilarText
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "similar_text/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "similar_text"
7
+ s.version = SimilarText::VERSION
8
+ s.authors = ["Arthur Murauskas"]
9
+ s.email = ["arthur.murauskas@gmail.com"]
10
+ s.homepage = ""
11
+ s.summary = %q{Analogue of the similar_text function in PHP}
12
+ s.description = %q{Analogue of the similar_text function in PHP}
13
+
14
+ s.rubyforge_project = "similar_text"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths << "lib"
20
+ s.extensions << "ext/similar_text/extconf.rb"
21
+
22
+ # specify any dependencies here; for example:
23
+ # s.add_development_dependency "rspec"
24
+ # s.add_runtime_dependency "rest-client"
25
+ end
@@ -0,0 +1,14 @@
1
+ require 'test_init.rb'
2
+
3
+ class SimilarTextTest < Test::Unit::TestCase
4
+ def test_similar
5
+ assert_equal(100.0, "Hello, World!".similar("Hello, World!"))
6
+ assert_equal(96.0, "Hello, World!".similar("Hello World!"))
7
+ end
8
+
9
+ def test_similar?
10
+ assert_equal(true, "Hello, World!".similar?("Hello, World!"))
11
+ assert_equal(false, "Hello WORLD!".similar?("Hello, World!"))
12
+ end
13
+ end
14
+
@@ -0,0 +1,3 @@
1
+ require 'test/unit'
2
+ require File.dirname(__FILE__) + '/../lib/similar_text'
3
+
metadata ADDED
@@ -0,0 +1,58 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: similar_text
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Arthur Murauskas
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-09-12 00:00:00.000000000Z
13
+ dependencies: []
14
+ description: Analogue of the similar_text function in PHP
15
+ email:
16
+ - arthur.murauskas@gmail.com
17
+ executables: []
18
+ extensions:
19
+ - ext/similar_text/extconf.rb
20
+ extra_rdoc_files: []
21
+ files:
22
+ - .gitignore
23
+ - Gemfile
24
+ - README.markdown
25
+ - Rakefile
26
+ - ext/similar_text/extconf.rb
27
+ - ext/similar_text/similar_text.c
28
+ - lib/similar_text.rb
29
+ - lib/similar_text/version.rb
30
+ - similar_text.gemspec
31
+ - test/similar_text_test.rb
32
+ - test/test_init.rb
33
+ homepage: ''
34
+ licenses: []
35
+ post_install_message:
36
+ rdoc_options: []
37
+ require_paths:
38
+ - lib
39
+ - lib
40
+ required_ruby_version: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ required_rubygems_version: !ruby/object:Gem::Requirement
47
+ none: false
48
+ requirements:
49
+ - - ! '>='
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ requirements: []
53
+ rubyforge_project: similar_text
54
+ rubygems_version: 1.8.6
55
+ signing_key:
56
+ specification_version: 3
57
+ summary: Analogue of the similar_text function in PHP
58
+ test_files: []