Skip to content

Commit

Permalink
fix(nlu): force coherent nlu native values (resolve #971) (#975)
Browse files Browse the repository at this point in the history
  • Loading branch information
epaminond authored Oct 3, 2018
1 parent e74c0d8 commit 991d9fb
Showing 1 changed file with 20 additions and 18 deletions.
38 changes: 20 additions & 18 deletions packages/functionals/botpress-nlu/src/providers/native.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,24 +144,26 @@ export default class NativeProvider extends Provider {

let allScores = zscore(classifications.map(c => parseFloat(c.value)))

const SIGMOID_ADJUSTMENT = -3.5
const sigmoid = t => 1 / (1 + Math.pow(Math.E, -(t + SIGMOID_ADJUSTMENT)))

allScores = allScores
.map((s, i) => {
const delta = Math.abs(s - allScores[i + 1] / s)
if (delta >= threshold) {
return s
}

return (
s -
Math.max(0, allScores[i + 1] || 0) * 0.5 -
Math.max(0, allScores[i + 2] || 0) * 0.75 -
Math.max(0, allScores[i + 3] || 0)
)
})
.map(sigmoid)
allScores = allScores.map((s, i) => {
const delta = Math.abs(s - allScores[i + 1] / s)
if (delta >= threshold) {
return s
}

const res =
s -
Math.max(0, allScores[i + 1] || 0) * 0.5 -
Math.max(0, allScores[i + 2] || 0) * 0.75 -
Math.max(0, allScores[i + 3] || 0)

if (res < 0) {
return 0
} else if (res > 1) {
return 1
} else {
return res
}
})

const intents = _.orderBy(
classifications.map((c, i) => ({
Expand Down

0 comments on commit 991d9fb

Please sign in to comment.