def learn():
...
for key,tem in handler.categories.items():
new_key = key
if key and key[0] and key[1] and key[2] and em_ext == '.aiml' and (not self._check_contain_english(key[0])):
new_key = (' '.join(key[0]), key[1], key[2])
elif key and key[0] and key[1] and key[2] and em_ext == '.aiml' and self._check_contain_english(key[0]):
new_key=(key[0].upper(), key[1], key[2])
self._brain.add(new_key, tem)
对aiml的python库做了简单修改,更加适配中文 Adapt aiml to Chinese better。与其每次费力迁就,不如一劳永逸改造它,做了几处小修改。
1.将Kernel里原来的self._check_contain_english改成了self._check_all_english,即判断问句是否为全英文。在learn和respons函数里的三处调用也尽数改为self._check_all_english。
def _check_contain_english(self, des_str):
for uchar in des_str:
if (uchar >= u'\u0041' and uchar <= u'\u005a') or (uchar >= u'\u0061' and uchar <= u'\u007a'):
return True
else:
return False
def _check_all_english(self, des_str):
for uchar in des_str:
if (uchar >= u'\u0041' and uchar <= u'\u005a') or (uchar >= u'\u0061' and uchar <= u'\u007a'):
pass
else:
return False
return True
2.在learn函数里不管是不是全为英文都执行upper操作,转大写。
if key and key[0] and key[1] and key[2] and em_ext == '.aiml' and (not self._check_all_english(key[0])):
new_key = (' '.join(key[0]).upper(), key[1], key[2])
elif key and key[0] and key[1] and key[2] and em_ext == '.aiml' and self._check_all_english(key[0]):
new_key=(key[0].upper(), key[1], key[2])