whitesimilarity 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.
- checksums.yaml +7 -0
- data/.gitignore +18 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +29 -0
- data/Rakefile +1 -0
- data/ext/whitesimilarity/extconf.rb +5 -0
- data/ext/whitesimilarity/white_similarity.c +133 -0
- data/ext/whitesimilarity/white_similarity.h +4 -0
- data/ext/whitesimilarity/whitesimilarity.c +30 -0
- data/lib/whitesimilarity/version.rb +3 -0
- data/whitesimilarity.gemspec +24 -0
- metadata +84 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: f8541649d68a1eb5b9c3a2c6cf124b48bac8208e
|
4
|
+
data.tar.gz: fbcd07629cc9fa1fabb3e972230aba7a307b823f
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 7b476b7d3721dbed81ee2faf7e536362f932d93150f02b5344e2c20ccad4de20cd686f01aedbb8d5372bc747eb3475cb957a628be995872421f8b8a543855696
|
7
|
+
data.tar.gz: 8a7271acc8e094628483db963ca7c75be8c18c1e03371f0454b2643995b8a8e5742f187ea51588156e91a6dc2ad47be7091d16985d23408369a18051dae05594
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Jeremy Fairbank
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# WhiteSimilarity
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'whitesimilarity'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install whitesimilarity
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'bundler/gem_tasks'
|
@@ -0,0 +1,133 @@
|
|
1
|
+
#include <ctype.h>
|
2
|
+
#include <stdlib.h>
|
3
|
+
#include <string.h>
|
4
|
+
#include <stdio.h>
|
5
|
+
#include "white_similarity.h"
|
6
|
+
|
7
|
+
#define _empty_char(c) (c == ' ')
|
8
|
+
#define _null_char(c) (c == '\0')
|
9
|
+
#define will_make_bad_pair(str, i) \
|
10
|
+
(_empty_char(*(str + i)) || _empty_char(*(str + i + 1)) || _null_char(*(str + i + 1)))
|
11
|
+
|
12
|
+
#define pairs_equal(a, b) (strcmp(a, b) == 0)
|
13
|
+
|
14
|
+
// Free up a pair
|
15
|
+
void destroy_pair(char *pair) {
|
16
|
+
if (pair != NULL) {
|
17
|
+
free(pair);
|
18
|
+
}
|
19
|
+
}
|
20
|
+
|
21
|
+
// Remove a pair struct from a pairs struct at the index p
|
22
|
+
void remove_pair_at(int num_pairs, char **pairs, int p) {
|
23
|
+
int i = 0;
|
24
|
+
|
25
|
+
// Free up the pair
|
26
|
+
destroy_pair(pairs[p]);
|
27
|
+
pairs[p] = NULL;
|
28
|
+
|
29
|
+
// Reassign positions of other pairs
|
30
|
+
for (i = p; i < num_pairs; i++) {
|
31
|
+
pairs[i] = pairs[i + 1];
|
32
|
+
}
|
33
|
+
}
|
34
|
+
|
35
|
+
// Get the total number of pairs in string
|
36
|
+
int get_num_pairs(char *str) {
|
37
|
+
int i = 0;
|
38
|
+
int num_pairs = 0;
|
39
|
+
|
40
|
+
while (*(str + i) != '\0') {
|
41
|
+
if (!will_make_bad_pair(str, i)) {
|
42
|
+
num_pairs++;
|
43
|
+
}
|
44
|
+
|
45
|
+
i++;
|
46
|
+
}
|
47
|
+
|
48
|
+
return num_pairs;
|
49
|
+
}
|
50
|
+
|
51
|
+
// Create the pairs from the string
|
52
|
+
char **letter_pairs(int num_pairs, char *str) {
|
53
|
+
int i = 0;
|
54
|
+
int l = strlen(str);
|
55
|
+
int counter = 0;
|
56
|
+
char **pairs = calloc(num_pairs, sizeof(char *));
|
57
|
+
char *pair = NULL;
|
58
|
+
|
59
|
+
for (i = 0; i < l; i++) {
|
60
|
+
if (will_make_bad_pair(str, i)) {
|
61
|
+
continue;
|
62
|
+
}
|
63
|
+
|
64
|
+
// Create and add pair
|
65
|
+
pair = calloc(3, sizeof(char));
|
66
|
+
strncpy(pair, str + i, 2);
|
67
|
+
pair[0] = toupper(pair[0]);
|
68
|
+
pair[1] = toupper(pair[1]);
|
69
|
+
pairs[counter] = pair;
|
70
|
+
|
71
|
+
// Increment the counter for adding the pair to the pairs struct
|
72
|
+
counter++;
|
73
|
+
}
|
74
|
+
|
75
|
+
return pairs;
|
76
|
+
}
|
77
|
+
|
78
|
+
void destroy_letter_pairs(int num_pairs, char **pairs) {
|
79
|
+
int i = 0;
|
80
|
+
|
81
|
+
// Free each pair
|
82
|
+
for (i = 0; i < num_pairs; i++) {
|
83
|
+
destroy_pair(pairs[i]);
|
84
|
+
}
|
85
|
+
|
86
|
+
// Free the array
|
87
|
+
free(pairs);
|
88
|
+
}
|
89
|
+
|
90
|
+
double white_similarity(char *str1, char *str2) {
|
91
|
+
int num_pairs1 = get_num_pairs(str1);
|
92
|
+
int num_pairs2 = get_num_pairs(str2);
|
93
|
+
|
94
|
+
char **pairs1 = letter_pairs(num_pairs1, str1);
|
95
|
+
char **pairs2 = letter_pairs(num_pairs2, str2);
|
96
|
+
|
97
|
+
int i = 0;
|
98
|
+
int j = 0;
|
99
|
+
int intersection = 0;
|
100
|
+
int sum = num_pairs1 + num_pairs2;
|
101
|
+
|
102
|
+
for (i = 0; i < num_pairs1; i++) {
|
103
|
+
for (j = 0; j < num_pairs2; j++) {
|
104
|
+
if (pairs_equal(pairs1[i], pairs2[j])) {
|
105
|
+
intersection++;
|
106
|
+
|
107
|
+
// Removing pair, so make sure to decrement `num_pairs2`
|
108
|
+
num_pairs2--;
|
109
|
+
remove_pair_at(num_pairs2, pairs2, j);
|
110
|
+
break;
|
111
|
+
}
|
112
|
+
}
|
113
|
+
}
|
114
|
+
|
115
|
+
destroy_letter_pairs(num_pairs1, pairs1);
|
116
|
+
destroy_letter_pairs(num_pairs2, pairs2);
|
117
|
+
|
118
|
+
return (2.0 * intersection) / sum;
|
119
|
+
}
|
120
|
+
|
121
|
+
int main(int argc, char *argv[]) {
|
122
|
+
double d;
|
123
|
+
|
124
|
+
if (argc < 3) {
|
125
|
+
printf("Please supply two strings to compare.\n");
|
126
|
+
return 1;
|
127
|
+
}
|
128
|
+
|
129
|
+
d = white_similarity(argv[1], argv[2]);
|
130
|
+
printf("White Similarity for '%s' and '%s' is %f\n", argv[1], argv[2], d);
|
131
|
+
|
132
|
+
return 0;
|
133
|
+
}
|
@@ -0,0 +1,30 @@
|
|
1
|
+
#include "ruby.h"
|
2
|
+
#include "white_similarity.h"
|
3
|
+
|
4
|
+
// Ruby stuff
|
5
|
+
// ----------
|
6
|
+
|
7
|
+
// Contracts
|
8
|
+
VALUE WhiteSimilarity = Qnil;
|
9
|
+
void Init_whitesimilarity();
|
10
|
+
VALUE method_white_similarity(VALUE self, VALUE str1, VALUE str2);
|
11
|
+
|
12
|
+
// Defs
|
13
|
+
void Init_whitesimilarity() {
|
14
|
+
WhiteSimilarity = rb_define_module("WhiteSimilarity");
|
15
|
+
rb_define_singleton_method(WhiteSimilarity, "similarity", method_white_similarity, 2);
|
16
|
+
|
17
|
+
rb_require("whitesimilarity/version");
|
18
|
+
}
|
19
|
+
|
20
|
+
VALUE method_white_similarity(VALUE self, VALUE rstr1, VALUE rstr2) {
|
21
|
+
char *str1;
|
22
|
+
char *str2;
|
23
|
+
double d;
|
24
|
+
|
25
|
+
str1 = StringValueCStr(rstr1);
|
26
|
+
str2 = StringValueCStr(rstr2);
|
27
|
+
|
28
|
+
d = white_similarity(str1, str2);
|
29
|
+
return rb_float_new(d);
|
30
|
+
}
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'whitesimilarity/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'whitesimilarity'
|
8
|
+
spec.version = WhiteSimilarity::VERSION
|
9
|
+
spec.authors = ['Jeremy Fairbank']
|
10
|
+
spec.email = ['elpapapollo@gmail.com']
|
11
|
+
spec.description = %q{An implementation of the White Similarity Algorithm in C}
|
12
|
+
spec.summary = %q{White Similarity Algorithm}
|
13
|
+
spec.homepage = 'https://github.com/jfairbank/whitesimilarity'
|
14
|
+
spec.license = 'MIT'
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ['lib']
|
20
|
+
spec.extensions = ['ext/whitesimilarity/extconf.rb']
|
21
|
+
|
22
|
+
spec.add_development_dependency 'bundler', '~> 1.3'
|
23
|
+
spec.add_development_dependency 'rake'
|
24
|
+
end
|
metadata
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: whitesimilarity
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jeremy Fairbank
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-01-14 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.3'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
description: An implementation of the White Similarity Algorithm in C
|
42
|
+
email:
|
43
|
+
- elpapapollo@gmail.com
|
44
|
+
executables: []
|
45
|
+
extensions:
|
46
|
+
- ext/whitesimilarity/extconf.rb
|
47
|
+
extra_rdoc_files: []
|
48
|
+
files:
|
49
|
+
- ".gitignore"
|
50
|
+
- Gemfile
|
51
|
+
- LICENSE.txt
|
52
|
+
- README.md
|
53
|
+
- Rakefile
|
54
|
+
- ext/whitesimilarity/extconf.rb
|
55
|
+
- ext/whitesimilarity/white_similarity.c
|
56
|
+
- ext/whitesimilarity/white_similarity.h
|
57
|
+
- ext/whitesimilarity/whitesimilarity.c
|
58
|
+
- lib/whitesimilarity/version.rb
|
59
|
+
- whitesimilarity.gemspec
|
60
|
+
homepage: https://github.com/jfairbank/whitesimilarity
|
61
|
+
licenses:
|
62
|
+
- MIT
|
63
|
+
metadata: {}
|
64
|
+
post_install_message:
|
65
|
+
rdoc_options: []
|
66
|
+
require_paths:
|
67
|
+
- lib
|
68
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
69
|
+
requirements:
|
70
|
+
- - ">="
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
version: '0'
|
73
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
74
|
+
requirements:
|
75
|
+
- - ">="
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
requirements: []
|
79
|
+
rubyforge_project:
|
80
|
+
rubygems_version: 2.1.11
|
81
|
+
signing_key:
|
82
|
+
specification_version: 4
|
83
|
+
summary: White Similarity Algorithm
|
84
|
+
test_files: []
|