Module:JI ratios: Difference between revisions
No edit summary |
No edit summary |
||
| Line 14: | Line 14: | ||
-- 128 -> ~2500 ratios | -- 128 -> ~2500 ratios | ||
-- 100 -> ~1500 ratios | -- 100 -> ~1500 ratios | ||
local SEARCH_MAX = | local SEARCH_MAX = 150 | ||
local DEFAULT_INT_LIMIT = 50 | |||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
| Line 25: | Line 27: | ||
function p.search_by_int_limit(integer_limit, max_cents) | function p.search_by_int_limit(integer_limit, max_cents) | ||
local max_cents = max_cents or 1200 | local max_cents = max_cents or 1200 | ||
local integer_limit = integer_limit or | local integer_limit = integer_limit or DEFAULT_INT_LIMIT | ||
integer_limit = math.max(0, math.min(SEARCH_MAX, integer_limit)) | integer_limit = math.max(0, math.min(SEARCH_MAX, integer_limit)) | ||
| Line 89: | Line 91: | ||
end | end | ||
---- | -- Search for ratios based on params passed in. Each param is its own | ||
-- function call. Params must be parsed first. | |||
-- | function p.search_by_params(params, max_cents) | ||
local max_cents = max_cents or 1200 | |||
-- | |||
-- First get ratios up to an int limit. If no int limit was passed in, it | |||
-- will default to the hardcoded default value. | |||
local ratios = p.search_by_int_limit(params["Int Limit"], max_cents) | |||
if params["Prime Limit"] ~= nil then | |||
ratios = p.filter_by_prime_limit(ratios, params["Prime Limit"]) | |||
end | end | ||
if params["Tenney Height"] ~= nil then | |||
ratios = p.filter_by_tenney_height(ratios, params["Tenney Height"]) | |||
ratios = p.filter_by_tenney_height(ratios, | |||
end | end | ||
return ratios | |||
end | end | ||
| Line 143: | Line 114: | ||
function p.parse_search_params(search_params) | function p.parse_search_params(search_params) | ||
local parsed = tip.parse_kv_pairs(search_params) | local parsed = tip.parse_kv_pairs(search_params) | ||
if parsed["Tenney Height"] ~= nil then | |||
parsed["Tenney Height"] = tonumber(parsed["Tenney Height"]) | |||
end | |||
if parsed["Tenney Height"] ~= nil then | if parsed["Tenney Height"] ~= nil then | ||
| Line 196: | Line 171: | ||
function p.filter_by_nonprime_subgroup(ratios, subgroup) | function p.filter_by_nonprime_subgroup(ratios, subgroup) | ||
end | |||
-------------------------------------------------------------------------------- | |||
--------------------------- RATIO SORTING FUNCTIONS ---------------------------- | |||
-------------------------------------------------------------------------------- | |||
-- Sorts ratios by closeness to cent values. | |||
function p.sort_by_closeness_to_cent_values(ratios, cent_values, tolerance) | |||
local sorted_ratios = {} | |||
local curr_index = 1 -- Index of current_ratio | |||
for i = 1, #cent_values do | |||
local lower_bound = cent_values[i] - tolerance | |||
local upper_bound = cent_values[i] + tolerance | |||
local cents_within_range = true | |||
local curr_ratios = {} | |||
for j = curr_index, #ratios do | |||
local curr_ratio = ratios[j] | |||
local curr_cents = rat.cents(curr_ratio) | |||
if lower_bound < curr_cents and curr_cents < upper_bound then | |||
table.insert(curr_ratios, curr_ratio) | |||
elseif curr_cents > upper_bound then | |||
curr_index = j | |||
break | |||
end | |||
end | |||
table.insert(sorted_ratios, curr_ratios) | |||
end | |||
return sorted_ratios | |||
end | end | ||
| Line 207: | Line 215: | ||
local delimiter = delimiter or ", " | local delimiter = delimiter or ", " | ||
local text = add_links and string.format("[[%s]]", rat.as_ratio(ratios[1])) or rat.as_ratio(ratios[1]) | local text = "" | ||
if #ratios ~= 0 then | |||
text = add_links and string.format("[[%s]]", rat.as_ratio(ratios[1])) or rat.as_ratio(ratios[1]) | |||
for i = 2, #ratios do | |||
text = text .. (add_links and string.format("%s[[%s]]", delimiter, rat.as_ratio(ratios[i])) or string.format("%s%s", delimiter, rat.as_ratio(ratios[i]))) | |||
end | |||
end | end | ||
return text | return text | ||
| Line 228: | Line 239: | ||
function p.tester() | function p.tester() | ||
local | local params = p.parse_search_params("Prime Limit: 7; Tenney Height: 11") | ||
ratios = p. | ratios = p.search_by_params(params) | ||
ratios = p.sort_by_closeness_to_cent_values(ratios, {0, 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000, 1100, 1200}, 15) | ratios = p.sort_by_closeness_to_cent_values(ratios, {0, 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000, 1100, 1200}, 15) | ||