From 80fafebddf132b6477bfb28e794fc5d58668aead Mon Sep 17 00:00:00 2001 From: Alex Bondar Date: Fri, 18 Mar 2022 15:02:04 -0700 Subject: [PATCH 1/3] copy of a lambda function and the readme file --- backend/scripts/nlp/README.md | 6 +++ backend/scripts/nlp/lambda_function.py | 56 ++++++++++++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 backend/scripts/nlp/README.md create mode 100644 backend/scripts/nlp/lambda_function.py diff --git a/backend/scripts/nlp/README.md b/backend/scripts/nlp/README.md new file mode 100644 index 0000000..49e181b --- /dev/null +++ b/backend/scripts/nlp/README.md @@ -0,0 +1,6 @@ +## Question/No question lambda function documentation +// To be complete + + +API Endpoint: https://050l3ftzue.execute-api.us-west-2.amazonaws.com/default/ab-isQuestion + diff --git a/backend/scripts/nlp/lambda_function.py b/backend/scripts/nlp/lambda_function.py new file mode 100644 index 0000000..5e4e02c --- /dev/null +++ b/backend/scripts/nlp/lambda_function.py @@ -0,0 +1,56 @@ +""" This is a copy of the Lambda function that was uploaded to AWS, it is not intended to run locally +and it will not work without modification""" + +import json +from math import isqrt +import nltk +import requests +nltk.download('punkt') + + + + + +def lambda_handler(event=None, context=None): + sentence = None + # sentence = event["sentence"] + sentence.lower() + tokens = nltk.word_tokenize(sentence) + tokenlist = '' + firstword = tokens[0] + print(firstword) + + isQuestion = False + + + + for s in tokens: + tokenlist += s + + print(tokenlist) + + if '?' in tokenlist: + isQuestion = True + + wh_question = ['what', 'when', 'where', 'who', + 'whom', 'which', 'whose', 'why', 'how'] + yN_question = ["am", "is", "are", "do", "does", "did", "have", "has", "was", "were", "can", "cannot", "could", + "couldn't", "dare", "may", "might", "must", "need", "ought", "shall", "should", "shouldn't", "will", "would"] + + if firstword in wh_question or firstword in yN_question: + isQuestion = True + print(isQuestion) + + answer = '' + if (isQuestion): + answer = 'This is a question' + else: + answer = "This is not a question" + + return answer + # return { + # 'statusCode': 200, + # 'answer': answer + # # 'body': json.dumps(answer) + # } +print(lambda_handler()) \ No newline at end of file From 8a713fa0a82695ecec7575e6fe0754507df801b2 Mon Sep 17 00:00:00 2001 From: Alex Bondar Date: Sat, 19 Mar 2022 11:12:44 -0700 Subject: [PATCH 2/3] Removed nused import --- backend/scripts/nlp/lambda_function.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/backend/scripts/nlp/lambda_function.py b/backend/scripts/nlp/lambda_function.py index 5e4e02c..1b81dec 100644 --- a/backend/scripts/nlp/lambda_function.py +++ b/backend/scripts/nlp/lambda_function.py @@ -2,7 +2,6 @@ and it will not work without modification""" import json -from math import isqrt import nltk import requests nltk.download('punkt') @@ -12,10 +11,11 @@ def lambda_handler(event=None, context=None): - sentence = None + sentence = "Who am I" # sentence = event["sentence"] - sentence.lower() - tokens = nltk.word_tokenize(sentence) + + print(sentence) + tokens = nltk.word_tokenize(sentence.lower()) tokenlist = '' firstword = tokens[0] print(firstword) From 686f68166a6a1758dbf8ede77b153ec7d33f04fa Mon Sep 17 00:00:00 2001 From: Alex Bondar Date: Tue, 22 Mar 2022 11:57:36 -0700 Subject: [PATCH 3/3] Removed readme file, updated the lambda function code to the one in AWS --- backend/scripts/nlp/README.md | 6 -- backend/scripts/nlp/lambda_function.py | 102 +++++++++++++------------ 2 files changed, 52 insertions(+), 56 deletions(-) delete mode 100644 backend/scripts/nlp/README.md diff --git a/backend/scripts/nlp/README.md b/backend/scripts/nlp/README.md deleted file mode 100644 index 49e181b..0000000 --- a/backend/scripts/nlp/README.md +++ /dev/null @@ -1,6 +0,0 @@ -## Question/No question lambda function documentation -// To be complete - - -API Endpoint: https://050l3ftzue.execute-api.us-west-2.amazonaws.com/default/ab-isQuestion - diff --git a/backend/scripts/nlp/lambda_function.py b/backend/scripts/nlp/lambda_function.py index 1b81dec..0db117c 100644 --- a/backend/scripts/nlp/lambda_function.py +++ b/backend/scripts/nlp/lambda_function.py @@ -4,53 +4,55 @@ import json import nltk import requests -nltk.download('punkt') - - - - - -def lambda_handler(event=None, context=None): - sentence = "Who am I" - # sentence = event["sentence"] - - print(sentence) - tokens = nltk.word_tokenize(sentence.lower()) - tokenlist = '' - firstword = tokens[0] - print(firstword) - - isQuestion = False - - - - for s in tokens: - tokenlist += s - - print(tokenlist) - - if '?' in tokenlist: - isQuestion = True - - wh_question = ['what', 'when', 'where', 'who', - 'whom', 'which', 'whose', 'why', 'how'] - yN_question = ["am", "is", "are", "do", "does", "did", "have", "has", "was", "were", "can", "cannot", "could", - "couldn't", "dare", "may", "might", "must", "need", "ought", "shall", "should", "shouldn't", "will", "would"] - - if firstword in wh_question or firstword in yN_question: - isQuestion = True - print(isQuestion) - - answer = '' - if (isQuestion): - answer = 'This is a question' - else: - answer = "This is not a question" - - return answer - # return { - # 'statusCode': 200, - # 'answer': answer - # # 'body': json.dumps(answer) - # } -print(lambda_handler()) \ No newline at end of file +nltk.data.path.append("/tmp") + +nltk.download('punkt', download_dir="/tmp") + + + + + +def lambda_handler(event, context): + answer = {} + invalidReq = False + try: + sentence = event["sentence"] + except: + answer = { + "error": "not a valid input" + } + invalidReq = True + if not invalidReq: + + tokens = nltk.word_tokenize(sentence.lower()) + tokenlist = '' + firstword = tokens[0] + + isQuestion = False + + + + for s in tokens: + tokenlist += s + + + + if '?' in tokenlist: + isQuestion = True + + wh_question = ['what', 'when', 'where', 'who', + 'whom', 'which', 'whose', 'why', 'how'] + yN_question = ["am", "is", "are", "do", "does", "did", "have", "has", "was", "were", "can", "cannot", "could", + "couldn't", "dare", "may", "might", "must", "need", "ought", "shall", "should", "shouldn't", "will", "would"] + + if firstword in wh_question or firstword in yN_question: + isQuestion = True + + answer = False + if (isQuestion): + answer = True + + + return { + 'answer': answer + } \ No newline at end of file