{"action":"created","issue":{"url":"https://api.github.com/repos/njyjn/ARIADNE/issues/3","repository_url":"https://api.github.com/repos/njyjn/ARIADNE","labels_url":"https://api.github.com/repos/njyjn/ARIADNE/issues/3/labels{/name}","comments_url":"https://api.github.com/repos/njyjn/ARIADNE/issues/3/comments","events_url":"https://api.github.com/repos/njyjn/ARIADNE/issues/3/events","html_url":"https://github.com/njyjn/ARIADNE/issues/3","id":156195711,"number":3,"title":"tokenizing with regex","user":{"login":"darrenwee","id":12949331,"avatar_url":"https://avatars.githubusercontent.com/u/12949331?v=3","gravatar_id":"","url":"https://api.github.com/users/darrenwee","html_url":"https://github.com/darrenwee","followers_url":"https://api.github.com/users/darrenwee/followers","following_url":"https://api.github.com/users/darrenwee/following{/other_user}","gists_url":"https://api.github.com/users/darrenwee/gists{/gist_id}","starred_url":"https://api.github.com/users/darrenwee/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/darrenwee/subscriptions","organizations_url":"https://api.github.com/users/darrenwee/orgs","repos_url":"https://api.github.com/users/darrenwee/repos","events_url":"https://api.github.com/users/darrenwee/events{/privacy}","received_events_url":"https://api.github.com/users/darrenwee/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/njyjn/ARIADNE/labels/enhancement","name":"enhancement","color":"84b6eb"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":2,"created_at":"2016-05-23T03:55:57Z","updated_at":"2016-05-23T07:27:08Z","closed_at":null,"body":"We can start refactoring some code to use regex for tokenizing rather than enforcing underscores-for-spaces on the user-end. \r\n\r\nFor example, suppose I want to start as a user `John Doe Xiao Ming` from `Black` house. The current implementation requires the user to tokenize on the bot's behalf, i.e. the command `/start John_Doe_Xiao_Ming Black` must be entered.\r\n\r\nWe can refactor such parts of the code to use regular expressions to perform the tokenizing instead. Using the same example as above, we can replace lines [lines 538 to 548](https://github.com/njyjn/ARIADNE/blob/master/main.py#L538-L548) to the following example:\r\n\r\n````python\r\nimport re # regular expression module\r\n\r\n# the message received from the API\r\nmessage = '/start John Doe Xiao Ming Black'\r\n\r\n# make the regex pattern for this command\r\nstart_re = re.compile('(/start)\\s+([a-z\\s]+)\\s+(green|black|purple|blue|red|orange)', re.IGNORECASE)\r\n\r\n# perform matching\r\nstart_tokens = re.match(start_re, command)\r\n\r\nregister(chat_id, start_tokens.group(2), start_tokens.group(3) + ':')\r\n````\r\n\r\nThis will then enable you to enter commands without having to substitute underscores for spaces. We can do the same for other parts of the bot, e.g. `/key` and `/answer` for `withinus.py`. It should also reduce user error because of sloppy typing and enable you to sanitize inputs (e.g. the pattern looks for either `green`, `black`, `purple`, `blue`, `red`, and `orange` as the last word and fails to match if otherwise).\r\n\r\nIf it looks too sloppy to enter `/start John Doe Xiao Ming Black`, then we can add single/double quotes to help the user \"chunk\" the parameters, i.e. `/start 'John Doe Xiao Ming' 'Black'` and do the same in the regex pattern.\r\n\r\nA further enhancement to `/key` and `/answer` would be to use fuzzy string matching to match close but not exact answers with the `fuzzywuzzy` module, but that can be a separate item on the to-do list.\r\n\r\nSounds good?"},"comment":{"url":"https://api.github.com/repos/njyjn/ARIADNE/issues/comments/220905868","html_url":"https://github.com/njyjn/ARIADNE/issues/3#issuecomment-220905868","issue_url":"https://api.github.com/repos/njyjn/ARIADNE/issues/3","id":220905868,"user":{"login":"njyjn","id":10694375,"avatar_url":"https://avatars.githubusercontent.com/u/10694375?v=3","gravatar_id":"","url":"https://api.github.com/users/njyjn","html_url":"https://github.com/njyjn","followers_url":"https://api.github.com/users/njyjn/followers","following_url":"https://api.github.com/users/njyjn/following{/other_user}","gists_url":"https://api.github.com/users/njyjn/gists{/gist_id}","starred_url":"https://api.github.com/users/njyjn/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/njyjn/subscriptions","organizations_url":"https://api.github.com/users/njyjn/orgs","repos_url":"https://api.github.com/users/njyjn/repos","events_url":"https://api.github.com/users/njyjn/events{/privacy}","received_events_url":"https://api.github.com/users/njyjn/received_events","type":"User","site_admin":false},"created_at":"2016-05-23T07:27:08Z","updated_at":"2016-05-23T07:27:08Z","body":"Let's implement the regex functionality for /start first."}}
{"actor":{"display_login":"njyjn"}}