Created
May 30, 2015 15:27
-
-
Save apenney/2fc9c3bb787bd9b0d292 to your computer and use it in GitHub Desktop.
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
class Text | |
def initialize | |
end | |
def file | |
File.read('test-words') | |
end | |
def words | |
#file.split(/([a-zA-Z0-9']+)/) | |
#file.split(/([\w\S]+)/) | |
file.scan(/[a-zA-Z0-9']+[^a-zA-Z0-9' ]+/) | |
end | |
def strip_word(word) | |
#words.map {|word| word.gsub(/[^'0-9a-zA-Z]/i, '').downcase} | |
word.gsub(/[^'0-9a-zA-Z]/i, '').downcase | |
end | |
def word_positions | |
hash = Hash.new { |h,k| h[k] = [] } | |
words.each_with_index do |word, index| | |
hash[strip_word(word)] << index | |
end | |
require 'pry' | |
binding.pry | |
hash | |
end | |
def search(word, context) | |
abort "ERROR: Context must be numeric" unless context.is_a?(Integer) | |
abort "ERROR: Word must be a string." unless word.is_a?(String) | |
result = [] | |
count = context*2 | |
word_positions[word].each do |pos| | |
joined = words[(pos-context)..(pos+context)].join('') | |
# Add back the missing ., so awful. | |
joined.gsub!(/\. /, '. ') | |
result << joined | |
end | |
return result | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment