Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2142 from botpress/fl_merge_tokens_slots
fix(nlu): merge tokens if both are made of chosen special characters in slot extraction
- Loading branch information
Showing
7 changed files
with
103 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import _ from 'lodash' | ||
import { Token } from '../typings' | ||
|
||
const SPACE = '\u2581' | ||
|
||
export const makeTokens = (stringTokens: string[], text: string) => { | ||
return stringTokens.reduce(reduceTokens(text), [] as Token[]) | ||
} | ||
|
||
const reduceTokens = (text: string) => (currentTokens: Token[], token: string) => { | ||
const trimedToken = token.replace(SPACE, '') | ||
|
||
const previousToken = currentTokens[currentTokens.length - 1] | ||
const cursor = previousToken ? previousToken.end : 0 | ||
|
||
const cutText = text.substring(cursor).toLowerCase() | ||
const start = cutText.indexOf(trimedToken) + cursor | ||
const sanitized = text.substr(start, trimedToken.length) | ||
|
||
const newToken = { | ||
value: token, | ||
cannonical: sanitized, | ||
start, | ||
end: start + trimedToken.length, | ||
matchedEntities: [] | ||
} as Token | ||
|
||
return currentTokens.concat(newToken) | ||
} | ||
|
||
function tokenIsAllMadeOf(tok: string, chars: string[]) { | ||
const tokenCharsLeft = _.without(tok.split(''), ...chars) | ||
return _.isEmpty(tokenCharsLeft) | ||
} | ||
|
||
export const mergeSpecialCharactersTokens = (tokens: Token[], specialChars: string[]) => { | ||
let current: Token | undefined | ||
const final: Token[] = [] | ||
|
||
for (const head of tokens) { | ||
if (!current) { | ||
current = head | ||
continue | ||
} | ||
|
||
const currentIsAllSpecialChars = tokenIsAllMadeOf(current!.value.replace(SPACE, ''), specialChars) | ||
|
||
const headHasNoSpace = !head.value.includes(SPACE) | ||
const headIsAllSpecialChars = tokenIsAllMadeOf(head.value, specialChars) | ||
|
||
if (currentIsAllSpecialChars && headIsAllSpecialChars && headHasNoSpace) { | ||
current.value += head.value | ||
current.cannonical += head.cannonical | ||
current.end = head.end | ||
current.matchedEntities = current.matchedEntities.concat(head.matchedEntities) | ||
} else { | ||
final.push(current) | ||
current = head | ||
} | ||
} | ||
return current ? [...final, current] : final | ||
} |