<commit_before>#
# RPMK - Ruby Pyramide Match Kernel Library
# Copyright (c) <NAME>, 2010
# http://github.com/cheind/rpmk
#

require './test/test_helper.rb'

class TestSimilarity < Test::Unit::TestCase
  include FeatureAssertions
  
  def test_similarity
    box = PMK::Box.new([0.0, 0.0], [1.0, 1.0])
    features = [[0.0, 0.0], [1.0, 1.0], [0.3, 0.7]]
    root = PMK::Subdivision.new(
      PMK::SubdivisionController.new(:nlevels => 3)
    ).generate(features, box)
    levels_root = PMK::PyramideView.new(:nlevels => 3, :ndims => 2).generate(root)
    
    assert_equal(features.length, PMK.similarity(levels_root, levels_root))
    assert_equal(features.length, PMK.similarity(levels_root, levels_root, :nlevels => 2))
    assert_equal(features.length, PMK.similarity(levels_root, levels_root, :nlevels => 1))
  end
  
  def test_normed_similarity
    box = PMK::Box.new([0.0, 0.0], [1.0, 1.0])
    features = [[0.0, 0.0], [1.0, 1.0], [0.3, 0.7]]
    root = PMK::Subdivision.new(
      PMK::SubdivisionController.new(:nlevels => 3)
    ).generate(features, box)
    levels_root = PMK::PyramideView.new(:nlevels => 3, :ndims => 2).generate(root)
    
    assert_equal(1.0, PMK.similarity_normed(levels_root, levels_root))
    assert_equal(1.0, PMK.similarity_normed(levels_root, levels_root, :nlevels => 2))
    assert_equal(1.0, PMK.similarity_normed(levels_root, levels_root, :nlevels => 1))
  end
  
  
end<commit_msg>added random test<commit_after>#
# RPMK - Ruby Pyramide Match Kernel Library
# Copyright (c) <NAME>, 2010
# http://github.com/cheind/rpmk
#

require './test/test_helper.rb'

class TestSimilarity < Test::Unit::TestCase
  include FeatureAssertions
  
  def test_similarity
    box = PMK::Box.new([0.0, 0.0], [1.0, 1.0])
    features = [[0.0, 0.0], [1.0, 1.0], [0.3, 0.7]]
    root = PMK::Subdivision.new(
      PMK::SubdivisionController.new(:nlevels => 3)
    ).generate(features, box)
    levels_root = PMK::PyramideView.new(:nlevels => 3, :ndims => 2).generate(root)
    
    assert_equal(features.length, PMK.similarity(levels_root, levels_root))
    assert_equal(features.length, PMK.similarity(levels_root, levels_root, :nlevels => 2))
    assert_equal(features.length, PMK.similarity(levels_root, levels_root, :nlevels => 1))
  end
  
  def test_normed_similarity
    box = PMK::Box.new([0.0, 0.0], [1.0, 1.0])
    features = [[0.0, 0.0], [1.0, 1.0], [0.3, 0.7]]
    root = PMK::Subdivision.new(
      PMK::SubdivisionController.new(:nlevels => 3)
    ).generate(features, box)
    levels_root = PMK::PyramideView.new(:nlevels => 3, :ndims => 2).generate(root)
    
    assert_equal(1.0, PMK.similarity_normed(levels_root, levels_root))
    assert_equal(1.0, PMK.similarity_normed(levels_root, levels_root, :nlevels => 2))
    assert_equal(1.0, PMK.similarity_normed(levels_root, levels_root, :nlevels => 1))
  end
  
  def test_normed_similarity_random
    n = 100
    d = 10
    levs = 10
    
    #box = PMK::Box.new([-0.5]*d, [1.5]*d)
    box = PMK::Box.new([-0.5]*d, [1.5]*d)
    box.lower.map! {|e| e - 0.5 + rand()*1.0}
    box.upper.map! {|e| e - 0.5 + rand()*1.0}
    
    subdiv = PMK::Subdivision.new(PMK::SubdivisionController.new(:nlevels => levs))
    pyr = PMK::PyramideView.new(:nlevels => levs, :ndims => d)
    
    srand(10)
    f_a = Array.new(n) {Array.new(d){rand()}}   
    f_b = f_a.map {|f| f.map {|e| e + rand()*0.001} }
    puts ""
    
    pv_a = pyr.generate(subdiv.generate(f_a, box))
    pv_b = pyr.generate(subdiv.generate(f_b, box))
    
    puts PMK.similarity_normed(pv_a, pv_b)
  end
  
  
end
{'fraction_non_alphanumeric': 0.17364428210171395, 'fraction_numerical': 0.041865692610283785, 'mean_word_length': 3.575835475578406, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 28, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7193927', 'n_tokens_mistral': 1549, 'n_tokens_neox': 1424, 'n_words': 251}