开源bot工具Rasa学习---1

Rasa是一个不错的开源bot工具,全部基于python实现,主页是https://rasa-nlu.readthedocs.io/en/latest/index.html

本节是关于工具安装和初步运行的记录。


根据其文档进行安装,我选择的安装方式是:

git clone [email protected]:RasaHQ/rasa_nlu.git
cd rasa_nlu
pip install -r requirements.txt
python setup.py install


后端我选择的是“spaCy + sklearn”, 其中提示我需要安装sklearn-crfsuite。

然后当我安装这个工具时,又是各种与当前环境不匹配或找不到可用版本,最后通过以下方式解决了。

conda install -c conda-forge python-crfsuite=0.9.2


最后是在Ipython中针对自带的例子“ata\examples\rasa\demo-rasa.json”进行模型训练:

In [2]: run  rasa_nlu/train.py   -c config_spacy.json
INFO:rasa_nlu.utils.spacy_utils:Trying to load spacy model with name 'en'
INFO:rasa_nlu.components:Added 'nlp_spacy' to component cache. Key 'nlp_spacy-en'.
INFO:rasa_nlu.converters:Training data format at ./data/examples/rasa/demo-rasa.json is rasa_nlu
INFO:rasa_nlu.training_data:Training data stats:
        - intent examples: 40 (4 distinct intents)
        - found intents: 'affirm', 'goodbye', 'greet', 'restaurant_search'
        - entity examples: 9 (2 distinct entities)
        - found entities: 'cuisine', 'location'
INFO:rasa_nlu.model:Starting to train component nlp_spacy
INFO:rasa_nlu.model:Finished training component.
INFO:rasa_nlu.model:Starting to train component ner_crf
INFO:rasa_nlu.model:Finished training component.
INFO:rasa_nlu.model:Starting to train component ner_synonyms
INFO:rasa_nlu.model:Finished training component.
INFO:rasa_nlu.model:Starting to train component intent_featurizer_spacy
INFO:rasa_nlu.model:Finished training component.
INFO:rasa_nlu.model:Starting to train component intent_classifier_sklearn
Fitting 2 folds for each of 6 candidates, totalling 12 fits
[Parallel(n_jobs=1)]: Done  12 out of  12 | elapsed:    0.0s finished
INFO:rasa_nlu.model:Finished training component.
INFO:rasa_nlu.model:Successfully saved model into 'D:\Work\NLP\Task\Rasa\workspace\models\model_20170709-153926'
INFO:__main__:Finished training

针对以上得到的模型,进行预测的代码如下:

from rasa_nlu.model import Metadata, Interpreter
from rasa_nlu.config import RasaNLUConfig
metadata = Metadata.load('models/model_20170709-153926')   # where model_directory points to the folder the model is persisted in
interpreter = Interpreter.load(metadata, RasaNLUConfig("config_spacy.json"))
interpreter.parse(u"I am looking for Chinese food")

Out[9]:
{'entities': [],
 'intent': {'confidence': 0.7729154931115908, 'name': 'restaurant_search'},
 'intent_ranking': [{'confidence': 0.7729154931115908,
   'name': 'restaurant_search'},
  {'confidence': 0.093570000080283558, 'name': 'goodbye'},
  {'confidence': 0.093241162858677284, 'name': 'greet'},
  {'confidence': 0.040273343949448648, 'name': 'affirm'}],
 'text': 'I am looking for Chinese food'}




你可能感兴趣的:(NLP)