Randomly produce a word and then determine whether it is a true (misspelled) Word:)

Source: Internet
Author: User

Linux under the fun small stuff is more Ah! This cat also found a spell program, if the word is spelled, the god horse also does not output, otherwise the output of suspected misspelled words. You can put several words in a file, or you can use a pipeline to enter spell. For the sake of simplicity the cat adopted the latter method, It might be a little slower! The previous method will be implemented later to see how much more efficient it is.

The first is the method of randomly generating words:

def rand_words (n=10000,min_len=2,max_len=12) chars = (("A" ... " Z "). To_a * max_len). freezewords = []srandn.times do |x|len = Min_len + (rand*1000). to_i% max_lenidxes = []len.times {idxe s<< (rand*100)%26}chars.shufflewords << chars.values_at (*idxes). Joinidxes.clearend wordsend

considering that the same letter can appear multiple times in a word, it is true that the alphabet is repeated max_len times, and max_len letters can be the same in extreme cases. Then write down a method to determine whether a word can be spelled:

#ret word that can spell or RET nildef spell_word (word) cmd = ' echo #{word}|spell '. chompif cmd = = Wordreturn Nilelsereturn Wordendend

Finally, call the parameter to try it:

Rand_words (argv[0].to_i,argv[1].to_i,argv[2].to_i). Each do |w|printf w+ "" If Spell_word (w) End

On the test code:

[Email protected]:~/src/ruby_src$ time/a.rb 3 12usage:a.rb [-all|-all2] words_count min_len max_leneta War Raw CH Um lab err Woe coil pee swum yap sap mud who sin real0m22.770suser0m3.350ssys0m7.216s

The speed seems not very ideal Ah, 2000 words with more than 22 seconds! The following method to pass the file to spell to see how fast, then write a spell_words method:

#spell all words by Tmpfiledef spell_words (words) puts "using spell_words ..." f = tempfile.new ("#{$$}_spell_blablabla") #f = File.Open ("Spell_test", "w+") #f. Write Marshal.dump (words) f.write words.join ("") F.closecmd = ' spell #{f.path} ' No_ Spell_words = Cmd.split ("\ n") words-no_spell_wordsend

To modify the parameter invocation method:

If argv[0] =~/^-all$/puts spell_words (rand_words (argv[1].to_i,argv[2].to_i,argv[3].to_i)). Join ("") Elserand_words ( argv[0].to_i,argv[1].to_i,argv[2].to_i). Each do |w|printf w+ "" If Spell_word (w) endend

Look at the speed there is no promotion AH:

[Email protected]:~/src/ruby_src$ time/a.rb-all 3 12usage:a.rb [-all|-all2] words_count min_len max_lenusing SPE Ll_words...work Bah Air hop pus etc bobreal0m3.945suser0m0.163ssys0m0.050s

Efficiency has become faster, only in less than 4 seconds Ah! Finally, if spell writes the results directly to the file, then will it be quicker to read them again? Write a Spell_words2 method.

#spell all words by tmpfile and spell ret are also use tmpfiledef spell_words2 (words) puts "using spell_words2 ..." f_words = Tempfile.new ("#{$$}_spell_words") F_ret = Tempfile.new ("#{$$}_spell_ret") F_ret.closef_words.write Words.join ("") f_ Words.closecmd = ' spell #{f_words.path} > #{f_ret.path} ' F=file.open (f_ret.path) no_spell_words = F.read.split ("\ n") F.closewords-no_spell_wordsend

The final modified source code is as follows:

#!/usr/bin/ruby#code by Hopy 2014.12.08#random create some words and check if a valid Word!require ' Tempfile ' def Rand_word S (n=10000,min_len=2,max_len=12) chars = (("A" ... " Z "). To_a * max_len). freezewords = []srandn.times do |x|len = Min_len + (rand*1000). to_i% max_lenidxes = []len.times {idxe s<< (rand*100)%26}chars.shufflewords << chars.values_at (*idxes). Joinidxes.clearend Wordsend#ret Word That can spell or RET nildef spell_word (word) cmd = ' echo #{word}|spell '. chompif cmd = = Wordreturn Nilelsereturn wordendend #spell all words by Tmpfiledef spell_words (words) puts "using spell_words ..." f = tempfile.new ("#{$$}_spell_blablabla") #f = File.Open ("Spell_test", "w+") #f. Write Marshal.dump (words) f.write words.join ("") F.closecmd = ' spell #{f.path} ' No_ Spell_words = Cmd.split ("\ n") Words-no_spell_wordsend#spell all words by tmpfile and spell ret are also use tmpfiledef SPE LL_WORDS2 (words) puts "using spell_words2 ..." f_words = Tempfile.new ("#{$$}_spell_words") F_ret = Tempfile.new ("#{$$}_ SpelL_ret ") F_ret.closef_words.write Words.join (" ") F_words.closecmd = ' spell #{f_words.path} > #{f_ret.path} ' f= File.Open (f_ret.path) no_spell_words = F.read.split ("\ n") f.closewords-no_spell_wordsendputs "Usage: #{$0[2..-1]} [- ALL|-ALL2] Words_count min_len max_len "If Argv[0" =~/^-all$/puts spell_words (Rand_words (ARGV[1].to_i,ARGV[2].to_i, argv[3].to_i). Join ("") elsif argv[0] =~/^-all2$/puts spell_words2 (Rand_words (argv[1].to_i,argv[2].to_i,argv[3].to _i). Join ("") Elserand_words (argv[0].to_i,argv[1].to_i,argv[2].to_i). Each do |w|printf w+ "" If Spell_word (w) endend

The test is as follows:

[Email protected]:~/src/ruby_src$ time/a.rb-all2 3 12usage:a.rb [-all|-all2] words_count Min_len max_lenusing sp Ell_words2...pus TTY sis aft cutreal0m4.443suser0m0.163ssys0m0.050s

Seemingly spell_words2 method than spell_words still slightly slower ah!? 2 Methods The basic efficiency is almost AH! A function has written 3 kinds of implementation methods, this cat is not idle egg pain? Not really, can only say more than one try, more than one possibility!

Randomly produce a word and then determine whether it is a true (misspelled) Word:)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.