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