Skip to content

Commit 1b604e0

Browse files
committed
Tweak logic
1 parent 56b8470 commit 1b604e0

File tree

2 files changed

+13
-13
lines changed

2 files changed

+13
-13
lines changed

lib/core_ext/string.rb

+5-10
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,11 @@ def malware?
4646
unl0ck3r h4ck m0ney 0day exploit expl0it).any? { |i| !!(self =~ /#{i}/i) }
4747
end
4848

49-
def near_match? other, percent=80
50-
min_length = [self.length, other.length].min
51-
52-
match_count = 0
53-
54-
min_length.times do |i|
55-
match_count += 1 if self[i] == other[i]
56-
end
57-
58-
(match_count.to_f / min_length) * 100 >= percent
49+
def near_match? other, threshold=0.8
50+
intersection = (self.chars & other.chars).uniq.length
51+
union = (self.chars | other.chars).uniq.length
52+
similarity = intersection.to_f / union
53+
similarity > threshold
5954
end
6055

6156
def translate_url

spec/repo_spec.rb

+8-3
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,14 @@
8888
end
8989

9090
it "is true when name and description are near matches" do
91-
repo.name = "MyProject91262"
92-
repo.description = "MyPorject91262"
93-
expect(repo).to be_malware
91+
[
92+
["MyProject91262", "MyPorject91262"],
93+
["-Projectus2", "Projectus2"]
94+
].each do |tuple|
95+
repo.name = tuple[0]
96+
repo.description = tuple[1]
97+
expect(repo).to be_malware
98+
end
9499
end
95100

96101
it "is false when name and description are not near matches" do

0 commit comments

Comments
 (0)