How to make Watson capture a sequence of numbers (input.text.extract)

后端 未结 1 1730
孤独总比滥情好
孤独总比滥情好 2021-01-14 13:43

In my example, I ask for a personal documentation number, the number has 11 digits, and I need that in case the user type the 11 numbers correctly the watson continue with t

相关标签:
1条回答
  • 2021-01-14 14:23

    To match numbers with Watson conversation service you can either use the entity sys-number that can be turned on in the entities tab - but this will match all the numbers and yours is a specific one.

    For this use case you can add additional check of the textual user input. The Watson conversation supports regexps checks. If you create a condition of dialog node in this way: input.text.matches('^[^\d]*[\d]{11}[^\d]*$') then this node will match only if the input.text which is an accessor to the exact text String that was submitted by the user will match the regular expression (regexp) defined as ^[^\d]*[\d]{11}[^\d]*$.

    This particular expression will match only if there is 11 digits number in the input and no other digits elsewhere, but additional text in front and after the number is allowed.

    Now to capture this number to a variable you can add the following to the context of the dialog node that is matching this number:

    "context": {
        "number": "<?input.text.extract('^[^\\d]*[\\d]{11}[^\\d]*$',0)?>"
    }
    

    Note that there is different escaping of \\don the context due to JSON nature of the context field.

    In the output text of a dialog node you can then write something like "Ok, number $number was matched." to display the number in the chat window.

    One more thing - great place with info about regexps where you can also try various type of regular expressions and what they match is Regex 101 web page.

    0 讨论(0)
提交回复
热议问题