|
| 1 | +--- |
| 2 | +title: Identify vehicle registration plates with the RegexExtractor |
| 3 | +duration: |
| 4 | +--- |
| 5 | + |
| 6 | +--sep-- |
| 7 | +--- |
| 8 | +title: Introduction |
| 9 | +duration: 3 |
| 10 | +--- |
| 11 | + |
| 12 | +# Introduction |
| 13 | + |
| 14 | +Botfuel natively supports 31 built-in entities such as `forename`, `location`, `duration` or `url`. You can create your own dictionary of custom entities using the <a href="https://docs.botfuel.io/dialog/reference/entities/custom-entities" target="_blank">CorpusExtractor</a> class. |
| 15 | +However, you may want to use an extractor based on string scheme. |
| 16 | + |
| 17 | +In this tutorial, you will learn how to create an extractor for French vehicle registration plates using a RegexExtractor |
| 18 | + |
| 19 | +## What you will need |
| 20 | +* Have completed the <a href="/#/codelab/getting-started" target="_blank">Getting Started tutorial</a> |
| 21 | +* A very basic understanding of Regular expression (but don't panic, we said very basic!) |
| 22 | + |
| 23 | +--sep-- |
| 24 | +--- |
| 25 | +title: The French vehicle registration system |
| 26 | +duration: 4 |
| 27 | +--- |
| 28 | + |
| 29 | +# The French vehicle registration system |
| 30 | + |
| 31 | +Shorted SIV for <i>système d'immatriculation des véhicules</i>, the French vehicle registration system is used by all cars registered since 2009. |
| 32 | + |
| 33 | + |
| 34 | +<aside class="infos"> |
| 35 | +<b>Note:</b> Under the SIV system, registration plates contain seven alphanumeric characters: two letters, a dash, three numbers, a dash and two letters, such as AA-229-AA. The system is nationwide and chronological. The first car registered in France under the SIV received a AA-001-AA registration plate, the second one AA-002-AA, the third AA-003-AA. The system will be exhausted when ZZ-999-ZZ is reached, which is scheduled to occur after 80 years of use. |
| 36 | + |
| 37 | +You can find more information on <a href="https://en.wikipedia.org/wiki/Vehicle_registration_plates_of_France#SIV" target="_blank">wikipedia</a> |
| 38 | +</aside> |
| 39 | + |
| 40 | +This SIV system makes is very easy to identify wherease a string is a licence plate. However, as mentioned in wikipedia `The SIV format provides ((23 x 23) - 2) x 999 x ((23 x 23) - 1), or 277,977,744, different combinations`. We don't recommand you build a corpus of entities with every possible combination... |
| 41 | + |
| 42 | +--sep-- |
| 43 | +--- |
| 44 | +title: The RegexExtractor |
| 45 | +duration: 8 |
| 46 | +--- |
| 47 | + |
| 48 | +# The RegexExtractor |
| 49 | + |
| 50 | +Botfuel provides a RegexExtractor that you can use to easily extract when a patern appears in a phrase. In our case, we want to extract as entities any string that looks like `AA-999-AA`. |
| 51 | + |
| 52 | +# Build the regexp |
| 53 | + |
| 54 | +We mentioned in the introduction that you needed a basic understanding of Regular expression to do this tutorial. But don't worry, the regex we are going to build here is very simple. |
| 55 | + |
| 56 | +Our licence plate is separated in 5 sections : |
| 57 | +1. `AA`: Here we use `[A-Za-z]{2}` to match any combinaison of two letters between ranges A and Z or a an d z as we don't want to the licence plate to be case sensitive. We want to understand if the user sends us one in lowercase |
| 58 | +2. `-`: A literal dash character |
| 59 | +3. `999`: We use [0-9]{2,3} as we want to match any combination of 2 or 3 digits. (Scooters use 2 letters) |
| 60 | +4. `-`: Another literal dash character |
| 61 | +5. `AA`: The last section is the same as the first section |
| 62 | + |
| 63 | + |
| 64 | +<center> |
| 65 | +<img src="images/regex.png" alt="Regex explanation" title="Regex explanation"/> |
| 66 | +</center> |
| 67 | + |
| 68 | +Once we have our regular expression to extract a licence plate, we can use is in our extractor |
| 69 | + |
| 70 | +# Use it in the RegexExtractor |
| 71 | + |
| 72 | +Create a new file called `immatriculation-extractor.js` in the `extractors` folder containing the code below: |
| 73 | + |
| 74 | +```javascript |
| 75 | +const { RegexExtractor } = require('botfuel-dialog') |
| 76 | + |
| 77 | +class ImmatriculationExtractor extends RegexExtractor { |
| 78 | + constructor() { |
| 79 | + super({ |
| 80 | + dimension: 'immatriculation', |
| 81 | + regex: /[A-Za-z]{1,2}-[0-9]{2,3}-[A-Za-z]{1,2}/ |
| 82 | + }) |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +module.exports = ImmatriculationExtractor |
| 87 | +``` |
| 88 | + |
| 89 | +As you can see, creating a new RegexExtractor is very easy. You just need to specify a dimention that is going to be used in your dialog and the regex you want to match to extract your entity. |
| 90 | + |
| 91 | +--sep-- |
| 92 | +--- |
| 93 | +title: Test the extractor |
| 94 | +duration: 5 |
| 95 | +--- |
| 96 | + |
| 97 | +# Use the extractor |
| 98 | + |
| 99 | +## Trainer |
| 100 | + |
| 101 | +Got to <a href="https://app.botfuel.io" target="_blank">https://app.botfuel.io</a>, open your project and create a new intent called `licenceplate` |
| 102 | + |
| 103 | +<center> |
| 104 | +<img src="images/trainer.png" alt="intent in trainer" title="Intent in trainer"/> |
| 105 | +</center> |
| 106 | + |
| 107 | +Add a few training phrases the user may enter to give you his licence plate. |
| 108 | + |
| 109 | +## Create the Dialog |
| 110 | + |
| 111 | +To test the extractor, go in you dialogs folder and create a new dialog called `licenceplate-dialog.js` with the code below: |
| 112 | + |
| 113 | +```javascript |
| 114 | +const { PromptDialog } = require('botfuel-dialog'); |
| 115 | + |
| 116 | +class LicencePlateDialog extends PromptDialog {} |
| 117 | + |
| 118 | +LicencePlateDialog.params = { |
| 119 | + namespace: 'immatriculation', |
| 120 | + entities: { |
| 121 | + licenceplate: { |
| 122 | + dim: 'immatriculation', |
| 123 | + }, |
| 124 | + }, |
| 125 | +}; |
| 126 | + |
| 127 | +module.exports = LicencePlateDialog; |
| 128 | +``` |
| 129 | + |
| 130 | +Here, you just specified that the `LicencePlateDialog` will have an entity of dimention `immatriculation`. The key `licenceplate` is used in your dialog and view to retrieve the entity. |
| 131 | + |
| 132 | +## Create the View |
| 133 | + |
| 134 | +You can use your entity in your view the same way you use any other entity in Botfuel |
| 135 | + |
| 136 | +Create a file `licenceplate-view.js` in the view folder with the code below: |
| 137 | + |
| 138 | +```javascript |
| 139 | +const { PromptView, BotTextMessage } = require('botfuel-dialog'); |
| 140 | + |
| 141 | +class LicencePlateView extends PromptView { |
| 142 | + render(userMessage, { matchedEntities }) { |
| 143 | + const licencePlate = matchedEntities.licenceplate && matchedEntities.immatriculation.values[0].value; |
| 144 | + |
| 145 | + if (licencePlate) { |
| 146 | + return [ |
| 147 | + new BotTextMessage(`Thanks, you licence plate is ${licencePlate.toUpperCase()}.`), |
| 148 | + ]; |
| 149 | + } |
| 150 | + |
| 151 | + return [new BotTextMessage(`Sorry, I did not understand your licence plate.`)]; |
| 152 | + } |
| 153 | +} |
| 154 | + |
| 155 | +module.exports = LicencePlateView; |
| 156 | +``` |
| 157 | + |
| 158 | +## Test |
| 159 | + |
| 160 | +Start the bot with the `BOTFUEL_APP_ID`, `BOTFUEL_APP_KEY` and `BOTFUEL_APP_TOKEN` environment variables using your app’s credentials: |
| 161 | + |
| 162 | +```bash |
| 163 | +BOTFUEL_APP_TOKEN=<the BOTFUEL_APP_TOKEN> BOTFUEL_APP_ID=<the BOTFUEL_APP_ID> BOTFUEL_APP_KEY=<the BOTFUEL_APP_KEY> npm start |
| 164 | +``` |
| 165 | + |
| 166 | +You can then try your new intent in your bot. |
| 167 | +<center> |
| 168 | +<img src="images/terminal.png" alt="Bot test in terminal" title="Bot test in terminal"/> |
| 169 | +</center> |
| 170 | + |
| 171 | +--sep-- |
| 172 | +--- |
| 173 | +title: Congratulations |
| 174 | +duration: 1 |
| 175 | +--- |
| 176 | + |
| 177 | +# Congratulation |
| 178 | + |
| 179 | +You have reached the end of this tutorial. The `RegexExtractor` opens endless possibilities to create custom extractors |
| 180 | + |
| 181 | +## <i class="fas fa-heart"></i> Support us |
| 182 | + |
| 183 | +Did you have a good time using Botfuel to build Chatbots? You can support our developers by staring our open source SDK on <a href="https://github.com/Botfuel/botfuel-dialog" target="_blank">Github <i class="fab fa-github"></i></a> |
| 184 | + |
| 185 | +## Learn more |
| 186 | + |
| 187 | +* The SDK <a href="https://docs.botfuel.io/" target="_blank">documentation</a> |
| 188 | +* Deploy your chatbot on Heroku <a href="https://tutorials.botfuel.io/#/codelab/deploy-heroku?step=1" target="_blank">this tutorial</a> |
| 189 | +* Deploy your chatbot on Facebook Messenger with <a href="https://tutorials.botfuel.io/#/codelab/connect-messenger?step=1" target="_blank">this tutorial</a> |
0 commit comments