Spaces:
Runtime error
Runtime error
Greg Thompson commited on
Commit ·
71befd1
1
Parent(s): 2051ea9
Update nlu evaluation with basic intent classification using fuzzy comparison
Browse filesFiles changed (5)
96 unmodified lines97989910010110010110210310496 unmodified lines
Output - int_data_dict or sent_data_dict: dict - the type of NLU run and result {'type':'integer', 'data': '8'} {'type':'sentiment', 'data': 'negative'} {'type':'integer', 'data': '8', 'confidence': 0} {'type':'sentiment', 'data': 'negative', 'confidence': 0.99} """ data_dict = await request.json() message_data = data_dict.get('message_data', '')123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919223456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105input,output,text2int,scorenotanumber,32202,32202,Truethis is not a number,32202,32202,Truefourteen,14,14,Trueforteen,14,14,Trueone thousand four hundred ninety two,1492,1492,Trueone thousand ninety two,1092,1092,TrueFourteen Hundred Ninety-Two,1492,1492,TrueFourteen Hundred,1400,1400,TrueNinety nine,99,99,Truefifteen thousand five hundred-sixty,15560,15560,Truethree hundred fifty,350,350,Trueone nine eight five,1985,1985,Truenineteen eighty-five,1985,1605,Falseoh one,1,1,Truesix oh 1,601,601,Truesex,6,6,Truesix,6,6,Trueeight oh,80,8,Falseeighty,80,80,Trueate,8,1,Falsedouble eight,88,32202,Falseeight three seven five three O nine,8375309,8375329,Falseeight three seven five three oh nine,8375309,8375309,Trueeight three seven five three zero nine,8375309,8375309,Trueeight three seven five three oh ni-ee-ine,8375309,837530611,Falsetwo eight,28,16,Falseseven oh eleven,7011,77,Falseseven elevens,77,77,Trueseven eleven,711,77,Falseninety nine oh five,9905,149,Falseseven 0 seven 0 seven 0 seven,7070707,7070707,True123 hundred,123000,223,False5 o 5,505,525,False15 o 5,1505,22,False15-o 5,1505,22,False15 o-5,1505,22,False911-thousand,911000,911000,Truetwenty-two twenty-two,2222,44,Falsetwenty-two twenty-twos,484,44,Falsefour eighty four,484,404,Falsefour eighties,320,72,Falsefour eighties and nine nineties,1130,243,Falseninety nine hundred and seventy seven,9977,276,Falseseven thousands,7000,7000,True2 hundreds,200,200,True99 thousands and one,99001,99001,True"forty-five thousand, seven hundred and nine",45709,1161,Falseeighty eight hundred eighty,8880,268,Falsea hundred hundred,10000,32202,Falsea hundred thousand,100000,32202,Falsea hundred million,100000000,32202,Falsenineteen ninety nine,1999,1809,Falseforteen twenty seven,1427,307,Falseseventeen-thousand and seventy two,17072,17072,Truetwo hundred and nine,209,209,Truetwo thousand ten,2010,2010,Truetwo thousand and ten,2010,2010,Truetwelve million,12000000,12000000,True8 billion,8000000000,8000000000,Truetwenty ten,2010,2010,Truethirty-two hundred,3200,3200,Truenine,9,9,Trueforty two,42,42,True1 2 three,123,123,Truefourtean,14,14,Trueone tousand four hundred ninty two,1492,1492,TrueFurteen Hundrd Ninety-Too,1492,1492,Trueforrteen,14,14,Truesevnteen-thosand and seventy two,17072,17072,Trueninety nine hundred ad seventy seven,9977,32202,Falseseven thusands,7000,7000,True2 hunreds,200,200,True99 tousands and one,99001,99001,Trueeighty ate hundred eighty,8880,261,Falsefourteen Hundred,1400,1400,True8 Bilion,8000000000,8000000,Falseone million three thousand one,1003001,1003001,Truefour million nine thousand seven,4009007,4009007,Truetwo million five hundred thousand,2500000,2001500,Falsetwo tousand ten,2010,2010,Truetwo thousand teen,2010,2007,Falsetvelve milion,12000000,12000000,Truetventy ten,2010,2010,Truetirty-twoo hunred,3200,3200,Truesevn thoosands,7000,7000,Truefive,5,5,Trueten,10,10,Trueone two three and ten,12310,51,FalseONE MILLion three hunded and fiv,1000305,1000305,True"50,500 and six",50506,50506,Trueone_million_and_five,1000005,1000005,Truenotanumber,32202.0,32202.0,Truethis is not a number,32202.0,32202.0,Truefourteen,14.0,14.0,Trueforteen,14.0,14.0,Trueone thousand four hundred ninety two,1492.0,1492.0,Trueone thousand ninety two,1092.0,1092.0,TrueFourteen Hundred Ninety-Two,1492.0,1492.0,TrueFourteen Hundred,1400.0,1400.0,TrueNinety nine,99.0,99.0,Truefifteen thousand five hundred-sixty,15560.0,15560.0,Truethree hundred fifty,350.0,350.0,Trueone nine eight five,1985.0,1985.0,Truenineteen eighty-five,1985.0,1605.0,Falseoh one,1.0,1.0,Truesix oh 1,601.0,601.0,Truesex,6.0,6.0,Truesix,6.0,6.0,Trueeight oh,80.0,8.0,Falseeighty,80.0,80.0,Trueate,8.0,1.0,Falsedouble eight,88.0,8.0,Falseeight three seven five three O nine,8375309.0,8375329.0,Falseeight three seven five three oh nine,8375309.0,8375309.0,Trueeight three seven five three zero nine,8375309.0,8375309.0,Trueeight three seven five three oh ni-ee-ine,8375309.0,837530619.0,Falsetwo eight,28.0,16.0,Falseseven oh eleven,7011.0,77.0,Falseseven elevens,77.0,77.0,Trueseven eleven,711.0,77.0,Falseninety nine oh five,9905.0,149.0,Falseseven 0 seven 0 seven 0 seven,7070707.0,7070707.0,True123 hundred,123000.0,223.0,False5 o 5,505.0,525.0,False15 o 5,1505.0,22.0,False15-o 5,1505.0,22.0,False15 o-5,1505.0,22.0,False911-thousand,911000.0,911000.0,Truetwenty-two twenty-two,2222.0,44.0,Falsetwenty-two twenty-twos,484.0,44.0,Falsefour eighty four,484.0,404.0,Falsefour eighties,320.0,72.0,Falsefour eighties and nine nineties,1130.0,243.0,Falseninety nine hundred and seventy seven,9977.0,276.0,Falseseven thousands,7000.0,7000.0,True2 hundreds,200.0,200.0,True99 thousands and one,99001.0,99001.0,True"forty-five thousand, seven hundred and nine",45709.0,1161.0,Falseeighty eight hundred eighty,8880.0,268.0,Falsea hundred hundred,10000.0,100.0,Falsea hundred thousand,100000.0,100.0,Falsea hundred million,100000000.0,100.0,Falsenineteen ninety nine,1999.0,1809.0,Falseforteen twenty seven,1427.0,307.0,Falseseventeen-thousand and seventy two,17072.0,17072.0,Truetwo hundred and nine,209.0,209.0,Truetwo thousand ten,2010.0,2010.0,Truetwo thousand and ten,2010.0,2010.0,Truetwelve million,12000000.0,12000000.0,True8 billion,8000000000.0,8000000000.0,Truetwenty ten,2010.0,2010.0,Truethirty-two hundred,3200.0,3200.0,Truenine,9.0,9.0,Trueforty two,42.0,42.0,True1 2 three,123.0,123.0,Truefourtean,14.0,14.0,Trueone tousand four hundred ninty two,1492.0,1492.0,TrueFurteen Hundrd Ninety-Too,1492.0,1492.0,Trueforrteen,14.0,14.0,Truesevnteen-thosand and seventy two,17072.0,17072.0,Trueninety nine hundred ad seventy seven,9977.0,90.0,Falseseven thusands,7000.0,7000.0,True2 hunreds,200.0,200.0,True99 tousands and one,99001.0,99001.0,Trueeighty ate hundred eighty,8880.0,261.0,Falsefourteen Hundred,1400.0,1400.0,True8 Bilion,8000000000.0,8000000.0,Falseone million three thousand one,1003001.0,1003001.0,Truefour million nine thousand seven,4009007.0,4009007.0,Truetwo million five hundred thousand,2500000.0,2001500.0,Falsetwo tousand ten,2010.0,2010.0,Truetwo thousand teen,2010.0,2007.0,Falsetvelve milion,12000000.0,12000000.0,Truetventy ten,2010.0,2010.0,Truetirty-twoo hunred,3200.0,3200.0,Truesevn thoosands,7000.0,7000.0,Truefive,5.0,5.0,Trueten,10.0,10.0,Trueone two three and ten,12310.0,51.0,FalseONE MILLion three hunded and fiv,1000305.0,1000305.0,True"50,500 and six",50506.0,50506.0,Trueone_million_and_five,1000005.0,1000005.0,True2.0,2.0,2.0,True4.5,4.5,4.5,True12345.001,12345.001,12345.001,True7..0,7.0,7.0,True0.06,0.06,0.06,True"0,25",0.25,25.0,Falseo.45,0.45,32202.0,False0.1.2,0.12,32202.0,False0.00009,9e-05,9e-05,True0.01.,0.01,0.01,TrueI don't know 8,8.0,8.0,True"You're wrong it's not 20, it's 45",45.0,20.0,FalseI don't understand why it's 19,19.0,19.0,True12344 unmodified lines91011111213141516171819202122231718192021222425262728293031323334353637383940414243444531464748497 unmodified lines5758596061626364653 unmodified lines6970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113 unmodified lines145146147148149150151152153154155156157from fuzzywuzzy import fuzzfrom mathtext_fastapi.logging import prepare_message_data_for_loggingfrom mathtext.sentiment import sentimentfrom mathtext.text2int import text2int4 unmodified lines """ Turns nlu results into an object to send back to Turn.io Inputs - type: str - the type of nlu run (integer or sentiment-analysis) - data: str - the student message - data: str/int - the student message - confidence: - the nlu confidence score (sentiment) or '' (integer)
>>> build_nlu_response_object('integer', 8, 0) {'type': 'integer', 'data': 8, 'confidence': 0}
>>> build_nlu_response_object('sentiment', 'POSITIVE', 0.99) {'type': 'sentiment', 'data': 'POSITIVE', 'confidence': 0.99} """ return {'type': type, 'data': data, 'confidence': confidence}
def test_for_float_or_int(message_data, message_text): nlu_response = {} if type(message_text) == int or type(message_text) == float: nlu_response = build_nlu_response_object('integer', message_text, '') prepare_message_data_for_logging(message_data, nlu_response) return nlu_response# def test_for_float_or_int(message_data, message_text):# nlu_response = {}# if type(message_text) == int or type(message_text) == float:# nlu_response = build_nlu_response_object('integer', message_text, '')# prepare_message_data_for_logging(message_data, nlu_response)# return nlu_response
def test_for_number_sequence(message_text_arr, message_data, message_text): """ Determines if the student's message is a sequence of numbers
>>> test_for_number_sequence(['1','2','3'], {"author_id": "57787919091", "author_type": "OWNER", "contact_uuid": "df78gsdf78df", "message_body": "I am tired", "message_direction": "inbound", "message_id": "dfgha789789ag9ga", "message_inserted_at": "2023-01-10T02:37:28.487319Z", "message_updated_at": "2023-01-10T02:37:28.487319Z"}, '1, 2, 3') {'type': 'integer', 'data': '1,2,3', 'confidence': 0}
>>> test_for_number_sequence(['a','b','c'], {"author_id": "57787919091", "author_type": "OWNER", "contact_uuid": "df78gsdf78df", "message_body": "I am tired", "message_direction": "inbound", "message_id": "dfgha789789ag9ga", "message_inserted_at": "2023-01-10T02:37:28.487319Z", "message_updated_at": "2023-01-10T02:37:28.487319Z"}, 'a, b, c') {} """ nlu_response = {} if all(ele.isdigit() for ele in message_text_arr): nlu_response = build_nlu_response_object( 'integer', ','.join(message_text_arr), '' 0 ) prepare_message_data_for_logging(message_data, nlu_response) return nlu_response7 unmodified lines
Output - student_response_arr: list - a set of integers (32202 for error code)
>>> run_text2int_on_each_list_item(['1','2','3']) [1, 2, 3] """ student_response_arr = [] for student_response in message_text_arr:3 unmodified lines
def run_sentiment_analysis(message_text): """ Evaluates the sentiment of a student message
>>> run_sentiment_analysis("I am tired") [{'label': 'NEGATIVE', 'score': 0.9997807145118713}]
>>> run_sentiment_analysis("I am full of joy") [{'label': 'POSITIVE', 'score': 0.999882698059082}] """ # TODO: Add intent labelling here # TODO: Add logic to determine whether intent labeling or sentiment analysis is more appropriate (probably default to intent labeling) return sentiment(message_text)
def run_intent_classification(message_text): """ Process a student's message using basic fuzzy text comparison
>>> run_intent_classification("exit") {'type': 'intent', 'data': 'exit', 'confidence': 1.0} >>> run_intent_classification("exi") {'type': 'intent', 'data': 'exit', 'confidence': 0.86} >>> run_intent_classification("eas") {'type': 'intent', 'data': '', 'confidence': 0} >>> run_intent_classification("hard") {'type': 'intent', 'data': '', 'confidence': 0} >>> run_intent_classification("hardier") {'type': 'intent', 'data': 'harder', 'confidence': 0.92} """ label = '' ratio = 0 nlu_response = {'type': 'intent', 'data': label, 'confidence': ratio} commands = [ 'easier', 'exit', 'harder', 'hint', 'next' 'stop', ] for command in commands: ratio = fuzz.ratio(command, message_text.lower()) if ratio > 80: nlu_response['data'] = command nlu_response['confidence'] = ratio / 100 return nlu_response
def evaluate_message_with_nlu(message_data): """ Process a student's message using NLU functions and send the result >>> evaluate_message_with_nlu({"author_id": "57787919091", "author_type": "OWNER", "contact_uuid": "df78gsdf78df", "message_body": "8", "message_direction": "inbound", "message_id": "dfgha789789ag9ga", "message_inserted_at": "2023-01-10T02:37:28.487319Z", "message_updated_at": "2023-01-10T02:37:28.487319Z"}) {'type': 'integer', 'data': 8, 'confidence': 0}
>>> evaluate_message_with_nlu({"author_id": "57787919091", "author_type": "OWNER", "contact_uuid": "df78gsdf78df", "message_body": "I am tired", "message_direction": "inbound", "message_id": "dfgha789789ag9ga", "message_inserted_at": "2023-01-10T02:37:28.487319Z", "message_updated_at": "2023-01-10T02:37:28.487319Z"}) {'type': 'sentiment', 'data': 'NEGATIVE', 'confidence': 0.9997807145118713} """ # Keeps system working with two different inputs - full and filtered @event object try: message_text = message_data['message_body']13 unmodified lines number_api_resp = text2int(message_text.lower())
if number_api_resp == 32202: print("MESSAGE TEXT") print(message_text) print("============") intent_api_response = run_intent_classification(message_text) if intent_api_response['data']: return intent_api_response
sentiment_api_resp = sentiment(message_text) nlu_response = build_nlu_response_object( 'sentiment',12234567891011dillen-core-web-sm @ https://github.com/explosion/spacy-models/releases/download/en_core_web_sm-3.4.1/en_core_web_sm-3.4.1-py3-none-any.whl en-core-web-sm @ https://github.com/explosion/spacy-models/releases/download/en_core_web_sm-3.4.1/en_core_web_sm-3.4.1-py3-none-any.whlfuzzywuzzyjsonpicklemathtext @ git+https://gitlab.com/tangibleai/community/mathtext@mainfastapi==0.74.*pydantic==1.10.*python-Levenshteinrequests==2.27.*sentencepiece==0.1.*supabase57 unmodified lines5859606162616263646557 unmodified lines print(request)
# run_simulated_request('sentiment-analysis', 'I reject it')# run_simulated_request('text2int', 'seven thousand nine hundred fifty seven')run_simulated_request('sentiment-analysis', 'I reject it')run_simulated_request('text2int', 'seven thousand nine hundred fifty seven')run_simulated_request('nlu', 'test message')run_simulated_request('nlu', 'eight')run_simulated_request('nlu', 'is it 8')app.py+2-2
mathtext_fastapi/data/text2int_results.csv+104-91
mathtext_fastapi/nlu.py+84-8
requirements.txt+3-1
scripts/make_request.py+2-2