-->

pyaiml does not respond on tag

2019-07-15 05:45发布

问题:

I am trying the PyAiml package to write a chatbot. I wrote a very basic program with all those default aiml files from A.L.I.C.E. Everything works fine so far except the <that> tag. I thought it was the session problem. Then I fixed the session. But still no luck with <that> tag for contextual conversation. Anyone knows how to make it work? Or the PyAiml has some bug with <that> tag parsing?

Here is my bot program and a very minimal aiml file I am testing with:

testbot.py

import aiml
import marshal
import os
from pprint import pprint

BOOTSTRAP_FILE = "/var/www/html/chatbot/std-startup.xml"
BOT_SESSION_PATH = "/var/www/html/chatbot/"

sess_id = 'user_id_moshfiqur'

while True:
    k = aiml.Kernel()
    k.bootstrap(learnFiles=BOOTSTRAP_FILE, commands="load aiml b")

    if os.path.isfile(BOT_SESSION_PATH + sess_id + ".ses"):
        sessionFile = file(BOT_SESSION_PATH + sess_id + ".ses", "rb")
        sessionData = marshal.load(sessionFile)
        sessionFile.close()

        for pred, value in sessionData.items():
            k.setPredicate(pred, value, sess_id)

    response = k.respond(raw_input(">> "), sessionID=sess_id)

    sessionData = k.getSessionData(sess_id)
    pprint(sessionData)
    sessionFile = file(BOT_SESSION_PATH + sess_id + ".ses", "wb")
    marshal.dump(sessionData, sessionFile)
    sessionFile.close()

    pprint("<< " + response)

minimal.aiml

<aiml version="1.0.1" encoding="UTF-8">
    <category>
        <pattern>TEST1</pattern>
        <template>testing one</template>
    </category>
    <category>
        <pattern>TEST2</pattern>
        <that>testing one</that>
        <template>Success</template>
    </category>
</aiml>

回答1:

Regarding your <that> tag issue, all I can tell you is that it's fine on the AIML part, what I came to offer is an alternative to using that tag (if that's how you were planning to use it):

<category>
      <pattern>TEST1</pattern>
      <template>testing one<think>
          <set name="xfunc">XTEST2</set>
      </think></template>
  </category>

  <category>
      <pattern>XTEST2</pattern>
      <template>Success</template>
  </category>

  <category>
      <pattern>TEST2</pattern>
      <template><condition name="xfunc">
          <li value="xxnull"><srai>XDEFAULT ANSWER</srai></li>
          <li value="*"><think>
              <set var="temp"><get name="xfunc"/></set>
              <set name="xfunc">xxnull</set>
          </think><srai><get var="temp"/></srai></li>
          <li><srai>XDEFAULT ANSWER</srai></li>
      </condition></template>
  </category>

  <category>
      <pattern>*</pattern>
      <template><srai>XDEFAULT ANSWER</srai></template>
  </category>

  <category>
      <pattern>XDEFAULT ANSWER</pattern>
      <template>Bad input</template>
  </category>

The above will save the function that leads to the next part of the conversation and then let it be used if there's an answer that actually has use for the variable that triggers it, this is useful in situations where you have a pattern that says "yes" for example, and is needed for many categories. Do note that there's more to improve on this code to make it more fluent. Let me know if you found this helpful and want me to expand on it :)



回答2:

   <category>
    <pattern>TEST THAT</pattern>
    <template>DO YOU LIKE MOVIES</template>
 </category>

 <category>
    <pattern>YES</pattern>
    <that>DO YOU LIKE MOVIES</that>
    <template> What's your favorite movie? </template>
 </category>

For me this is working...



标签: python aiml