Created
February 9, 2012 16:58
-
-
Save jpowell/1781090 to your computer and use it in GitHub Desktop.
simple tokenizers for ruby
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
class EmailTokenizer < StringTokenizer | |
def initialize text | |
super text, /(\s|,|;)+/ | |
end | |
def self.filter array=[] | |
result = [] | |
array.each do |item| | |
result << item.downcase if item =~ /.+@.+/ | |
end | |
result | |
end | |
protected | |
def tokenize | |
self.class.filter super | |
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
class StringTokenizer | |
def initialize text='', delim=/\s+/ | |
raise ArgumentError, 'Text must be a string' if text.nil? | |
@text, @delimiter = text, delim | |
end | |
def tokens | |
@tokens ||= tokenize | |
end | |
protected | |
def tokenize | |
@text.split @delimiter | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment