Module: BBLib
- Defined in:
- lib/bblib.rb,
lib/os/bbos.rb,
lib/os/bbsys.rb,
lib/gem/bbgem.rb,
lib/time/cron.rb,
lib/file/bbfile.rb,
lib/opal/bbopal.rb,
lib/time/bbtime.rb,
lib/string/cases.rb,
lib/string/roman.rb,
lib/array/bbarray.rb,
lib/bblib/version.rb,
lib/hash/hash_path.rb,
lib/hash/path_hash.rb,
lib/number/bbnumber.rb,
lib/object/bbobject.rb,
lib/string/bbstring.rb,
lib/string/matching.rb,
lib/time/task_timer.rb,
lib/object/lazy_class.rb,
lib/hash/hash_path_proc.rb,
lib/string/fuzzy_matcher.rb
Overview
String Comparison Algorithms
Defined Under Namespace
Modules: Attr, HashPath, Hooks, OS Classes: Cron, FuzzyMatcher, LazyClass, PathHash, TaskTimer
Constant Summary collapse
- CONFIGS_PATH =
'config/'- FILE_SIZES =
{ byte: { mult: 1, exp: ['b', 'byt', 'byte'] }, kilobyte: { mult: 1024, exp: ['kb', 'kilo', 'k', 'kbyte', 'kilobyte'] }, megabyte: { mult: 1048576, exp: ['mb', 'mega', 'm', 'mib', 'mbyte', 'megabyte'] }, gigabyte: { mult: 1073741824, exp: ['gb', 'giga', 'g', 'gbyte', 'gigabyte'] }, terabyte: { mult: 1099511627776, exp: ['tb', 'tera', 't', 'tbyte', 'terabyte'] }, petabyte: { mult: 1125899906842624, exp: ['pb', 'peta', 'p', 'pbyte', 'petabyte'] }, exabyte: { mult: 1152921504606846976, exp: ['eb', 'exa', 'e', 'ebyte', 'exabyte'] }, zettabyte: { mult: 1180591620717411303424, exp: ['zb', 'zetta', 'z', 'zbyte', 'zettabyte'] }, yottabyte: { mult: 1208925819614629174706176, exp: ['yb', 'yotta', 'y', 'ybyte', 'yottabyte'] } }
- TIME_EXPS =
{ yocto: { mult: 0.000000000000000000001, styles: {full: ' yoctosecond', medium: ' yocto', short: 'ys'}, exp: ['yoctosecond', 'yocto', 'yoctoseconds', 'yoctos', 'ys'] }, zepto: { mult: 0.000000000000000001, styles: {full: ' zeptosecond', medium: ' zepto', short: 'zs'}, exp: ['zeptosecond', 'zepto', 'zeptoseconds', 'zeptos', 'zs'] }, atto: { mult: 0.000000000000001, styles: {full: ' attosecond', medium: ' atto', short: 'as'}, exp: ['attoseconds', 'atto', 'attoseconds', 'attos', 'as'] }, femto: { mult: 0.000000000001, styles: {full: ' femtosecond', medium: ' fempto', short: 'fs'}, exp: ['femtosecond', 'fempto', 'femtoseconds', 'femptos', 'fs'] }, pico: { mult: 0.000000001, styles: {full: ' picosecond', medium: ' pico', short: 'ps'}, exp: ['picosecond', 'pico', 'picoseconds', 'picos', 'ps'] }, nano: { mult: 0.000001, styles: {full: ' nanosecond', medium: ' nano', short: 'ns'}, exp: ['nanosecond', 'nano', 'nanoseconds', 'nanos', 'ns'] }, micro: { mult: 0.001, styles: {full: ' microsecond', medium: ' micro', short: 'μs'}, exp: ['microsecond', 'micro', 'microseconds', 'micros', 'μs'] }, milli: { mult: 1, styles: {full: ' millisecond', medium: ' mil', short: 'ms'}, exp: ['ms', 'mil', 'mils', 'milli', 'millis', 'millisecond', 'milliseconds', 'milsec', 'milsecs', 'msec', 'msecs', 'msecond', 'mseconds']}, sec: { mult: 1000, styles: {full: ' second', medium: ' sec', short: 's'}, exp: ['s', 'sec', 'secs', 'second', 'seconds']}, min: { mult: 60000, styles: {full: ' minute', medium: ' min', short: 'm'}, exp: ['m', 'mn', 'mns', 'min', 'mins', 'minute', 'minutes']}, hour: { mult: 3600000, styles: {full: ' hour', medium: ' hr', short: 'h'}, exp: ['h', 'hr', 'hrs', 'hour', 'hours']}, day: { mult: 86400000, styles: {full: ' day', medium: ' day', short: 'd'}, exp: ['d', 'day', 'days']}, week: { mult: 604800000, styles: {full: ' week', medium: ' wk', short: 'w'}, exp: ['w', 'wk', 'wks', 'week', 'weeks']}, month: { mult: 2592000000, styles: {full: ' month', medium: ' mo', short: 'mo'}, exp: ['mo', 'mon', 'mons', 'month', 'months', 'mnth', 'mnths', 'mth', 'mths']}, year: { mult: 31536000000, styles: {full: ' year', medium: ' yr', short: 'y'}, exp: ['y', 'yr', 'yrs', 'year', 'years']} }
- VERSION =
"0.3.0"- HASH_PATH_PROC_TYPES =
{ evaluate: { aliases: [:eval, :equation, :equate]}, append: { aliases: [:suffix]}, prepend: { aliases: [:prefix]}, split: { aliases: [:delimit, :delim, :separate, :msplit]}, replace: { aliases: [:swap]}, extract: { aliases: [:grab, :scan]}, extract_first: { aliases: [:grab_first, :scan_first]}, extract_last: { aliases: [:grab_last, :scan_last]}, parse_date: { aliases: [:date, :parse_time, :time]}, parse_date_unix: { aliases: [:unix_time, :unix_date]}, parse_duration: { aliases: [:duration]}, parse_file_size: { aliases: [:file_size]}, to_string: { aliases: [:to_s, :stringify]}, downcase: { aliases: [:lower, :lowercase, :to_lower]}, upcase: { aliases: [:upper, :uppercase, :to_upper]}, roman: { aliases: [:convert_roman, :roman_numeral, :parse_roman]}, remove_symbols: { aliases: [:chop_symbols, :drop_symbols]}, format_articles: { aliases: [:articles]}, reverse: { aliases: [:invert]}, delete: { aliases: [:del]}, remove: { aliases: [:rem]}, custom: { aliases: [:send]}, encapsulate: { aliases: []}, uncapsulate: {aliases: []}, extract_integers: { aliases: [:extract_ints]}, extract_floats: { aliases: []}, extract_numbers: { aliases: []}, max_number: { aliases: [:max, :maximum, :maximum_number]}, min_number: { aliases: [:min, :minimum, :minimum_number]}, avg_number: { aliases: [:avg, :average, :average_number]}, sum_number: { aliases: [:sum]}, strip: { aliases: [:trim]}, concat: { aliases: [:join, :concat_with]}, reverse_concat: { aliases: [:reverse_join, :reverse_concat_with]} }
Class Method Summary collapse
- .camel_case(str, style = :lower) ⇒ Object
-
.composition_similarity(a, b) ⇒ Object
Calculates a percentage based match of two strings based on their character composition.
- .delimited_case(str, delimiter = '_') ⇒ Object
-
.drop_symbols(str) ⇒ Object
Quickly remove any symbols from a string leaving only alpha-numeric characters and white space.
-
.extract_floats(str, convert: true) ⇒ Object
Extracts all integers or decimals from a string into an array.
-
.extract_integers(str, convert: true) ⇒ Object
Extract all integers from a string.
-
.extract_numbers(str, convert: true) ⇒ Object
Extracts any correctly formed integers or floats from a string.
- .from_roman(str) ⇒ Object
- .gem_installed?(name) ⇒ Boolean
- .gem_list ⇒ Object
- .hash_path(hash, *paths, multi_path: false, multi_join: false) ⇒ Object
- .hash_path_copy(hash, *paths, symbols: true, array: false, overwrite: true, skip_nil: true) ⇒ Object
- .hash_path_copy_to(from, to, *paths, symbols: true, array: false, overwrite: true, skip_nil: true) ⇒ Object
- .hash_path_delete(hash, *paths) ⇒ Object
- .hash_path_key_for(hash, value) ⇒ Object
- .hash_path_keys(hash) ⇒ Object
- .hash_path_move(hash, *paths) ⇒ Object
- .hash_path_move_to(from, to, *paths) ⇒ Object
- .hash_path_proc(hash, action, paths, *args) ⇒ Object
- .hash_path_set(hash, *paths, symbols: true, bridge: true) ⇒ Object
- .in_opal? ⇒ Boolean
- .interleave(a, b) ⇒ Object
-
.keep_between(num, min, max) ⇒ Object
Used to keep any numeric number between a set of bounds.
-
.levenshtein_distance(a, b) ⇒ Object
A simple rendition of the levenshtein distance algorithm.
-
.levenshtein_similarity(a, b) ⇒ Object
Calculates a percentage based match using the levenshtein distance algorithm.
-
.move_articles(str, position = :front, capitalize: true) ⇒ Object
Used to move the position of the articles ‘the’, ‘a’ and ‘an’ in strings for normalization.
- .named_args(*args) ⇒ Object
- .named_args!(*args) ⇒ Object
-
.numeric_similarity(a, b) ⇒ Object
Extracts all numbers from two strings and compares them and generates a percentage of match.
-
.parse_duration(str, output: :sec, min_interval: :sec) ⇒ Object
Parses known time based patterns out of a string to construct a numeric duration.
-
.parse_file_size(str, output: :byte) ⇒ Object
A file size parser for strings.
- .path_hash(hash) ⇒ Object
-
.pathify(*strings) ⇒ Object
Takes one or more strings and normalizes slashes to create a consistent file path Useful when concating two strings that when you don’t know if one or both will end or begin with a slash.
-
.phrase_similarity(a, b) ⇒ Object
Calculates a percentage based match between two strings based on the similarity of word matches.
-
.qwerty_distance(a, b) ⇒ Object
A simple character distance calculator that uses qwerty key positions to determine how similar two strings are.
-
.scan_dir(path = Dir.pwd, filter: nil, recursive: false) ⇒ Object
Scan for files and directories.
-
.scan_dirs(path, filter: nil, recursive: false, mode: :path) ⇒ Object
Uses BBLib.scan_dir but returns only directories.
-
.scan_files(path, filter: nil, recursive: false, mode: :path) ⇒ Object
Uses BBLib.scan_dir but returns only files.
- .snake_case(str) ⇒ Object
- .spinal_case(str) ⇒ Object
- .start_case(str, first_only: false) ⇒ Object
-
.string_to_file(path, str, mkpath = true, mode: 'a') ⇒ Object
Shorthand method to write a string to disk.
- .string_to_roman(str) ⇒ Object
- .title_case(str, first_only: true) ⇒ Object
-
.to_duration(num, input: :sec, stop: :milli, style: :medium) ⇒ Object
Turns a numeric input into a time string.
- .to_hash(obj) ⇒ Object
-
.to_roman(num) ⇒ Object
Converts any integer up to 1000 to a roman numeral.
- .train_case(str) ⇒ Object
Class Method Details
.camel_case(str, style = :lower) ⇒ Object
38 39 40 41 42 43 44 45 |
# File 'lib/string/cases.rb', line 38 def self.camel_case str, style = :lower regx = /[[:space:]]+|[^[[:alnum:]]]+/ words = str.split(regx).map do |word| word.capitalize end words[0].downcase! if style == :lower words.join end |
.composition_similarity(a, b) ⇒ Object
Calculates a percentage based match of two strings based on their character composition.
27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/string/matching.rb', line 27 def self.composition_similarity a, b if a.length <= b.length then t = a; a = b; b = t; end matches, temp = 0, b.dup a.chars.each do |c| if temp.chars.include? c matches+=1 temp = temp.sub(c, '') end end (matches / [a.length, b.length].max.to_f )* 100.0 end |
.delimited_case(str, delimiter = '_') ⇒ Object
47 48 49 50 |
# File 'lib/string/cases.rb', line 47 def self.delimited_case str, delimiter = '_' regx = /[[:space:]]+|[^[[:alnum:]]]+|\#{delimiter}+/ words = str.split(regx).join(delimiter) end |
.drop_symbols(str) ⇒ Object
Quickly remove any symbols from a string leaving only alpha-numeric characters and white space.
14 15 16 |
# File 'lib/string/bbstring.rb', line 14 def self.drop_symbols str str.gsub(/[^\w\s\d]|_/, '') end |
.extract_floats(str, convert: true) ⇒ Object
Extracts all integers or decimals from a string into an array.
24 25 26 |
# File 'lib/string/bbstring.rb', line 24 def self.extract_floats str, convert: true BBLib.extract_numbers(str, convert:false).reject{ |r| !r.include?('.') }.map{ |m| convert ? m.to_f : m } end |
.extract_integers(str, convert: true) ⇒ Object
Extract all integers from a string. Use extract_floats if numbers may contain decimal places.
19 20 21 |
# File 'lib/string/bbstring.rb', line 19 def self.extract_integers str, convert: true BBLib.extract_numbers(str, convert:false).reject{ |r| r.include?('.') }.map{ |m| convert ? m.to_i : m } end |
.extract_numbers(str, convert: true) ⇒ Object
Extracts any correctly formed integers or floats from a string
29 30 31 |
# File 'lib/string/bbstring.rb', line 29 def self.extract_numbers str, convert: true str.scan(/\d+\.\d+[^\.]|\d+[^\.]/).map{ |f| convert ? (f.include?('.') ? f.to_f : f.to_i) : f } end |
.from_roman(str) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/string/roman.rb', line 31 def self.from_roman str sp = str.split(' ') (0..1000).each do |n| num = BBLib.to_roman n if !sp.select{ |i| i[/#{num}/i]}.empty? for i in 0..(sp.length-1) if sp[i].drop_symbols.upcase == num sp[i] = sp[i].sub(num ,n.to_s) end end end end sp.join ' ' end |
.gem_installed?(name) ⇒ Boolean
10 11 12 |
# File 'lib/gem/bbgem.rb', line 10 def self.gem_installed? name BBLib.gem_list.include? name end |
.gem_list ⇒ Object
6 7 8 |
# File 'lib/gem/bbgem.rb', line 6 def self.gem_list Gem::Specification.map(&:name).uniq end |
.hash_path(hash, *paths, multi_path: false, multi_join: false) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/hash/hash_path.rb', line 5 def self.hash_path hash, *paths, multi_path: false, multi_join: false if multi_path || multi_join results = paths.map{ |path| BBLib.hash_path(hash, path)} results = (0..results.max_by{ |m| m.size }.size - 1).map{ |i| results.map{ |r| r[i] } } if multi_join return results end path = split_path(*paths) matches, recursive = [hash], false until path.empty? || matches.empty? current = path.shift.to_s current = current[0..-2] + '.' + path.shift.to_s if current.end_with?("\\") if current.strip == '' recursive = true next end key, formula = BBLib.analyze_hash_path(current) matches = matches.map do |match| if recursive match.dive(key.to_sym, key) elsif key == '*' match.is_a?(Hash) ? match.values : (match.is_a?(Array) ? match : nil) elsif match.is_a?(Hash) key.is_a?(Regexp) ? match.map{ |k,v| k.to_s =~ key ? v : nil } : [(BBLib::in_opal? ? nil : match[key.to_sym]), match[key]] elsif match.is_a?(Array) && (key.is_a?(Fixnum) || key.is_a?(Range)) key.is_a?(Range) ? match[key] : [match[key]] else nil end end.flatten(1).reject{ |m| m.nil? } matches = BBLib.analyze_hash_path_formula(formula, matches) recursive = false end matches end |
.hash_path_copy(hash, *paths, symbols: true, array: false, overwrite: true, skip_nil: true) ⇒ Object
67 68 69 70 71 72 73 74 75 |
# File 'lib/hash/hash_path.rb', line 67 def self.hash_path_copy hash, *paths, symbols: true, array: false, overwrite: true, skip_nil: true paths = paths.find{ |a| a.is_a?(Hash) } paths.each do |from, to| value = BBLib.hash_path(hash, from) value = value.first unless array hash.bridge(to, value: value, symbols:symbols, overwrite: overwrite) unless value.nil? && skip_nil end hash end |
.hash_path_copy_to(from, to, *paths, symbols: true, array: false, overwrite: true, skip_nil: true) ⇒ Object
77 78 79 80 81 82 83 84 85 |
# File 'lib/hash/hash_path.rb', line 77 def self.hash_path_copy_to from, to, *paths, symbols: true, array: false, overwrite: true, skip_nil: true paths = paths.find{ |a| a.is_a?(Hash) } paths.each do |p_from, p_to| value = BBLib.hash_path(from, p_from) value = value.first unless array to.bridge(p_to, value:value, symbols:symbols, overwrite: overwrite) unless value.nil? && skip_nil end to end |
.hash_path_delete(hash, *paths) ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/hash/hash_path.rb', line 87 def self.hash_path_delete hash, *paths deleted = Array.new paths.each do |path| parts = split_path(path) BBLib.hash_path(hash, *parts[0..-2]).each do |match| key, formula = BBLib.analyze_hash_path(parts.last) if match.is_a?(Hash) deleted << match.delete(key) << match.delete(key.to_sym) elsif match.is_a?(Array) && key.is_a?(Fixnum) deleted << match.delete_at(key) end end end deleted.flatten.reject{ |v| v.nil? } end |
.hash_path_key_for(hash, value) ⇒ Object
44 45 46 |
# File 'lib/hash/hash_path.rb', line 44 def self.hash_path_key_for hash, value hash.squish.find_all{ |k,v| value.is_a?(Regexp) ? v =~ value : v == value }.to_h.keys end |
.hash_path_keys(hash) ⇒ Object
40 41 42 |
# File 'lib/hash/hash_path.rb', line 40 def self.hash_path_keys hash hash.squish.keys end |
.hash_path_move(hash, *paths) ⇒ Object
103 104 105 106 107 |
# File 'lib/hash/hash_path.rb', line 103 def self.hash_path_move hash, *paths BBLib.hash_path_copy hash, *paths BBLib.hash_path_delete hash, *paths.find{|pt| pt.is_a?(Hash) }.keys hash end |
.hash_path_move_to(from, to, *paths) ⇒ Object
109 110 111 112 113 |
# File 'lib/hash/hash_path.rb', line 109 def self.hash_path_move_to from, to, *paths BBLib.hash_path_copy_to from, to, *paths BBLib.hash_path_delete from, *paths.find{|pt| pt.is_a?(Hash) }.keys to end |
.hash_path_proc(hash, action, paths, *args) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/hash/hash_path_proc.rb', line 19 def self.hash_path_proc hash, action, paths, *args params = BBLib::named_args(*args) action = HASH_PATH_PROC_TYPES.keys.find{ |k| k == action || HASH_PATH_PROC_TYPES[k][:aliases].include?(action) } return nil unless action paths.to_a.each do |path| hash.hash_path(path).each do |value| if params.include?(:condition) && params[:condition] begin next unless eval(params[:condition].gsub('$', value.to_s)) rescue StandardError, SyntaxError => e next end end HashPath.send(action, hash, path, value, *args) end end return hash end |
.hash_path_set(hash, *paths, symbols: true, bridge: true) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/hash/hash_path.rb', line 48 def self.hash_path_set hash, *paths, symbols: true, bridge: true paths = paths.find{ |a| a.is_a?(Hash) } paths.each do |path, value| parts = split_path(path) matches = BBLib.hash_path(hash, *parts[0..-2]) matches.each do |match| key, formula = BBLib.analyze_hash_path(parts.last) key = match.include?(key.to_sym) || (symbols && !match.include?(key) ) ? key.to_sym : key if match.is_a?(Hash) match[key] = value elsif match.is_a?(Array) && key.is_a?(Fixnum) match[key] = value end end hash.bridge(path, value:value, symbols:symbols) if matches.empty? && bridge end hash end |
.in_opal? ⇒ Boolean
3 4 5 |
# File 'lib/opal/bbopal.rb', line 3 def self.in_opal? RUBY_ENGINE == 'opal' end |
.interleave(a, b) ⇒ Object
5 6 7 8 9 10 11 12 |
# File 'lib/array/bbarray.rb', line 5 def self.interleave a, b ary = Array.new [a.size, b.size].max.times do |i| ary.push(a[i]) if i < a.size ary.push(b[i]) if i < b.size end ary end |
.keep_between(num, min, max) ⇒ Object
Used to keep any numeric number between a set of bounds. Passing nil as min or max represents no bounds in that direction. min and max are inclusive to the allowed bounds.
5 6 7 8 9 10 |
# File 'lib/number/bbnumber.rb', line 5 def self.keep_between num, min, max raise "Argument must be numeric: #{num} (#{num.class})" unless Numeric === num if !min.nil? && num < min then num = min end if !max.nil? && num > max then num = max end return num end |
.levenshtein_distance(a, b) ⇒ Object
A simple rendition of the levenshtein distance algorithm
8 9 10 11 12 13 14 15 16 17 |
# File 'lib/string/matching.rb', line 8 def self.levenshtein_distance a, b costs = (0..b.length).to_a (1..a.length).each do |i| costs[0], nw = i, i - 1 (1..b.length).each do |j| costs[j], nw = [costs[j] + 1, costs[j-1] + 1, a[i-1] == b[j-1] ? nw : nw + 1].min, costs[j] end end costs[b.length] end |
.levenshtein_similarity(a, b) ⇒ Object
Calculates a percentage based match using the levenshtein distance algorithm
20 21 22 23 24 |
# File 'lib/string/matching.rb', line 20 def self.levenshtein_similarity a, b distance = BBLib.levenshtein_distance a, b max = [a.length, b.length].max.to_f return ((max - distance.to_f) / max) * 100.0 end |
.move_articles(str, position = :front, capitalize: true) ⇒ Object
Used to move the position of the articles ‘the’, ‘a’ and ‘an’ in strings for normalization.
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/string/bbstring.rb', line 34 def self.move_articles str, position = :front, capitalize: true return str unless [:front, :back, :none].include?(position) articles = ["the", "a", "an"] articles.each do |a| starts, ends = str.downcase.start_with?(a + ' '), str.downcase.end_with?(' ' + a) if starts && position != :front if position == :none str = str[(a.length + 1)..str.length] elsif position == :back str = str[(a.length + 1)..str.length] + (!ends ? ", #{capitalize ? a.capitalize : a}" : '') end end if ends && position != :back if position == :none str = str[0..-(a.length + 2)] elsif position == :front str = (!starts ? "#{capitalize ? a.capitalize : a} " : '') + str[0..-(a.length + 2)] end end end while str.strip.end_with?(',') str = str.strip str = str.chop end str end |
.named_args(*args) ⇒ Object
32 33 34 |
# File 'lib/object/bbobject.rb', line 32 def self.named_args *args args.last.is_a?(Hash) && args.last.keys.all?{|k|k.is_a?(Symbol)} ? args.last : Hash.new end |
.named_args!(*args) ⇒ Object
36 37 38 39 40 41 42 |
# File 'lib/object/bbobject.rb', line 36 def self.named_args! *args if args.last.is_a?(Hash) && args.last.keys.all?{|k|k.is_a?(Symbol)} args.delete_at(-1) else Hash.new end end |
.numeric_similarity(a, b) ⇒ Object
Extracts all numbers from two strings and compares them and generates a percentage of match. Percentage calculations here need to be weighted better…TODO
54 55 56 57 58 59 60 61 62 |
# File 'lib/string/matching.rb', line 54 def self.numeric_similarity a, b a, b = a.extract_numbers, b.extract_numbers return 100.0 if a.empty? && b.empty? || a == b matches = [] for i in 0..[a.size, b.size].max-1 matches << 1.0 / ([a[i].to_f, b[i].to_f].max - [a[i].to_f, b[i].to_f].min + 1.0) end (matches.inject{ |sum, m| sum + m } / matches.size.to_f) * 100.0 end |
.parse_duration(str, output: :sec, min_interval: :sec) ⇒ Object
Parses known time based patterns out of a string to construct a numeric duration.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/time/bbtime.rb', line 7 def self.parse_duration str, output: :sec, min_interval: :sec msecs = 0.0 # Parse time expressions such as 04:05. # The argument min_interval controls what time interval the final number represents str.scan(/\d+\:[\d+\:]+\d+/).each do |e| keys = TIME_EXPS.keys position = keys.index(min_interval) e.split(':').reverse.each do |sec| key = keys[position] msecs+= sec.to_f * TIME_EXPS[key][:mult] position+=1 end end # Parse expressions such as '1m' or '1 min' TIME_EXPS.each do |k, v| v[:exp].each do |e| numbers = str.downcase.scan(/(?=\w|\D|\A)\d*\.?\d+[[:space:]]*#{e}(?=\W|\d|\z)/i) numbers.each do |n| msecs+= n.to_f * v[:mult] end end end msecs / (TIME_EXPS[output][:mult] rescue 1) end |
.parse_file_size(str, output: :byte) ⇒ Object
A file size parser for strings. Extracts any known patterns for file sizes.
42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/file/bbfile.rb', line 42 def self.parse_file_size str, output: :byte output = FILE_SIZES.keys.find{ |f| f == output || FILE_SIZES[f][:exp].include?(output.to_s.downcase) } || :byte bytes = 0.0 FILE_SIZES.each do |k, v| v[:exp].each do |e| numbers = str.scan(/(?=\w|\D|^)\d*\.?\d+\s*#{e}s?(?=\W|\d|$)/i) numbers.each{ |n| bytes+= n.to_f * v[:mult] } end end return bytes / FILE_SIZES[output][:mult] end |
.path_hash(hash) ⇒ Object
8 9 10 |
# File 'lib/hash/path_hash.rb', line 8 def self.path_hash hash PathHash.new(hash) end |
.pathify(*strings) ⇒ Object
Takes one or more strings and normalizes slashes to create a consistent file path Useful when concating two strings that when you don’t know if one or both will end or begin with a slash
7 8 9 10 |
# File 'lib/file/bbfile.rb', line 7 def self.pathify *strings start = strings.first.start_with?('/') || strings.first.start_with?('\\') (start ? '/' : '' ) + strings.map(&:to_s).msplit('/', '\\').map(&:strip).join('/') end |
.phrase_similarity(a, b) ⇒ Object
Calculates a percentage based match between two strings based on the similarity of word matches.
40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/string/matching.rb', line 40 def self.phrase_similarity a, b temp = b.drop_symbols.split ' ' matches = 0 a.drop_symbols.split(' ').each do |w| if temp.include? w matches+=1 temp.delete_at temp.find_index w end end (matches.to_f / [a.split(' ').size, b.split(' ').size].max.to_f) * 100.0 end |
.qwerty_distance(a, b) ⇒ Object
A simple character distance calculator that uses qwerty key positions to determine how similar two strings are. May be useful for typo detection.
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/string/matching.rb', line 66 def self.qwerty_distance a, b a, b = a.downcase.strip, b.downcase.strip if a.length <= b.length then t = a; a = b; b = t; end qwerty = { 1 => ['1','2','3','4','5','6','7','8','9','0'], 2 => ['q','w','e','r','t','y','u','i','o','p'], 3 => ['a','s','d','f','g','h','j','k','l'], 4 => ['z','x','c','v','b','n','m'] } count, offset = 0, 0 a.chars.each do |c| if b.length <= count offset+=10 else ai = qwerty.keys.find{ |f| qwerty[f].include? c }.to_i bi = qwerty.keys.find{ |f| qwerty[f].include? b.chars[count] }.to_i offset+= (ai - bi).abs offset+= (qwerty[ai].index(c) - qwerty[bi].index(b.chars[count])).abs end count+=1 end offset end |
.scan_dir(path = Dir.pwd, filter: nil, recursive: false) ⇒ Object
Scan for files and directories. Can be set to be recursive and can also have filters applied.
13 14 15 16 17 18 19 20 |
# File 'lib/file/bbfile.rb', line 13 def self.scan_dir path = Dir.pwd, filter: nil, recursive: false if !filter.nil? filter = [filter].flatten.map{ |f| path.to_s + (recursive ? '/**/' : '/') + f.to_s } else filter = (path.to_s + (recursive ? '/**/*' : '/*')).gsub('//', '/') end Dir.glob(filter) end |
.scan_dirs(path, filter: nil, recursive: false, mode: :path) ⇒ Object
Uses BBLib.scan_dir but returns only directories. Mode can be used to return strings (:path) or Dir objects (:dir)
28 29 30 |
# File 'lib/file/bbfile.rb', line 28 def self.scan_dirs path, filter: nil, recursive: false, mode: :path BBLib.scan_dir(path, filter: filter, recursive: recursive).map{ |f| File.directory?(f) ? (mode == :dir ? Dir.new(f) : f ) : nil}.reject{ |r| r.nil? } end |
.scan_files(path, filter: nil, recursive: false, mode: :path) ⇒ Object
Uses BBLib.scan_dir but returns only files. Mode can be used to return strings (:path) or File objects (:file)
23 24 25 |
# File 'lib/file/bbfile.rb', line 23 def self.scan_files path, filter: nil, recursive: false, mode: :path BBLib.scan_dir(path, filter: filter, recursive: recursive).map{ |f| File.file?(f) ? (mode == :file ? File.new(f) : f) : nil}.reject{ |r| r.nil? } end |
.snake_case(str) ⇒ Object
52 53 54 |
# File 'lib/string/cases.rb', line 52 def self.snake_case str BBLib.delimited_case str, '_' end |
.spinal_case(str) ⇒ Object
56 57 58 |
# File 'lib/string/cases.rb', line 56 def self.spinal_case str BBLib.delimited_case str, '-' end |
.start_case(str, first_only: false) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/string/cases.rb', line 24 def self.start_case str, first_only: false regx = /[[:space:]]+|\-|\_|\"|\'|\(|\)|\[|\]|\{|\}|\#/ spacing = str.scan(regx).to_a words = str.split(regx).map do |word| if first_only word[0] = word[0].upcase word else word.capitalize end end words.interleave(spacing).join end |
.string_to_file(path, str, mkpath = true, mode: 'a') ⇒ Object
Shorthand method to write a string to disk. By default the path is created if it doesn’t exist. Set mode to w to truncate file or leave at a to append.
34 35 36 37 38 39 |
# File 'lib/file/bbfile.rb', line 34 def self.string_to_file path, str, mkpath = true, mode: 'a' if !Dir.exists?(path) && mkpath FileUtils.mkpath File.dirname(path) end File.write(path, str.to_s, mode:mode) end |
.string_to_roman(str) ⇒ Object
19 20 21 22 23 24 25 26 27 28 |
# File 'lib/string/roman.rb', line 19 def self.string_to_roman str sp = str.split ' ' sp.map do |s| if s.drop_symbols.to_i.to_s == s.drop_symbols && !(s =~ /\d+\.\d+/) s = s.sub(s.scan(/\d+/).first.to_s, BBLib.to_roman(s.to_i)) else s end end.join ' ' end |
.title_case(str, first_only: true) ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/string/cases.rb', line 3 def self.title_case str, first_only: true ignoreables = ['a', 'an', 'the', 'on', 'upon', 'and', 'but', 'or', 'in', 'with', 'to'] regx = /[[:space:]]+|\-|\_|\"|\'|\(|\)|\[|\]|\{|\}|\#/ spacing = str.scan(regx).to_a words = str.split(regx).map do |word| if ignoreables.include?(word.downcase) word.downcase else if first_only word[0] = word[0].upcase word else word.capitalize end end end # Always cap the first word words.first.capitalize words.interleave(spacing).join end |
.to_duration(num, input: :sec, stop: :milli, style: :medium) ⇒ Object
Turns a numeric input into a time string.
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/time/bbtime.rb', line 36 def self.to_duration num, input: :sec, stop: :milli, style: :medium return nil unless Numeric === num || num > 0 if ![:full, :medium, :short].include?(style) then style = :medium end expression = [] n, done = num * TIME_EXPS[input.to_sym][:mult], false TIME_EXPS.reverse.each do |k, v| next unless !done if k == stop then done = true end div = n / v[:mult] if div >= 1 val = (done ? div.round : div.floor) expression << "#{val}#{v[:styles][style]}#{val > 1 && style != :short ? "s" : nil}" n-= val.to_f * v[:mult] end end expression.join ' ' end |
.to_hash(obj) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/object/bbobject.rb', line 7 def self.to_hash obj return {obj => nil} unless !obj.instance_variables.empty? hash = {} obj.instance_variables.each do |var| value = obj.instance_variable_get(var) if value.is_a? Array hash[var.to_s.delete("@")] = value.map{ |v| v.respond_to?(:obj_to_hash) && !v.instance_variables.empty? ? v.obj_to_hash : v } elsif value.is_a? Hash begin if !hash[var.to_s.delete("@")].is_a?(Hash) then hash[var.to_s.delete("@")] = Hash.new end rescue hash[var.to_s.delete("@")] = Hash.new end value.each do |k, v| hash[var.to_s.delete("@")][k.to_s.delete("@")] = v.respond_to?(:obj_to_hash) && !v.instance_variables.empty? ? v.obj_to_hash : v end elsif value.respond_to?(:obj_to_hash) && !value.instance_variables.empty? hash[var.to_s.delete("@")] = value.obj_to_hash else hash[var.to_s.delete("@")] = value end end return hash end |
.to_roman(num) ⇒ Object
Converts any integer up to 1000 to a roman numeral
5 6 7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/string/roman.rb', line 5 def self.to_roman num return num.to_s if num > 1000 roman = {1000 => 'M', 900 => 'CM', 500 => 'D', 400 => 'CD', 100 => 'C', 90 => 'XC', 50 => 'L', 40 => 'XL', 10 => 'X', 9 => 'IX', 5 => 'V', 4 => 'IV', 3 => 'III', 2 => 'II', 1 => 'I'} numeral = "" roman.each do |n, r| while num >= n num-= n numeral+= r end end numeral end |
.train_case(str) ⇒ Object
60 61 62 |
# File 'lib/string/cases.rb', line 60 def self.train_case str BBLib.spinal_case(BBLib.start_case(str)) end |