Created
June 19, 2013 10:36
-
-
Save spikegrobstein/5813363 to your computer and use it in GitHub Desktop.
example of my concept implementation degrawlixer for teamonetickets. basic
This file contains hidden or 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
The Fucking Champs | |
Fucking Champs | |
The Fucking Am | |
Starfucker | |
Butthole Surfers | |
Turbonegro | |
Goblin Cock | |
Alabama Thunderpussy | |
God Below | |
God Forbid |
This file contains hidden or 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 'bundler/setup' | |
require 'pry' | |
class Degrawlixer | |
attr_reader :db | |
def initialize(db_path) | |
load_db(db_path) | |
end | |
def load_db(db_path) | |
@db = [] | |
File.open(db_path, 'r').each do |line| | |
@db << line.chomp | |
end | |
end | |
def translate(name) | |
return name unless grawlixed? name | |
r = convert_to_regex(name) | |
return name if r.nil? | |
db.each do |correct_name| | |
next unless correct_name.length == name.length | |
return correct_name if correct_name.match(r) | |
end | |
return name | |
end | |
def grawlixed?(name) | |
!!name.match(/[*\-]/) | |
end | |
# convert "The F***ing Champs" -> /^The F...ing Champs$/ | |
def convert_to_regex(name) | |
return nil unless gawlixed?(name) | |
return nil if name.gsub(/^[*\-]*$/, '') | |
o = name.gsub(/([*\-]*)/) do |m| | |
'.' * m.length | |
end | |
Regexp.new "^#{o}$", 'i' | |
end | |
end |
This file contains hidden or 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
source 'https://rubygems.org' | |
gem 'rspec' | |
gem 'pry' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment