string_comparision 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/Gemfile +4 -0
- data/Rakefile +2 -0
- data/lib/string_comparision.rb +62 -0
- data/lib/string_comparision/version.rb +3 -0
- data/string_comparision.gemspec +22 -0
- metadata +60 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Rakefile
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
module StringComparision
|
2
|
+
def self.quality_test(expected_hash, obtained_hash)
|
3
|
+
qarray = []
|
4
|
+
expected_hash.each do |ekey, evalue|
|
5
|
+
obtained_hash.each do |okey, ovalue|
|
6
|
+
if ekey == okey
|
7
|
+
if evalue.is_a?(Fixnum) && ovalue.is_a?(Fixnum)
|
8
|
+
qarray << calc_distance_for_number(evalue, ovalue)
|
9
|
+
else
|
10
|
+
qarray << calc_distance(evalue, ovalue)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
final_percentage = qarray.inject{ |sum, el| sum + el }.to_f / qarray.size #return average percentage after calculating diatance of each output result of an unit
|
16
|
+
return final_percentage > 0 ? final_percentage.round(2):0
|
17
|
+
end
|
18
|
+
|
19
|
+
# check distance between strings ie expected_string and obtained_string
|
20
|
+
def self.calc_distance(expected,other, ins=3, del=3, sub=2)
|
21
|
+
# ins, del, sub are weighted costs
|
22
|
+
return nil if expected.nil?
|
23
|
+
return nil if other.nil?
|
24
|
+
dm = [] # distance matrix
|
25
|
+
|
26
|
+
# Initialize first row values
|
27
|
+
dm[0] = (0..expected.length).collect { |i| i * ins }
|
28
|
+
fill = [0] * (expected.length - 1)
|
29
|
+
|
30
|
+
# Initialize first column values
|
31
|
+
for i in 1..other.length
|
32
|
+
dm[i] = [i * del, fill.flatten]
|
33
|
+
end
|
34
|
+
|
35
|
+
# populate matrix
|
36
|
+
for i in 1..other.length
|
37
|
+
for j in 1..expected.length
|
38
|
+
# critical comparison
|
39
|
+
dm[i][j] = [
|
40
|
+
dm[i-1][j-1] +
|
41
|
+
(expected[j-1] == other[i-1] ? 0 : sub),
|
42
|
+
dm[i][j-1] + ins,
|
43
|
+
dm[i-1][j] + del
|
44
|
+
].min
|
45
|
+
end
|
46
|
+
end
|
47
|
+
# The last value in matrix is the
|
48
|
+
# Levenshtein distance between the strings
|
49
|
+
distance = dm[other.length][expected.length]
|
50
|
+
expected_length = (expected.length + 2).to_f
|
51
|
+
((1- distance/expected_length)*100) > 0 ? ((1- distance/expected_length)*100) :0
|
52
|
+
end
|
53
|
+
|
54
|
+
# check distance between numbers
|
55
|
+
|
56
|
+
def self.calc_distance_for_number(expected, other)
|
57
|
+
expected_number = expected.to_s.chars.to_a
|
58
|
+
other_number = other.to_s.chars.to_a
|
59
|
+
distance = expected_number.zip(other_number).reject{|x| x[0] != x[1]}.flatten.uniq
|
60
|
+
return((distance.length/expected_number.length.to_f)*100) > 0 ? ((distance.length/expected_number.length.to_f)*100) : 0
|
61
|
+
end
|
62
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "string_comparision/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "string_comparision"
|
7
|
+
s.version = StringComparision::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Chandra Mohan Thakur"]
|
10
|
+
s.email = ["chandra.thakur@sprout-technology.com"]
|
11
|
+
s.homepage = "http://www.cmthakur.co.cc"
|
12
|
+
s.summary = %q{string comparision gem provides you the facility of comparision between two string and returns how much % the string is matched.
|
13
|
+
Similiarly it provides the quality test for a obtained hash and expected hash.}
|
14
|
+
s.description = %q{Quality control is the main theme of this gem. It cam be implied where there is two hash one is what you expect and one is what you got}
|
15
|
+
|
16
|
+
s.rubyforge_project = "string_comparision"
|
17
|
+
|
18
|
+
s.files = `git ls-files`.split("\n")
|
19
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
20
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
21
|
+
s.require_paths = ["lib"]
|
22
|
+
end
|
metadata
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: string_comparision
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.0.1
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Chandra Mohan Thakur
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2011-06-21 00:00:00 Z
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: Quality control is the main theme of this gem. It cam be implied where there is two hash one is what you expect and one is what you got
|
17
|
+
email:
|
18
|
+
- chandra.thakur@sprout-technology.com
|
19
|
+
executables: []
|
20
|
+
|
21
|
+
extensions: []
|
22
|
+
|
23
|
+
extra_rdoc_files: []
|
24
|
+
|
25
|
+
files:
|
26
|
+
- .gitignore
|
27
|
+
- Gemfile
|
28
|
+
- Rakefile
|
29
|
+
- lib/string_comparision.rb
|
30
|
+
- lib/string_comparision/version.rb
|
31
|
+
- string_comparision.gemspec
|
32
|
+
homepage: http://www.cmthakur.co.cc
|
33
|
+
licenses: []
|
34
|
+
|
35
|
+
post_install_message:
|
36
|
+
rdoc_options: []
|
37
|
+
|
38
|
+
require_paths:
|
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
|
+
|
54
|
+
rubyforge_project: string_comparision
|
55
|
+
rubygems_version: 1.7.2
|
56
|
+
signing_key:
|
57
|
+
specification_version: 3
|
58
|
+
summary: string comparision gem provides you the facility of comparision between two string and returns how much % the string is matched. Similiarly it provides the quality test for a obtained hash and expected hash.
|
59
|
+
test_files: []
|
60
|
+
|