Skip to content

Commit

Permalink
feat(nlu): added pattern entity examples
Browse files Browse the repository at this point in the history
  • Loading branch information
EFF committed Nov 12, 2019
1 parent de932bd commit ba60a96
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 11 deletions.
3 changes: 3 additions & 0 deletions modules/nlu/src/backend/validation.ts
Expand Up @@ -39,6 +39,9 @@ export const EntityDefCreateSchema = Joi.object().keys({
.required(),
sensitive: Joi.boolean(),
matchCase: Joi.boolean(),
examples: Joi.array()
.items(Joi.string())
.default([]),
occurences: Joi.array()
.items(EntityDefOccurenceSchema)
.default([]),
Expand Down
60 changes: 49 additions & 11 deletions modules/nlu/src/views/full/entities/PatternEntity.tsx
Expand Up @@ -12,6 +12,7 @@ import {
Tooltip
} from '@blueprintjs/core'
import { NLU } from 'botpress/sdk'
import _ from 'lodash'
import React, { useEffect, useState } from 'react'

import style from './style.scss'
Expand All @@ -21,28 +22,51 @@ interface Props {
updateEntity: (entity: NLU.EntityDefinition) => void // promise ?
}

// TODO add nlu migration (entity.matchCase & examples)

export const PatternEntityEditor: React.FC<Props> = props => {
const [matchCase, setMatchCase] = useState<boolean>(false)
const [matchCase, setMatchCase] = useState<boolean>(props.entity.matchCase)
const [sensitive, setSensitive] = useState<boolean>(props.entity.sensitive)
const [pattern, setPattern] = useState<string>(props.entity.pattern)
const [isValidPattern, setIsValidPattern] = useState<boolean>(true)
const [paternValid, setPatternValid] = useState<boolean>(true)
const [examplesStr, setExampleStr] = useState((props.entity.examples || []).join('\n'))
const [allExamplesMatch, setExamplesMatch] = useState<boolean>(true)

const validateExamples = _.debounce(() => {
let p = pattern
if (!p.startsWith('^')) {
p = '^' + p
}
if (!p.endsWith('$')) {
p = p + '$'
}
const rx = new RegExp(p, matchCase ? '' : 'i')
const allMatching = examplesStr
.split('\n')
.filter(ex => ex !== '')
.map(ex => rx.test(ex))
.every(Boolean)

setExamplesMatch(allMatching)
}, 750)

useEffect(() => {
try {
const re = new RegExp(pattern, matchCase ? '' : 'i')
setIsValidPattern(true)
new RegExp(pattern, matchCase ? '' : 'i')
setPatternValid(true)
const newEntity: NLU.EntityDefinition = {
...props.entity,
pattern,
sensitive,
matchCase
matchCase,
examples: examplesStr.trim().split('\n')
}
validateExamples()
props.updateEntity(newEntity)
// TODO try to match on every examples with created pattern, don't forget exact start and exact end logic
} catch (e) {
setIsValidPattern(false)
setPatternValid(false)
}
}, [pattern, matchCase, sensitive]) // TODO add examples in watchers or maybe create a 2nd handler
}, [pattern, matchCase, sensitive, examplesStr]) // TODO add examples in watchers or maybe create a 2nd handler

return (
<div>
Expand All @@ -53,7 +77,7 @@ export const PatternEntityEditor: React.FC<Props> = props => {
label="Regular expression"
labelFor="pattern"
labelInfo={
isValidPattern ? null : (
paternValid ? null : (
<Tag intent="danger" minimal style={{ float: 'right' }}>
pattern invalid
</Tag>
Expand All @@ -67,17 +91,31 @@ export const PatternEntityEditor: React.FC<Props> = props => {
id="pattern"
placeholder="insert a valid pattern"
value={pattern}
intent={isValidPattern ? 'none' : 'danger'}
intent={paternValid ? 'none' : 'danger'}
onChange={e => setPattern(e.target.value)}
/>
</FormGroup>
<FormGroup label="Matching examples" labelFor="examples">
<FormGroup
label="Matching examples"
labelFor="examples"
labelInfo={
examplesStr &&
paternValid && (
<Tag intent={allExamplesMatch ? 'success' : 'danger'} minimal style={{ float: 'right' }}>
{allExamplesMatch ? 'All examples match' : 'Examples non matching'}
</Tag>
)
}
>
<TextArea
id="examples"
fill
rows={6}
growVertically={true}
placeholder="Add examples that matchs your pattern. One by line."
value={examplesStr}
intent={allExamplesMatch ? 'none' : 'danger'}
onChange={e => setExampleStr(e.target.value)}
/>
</FormGroup>
</div>
Expand Down
1 change: 1 addition & 0 deletions src/bp/sdk/botpress.d.ts
Expand Up @@ -385,6 +385,7 @@ declare module 'botpress/sdk' {
type: EntityType
sensitive?: boolean
matchCase?: boolean
examples?: string[]
fuzzy?: boolean
occurences?: EntityDefOccurence[]
pattern?: string
Expand Down

0 comments on commit ba60a96

Please sign in to comment.