From 86db3e1340fa209a8748298f013ba01f448ee72b Mon Sep 17 00:00:00 2001 From: "Esteban Prince, Liam (UG - Computer Science)" <le00210@surrey.ac.uk> Date: Sun, 16 Feb 2020 13:08:08 +0000 Subject: [PATCH] Update parser.py --- lexer.py | 44 ++++++++++++++++++++++++++++++++++++++++++++ parser.py | 19 ------------------- 2 files changed, 44 insertions(+), 19 deletions(-) create mode 100644 lexer.py delete mode 100644 parser.py diff --git a/lexer.py b/lexer.py new file mode 100644 index 0000000..2fac1ca --- /dev/null +++ b/lexer.py @@ -0,0 +1,44 @@ +import json + +def lex(raw): + tokens = {} + + splitted = raw.split() + + types = ['ingredients', + 'units', + 'utensils', + 'prepositions', + 'articles', + 'conjunctions', + 'adjectives'] + + with open('database/ingredients.json', 'r') as f: + ingredients = json.load(f) + + with open('database/units.json', 'r') as f: + units = json.load(f) + + while len(splitted): + currentToken = splitted[0] + + if currentToken in ingredients: + tokenType = "Ingredient" + + elif currentToken in units: + tokenType = "Unit" + + elif currentToken.isdigit(): + tokenType = "Digit" + + else: + tokenType = "Unknown" + + tokens[currentToken] = tokenType + splitted = splitted[1:] + print(tokens) + +def isValid(): + pass + +lex("Peel 300 g of potatoes and then cut into strips") \ No newline at end of file diff --git a/parser.py b/parser.py deleted file mode 100644 index 15c6b4b..0000000 --- a/parser.py +++ /dev/null @@ -1,19 +0,0 @@ -def preprocess(): - -# Return the next token from the input -def lex(input): - tokens = [] - - while len(input.split()): - if input is not None: - continue - - if input[0] in ingredients: - input = input[1:] - else: - raise Exception('Don\'t know what this is: {}'.format(input[0])) - -def parse(): - preprocess() - lex(input) - isValid() \ No newline at end of file -- GitLab