AIML recognizing middle letter of any word - aiml

In aiml i am trying to make my bot recognize of middle letter of any word. for exemple if i ask "What is the middle letter of cat" then it should response "A" this is what code looks like
<category><pattern>MIDDLELETTER *</pattern>
<template>The middle letter of <star/> is: <srai>MIDDLEITEM <explode><star/></explode></srai></template>
</category>
<category><pattern>MIDDLEITEM * * *</pattern>
<template><srai>MIDDLEITEM <star index="2"/></srai></template>
</category>
<category><pattern>MIDDLEITEM *</pattern>
<template><star/></template>
</category>
The above code works fine in this scenario
HUMAN: MIDDLELETTER CAT
ROBOT: The middle letter of CAT is: A
HUMAN: MIDDLELETTER SKY
ROBOT: The middle letter of SKY is: K
But does not work properly in this case
HUMAN: MIDDLELETTER ROMAN
ROBOT: The middle letter of ROMAN is: O
HUMAN: MIDDLELETTER DCNOVAN
ROBOT: The middle letter of DCNOVAN is: C
I was expecting these kind of results
HUMAN: MIDDLELETTER ROMAN
ROBOT: The middle letter of ROMAN is: M
HUMAN: MIDDLELETTER DCNOVAN
ROBOT: The middle letter of DCNOVAN is: O
Also, what if if there are 8 letters in an word? i am expecting to make it work like
HUMAN: MIDDLELETTER ABSOLUTE
ROBOT: The middle letter of ABSOLUTE could be either O or L
What am i missing in my code?

To find the middle letter, you need to break up the word into individual letters and work out how long the word is. You can then use the length of the word to find the middle letter.
This method will work. For compatibility, I've assumed you have no math capabilities in your bot ie division. It works up to letters that are 11 letters long. For longer words, you should divide the word length by 2 to find the middle letter of a word.
<category>
<pattern>MIDDLELETTER *</pattern>
<template>
<think>
<set name="letternum">0</set>
<srai>BreakUpWord <explode><star/></explode></srai>
<condition name="letternum">
<li value="1"><set name="middle"><get name="char1"/></set></li>
<li value="2"><set name="middle"><get name="char1"/> or <get name="char2"/></set></li>
<li value="3"><set name="middle"><get name="char2"/></set></li>
<li value="4"><set name="middle"><get name="char2"/> or <get name="char3"/></set></li>
<li value="5"><set name="middle"><get name="char3"/></set></li>
<li value="6"><set name="middle"><get name="char3"/> or <get name="char4"/></set></li>
<li value="7"><set name="middle"><get name="char4"/></set></li>
<li value="8"><set name="middle"><get name="char4"/> or <get name="char5"/></set></li>
<li value="9"><set name="middle"><get name="char5"/></set></li>
<li value="10"><set name="middle"><get name="char5"/> or <get name="char6"/></set></li>
<li value="11"><set name="middle"><get name="char6"/></set></li>
</condition>
</think>
The middle letter of <star/> is <get name="middle"/>.
</template>
</category>
<category>
<pattern>BREAKUPWORD * *</pattern>
<template>
<srai>IncreaseLetternum <star/></srai>
<srai>BREAKUPWORD <star index="2"/></srai>
</template>
</category>
<category>
<pattern>BREAKUPWORD *</pattern>
<template>
<srai>IncreaseLetternum <star/></srai>
</template>
</category>
<category>
<pattern>INCREASELETTERNUM *</pattern>
<template>
<think>
<condition name="letternum">
<li value="0"><set name="letternum">1</set><set name="char1"><star/></set></li>
<li value="1"><set name="letternum">2</set><set name="char2"><star/></set></li>
<li value="2"><set name="letternum">3</set><set name="char3"><star/></set></li>
<li value="3"><set name="letternum">4</set><set name="char4"><star/></set></li>
<li value="4"><set name="letternum">5</set><set name="char5"><star/></set></li>
<li value="5"><set name="letternum">6</set><set name="char6"><star/></set></li>
<li value="6"><set name="letternum">7</set><set name="char7"><star/></set></li>
<li value="7"><set name="letternum">8</set><set name="char8"><star/></set></li>
<li value="8"><set name="letternum">9</set><set name="char9"><star/></set></li>
<li value="9"><set name="letternum">10</set><set name="char10"><star/></set></li>
<li value="10"><set name="letternum">11</set><set name="char11"><star/></set></li>
</condition>
</think>
</template>
</category>

Related

How to use AIML to find category out of topic?

I have been using AIML to make chatbot. I want search category out of topic after set data in topic. But, i don't have idea for that
set day
set class
search category out of topic use day and class
How could I do that?
Thanks in advance.
<category>
<pattern>SCHEDULE</pattern>
<template>
<think><set name="topic">SCHEDULE</set></think>
day ?</template>
</category>
<topic name="SCHEDULE">
<category>
<pattern><set>setdays</set></pattern>
<template>
<think><set name="day"><star/></set></think>
<condition name="day">
<li value="Monday"><set name="day"><star/></set> for class?</li>
<li value="Tuesday"><set name="day"><star/></set> for class?</li>
<li value="Wednesday"><set name="day"><star/></set> for class?</li>
<li value="Thursday"><set name="day"><star/></set> for class?</li>
<li value="Friday"><set name="day"><star/></set> for class?</li>
<li value="Saturday"><set name="day"><star/></set> for class?</li>
<li value="Sunday"><set name="day"><star/></set> for class?</li>
<li>not found</li>
</condition>
</template>
</category>
<category>
<pattern><set>setclass</set></pattern>
<template>
<think>
<set name="search"><get name="day"/> <set name="class"></set></set>
After set class i want to search category out of topic
</think>
</template>
</category>
</topic>
To search out of topic, reset your topic name to something that doesn't exist. Also you need <star/> instead of <set name="class"></set> in your template.
<category>
<pattern><set>setclass</set></pattern>
<template>
<think>
<set name="search"><get name="day"/> <star/></set>
<set name="topic">none</set>
</think>
</template>
</category>

how do i calculate how many days until monday in aiml

I cannot get this aiml script for calculationg how many days from this day till Monday.
I make chatbots on botlibre.
I tried the code I am going to post.
<category>
<pattern>HOW MANY DAYS UNTIL monday</pattern>
<template>
<interval>
<jformat>ddddd </jformat>
<style>days</style>
<from><date jformat="ddddd"/></from>
<to>saturday</to>
</interval>
days until saturday.
</template>
</category>
I expect the aiml script to display in the chatbot response how many days
until Monday.
that worked thanks i needed that.
i made a chatbot named pioyu on botlibre that acquires likes,dislikes and opinion about why people like or dislike something.
she makes them her own.
That is how I make my chatbot seem more human.
It could acquire sentences of things people have as well if they tell it and make them it's
own.
Ok, so first we need to know what day it is and set it in a predicate called "day". The format part is UNIX strftime and is explained here: https://devhints.io/datetime
We then check the value of "day" and can display a suitable response, including saying "day" rather than "days" when it's Sunday.
<category>
<pattern>HOW MANY DAYS UNTIL MONDAY</pattern>
<template>
<think><set name="day"><date format="%A"/></set></think>
<condition name="day">
<li value="Monday">0 days </li>
<li value="Tuesday">6 days </li>
<li value="Wednesday">5 days </li>
<li value="Thursday">4 days </li>
<li value="Friday">3 days </li>
<li value="Saturday">2 days </li>
<li value="Sunday">1 day </li>
</condition>
until Monday.
</template>
</category>

Replacing HTML text elements with increment variable

In the below HTML part, I want to replace, whenever a text is found, with an incremental variable:
<li class="cat-item">
<a href="#" >Beautiful Reclessness</a>
</li>
<li class="cat-item">
<a href="#" >Comfort vs. Appearance</a>
</li>
<li class="cat-item">
<a href="#" >Highlights of the Runway</a>
<ul class='children'>
<li class="cat-item">
<a href="#" >Christian Louboutin Show</a>
</li>
<li class="cat-item">
<a href="#" >Givenchy F/W 2016</a>
</li>
<li class="cat-item">
<a href="#" >Spring by Gaultier</a>
To this using the x++ increment:
<li class="cat-item">
<a href="#" >x1</a>
</li>
<li class="cat-item">
<a href="#" >x2</a>
</li>
<li class="cat-item">
<a href="#" >x3</a>
<ul class='children'>
<li class="cat-item">
<a href="#" >x4</a>
</li>
<li class="cat-item">
<a href="#" >x5</a>
</li>
<li class="cat-item">
<a href="#" >x6</a>
Is there a way in Notepad++ or Vim (looking for in between > <) to do find the text contents using REGEX and replace them with an x counter?
Simple vim answer:
Open the file—vim filename
Set up a convenience variable—:let num=1
Do the replaclement—:g/href/execute printf("normal! citx%d", num) | let num=num+1
The :global command allows one to perform an operation all lines matching a pattern (in this case, href). The operation we want to do is change the text inside the <a> tag to x followed by the contents of num, and increment num.
execute lets us build a command line from strings; I often combine with printf() because I find it easier to read. normal! is an Ex-command that lets us execute normal-mode commands. cit is a vim'ism for "change inside tag" from normal mode. Then we just feed it the appropriate replacement text (x%d) and increment the counter.
If you're wondering how I came up with this, it's a pretty well-established pattern among vimmers. In practice, it took me probably about a minute to get the whole sequence done (faster if I used it more often), so it isn't one of those "spend 30 minutes trying to write a good regex" answers—this can be done in a live editing session without too much thought, if the person editing has a good grasp of vim fundamentals.
Hope that helps.
Download python script plugin
plugins > python script > new script > save as "increment.py"
Develop your regex at regex101 or somewhere else and write the script
i=0
def increment(match):
global i
i=i+1
return "x"+str(i)
editor.rereplace('(?<=>)\\b[^><]+', increment)
Save and run your script: plugins > python script > scripts > increment
A slightly different approach on vim:
:let c=1 | g/a href="#" >\zs.*\ze</ s//\='x'.c/g | let c+=1
Using \zs and \ze we can select the pattern we want to remove. The counter
will gives the number sequence concatenated with space:
\='x'.c ................. concatenate 'x' with the counter

BeautifulSoup not able to parse perfectly

When I am using soup.find("h3", text="Main Address:").find_parents("section"), I am getting an output which is:
[<section class="otlnrw" itemscope="" itemtype="http://microformats.org/wiki/hCard">\n<header>\n<h3 i
temprop="name">Main Address:</h3>\n</header>\n<p>600 Dexter <abbr title="Avenue\r"><abbr title="Avenu
e\r">Ave.</abbr></abbr><br/><span class="locality">Montgomery</span>, <span class="region">AL</span>,
<span class="postal-code">36104</span></p> </section>]
Now I want to print only paragraph's text. I am not able to do that. Please tell me how can I print from here only text which is inside this paragraph of the section.
Or my HTML page is like this:
<article>
<header>
<h2 id="state-government">State Government</h2>
</header>
<section itemscope="" itemtype="http://microformats.org/wiki/hCard" class="otln">
<header><h3 itemprop="name">Official Name:</h3></header>
<p>Alaska
</p>
</section>
<section itemscope="" itemtype="http://microformats.org/wiki/hCard" class="otlnrw">
<header><h3 class="org">Governor:</h3></header>
<p>Bill Walker</p>
</section>
<section itemscope="" itemtype="http://microformats.org/wiki/hCard" class="otln">
<header><h3 itemprop="name">Main Address:</h3></header>
<p>120 East 4th Street<br>
<span class="locality">Juneau</span>,
<span class="region">AK</span>,
<span class="postal-code">99801</span></p>
</section>
<section itemscope="" itemtype="http://microformats.org/wiki/hCard" class="otlnrw">
<header><h3 itemprop="name">Phone Number:</h3></header>
<p class="spk tel">907-465-3708</p>
</section>
<p class="volver clearfix"><a href="#skiptarget">
<span class="icon-backtotop-dwnlvl">Back to Top</span></a></p>
<section>
<header><h2 id="state-agencies">State Agencies</h2></header>
<ul>
<li>Consumer Protection Offices</li>
<li>Corrections Department</li>
<li>Election Office</li>
<li>Motor Vehicle Offices</li>
<li>Surplus Property Sales</li>
<li>Travel and Tourism</li>
</ul>
</section>
<p class="volver clearfix"><a href="#skiptarget">
<span class="icon-backtotop-dwnlvl">Back to Top</span></a></p>
</article>
How should I get the address from it only text.
Your current code returns a list with one element. To get the <p> element in it, you can expand it a bit:
soup.find("h3", text="Main Address:").find_parents("section")[0]("p")
If you want to get what is inside that p element, you'll have to get the first element of that list again, and run decode_contents on it:
soup.find("h3", text="Main Address:").find_parents("section")[0]("p")[0].decode_contents(formatter="html")
In your case that will return:
u'120 East 4th Street<br/><span class="locality">Juneau</span>, <span class="region">AK</span>, <span class="postal-code">99801</span>'

Regular expression to remove lines with special characters

<a class='jdr' href='javascript:void(0);' onClick="return openDiv('jrtp');"></a>
<span class="jcn">
<a href="http://example.com/Ahmedabad/Aptech-N-Power-Hardware-Networking-<near>-Toll-Naka-Opp-Kakadia-Hospital-Below-Sankalp-Reataurant-Bapu-Nagar/079PXX79-XX79-110420173655-D4K6_QWhtZWRhYmFkIENDTkEgVHJhaW5pbmcgSW5zdGl0dXRlcw==_BZDET" title='Aptech N Power Hardware & Networking' >Aptech N Power Hardware & Networkin...</a>
</span>
<section class="jrat">
<a rel="nofollow" href="http://example.com/Ahmedabad/Aptech-N-Power-Hardware-Networking-<near>-Toll-Naka-Opp-Kakadia-Hospital-Below-Sankalp-Reataurant-Bapu-Nagar/079PXX79-XX79-110420173655-D4K6_QWhtZWRhYmFkIENDTkEgVHJhaW5pbmcgSW5zdGl0dXRlcw==_BZDET#rvw"><span class='s10'></span><span class='s10'></span><span class='s10'></span><span class='s10'></span><span class='s0'></span></a>
<a class="jrt" href="http://example.com/Ahmedabad/Aptech-N-Power-Hardware-Networking-<near>-Toll-Naka-Opp-Kakadia-Hospital-Below-Sankalp-Reataurant-Bapu-Nagar/079PXX79-XX79-110420173655-D4K6_QWhtZWRhYmFkIENDTkEgVHJhaW5pbmcgSW5zdGl0dXRlcw==_BZDET#rvw">2 ratings</a>
<span class="jrt"> |</span>
<a class="rate_this" onclick="_ct('ratethis','lspg');" href="http://example.com/Ahmedabad/Aptech-N-Power-Hardware-Networking-<near>-Toll-Naka-Opp-Kakadia-Hospital-Below-Sankalp-Reataurant-Bapu-Nagar/079PXX79-XX79-110420173655-D4K6_QWhtZWRhYmFkIENDTkEgVHJhaW5pbmcgSW5zdGl0dXRlcw==_BZDET/writereview">Rate this</a>
</section>
<section class="jcar">
<section class="jbc">
<a href="http://example.com/Ahmedabad/Aptech-N-Power-Hardware-Networking-<near>-Toll-Naka-Opp-Kakadia-Hospital-Below-Sankalp-Reataurant-Bapu-Nagar/079PXX79-XX79-110420173655-D4K6_QWhtZWRhYmFkIENDTkEgVHJhaW5pbmcgSW5zdGl0dXRlcw==_BZDET">
<img width="83" height="56" border="0" src="http://images.jdmagicbox.com/upload_test/ahmedabad/b4/079pxx79.xx79.110420172948.d4b4/logo/faf3f2409ed7993aaa70f848ab0bb6fb_t.jpg" class="Clogo" />
</a>
<!-- <span class="noLogo"></span> -->
<section class="jrcl">
<p>
**A/35, Lakhani Chamber, Toll Naka, Opp Kakadia Hospital, Below Sankalp Reataurant, Bapu Nagar, Ahmedabad - 380024** | View Map<br>
</p>
From the above XML data I want to extract the following---
A/35, Lakhani Chamber, Toll Naka, Opp Kakadia Hospital, Below Sankalp Reataurant, Bapu Nagar, Ahmedabad - 380024
I need help in creating a regular expression to find and remove all lines containing special characters.
I am using the following regex ----
/(\<.+?>)/g
Please help.Thanks
Try this
/(?<=\*{2})([^<>]*?)(?=\*{2})/g
it matches all content between the **.
I think you want to remove lines which are HTML tags, so try this:
/^<.*>\n/g