Class | Giver |
In: |
app/models/giver.rb
|
Parent: | Object |
get_filename_part returns an array of the $1 portion
of the match between filenames
under root_path and the regular expression frx, sorted by frequency of occurrence
# File app/models/giver.rb, line 8 8: def get_filename_part(public_path,frx) 9: char_freq={} #possible_chars and their frequency of 10: # occurrence in picture file_names 11: char_freq.default=0 12: Find.find(File.join(Dir.pwd,'public',public_path)) do 13: |path| 14: if FileTest.directory?(path) and 15: (File.basename(path)[0] == ?.) 16: Find.prune # Don't look any further into 17: #this directory. 18: else 19: if path.match(frx) 20: found=$1 21: if found.match('\.') #so that we 22: # preserve case for file names 23: char_freq[found]+=1 24: else 25: char_freq[found.upcase]+=1 26: end 27: end 28: end 29: end 30: char_freq.sort{|b,a| a[1]<=>b[1]}.collect{|x| x[0]} 31: end
get_matching_sentences returns sentences in root_dir folder matching rx
# File app/models/giver.rb, line 55 55: def get_matching_sentences (root_dir,rx) 56: files=get_filename_part(root_dir,'(.+txt)$') 57: sentences=[] 58: # p files 59: files.each do |fs| 60: # p("searching file #{fs}") 61: f=open(fs) 62: f.each do |s| 63: # p("searching sentence #{s}") 64: if s.match rx 65: sentences << s.strip 66: end 67: end 68: end 69: sentences 70: end
get_matching_words returns an array of words in txt files under root_dir that match rx,alphabetically sorted
# File app/models/giver.rb, line 42 42: def get_matching_words (root_dir, rx) 43: rxfile=Regexp.new('([\w,/, 44: ]*\.txt)$',Regexp::IGNORECASE) 45: # full path of txt files 46: @files=get_filename_part("words",rxfile) 47: @words=[] 48: @files.each do |f| 49: @words += matching_words_from_file(f,rx) 50: end 51: @words.uniq.sort 52: end
returns words that start sentences or appear after a comma or semicolon in root_dir
# File app/models/giver.rb, line 82 82: def get_starting_words(root_dir) 83: rx=/\w/ 84: ac=get_matching_sentences(root_dir,rx) 85: nw=ac.collect do |se| 86: if se =~ /^\s*(\w+)/ 87: $1 88: end 89: end 90: nw 91: end
get_subdirs returns array of subdirectories under
"public" that do not begin with a period
# File app/models/giver.rb, line 74 74: def get_subdirs(root_dir) 75: c=Dir.entries(File.join(Dir.pwd,'public',root_dir)) 76: #don't want items containing . 77: c.reject!{|d| d=~/\./} 78: c 79: end