Friday, 15 August 2014

regex - Find and print lines in a file exactly matching string or regexp (Ruby) -



regex - Find and print lines in a file exactly matching string or regexp (Ruby) -

in ruby 1.9.3, i'm trying write programme find words n number of characters taken arbitrary set of characters. instance, if i'm given characters [ b, a, h, s, v, i, e, y, k, s, ] , n = 5, need find 5-letter words can made using characters. using 2of4brif.txt word list http://wordlist.sourceforge.net/ (to include british words , spellings, too), have attempted next code:

a = %w[b h s v e y k s a] a.permutation(5).map(&:join).each |x| file.open('2of4brif.txt').each_line |line| puts line if line.match(/^[#{x}]+$/) end end

this nil (no error message, no output, if frozen). have attempted variations based on next threads:

what's best way search string in file?

ruby find string in file , print result

how search exact matching string in text file using ruby?

finding lines in text file matching regular expression

match content regexp in file?

how open file , search word [ruby]?

every variation have tried has resulted in either:

1) freezing;

2) printing words list contain 5-character permutations (i assume that's it's doing; didn't go through , check of thousands of printed words); or

3) printing 5-character permutations found within words in list (again, assume that's it's doing).

again, i'm not looking words contain 5-character permutations, i'm looking 5-character permutations finish words in , of themselves, line in text file should printed if perfect match permutation.

what doing wrong? in advance!

this works me using english.0 file on page (sorry, couldn't find specific file mentioned):

a = %w[b h s v e y k s l d n] dict = {} a.permutation(5).each |p| dict[p.join('')] = true end file.open('english.0').each_line |line| line.chomp!.downcase! puts line if dict[line] end

the construction should pretty clear - build dictionary of permutations front end in 1 giant hash (you may need revisit depending on input sizes, memory inexpensive these days), , used fact input "one word per line" key hash.

also note, in version, read through file once. in yours scan file 1 time per permutation, , there thousands of permutations.

ruby regex file-io

No comments:

Post a Comment