<commit_before>require 'spec_helper'

describe Address do

  it "should create a basic address" do
    address = FactoryGirl.create(:address)

    expect(address.tags.count).to eq(5)
    expect(address.postcode.label).to eq('ABC 123')
    expect(address.town.label).to eq('The Shire')
    expect(address.locality.label).to eq('Hobbitton')
    expect(address.street.label).to eq('Hobbit Drive')
    expect(address.paon.label).to eq('3')
    expect(address.valid_at).to eq(DateTime.parse("2015-01-01"))
  end

  it "creates a score" do
    address = FactoryGirl.create(:address)

    expect(address.score).to_not be_nil
  end

  context "scoring" do

    before :each do
      Address.skip_callback(:create, :after, :generate_score)

      35.times do |n|
        FactoryGirl.create(:address, tags: [
          FactoryGirl.create(:tag, label: "SW1A 1AA", tag_type: FactoryGirl.create(:tag_type, label: "postcode")),
          FactoryGirl.create(:tag, label: "The Shire", tag_type: FactoryGirl.create(:tag_type, label: "town"), point: "POINT (309250 411754)"),
          FactoryGirl.create(:tag, label: "Hobbitton", tag_type: FactoryGirl.create(:tag_type, label: "locality")),
          FactoryGirl.create(:tag, label: "Hobbit Drive", tag_type: FactoryGirl.create(:tag_type, label: "street")),
          FactoryGirl.create(:tag, label: n, tag_type: FactoryGirl.create(:tag_type, label: "paon"))
        ])
      end

      Address.set_callback(:create, :after, :generate_score)

      @address = FactoryGirl.create(:address, tags: [
        FactoryGirl.create(:tag, label: "SW1A 1AA", tag_type: FactoryGirl.create(:tag_type, label: "postcode")),
        FactoryGirl.create(:tag, label: "The Shire", tag_type: FactoryGirl.create(:tag_type, label: "town"), point: "POINT (309250 411754)"),
        FactoryGirl.create(:tag, label: "Hobbitton", tag_type: FactoryGirl.create(:tag_type, label: "locality")),
        FactoryGirl.create(:tag, label: "Hobbit Drive", tag_type: FactoryGirl.create(:tag_type, label: "street")),
        FactoryGirl.create(:tag, label: 50, tag_type: FactoryGirl.create(:tag_type, label: "paon"))
      ])
    end

    it "creates the correct score" do
      expect(@address.score).to be_within(0.0001).of(503.9596)
    end

    it "heuristically adjusts" do
      Timecop.travel(Date.today + 15.years)
      @address.generate_score
      expect(@address.score).to be_within(2).of(503.9596 / 2) # We want it to be roughly half of the original score
    end<commit_msg>Probably a more sane way of checking heuristics<commit_after>require 'spec_helper'

describe Address do

  it "should create a basic address" do
    address = FactoryGirl.create(:address)

    expect(address.tags.count).to eq(5)
    expect(address.postcode.label).to eq('ABC 123')
    expect(address.town.label).to eq('The Shire')
    expect(address.locality.label).to eq('Hobbitton')
    expect(address.street.label).to eq('Hobbit Drive')
    expect(address.paon.label).to eq('3')
    expect(address.valid_at).to eq(DateTime.parse("2015-01-01"))
  end

  it "creates a score" do
    address = FactoryGirl.create(:address)

    expect(address.score).to_not be_nil
  end

  context "scoring" do

    before :each do
      Address.skip_callback(:create, :after, :generate_score)

      35.times do |n|
        FactoryGirl.create(:address, tags: [
          FactoryGirl.create(:tag, label: "SW1A 1AA", tag_type: FactoryGirl.create(:tag_type, label: "postcode")),
          FactoryGirl.create(:tag, label: "The Shire", tag_type: FactoryGirl.create(:tag_type, label: "town"), point: "POINT (309250 411754)"),
          FactoryGirl.create(:tag, label: "Hobbitton", tag_type: FactoryGirl.create(:tag_type, label: "locality")),
          FactoryGirl.create(:tag, label: "Hobbit Drive", tag_type: FactoryGirl.create(:tag_type, label: "street")),
          FactoryGirl.create(:tag, label: n, tag_type: FactoryGirl.create(:tag_type, label: "paon"))
        ])
      end

      Address.set_callback(:create, :after, :generate_score)

      @address = FactoryGirl.create(:address, tags: [
        FactoryGirl.create(:tag, label: "SW1A 1AA", tag_type: FactoryGirl.create(:tag_type, label: "postcode")),
        FactoryGirl.create(:tag, label: "The Shire", tag_type: FactoryGirl.create(:tag_type, label: "town"), point: "POINT (309250 411754)"),
        FactoryGirl.create(:tag, label: "Hobbitton", tag_type: FactoryGirl.create(:tag_type, label: "locality")),
        FactoryGirl.create(:tag, label: "Hobbit Drive", tag_type: FactoryGirl.create(:tag_type, label: "street")),
        FactoryGirl.create(:tag, label: 50, tag_type: FactoryGirl.create(:tag_type, label: "paon"))
      ])
    end

    it "creates the correct score" do
      expect(@address.score).to be_within(0.0001).of(503.9596)
    end

    it "heuristically adjusts" do
      original_score = @address.score
      Timecop.travel(Date.today + 15.years)
      @address.generate_score
      expect(@address.score).to be_within(3).of(original_score / 2) # We want it to be roughly half of the original score
      Timecop.return
    end
{'fraction_non_alphanumeric': 0.14302623791674887, 'fraction_numerical': 0.025448806470704282, 'mean_word_length': 3.889103182256509, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '636372', 'n_tokens_mistral': 1787, 'n_tokens_neox': 1608, 'n_words': 367}