Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Yoda Speak Translator

by grep (Monsignor)
on Apr 26, 2002 at 06:15 UTC ( #162190=CUFP: print w/ replies, xml ) Need Help??

I was watching the Empire Strikes Back this evening and I wanted to see if I could write a sub to translate a normal sentence into Yoda-speak (read: too much free time). So here it is. I am welcome to suggestions, improvments, or more pivot words.


#!/usr/local/bin/perl -w use strict; my @sentences = ('I will teach you', 'The truth is out there', 'That is an Imperial shuttle', 'My husband is giant dork', #this is my wife's s +uggestion 'These fish are tasty' ); foreach (@sentences) { print yoda($_)."\n"; } sub yoda { my ($sentence) = @_; my @pivot_words = qw/is be will show do try are teach have/; # Find out if I have a pivot word and grab the one with the lowest + index my $pivot = (sort { $a->[1] <=> $b->[1]} grep {$_->[1] > 0} map { [" $_ ",index($sentence," $_ ")] } @pivot_word +s)[0]; #^^^^^^ ^^^^^^^ # Change to fix the pr +oblem [zdog] # pointed out - THX [z +dog] # No pivot words return $sentence if (!$pivot); # Pivot the sentence $sentence = substr($sentence,$pivot->[1]+length($pivot->[0]),lengt +h($sentence)). " ". substr($sentence,0,$pivot->[1]). $pivot->[0]; # Clear leading spaces $sentence =~ s/^\s+//; # Sentence case $sentence = ucfirst(lc($sentence)); }

Update: Fixed to handle pivot words inside another word.

Update 2: Fixed the word boundry problem with a better solution. The new sub follows:

sub yoda { my ($sentence) = @_; my @pivot_words = qw/is be will show do try are teach have/; # Find out if I have a pivot word and grab the one with the lowest + index my $pivot = (sort { $a->[1] <=> $b->[1]} # sort the lowest index to the top grep {$_->[1] > 0} # filter out non-matches map{$sentence =~ /\b$_\b/ ? [$_,$+[0]] : [$_,0]} @pi +vot_words)[0]; # create an anon array in the format # ['pivot word',index_of_where_it_is] # # @+ stores the indexes of the successful matches and + does not # require the gyrations that [pos] does # No pivot words return $sentence if (!$pivot); # Pivot the sentence $sentence = substr($sentence,$pivot->[1]). " ". substr($sentence,0,$pivot->[1]); # Clear leading spaces $sentence =~ s/^\s+//; # Sentence case $sentence = ucfirst(lc($sentence)); }


grep
Unix - where you can throw the manual on the keyboard and get a command

Comment on Yoda Speak Translator
Select or Download Code
Re: Yoda Speak Translator
by Fletch (Chancellor) on Apr 26, 2002 at 09:51 UTC

    Google for scripts could you, yes. Find many pivot words there, hrmmmm. IMDB's quotes also look at you might.

    IDEs. Heh! Syntax hilighting. Heh! A perl hacker craves not these things. For my ally is the Source, and a powerful ally it is.

    Aherm. I'm up waaaayyy too late apparently. :)

      hello. i am yoda.
Re: Yoda Speak Translator
by Dog and Pony (Priest) on Apr 26, 2002 at 15:25 UTC
    Putting 'You are so certain' or 'So certain you are' (which should it be?) in the examples doesn't work. :(

    And none of my feeble tries to add words did the trick.

    That aside I love it. Big ++ for this one you will receive. :)


    So certain are you. - Yoda
      Putting 'You are so certain' or 'So certain you are' (which should it be?)

      The original sentence should be, "Are you so certain?". Interogatives in general won't work because English has already inverted them, so the pattern for making them come out backwards is itself backwards. This could be solved by looking for '?' and using a modified algorithm.


      for(unpack("C*",'GGGG?GGGG?O__\?WccW?{GCw?Wcc{?Wcc~?Wcc{?~cc' .'W?')){$j=$_-63;++$a;for$p(0..7){$h[$p][$a]=$j%2;$j/=2}}for$ p(0..7){for$a(1..45){$_=($h[$p-1][$a])?'#':' ';print}print$/}
Re: Yoda Speak Translator
by Putzfrau (Beadle) on Apr 26, 2002 at 17:24 UTC
    mmm... Strong with this one the force is

    I had a lot of fun playing around with this nice little piece of code... might even right a jabba translator myself...
    One thing though, if the inputed phrase contains more than one pivot point more often then not it'll end up garbled:
    do not try to understand me... becomes
    to understand me do not try

    Still this made my day

    "when a new client is created, we have to kill all the children..." --Sams Teach yourself Perl 5
      To understand me; do not try. -- Makes sense to me :-)
      Eg: To wash between my toes; do not I try.

      It doesn't mean: "In order to understand me: do not try." As that would imply by not trying to understand, my understanding would be fullfilled in the absence of. Total nonsense absent a deeper meaning that I try not to understand.

      It means: "To be understanding of me; do not you try." The way I understand it, it's simply a matter of English trying to overload sentances to be understood in multiple contexts.

      ( Sorry couldn't resist... )
(zdog) Re: Yoda Speak Translator
by zdog (Priest) on Apr 28, 2002 at 05:16 UTC
    Good stuff. But I'm pretty sure I've found a little problem. I think you need to buffer the pivot words with a space on each side in order not to mistake a part of a word for a pivot word. For example, the word 'fish' contains 'is' and an error is made.

    Zenon Zabinski | zdog | zdog@perlmonk.org

Re: Yoda Speak Translator
by $name (Pilgrim) on Apr 29, 2002 at 18:44 UTC
    Ah you seek Yoda.wav take you to him I might., but first must eat. yes good food come, good foo....
    MGW Aplications Developer QuinnTeam Inc.
Re: Yoda Speak Translator
by grep (Monsignor) on Apr 30, 2002 at 01:06 UTC

    Well, since there has been an overwhelmingly positive reaction to this post (actually I'm embarrased, this is not great code). I thought I would roll this into Acme::Yoda for everyone.

    I was thinking 3 methods 'yoda', 'deyoda', and 'wisdom'. 'yoda' and 'deyoda', would obviously translate, and 'wisdom' would spout out yoda-like wisdom for perl somewhat like Fletch's
    IDEs. Heh! Syntax hilighting. Heh! A perl hacker craves not these things. For my ally is the Source, and a powerful ally it is.

    So if anyone has a suggestion, I'm open (well as long as it easy, this is a joke module :) ).

    grep
    Unix - where you can thrown the manual on the keyboard and get a command

Re: Yoda Speak Translator
by ChOas (Curate) on May 02, 2002 at 10:23 UTC
    Very, VERY cool!!!!

    As the master said: "More than one way to do it there is" :
    sub yoda { my ($sentence) = @_; my $Pivot=join '|',qw/is be will show do try are teach have/; # Move along, nothing to see here return $sentence unless ($sentence=~/\b$Pivot\b/); # Rearrange our words $sentence="$' $`$&"; # Clear leading spaces and Capitalise $sentence =~ s/^\s+//; $sentence = ucfirst(lc($sentence)); };


    GreetZ!,
      ChOas

    print "profeth still\n" if /bird|devil/;
Re: Yoda Speak Translator
by raven_bd (Initiate) on May 03, 2002 at 09:53 UTC
    this is a real cool use for perl, it helped to get a good mood 8) thank you a lot

      ...

      Gone offline it has. Purely splintered off into server oblivion, the Yoda translator, it has gone!

      no more, is it.

      gone, it is.

      rendered inaccessible, has it indeed become.

      lost to the internet, it is!

      not able to gain access to it, can we.

      no more it is.

      Dead is the translator of Yoda.

      sad and woeful are we.

      no more cute yodisms will we read.

      a sad day this has become.

      a restored translator, must be done.

      to return the translator webpage, must he.

      or forever lost will be The Source.

      lay in the balance, the eternal destiny of the Yoda Translator, it does.

      hmmm, indeed must it be restored, or the author forever cajoled, will he be.

      miss it, we do.

      bring it back, he must.

      the Prophecy of restoring the balance with the returned translator, is this.

      the one, is he.

      the one to restore the balance, must he become.

      to return to the Yoda Translator service on the internet, must he do.

      And to upgrade his code, else The Source leave him, must he fear.

      Yes! all these things be accomplished, must he set out to do.

      be with him, all the Power of Perl, must be.

      or else lost without Yoda, are we.

      and rioting monks in the streets, will become.

      a grave prophecy, this is.



      LOL!!



      -- ProphetPX BRING BACK THE YODA TRANSLATOR PERL PAGE!! http://www.yoda-speak.org <-- miss it! :-P

Yoda Speak is Everywhere
by Guildenstern (Deacon) on May 17, 2002 at 17:46 UTC
    Heh. So, I'm reading the local paper's weekly entertainment section to see what Star Wars goodies they have, and I notice a guide on how to talk like Yoda. Further down, I hit upon: "a computer code writer with the handle "grep" invented a translator at www.perlmonks.com (search "Yoda Speak Translator") that turns normal sentences into Yoda-speak. Strong with the dork Force is this one. "

    Just thought it was interesting to have PM pop up in the paper. Grats on cool code!

    If you want to read the article: Gazette.com

    Guildenstern
    Negaterd character class uber alles!
Re: Yoda Speak Translator - well it's official
by grep (Monsignor) on Jun 17, 2002 at 03:19 UTC

    I have just released V.01 of the yoda speak (yodish) translator.

    Things that will be added site:

  • Papers on translating Yodaish
  • A downloadable module
  • Suggestion Box (until them /msg me)
  • Graphics :)
  • Things added to the module:

  • More accurate results
  • Handling contractions
  • Translating back to english
  • www.yoda-speak.org



    grep
    Just me, the boy and these two monks, no questions asked.
      #!/usr/bin/perl -w use strict; my @sentences = ('I will teach you', 'The truth is out there', 'That is an Imperial shuttle', 'My husband is giant dork', 'These fish are tasty', 'When you reach 900 years, you will not look as good' +, 'I can help you', 'You see the future', 'The future is always in motion', 'There is no try', 'You have found someone, hmmm', 'It can save you', 'Master Obi-Wan has lost a planet', 'It is difficult to see' ); foreach (@sentences) { my $speak=yoda($_); $speak =~ s/ i / I /g; print "$speak\n"; } sub yoda { my ($sentence) = @_; my $Pivot=join '|', qw/is be will show do try are teach have look +help see can learn has/; # Move along, nothing to see here return $sentence unless ($sentence=~/\b$Pivot\b/); # Rearrange our words $sentence="$' $`$&"; # Clear leading spaces and Capitalise $sentence =~ s/^\s+//; $sentence = ucfirst(lc($sentence)); }
        Very good, but you miss "has" to translate Lost a planet master obi-wan has which leaves obi-wan lowercase ;) Also there is a t missing from $pivot and a weird char at the end of sentenceX)) in $sentence = ucfirst(lc($sentence));

      Surely that should be:

      Yoda Speak Translator - official it is

      <groan>

Reaped: Re: Yoda Speak Translator
by NodeReaper (Curate) on Jun 14, 2005 at 01:08 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: CUFP [id://162190]
Approved by andye
Front-paged by ignatz
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (13)
As of 2014-09-17 19:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (97 votes), past polls