Created
April 15, 2012 06:12
-
-
Save ridget/2390366 to your computer and use it in GitHub Desktop.
testing for keywords
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Keywords | |
MAX_KEYWORDS = 8 | |
def keywords_to_array(keywords) | |
@keywords = keywords | |
end | |
def assign_values(keywords) | |
keyword_values = Hash.new | |
MAX_KEYWORDS.downto(1){ |value, init=8| | |
keyword_assigned = keywords[init-value] | |
unless keyword_assigned == nil | |
keyword_values[keyword_assigned] = value | |
end | |
} | |
keyword_values | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Pagesorter | |
class Page | |
include Keywords | |
attr_accessor :keywords, :keyword_values | |
def initialize(keywords) | |
@keywords = keywords_to_array(keywords) | |
@keyword_values = assign_values(keywords) | |
end | |
end | |
end | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Pagesorter | |
class Sorter | |
def initialize | |
end | |
# TODO expand on interface | |
# Ideally options to create new pages/keyword matches | |
# run through sample data provided | |
def page_keyword_input(keywords) | |
@page = Page.new(keywords) | |
end | |
def query_keyword_input(keywords) | |
@query = Query.new(keywords) | |
end | |
def filter_uniques(array1,array2) | |
# Use set to grab elements in both arrays but remove duplicates and uniques | |
# gives keyword set | |
array1 & array2 | |
end | |
def calculate_value_for(page, query) | |
keyword_set = filter_uniques(page.keywords,query.keywords) | |
keyword_value_set = keyword_set.collect { |value| page.keyword_values[value] * query.keyword_values[value] } | |
ranking_score = keyword_value_set.inject(0) {|sum,item| sum + item} | |
end | |
def top_pages_for_query(pages,query) | |
top_pages = pages.collect { |page| | |
calculate_value_for(page,query) | |
} | |
top_pages.sort! | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "spec_helper" | |
module Pagesorter | |
describe Sorter do | |
let(:sorter) { Sorter.new() } | |
let(:keywords) { ["test","foo", "news"] } | |
let(:excess_keywords){["test","foo","news","woot"]} | |
let(:page) { page = sorter.page_keyword_input(keywords)} | |
let(:query) { query = sorter.query_keyword_input(excess_keywords) } | |
describe "#page_keyword_input" do | |
it "should apply the keywords to a Page" do | |
page = sorter.page_keyword_input(keywords) | |
keywords.each do |keyword| | |
page.keywords.should include(keyword) | |
end | |
end | |
end | |
describe "#query_keyword_input" do | |
it "should apply the keywords to a query" do | |
query = sorter.query_keyword_input(excess_keywords) | |
excess_keywords.each do |keyword| | |
query.keywords.should include(keyword) | |
end | |
end | |
end | |
describe "#filter_uniques" do | |
it "should remove excess keywords from array" do | |
filtered_array = sorter.filter_uniques(page.keyword_values.keys,query.keyword_values.keys) | |
filtered_array.should_not include("woot") | |
keywords.each { |keyword| | |
filtered_array.should include(keyword) | |
} | |
end | |
end | |
describe "#calculate_value" do | |
it "should add the values from query and page keywords" do | |
values = sorter.calculate_value_for(page, query) | |
values.should === 149 | |
end | |
end | |
describe "#top_pages_for_query" do | |
it "should store all the pages values in a sorted array" do | |
page2 = sorter.page_keyword_input(excess_keywords) | |
result = sorter.top_pages_for_query([page,page2],query) | |
result.should include(149) | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment