Automatically Generating Rivescript from Natural Language|Cerin|chrisspen@gmail.com|1227013586|Cerin|xx|0|76.124.21.185|Is there any tool or method for generating Rivescript from natural language like IM conversation, emails, newsgroup threads, etc?||||
Re: Automatically Generating Rivescript from Natural Language|Kirsle|casey@cuvou.net|1227052152|Kirsle|xx|0|72.37.252.50|One doesn't currently exist but it wouldn't be too difficult to make one...<br /><br />[code]#!/usr/bin/perl -w<br /><br /># read a two-person convo from a filehandle<br />my @convo = &lt;DATA&gt;;<br />chomp @convo;<br /><br /># keep track of (multiple?) replies to one message<br />my $replies = &#123;&#125;;<br /><br /># read the convo<br />my $lastTrigger = ''; # last thing Red said<br />my $newTrigger = 0; # this becomes 1 on Red's msgs<br />foreach my $line (@convo) &#123;<br />  if ($line =~ /^Red: (.+?)$/i) &#123;<br />   # Red's messages will be our triggers<br />   my $trigger = $1;<br /><br />   # Format it to work in RiveScript<br />   $trigger =~ s/[^A-Za-z0-9 ]//g; # remove symbols<br />   $trigger = lc($trigger); # lowercase<br />   # trim extra spaces<br />   $trigger =~ s/^\s+//g; $trigger =~ s/\s+$//g;<br /><br />   # store it temporarily<br />   $lastTrigger = $trigger;<br />   $newTrigger = 1;<br />  &#125;<br />  elsif ($line =~ /^Blue: (.+?)$/i) &#123;<br />   # Blue's messages are the response.<br />   if (not $newTrigger) &#123;<br />    # Blue replied twice to this IM, add it to the last one.<br />    $replies-&gt;&#123;$lastTrigger&#125;-&gt;[-1] .= &quot;\\n&quot; . $1;<br />   &#125;<br />   else &#123;<br />     push (@&#123;$replies-&gt;&#123;$lastTrigger&#125;&#125;, $1);<br />   &#125;<br />   $newTrigger = 0;<br />  &#125;<br />&#125;<br /><br /># write it out<br />open (OUT, &quot;&gt;convo.rs&quot;);<br />foreach my $trigger (sort keys %&#123;$replies&#125;) &#123;<br />  print OUT &quot;+ $trigger\n&quot;;<br />  foreach my $reply (sort @&#123;$replies-&gt;&#123;$trigger&#125;&#125;) &#123;<br />   print OUT &quot;- $reply\n&quot;;<br />  &#125;<br />&#125;<br />close (OUT);<br /><br />__DATA__<br />Red: hello<br />Blue: hi<br />Red: what's up?<br />Blue: not much<br />Blue: you?<br />...<br />Red: hello<br />Blue: hey[/code]<br /><br />might end up with something like<br /><br />[code]+ hello<br />- hi<br />- hey<br /><br />+ whats up<br />- not much\nyou?[/code]<br /><br />Natural language is a very difficult thing to work with for a program though. But if you just want to have it read over conversations to get a ton of RiveScript replies out of it, you can do something sorta like that. (to generate any RiveScript code with wildcards or anything intelligent like that would be a lot more work and ya might wonder if ya shouldn't be working for some artificial intelligence company if ya managed to pull it off :P ).<br /><br />The code I posted above would really only work to parse a one-to-one conversation but it wouldn't take context into account... i.e. if the conversation had a lull and the person who sent the last message sends a new one to strike up a new convo, it would be included as part of the response to the last message sent by the other person.<br /><br />I personally just think it's quicker to write the code myself than to try to programmatically parse natural language and then have to skim through it and fix the countless errors it made. &nbsp;::)||||
