TYING IT TOGETHER: DEVELOPING A LARGER RUBY APPLICATION in Font

Generator Data Matrix ECC200 in Font TYING IT TOGETHER: DEVELOPING A LARGER RUBY APPLICATION

CHAPTER 12 TYING IT TOGETHER: DEVELOPING A LARGER RUBY APPLICATION
Data Matrix 2d Barcode Encoder In None
Using Barcode creation for Font Control to generate, create ECC200 image in Font applications.
www.OnBarcode.com
Drawing UPC A In None
Using Barcode maker for Font Control to generate, create UPC-A Supplement 5 image in Font applications.
www.OnBarcode.com
These examples are more complex but prove that switch_pronouns can handle a few more complex situations with multiple pronouns. You can construct tests that cause switch_pronouns to fail:
Printing Data Matrix ECC200 In None
Using Barcode creation for Font Control to generate, create Data Matrix ECC200 image in Font applications.
www.OnBarcode.com
Barcode Maker In None
Using Barcode drawer for Font Control to generate, create Barcode image in Font applications.
www.OnBarcode.com
def test_complex_pronouns assert_equal("yes, i rule", WordPlay.switch_pronouns("yes, you rule")) assert_equal("why do i cry", WordPlay.switch_pronouns("why do you cry")) end
GS1 128 Drawer In None
Using Barcode creation for Font Control to generate, create UCC.EAN - 128 image in Font applications.
www.OnBarcode.com
Draw EAN 13 In None
Using Barcode maker for Font Control to generate, create EAN13 image in Font applications.
www.OnBarcode.com
These tests both fail, because they circumvent the trick you used to make sure that you is translated to me and I in the right situations. In these situations, they should become I, but because I isn t at the start of the sentence, they become me instead. It s important to notice that basic statements tend to work okay, whereas questions or more elaborate statements can fail. However, for your bot s purposes, the basic substitutions suffice. If you were to focus solely on producing an accurate language processor, you could use tests such as these to guide your development, and you ll probably use this technique when developing libraries to deal with edge cases such as these in your own projects.
Code 128 Code Set B Encoder In None
Using Barcode maker for Font Control to generate, create USS Code 128 image in Font applications.
www.OnBarcode.com
Generate Code11 In None
Using Barcode generation for Font Control to generate, create Code11 image in Font applications.
www.OnBarcode.com
WordPlay s Source Code
Data Matrix ECC200 Maker In Objective-C
Using Barcode generation for iPad Control to generate, create ECC200 image in iPad applications.
www.OnBarcode.com
ECC200 Printer In Java
Using Barcode printer for Android Control to generate, create Data Matrix 2d barcode image in Android applications.
www.OnBarcode.com
Your nascent WordPlay library is complete, for now, and in a state that you can use its features to make your bot s source code simpler and easier to read. Next I ll present the source code for the library as is, as well as its associated unit test file. As an addition, the code also includes comments prior to each class and method definition, so that you can use RDoc to produce HTML documentation files, as covered in 8.
Encoding Code-128 In Visual Studio .NET
Using Barcode drawer for ASP.NET Control to generate, create Code 128 Code Set B image in ASP.NET applications.
www.OnBarcode.com
Generate EAN / UCC - 13 In Java
Using Barcode creation for Java Control to generate, create EAN / UCC - 14 image in Java applications.
www.OnBarcode.com
Note Remember that source code for this book is available in the Source Code/Download area at
Creating Linear Barcode In Java
Using Barcode creator for Java Control to generate, create Linear Barcode image in Java applications.
www.OnBarcode.com
Draw Barcode In Objective-C
Using Barcode printer for iPhone Control to generate, create Barcode image in iPhone applications.
www.OnBarcode.com
http://www.apress.com, so it isn t necessary to type in code directly from the book.
QR Code Decoder In Visual Basic .NET
Using Barcode scanner for VS .NET Control to read, scan read, scan image in .NET applications.
www.OnBarcode.com
PDF 417 Drawer In .NET Framework
Using Barcode drawer for ASP.NET Control to generate, create PDF-417 2d barcode image in ASP.NET applications.
www.OnBarcode.com
wordplay.rb
Recognizing DataMatrix In VB.NET
Using Barcode scanner for .NET Control to read, scan read, scan image in Visual Studio .NET applications.
www.OnBarcode.com
ECC200 Printer In None
Using Barcode creator for Online Control to generate, create Data Matrix ECC200 image in Online applications.
www.OnBarcode.com
Here s the code for the WordPlay library:
UPC A Maker In None
Using Barcode printer for Software Control to generate, create UPC-A Supplement 5 image in Software applications.
www.OnBarcode.com
Paint Barcode In Java
Using Barcode generation for Java Control to generate, create Barcode image in Java applications.
www.OnBarcode.com
class String def sentences self.gsub(/\n|\r/, ' ').split(/\.\s*/) end
CHAPTER 12 TYING IT TOGETHER: DEVELOPING A LARGER RUBY APPLICATION
def words self.scan(/\w[\w\'\-]*/) end end
class WordPlay def self.switch_pronouns(text) text.gsub(/\b(I am|You are|I|You|Me|Your|My)\b/i) do |pronoun| case pronoun.downcase when "i" "you" when "you" "me" when "me" "you" when "i am" "you are" when "you are" "i am" when "your" "my" when "my" "your" end end.sub(/^me\b/i, 'i') end def self.best_sentence(sentences, desired_words) ranked_sentences = sentences.sort_by do |s| s.words.length - (s.downcase.words - desired_words).length end ranked_sentences.last end end
CHAPTER 12 TYING IT TOGETHER: DEVELOPING A LARGER RUBY APPLICATION
test_wordplay.rb
Here s the test suite associated with the WordPlay library:
require 'test/unit' require 'wordplay' # Unit testing class for the WordPlay library class TestWordPlay < Test::Unit::TestCase # Test that multiple sentence blocks are split up into individual # words correctly def test_sentences assert_equal(["a", "b", "c d", "e f g"], "a. b. c d. e f g.".sentences) test_text = %q{Hello. This is a test of sentence separation. This is the end of the test.} assert_equal("This is the end of the test", test_text.sentences[2]) end # Test that sentences of words are split up into distinct words correctly def test_words assert_equal(%w{this is a test}, "this is a test".words) assert_equal(%w{these are mostly words}, "these are, mostly, words".words) end # Test that the correct sentence is chosen, given the input def test_sentence_choice assert_equal('This is a great test', WordPlay.best_sentence(['This is a test', 'This is another test', 'This is a great test'], %w{test great this})) assert_equal('This is a great test', WordPlay.best_sentence(['This is a great test'], %w{still the best})) end # Test that basic pronouns are switched by switch_pronouns def test_basic_pronouns assert_equal("i am a robot", WordPlay.switch_pronouns("you are a robot")) assert_equal("you are a person", WordPlay.switch_pronouns("i am a person"))
CHAPTER 12 TYING IT TOGETHER: DEVELOPING A LARGER RUBY APPLICATION
assert_equal("i love you", WordPlay.switch_pronouns("you love me")) end # Test more complex sentence switches using switch_pronouns def test_mixed_pronouns assert_equal("you gave me life", WordPlay.switch_pronouns("i gave you life")) assert_equal("i am not what you are", WordPlay.switch_pronouns("you are not what i am")) end end
Building the Bot s Core
In the previous section you put together the WordPlay library to provide some features you knew that your bot would need, such as basic sentence and word separation. Now you can get on with the task of fleshing out the logic of the bot itself. You ll create the bot within a Bot class, allowing you to create multiple bot instances and assign them different names and datasets, and work with them separately. This is the cleanest structure, as it allows you to keep the bot s logic separated from the logic of interacting with the bot. For example, if your finished Bot class exists in bot.rb, writing a Ruby program to allow a user to converse with the bot using the keyboard could be as simple as this:
require 'bot' bot = Bot.new(:name => "Botty", :data_file => "botty.bot") puts bot.greeting while input = gets and input.chomp != 'goodbye' puts ">> " + bot.response_to(input) end puts bot.farewell
You ll use this barebones client program as a yardstick while creating the Bot class. In the previous example, you created a bot object and passed in some parameters, which enables you to use the bot s methods, along with keyboard input, to make the bot converse with the user.
Copyright © OnBarcode.com . All rights reserved.