2ku-bot-message-parser 中文文档教程
bot-message-parser
聊天机器人从用户输入中提取有用数据的简单工具。
operation
创建一个新的 BotMessageParser
对象。
const BotMessageParser = require("bot-message-parser")
const parser = new BotMessageParser({
debug: false,
useCompromise: false,
commandPrefix: "/",
namesList: [
"ryan forever",
"alice springs"
]
});
parser.process(text).then(console.log)
`
.process()
是一个异步函数,将产生以下输出:
.raw
:原始文本
.text
:原始文本,扁平化为小写
.clean
:清除任何标点符号的文本,扁平化为小写,并应用字典映射。
trigger
:已解析的触发器(即 !bot 或 !command 等)
.hasTrigger
:true
如果消息包含触发器
.triggerOnly
:true
如果消息仅包含触发器
.yes
:boolean
如果用户说“yes”、“yeah” , "
" 等
yep code>:boolean
如果用户说“也许”、“不确定”、“可能”等
.idk
:boolean
如果用户说“idk”,“我不知道”,“不知道”之类的话。 etc
.what
:boolean
如果用户说“什么?”、“嗯?”、“我不明白”等
arguments
:将消息字符串转换为可用于获取参数的数组
中指定了 namesList
,则返回找到的名称数组
names
:如果在设置intent:NLP ONLY 消息的意图
numEntities
:NLP ONLY 找到的实体数
entities
: NLP ONLY 一组实体
sentiment
:NLP ONLY 选项中消息的情绪(正面、负面或中性)
compromise
,如果您设置 useCompromise
为 true
,您可以从 compromise 激活 nlp 处理,
这将搜索对于消息中的一些有用的东西,例如 hashtags
、urls
、emails
、phoneNumbers
、verbs
和 名词
nlp-js
目前,这使用 nlp-js v3 .10.0。 如果您使用此模块训练模型,则可以在选项中指定它的路径。
output
/*
{
id: undefined,
type: undefined,
timestamp: undefined,
raw: '/bot hello there? my name is ryan forever. do you know ALICE SPRINGS?',
text: 'hello there? my name is ryan forever. do you know ALICE SPRINGS?',
clean: 'hello there my name is ryan forever do you know alice springs',
attachments: undefined,
command: 'bot',
commandRaw: '/bot',
hasCommand: true,
commandOnly: false,
botMentioned: undefined,
yes: undefined,
no: undefined,
maybe: undefined,
idk: undefined,
what: undefined,
arguments: [
'hello', 'there',
'my', 'name',
'is', 'ryan',
'forever', 'do',
'you', 'know',
'alice', 'springs'
],
names: [ 'ryan forever', 'alice springs' ],
hasIntent: undefined,
intent: undefined,
numEntities: 0,
entities: [],
sentiment: undefined,
hashtags: [],
urls: [],
emails: [],
phoneNumbers: [],
verbs: [],
nouns: []
}
*/
bot-message-parser
a simple tool for chatbots to extract useful data from user input.
operation
Create a new BotMessageParser
object.
const BotMessageParser = require("bot-message-parser")
const parser = new BotMessageParser({
debug: false,
useCompromise: false,
commandPrefix: "/",
namesList: [
"ryan forever",
"alice springs"
]
});
parser.process(text).then(console.log)
`
.process()
is an async function and will produce the following outputs:
.raw
: the original text
.text
: the original text, flattned to lower case
.clean
: text cleaned of any punctuation, flattened to lowercase, and the dictionary mappings applied.
trigger
: the parsed trigger (i.e. !bot or !command, etc)
.hasTrigger
: true
if message contains a trigger
.triggerOnly
: true
if the message contains just the trigger
.yes
: boolean
if user says something like "yes", "yeah", "yep", etc
.no
: boolean
if user says something like "no", "nah", "hell no", etc
.maybe
: boolean
if user says something like "maybe", "not sure", "possibly", etc
.idk
: boolean
if user says something like "idk", "i dont know", "no idea". etc
.what
: boolean
if user says something like "what?", "huh?","i dont understand", etc
arguments
: converts the message string into an array that can be useful for getting arguments
names
: returns an array of found names, if a namesList
is specified in settings
intent
: NLP ONLY intent of the message
numEntities
: NLP ONLY number of entities found
entities
: NLP ONLY an array of entities
sentiment
: NLP ONLY sentiment of the message (positive, negative, or neutral)
compromise
in the options, if you set useCompromise
to true
, you can activate the nlp processing from compromise
this will search for sever useful things in the message, such as hashtags
, urls
, emails
, phoneNumbers
, verbs
, and nouns
nlp-js
currently, this this uses nlp-js v3.10.0. if you train a model using this module, you can specify a path to it in the options.
output
/*
{
id: undefined,
type: undefined,
timestamp: undefined,
raw: '/bot hello there? my name is ryan forever. do you know ALICE SPRINGS?',
text: 'hello there? my name is ryan forever. do you know ALICE SPRINGS?',
clean: 'hello there my name is ryan forever do you know alice springs',
attachments: undefined,
command: 'bot',
commandRaw: '/bot',
hasCommand: true,
commandOnly: false,
botMentioned: undefined,
yes: undefined,
no: undefined,
maybe: undefined,
idk: undefined,
what: undefined,
arguments: [
'hello', 'there',
'my', 'name',
'is', 'ryan',
'forever', 'do',
'you', 'know',
'alice', 'springs'
],
names: [ 'ryan forever', 'alice springs' ],
hasIntent: undefined,
intent: undefined,
numEntities: 0,
entities: [],
sentiment: undefined,
hashtags: [],
urls: [],
emails: [],
phoneNumbers: [],
verbs: [],
nouns: []
}
*/