Skip to content

Commit

Permalink
Merge pull request #3160 from botpress/f_fix-nlu-testing-edit
Browse files Browse the repository at this point in the history
fix(nlu): allow no intent condition
  • Loading branch information
slvnperron authored Apr 7, 2020
2 parents 7d60158 + 026c48e commit 9f6c2d3
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 14 deletions.
33 changes: 21 additions & 12 deletions modules/nlu-testing/src/views/full/TestModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ interface Props {
onTestCreated: (test: any) => void
}

const NOTHING_EXPECTED_INTENT = {
name: '',
slots: []
}

export const TestModal: FC<Props> = props => {
const [intents, setIntents] = useState<sdk.NLU.IntentDefinition[]>([])
const [availableCtx, setAvailableCtxs] = useState([])
Expand All @@ -24,7 +29,7 @@ export const TestModal: FC<Props> = props => {
useEffect(() => {
// tslint:disable-next-line: no-floating-promises
props.api.fetchIntents().then(intents => {
setIntents([...intents, NONE_INTENT])
setIntents([...intents, NONE_INTENT, NOTHING_EXPECTED_INTENT])
const ctxs = _.chain(intents)
.flatMap(i => i.contexts)
.uniq()
Expand All @@ -49,18 +54,21 @@ export const TestModal: FC<Props> = props => {

const createTest = async e => {
e.preventDefault()
const { expectedCtx, expectedIntent, slotConditions: slotsConds, testingCtx: context, utterance } = state

const conditions = [
['context', 'is', expectedCtx],
expectedIntent?.name ? ['intent', 'is', expectedIntent.name] : [],
..._.toPairs(slotsConds)
.filter(([_, value]) => !!value)
.map(([slotName, value]) => [`slot:${slotName}`, 'is', value])
].filter(c => !_.isEmpty(c)) as Condition[]

const test: Test = {
id: isEditing() ? props.test.id : Date.now().toString(),
utterance: state.utterance,
context: state.testingCtx,
conditions: [
['context', 'is', state.expectedCtx],
['intent', 'is', state.expectedIntent.name],
..._.toPairs(state.slotConditions)
.filter(([_, value]) => !!value)
.map(([slotName, value]) => [`slot:${slotName}`, 'is', value])
] as Condition[]
utterance,
context,
conditions
}

await props.api.updateTest(test)
Expand Down Expand Up @@ -122,17 +130,18 @@ export const TestModal: FC<Props> = props => {
.filter(
i =>
i.name === NONE_INTENT.name ||
i.name === NOTHING_EXPECTED_INTENT.name ||
(state.testingCtx === TEST_ALL_CTX && i.contexts.includes(state.expectedCtx)) ||
i.contexts.includes(state.testingCtx)
)
.map(x => ({ value: x.name, label: x.name }))
.uniqBy('value')
.value()}
onChange={expectedIntentChanged}
value={state.expectedIntent.name}
value={state.expectedIntent?.name ?? ''}
/>
</FormGroup>
{state.expectedIntent.slots.map((slot, idx) => (
{state.expectedIntent?.slots.map((slot, idx) => (
<FormGroup key={slot.name} label={`Slot: ${slot.name}`}>
<InputGroup
tabIndex={5 + idx}
Expand Down
4 changes: 2 additions & 2 deletions modules/nlu-testing/src/views/full/test-reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ interface TestState {
utterance: string
testingCtx: string
expectedCtx: string
expectedIntent: sdk.NLU.IntentDefinition
expectedIntent?: sdk.NLU.IntentDefinition
slotConditions: _.Dictionary<string>
}

Expand Down Expand Up @@ -38,7 +38,7 @@ export function TestModalReducer(state: TestState, action): TestState {
const intentCondition = test.conditions.find(([key]) => key === 'intent')

const expectedCtx = testingCtx === TEST_ALL_CTX ? ctxCondition[2] : undefined
const expectedIntent = data.intents.find(i => i.name === intentCondition[2])
const expectedIntent = intentCondition ? data.intents.find(i => i.name === intentCondition[2]) : undefined
const slotConditions = test.conditions
.filter(([key]) => key.startsWith('slot'))
.reduce((slotsDic, [key, is, slotValue]) => {
Expand Down

0 comments on commit 9f6c2d3

Please sign in to comment.