Sunday, March 13, 2011

Reverse*a*Sentence*With*Ruby code

For example , if you want "One, Two, Three, Four." to be converted into “.Four ,Three ,Two ,One”, take a look at my ruby version for your reference, hope it helps in some degree :) I know you may never need this function, but some people may let you write such kind of things...

 # Ruby Version 1.9.2-p180  
 def reverseTest(inputtext)  
  @a=""  
  regextext = /[!,.;"()<>\[\]{}]/  
  arr = inputtext.split(/\s/)  
  arr.reverse_each { |item|  
   len = @a.length  
   # Dealing with a word surrounding by one or multiple punctuation  
   if item.partition(regextext).at(1)!=''  
    # Partition the word by particular punctuation, until partition by the last punctuation  
    begin  
     @a .insert(len,item.partition(regextext).at(0))  
     @a .insert(len,item.partition(regextext).at(1))  
     item = item.partition(regextext).at(2)  
    end while item.partition(regextext).at(1)!='' && item.partition(regextext).at(2)!=''  
    # When punctuation is at the end coming after a word  
    @a .insert(len,item.partition(regextext).at(0))  
    @a .insert(len,item.partition(regextext).at(1))  
    @a << ' '  
   # Dealing with a word without surrounding any punctuation  
   else  
    @a << item  
    @a << ' '  
   end  
  }  
  #Remove space at the end of the string and swap symmetry punctuation  
  @a.strip!  
  @a.gsub!(/[()<>\[\]{}]/, '('=>')', ')'=>'(', '<'=>'>', '>'=>'<', '['=>']',']'=>'[', '{'=>'}', '}'=>'{')  
  print @a  
 end  

2 comments:

  1. The rule about punctuation is a bit strange... I wrote a python version at https://gist.github.com/868885

    ReplyDelete
  2. I listed my pre-defined 12 cases for own testing :)

    case1: there is only a dot at the end
    input = "I am doing testing."
    expected result: .testing doing am I

    case2: there is a comma following by a word in the middle and exclamatory mark at the end
    input = "Hello, please fix me!"
    expected result: !me fix please ,Hello

    case3: there is a comma following by each word
    input = "One, Two, Three, Four."
    expected result: .Four ,Three ,Two ,One

    case4: one word followed by three dot marks
    input = "\"Oh... My God!\""
    expected result: "!God My ...Oh"

    case5: there are two punctuation before a word and two after another word
    input = "She said :\"I will be there!\""
    expected result: "!there be will I": said She

    case6: There is one special punctuation(') in the middle of one word, do not change its position
    input = "It's a cool stuff!"
    expected result: !stuff cool a It's

    case7: Only one word within quotation marks
    input = "I like watching \"funny\" movie."
    expected result: .movie "funny" watching like I

    case8: Two words within quotation marks
    input = "Testing starts \"for Google\"."
    expected result: ."Google for" starts Testing

    case9: There is two punctuation before a word and two punctuation after this word
    input = "He said to her :\"Sorry!\""
    expected result: "!Sorry": her to said He

    case10: One word within Parentheses
    input = "Testing starts (Google)."
    expected result: .(Google) starts Testing

    case11: Two words within Parentheses
    input = "Testing starts (for Google)."
    expected result: .(Google for) starts Testing

    case12: there is $ mark in front of number
    input = "The book costs $50."
    expected result: .$50 costs book The

    ReplyDelete