MY MEMO

[PYTHON] Rivescript 개념 본문

STUDYING/PYTHON

[PYTHON] Rivescript 개념

l_j_yeon 2017. 10. 15. 16:39

출처 : https://www.rivescript.com/docs/tutorial


.rive 파일


+ : trigger -> match the user's message

+) trigger is always lower cased 

   "Hello Bot", "HELLO BOT", or "hello bot" and it will match the trigger all the same.


- : command is how you define a reponse to a trigger

if you writing the many - command, rivescript will randomly choose the answer


Say something Random


+ say something random

- This {random}message|sentence{/random} has a random word.


Weight


give the importancy to the answer

You can not use weights inside a {random} tag.


+ greetings

- Hi there!{weight=20}

- Hello!{weight=25}

- Yos kyoco duckeb!


Line Breaking


if bot's answer is long, you can break the line , \n


+ tell me a poem

- Little Miss Muffit sat on her tuffet,\n

^ In a nonchalant sort of way.\n

^ With her forcefield around her,\n

^ The Spider, the bounder,\n

^ Is not in the picture today.


Space


To make sure there's a space between continuations, use the escape sequence \s where you want the space to appear.

but here is the example that you don't have to use \s


// Tell the parser to join continuation lines with line breaks

! local concat = newline


// Now we don't need to explicitly write the \n characters every time!

+ tell me a poem

- Little Miss Muffit sat on her tuffet,

^ In a nonchalant sort of way.

^ With her forcefield around her,

^ The Spider, the bounder,

^ Is not in the picture today.


// Now change the concat mode to spaces

! local concat = space


// Here we don't have to use \s like in the earlier example.

+ what are you

- I am an artificial intelligence programmed

^ using RiveScript.


// Go back to the default concatenation mode (which doesn't insert ANY

// character when joining lines)

! local concat = none


! : local concat setting changes the way the parser handles continuation lines


begin.rive


begin.rive that contains some configuration settings for your bot's brain. 

The most useful settings that would be set here include substitutions, 

which are able to make changes to the user's message before a reply is looked for.

+) Substitutions are always lowercased


! Command


! : command is used for Definitions (just like the + is for Triggers and the - is for Replies).


Comment Command


// characters are for. Like in most programming languages, 

RiveScript allows you to include Comments in your source code. 


Open Ended Triggers


Wildcards come into play. With wildcards, you can mark a part of the trigger as being open-ended.


+ my name is *

- Nice to meet you, <star1>!


+ * told me to say *

- Why would <star1> tell you to say "<star2>"?

- Did you say "<star2>" after <star1> told you to?


+ i am * years old

- A lot of people are <star1> years old.


catch-all trigger


+ *

- I'm not sure how to reply to that.

- Try asking your question a different way.

- Let's change the subject.


Specialized Wildcards


# symbol : a wildcard that will only match a number. 

_ symbol : one that will only match a word with no numbers or spaces in it.


+ i am # years old

- A lot of people are <star> years old.


+ i am _ years old

- Tell me that again but with a number this time.


+ i am * years old

- Can you use a number instead?


Alternatives and Optionals


+ what is your (home|office|cell) number

- You can reach me at: 1 (800) 555-1234.


+ i am (really|very|super) tired

- I'm sorry to hear that you are <star> tired.


+ i (like|love) the color *

- What a coincidence! I <star1> that color too!

- I also have a soft spot for the color <star2>!

- Really? I <star1> the color <star2> too!

- Oh I <star1> <star2> too!


+ how [are] you

- I'm great, you?


+ what is your (home|office|cell) [phone] number

- You can reach me at: 1 (800) 555-1234.


+ i have a [red|green|blue] car

- I bet you like your car a lot.


+ [*] the machine [*]

- How do you know about the machine!?


Arras in Triggers


+ what color is my (red|blue|green|yellow) *

- Your <star2> is <star1>, silly!


=> you can define color instead


! array colors = red blue green yellow


+ what color is my (@colors) *

- Your <star2> is <star1>, silly!

- Do I look dumb to you? It's <star1>!


=> a lot of ways to define the definition


// Single word array items

! array colors = red blue green yellow


// Multiple word items

! array blues = light blue|dark blue|medium blue

+ i am wearing a (@colors) shirt

- Do you really like <star>?


// A lot of colors!

! array colors = red blue green yellow orange cyan fuchsia magenta

^ light red|dark red|light blue|dark blue|light yellow|dark yellow

^ light orange|dark orange|light cyan|dark cyan|light fuchsia

^ dark fuchsia|light magenta|dark magenta

^ black gray white silver

^ light gray|dark gray


Priority Triggers


+ google *

- Google search: <a href="http://google.com/search?q=<star>">Click Here</a>


+ * perl script

- You need Perl to run a Perl script.


more commands Redirections


+ hello

- Hi there!

- Hey!

- Howdy!


+ hey

@ hello


+ hi

@ hello


+ * or something {weight=100}

- Or something. {@ <star>}


=> If you just want to use {@ <star>}, you can use a shortcut tag instead: <@>. Use the {@...} format for everything else:


+ hello *

- {@ hello} <@>


+ hello

- Hi there!


+ are you a bot

- How did you know I'm a machine?


Short Discussions : % command (Previous)


+ knock knock

- Who's there?


+ *

% who is there

- <star> who?


+ *

% * who

- LOL! <star>! That's funny!


=> In the % command, the bot's previous response is sent through the same substitutions as the user's messages 


+ i have a dog

- What color is it?


+ (@colors)

% what color is it

- That's a silly color for a dog!


Conditionals : Learning Things


+ my name is *

- <set name=<star>>It's nice to meet you, <get name>.


+ what is my name

- Your name is <get name>, silly!


+ i am # years old

- <set age=<star>>I will remember that you are <get age> years old.


+ how old am i

- You are <get age> years old.


+ what is your name

- You can call me <bot name>.

- My name is <bot name>.


+ how old are you

- I am <bot age> years old.


=> The <formal> tag is a shortcut for {formal}<star>{/formal}, 

so you will need to use the {formal}...{/formal} syntax to formalize other things.


+ my name is *

- <set name=<formal>>Nice to meet you, <get name>!


Writing Conditionals


+ what is my name

* <get name> == undefined => You never told me your name.

- Your name is <get name>, silly!

- Aren't you <get name>?


+ my name is *

* <formal>   == <bot name> => Wow, we have the same name!<set name=<formal>>

* <get name> == undefined  => <set name=<formal>>Nice to meet you!

- <set oldname=<get name>><set name=<formal>>

^ I thought your name was <get oldname>?


==  equal to

eq  equal to (alias)

!=  not equal to

ne  not equal to (alias)

<>  not equal to (alias)

<   less than

<=  less than or equal to

>   greater than

>=  greater than or equal to


+ what am i old enough to do

* <get age> == undefined => I don't know how old you are.

* <get age> >  25 => You can do anything you want.

* <get age> == 25 => You're old enough to rent a car with no extra fees.

* <get age> >  21 => You're old enough to drink, but not rent a car.

* <get age> == 21 => You're exactly old enough to drink.

* <get age> >  18 => You're old enough to gamble, but not drink.

* <get age> == 18 => You're exactly old enough to gamble.

- You're not old enough to do much of anything yet.


Topics


+ i hate you

- You're really mean! I'm not talking again until you apologize.{topic=sorry}


> topic sorry


  // This will match if the word "sorry" exists ANYWHERE in their message

  + [*] sorry [*]

  - It's OK, I'll forgive you!{topic=random}


  + *

  - Nope, not until you apologize.

  - Say you're sorry!

  - Apologize!


< topic


> begin


  // If we don't know their name, set the new_user topic and continue.

  + request

  * <get met> == undefined => <set met=true>{topic=new_user}{ok}

  - {ok}


< begin


> topic new_user


  + *

  - Hi! I'm <bot name>! I'm a chatbot written in RiveScript.\s

  ^ What is your name?{topic=asked_name}


< topic


> topic asked_name


  + #

  - Your name is a number?


  + *

  - I only want your first name.


  + _

  - <set name=<formal>>Nice to meet you, <get name>!{topic=random}


< topic


//here is another example that would use the post-processing capability for the Begin Block:

> begin


  // Change the reply formatting based on the bot's mood

  + request

  * <bot mood> == happy => {sentence}{ok}{/sentence}

  * <bot mood> == angry => {uppercase}{ok}{/uppercase}

  * <bot mood> == sad   => {lowercase}{ok}{/lowercase}...

  - {ok}


< begin


// The object name is "hash", written in Perl

> object hash perl

  my ($rs, $args) = @_;

  my $method = shift @{$args};

  my $string = join " ", @{$args};


  # Here, $method is a hashing method (MD5 or SHA1), and $string

  # is the text to hash.


  if ($method eq "MD5") {

    require Digest::MD5;

    return Digest::MD5::md5_hex($string);

  }

  elsif ($method eq "SHA1") {

    require Digest::SHA1;

    return Digest::SHA1::sha1_hex($string);

  }

< object


// You call an object using the <call> tag.

+ what is the md5 hash of *

- The hash of "<star>" is: <call>hash MD5 <star></call>


+ what is the sha1 hash of *

- The hash of "<star>" is: <call>hash SHA1 <star></call>


// To gain botmaster power, say "I am your master"...

+ i am your master

- Then you must know the secret password:


// And then enter the botmaster password...

+ *

% then you must know the secret password

* <star> == rivescript is awesome => Correct password!<set master=true>

- That's not the right password. :-P


// And after authenticated, let them get the bot's IP address!

+ what is your ip address

* <get master> == true => My IP address is: <call>myip</call>

- You're not my master so you don't need to know! :-P


// The object macro that fetches an IP address.

> object myip perl

  my ($rs, $args) = @_;


  # Fetch the IP.

  use LWP::Simple;

  my $ip = get "http://icanhazip.com";

  return $ip;

< object


Global Variables


Debug Mode


! global debug = true

! global debug = false

! global depth = 50


Because RiveScript replies can redirect to each other, there is some protection in place to avoid infinite recursion 

(for example, a trigger that redirects to another one, and that trigger redirects back to the first one, forever). 

By default the recursion depth limit is set to 50, meaning if RiveScript can't find a reply after 50 redirects 

it will give up and say "ERR: Deep Recursion Detected"


+ i bought a new *

- Oh? What color is your new <star>?


+ (@colors)

% oh what color is your new *

- <star> is a pretty color for a <botstar>.


// If the user repeats the bot's previous message

+ <reply>

- Don't repeat what I say.


// If the user keeps repeating themselves over and over.

+ <input1>

* <input1> == <input2> => That's the second time you've repeated yourself.

* <input1> == <input3> => If you repeat yourself again I'll stop talking.

* <input1> == <input4> => That's it. I'm not talking.{topic=sorry}

- Please don't repeat yourself.


// An example that uses both tags

+ why did you say that

- I said, "<reply>", because you said, "<input>".


! var master = kirsle


+ am i your master

* <id> == <bot master> => Yes, you are. Hi Kirsle!

- No, <bot master> is my master, and you are <id>.


=> The <bot> tag is used for retrieving a bot variable. It can also be used to set a bot variable.


+ what is your name

- You can call me <bot name>.


+ tell me about yourself

- I am <bot name>, a chatterbot written by <bot master>.


// Setting a bot variable dynamically

+ i hate you

- Aww! You've just ruined my day.<bot mood=depressed>


=> The <env> tag is used for retrieving global variables. It can also be used to set a global variable.


+ what is my ip

- Your IP address is: <env REMOTE_ADDR>


+ set debug mode (true|false)

* <id> == <bot master> => <env debug=<star>>Debug mode set to <star>.

- You're not my master.


{@}, <@>

This tag performs an inline redirection to a different trigger. <@> is an alias for {@ <star>}


+ your *

- I think you mean to say "you are" or "you're", not "your". {@you are <star>}


=> Formal text makes the first letter of each word uppercase. 

This is useful for names and other proper nouns.

Sentence text makes the first word of each sentence uppercase.

Uppercase and lowercase make the entire string upper or lower case.


// Call a macro named "reverse" and give it an argument

+ say * to me in reverse

- <call>reverse <star></call>



'STUDYING > PYTHON' 카테고리의 다른 글

[PYTHON] Rivescript 추가  (0) 2017.10.16
[PYTHON] pycharm 오류 해결  (0) 2017.10.16
[PYTHON] 추가  (0) 2017.10.15
[PYTHON] 개념 다지기 - 2  (0) 2017.10.07
[PYTHON] 개념 다지기 -1  (0) 2017.10.06
Comments