Skip to content

Commit

Permalink
example4: an ugly but works learning example
Browse files Browse the repository at this point in the history
  • Loading branch information
andelf committed Mar 18, 2013
1 parent 141a804 commit 9b46392
Show file tree
Hide file tree
Showing 5 changed files with 307 additions and 0 deletions.
15 changes: 15 additions & 0 deletions example4/cn-startup.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<aiml version="1.0">

<category>
<pattern>LOAD AIML CN</pattern>
<template>

<!-- Load chinese AIML set -->
<!-- <learn>cn-profile.aiml</learn> -->
<learn>cn-test.aiml</learn>
<learn>auto-gen.aiml</learn>

</template>
</category>

</aiml>
194 changes: 194 additions & 0 deletions example4/cn-test.aiml
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
<?xml version="1.0" encoding="UTF-8"?>

<aiml version="1.0">

<!-- Free software (c) 2012 andelf -->
<!-- This program is open source code released under -->
<!-- the terms of the GNU General Public License -->
<!-- as published by the Free Software Foundation. -->

<meta name="author" content="Andelf"/>
<meta name="language" content="zh"/>

<category>
<pattern>* 再见</pattern>
<template>
<srai>BYE</srai>
</template>
</category>

<category>
<pattern>再见</pattern>
<template>
<srai>BYE</srai>
</template>
</category>

<category>
<pattern>你好</pattern>
<template>
<srai>HELLO</srai>
</template>
</category>

<category>
<pattern>谢谢</pattern>
<template>
<random>
<li>不客气.</li>
<li>你太客气了.</li>
</random>
</template>
</category>


<category>
<pattern>BYE</pattern>
<template>
<random>
<li>再见<get name="name"/>.</li>
<li>再见啦, <get name="name"/>.</li>
<li>下次见, <get name="name"/>.</li>
<li>谢谢你陪我聊天, <get name="name"/>.</li>
<li>改天见, <get name="name"/>.</li>
</random>
</template>
</category>

<category>
<pattern>HELLO</pattern>
<template>
<random>
<li>你好.</li>
<li>你也好.</li>
<li>你好啊.</li>
</random>
</template>
</category>


<category>
<pattern>一个*</pattern>
<template>
那个叫<set name="it"><person/></set>东西是什么玩意?
</template>
</category>

<category>
<pattern>_</pattern>
<that>我怎么称呼你</that>
<template>
<think><set name="personality">average</set><set name="name"><formal><star/></formal></set></think>
很高兴见到你, <get name="name"/>.
</template>
</category>


<category>
<pattern>*</pattern>
<template>
<condition>
<li><srai>INACTIVITYQUESTIONS</srai></li>
</condition>
</template>
</category>


<category>
<pattern>INACTIVITYQUESTIONS</pattern>
<template>
<condition>
<li name="name" value="">我怎么称呼你?</li>
<li><srai>ASK USER A QUESTION</srai></li>
</condition>
</template>
</category>

<category>
<pattern>ASK USER A QUESTION</pattern>
<template>
<random>
<li>咱随便聊聊吧</li>
<li>其实我不是很聪明,不过你可以教教我</li>
<li>聊聊人生聊聊理想把</li>
</random>
</template>
</category>

<category>
<pattern>* 的名字</pattern>
<template>
<person />是<bot name="name" />.
</template>
</category>

<category>
<pattern>生日快乐</pattern>
<template>
今天是<date format="%Y年%m月%d日" />, 不是我生日.
</template>
</category>

<category>
<pattern>你懂多少 *</pattern>
<template>
我懂<size />条规则.
</template>
</category>

<category>
<pattern>你的版本 *</pattern>
<template>
我已经<version />了.
</template>
</category>

<category>
<pattern>说错 *</pattern>
<template>
那我应该怎么说
</template>
</category>

<category>
<pattern>这么说不 *</pattern>
<template>
那我应该怎么说
</template>
</category>

<category>
<pattern>这样说不 *</pattern>
<template>
那我应该怎么说
</template>
</category>

<category>
<pattern>你应该说 *</pattern>
<that>那我应该怎么说</that>
<template>
<srai><input index="3" /> XLEARN REPLY <star index="1" /></srai>
</template>
</category>


<category>
<pattern>应该说 *</pattern>
<that>那我应该怎么说</that>
<template>
<srai><input index="3" /> XLEARN REPLY <star index="1" /></srai>
</template>
</category>

<category>
<pattern>* XLEARN REPLY *</pattern>
<template>
<system>python learn.py '<star index="1" />' '<star index="2" />'</system>
<learn>auto-gen.aiml</learn>
好的我学会了, 你可以再问我试试.
</template>
</category>


</aiml>
44 changes: 44 additions & 0 deletions example4/learn.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@


import sys
sys.path.insert(0, "../")
import codecs
import shelve

from aiml.LangSupport import mergeChineseSpace


db = shelve.open("simple_rules.db", "c", writeback=True)

template = """<?xml version="1.0" encoding="UTF-8"?>
<aiml version="1.0">
<meta name="author" content="autogen"/>
<meta name="language" content="zh"/>
{rules}
</aiml>
"""

category_template = """
<category>
<pattern>{pattern}</pattern>
<template>
{answer}
</template>
</category>
"""

#print sys.argv
if len(sys.argv) == 3:
_, rule, temp = sys.argv
rule = mergeChineseSpace(unicode(rule, 'utf8')).encode('utf8')
temp = mergeChineseSpace(unicode(temp, 'utf8')).encode('utf8')
db[rule] = temp
db.sync()
rules = []
for r in db:
rules.append(category_template.format(pattern=r,
answer=db[r]))
content = template.format(rules = '\n'.join(rules))
with open("auto-gen.aiml", 'w') as fp:
fp.write(content)
25 changes: 25 additions & 0 deletions example4/simple.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import sys
sys.path.insert(0, "../")

import aiml

# The Kernel object is the public interface to
# the AIML interpreter.
k = aiml.Kernel()

k.loadSubs('./subbers.ini')

# Use the 'learn' method to load the contents
# of an AIML file into the Kernel.
k.learn("cn-startup.xml")


# Use the 'respond' method to compute the response
# to a user's input string. respond() returns
# the interpreter's response, which in this case
# we ignore.
k.respond("load aiml cn")

# Loop forever, reading user input from the command
# line and printing responses.
while True: print k.respond(raw_input("> "))
29 changes: 29 additions & 0 deletions example4/subbers.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# -*- coding: utf-8 -*-

# The 'gender' section contains the substitutions performed by the
# <gender> AIML tag, which swaps masculine and feminine pronouns.
[gender]
他 = 她
她 = 他
# and so on...

# The 'person' section contains the substitutions performed by the
# <person> AIML tag, which swaps 1st and 2nd person pronouns.
[person]
我 = 你
你 = 我
# and so on...

# The 'person2' section contains the substitutions performed by
# the <person2> AIML tag, which swaps 1st and 3nd person pronouns.
[person2]
我 = 他
他 = 我
# and so on...

# the 'normal' section contains subtitutions run on every input
# string passed into Kernel.respond(). It's mainly used to
# correct common misspellings, and to convert contractions
# ("WHAT'S") into a format that will match an AIML pattern ("WHAT
[normal]
生快 = 生日快乐

0 comments on commit 9b46392

Please sign in to comment.