Searching in a .txt file and Comparing the two values of a string in python?

2024/10/15 13:22:42
"cadence_regulatable_result": "completeRecognition","appserver_results": {"status": "success","final_response": 0,"payload": {"actions": [{"speaker": "user","type": "conversation","nbest_text": {"confidences": [478,0,0],"words": [[{"stime": 0,"etime": 1710,"word": "ConnectedDrive\\*no-space-before","confidence": "0.241"}],[{"stime": 0,"etime": 1020,"word": "Connected\\*no-space-before","confidence": "0.0"},{"stime": 1020,"etime": 1710,"word": "drive","confidence": "0.0"}],[{"stime": 0,"etime": 900,"word": "Connect\\*no-space-before","confidence": "0.0"},{"stime": 900,"etime": 980,"word": "to","confidence": "0.0"},{"stime": 980,"etime": 1710,"word": "drive","confidence": "0.0"}]],"transcriptions"= ["ConnectedDrive","Connected drive","Connect to drive"]}}]}},"final_response": 0,"prompt": "","result_format": "appserver_post_results"
}: form-data;name="QueryResult"Content-Type: application/JSON;charset=utf-8Nuance-Context: efb3d3ce-ef50-4e83-8c31-063c3f5208aa{"status_code": 0,"result_type": "DRAGON_NLU_ASR_CMD","NMAS_PRFX_SESSION_ID": "f786f0be-d547-4fca-8d72-96429a30c9db","NMAS_PRFX_TRANSACTION_ID": "1","audio_transfer_info": {"packages": [{"time": "20151221085512579","bytes": 1633},{"time": "20151221085512598","bytes": 3969}],"nss_server": "10.56.11.186:4503","end_time": "20151221085512596","audio_id": 1,"start_time": "20151221085512303"},"cadence_regulatable_result": "completeRecognition","appserver_results": {"status": "success","final_response": 1,"payload": {"diagnostic_info": {"adk_dialog_manager_status": "undefined","nlu_version": "[NLU_PROJECT:NVCCP-eng-USA];[D0160932];[VL-Models:Version: vl.1.100.12-2-GMT20151130160335]","nlps_host": "mt-dmz-nlps002.nuance.com:8636","nlps_ip": "10.56.10.51","application": "AUDI_2017","nlu_component_flow": "[Input:VoiceJSON] [FieldID|auto_main] [NLUlib|C-eckart-r$Rev$.f20151118.1250] [build|G-r72490M.f20151130.1055] [vlmodel|Version: 2-GMT20151130160335] [Flow|+VlingoTokenized]","third_party_delay": "0","nmaid": "AUDI_SDS_2017_EXT_20151203","nlps_profile": "AUDI_2017","fieldId": "auto_main","nlps_profile_package_version": "r159218","nlu_annotator": "com-GBR.ncs51.VlingoNLU-client-qNVCCP_NCS51","ext_map_time": "2","nlu_use_literal_annotator": "0","int_map_time": "2","nlps_nlu_type": "nlu_project","nlu_language": "eng-GBR","timing": {"finalRespSentDelay": "188","intermediateRespSentDelay": "648"},"nlps_profile_package": "AUDI_2017"},"actions": [{"Input": {"Interpretations": ["ConnectedDrive"],"Type": "asr"},"Instances": [{"nlu_classification": {"Domain": "UDE","Intention": "Unspecified"},"nlu_interpretation_index": 1,"nlu_slot_details": {"Name": {"literal": "ConnectedDrive"},"Search-phrase": {"literal": "connecteddrive"}},"interpretation_confidence": 4549}],"type": "nlu_results","api_version": "1.0"}],"nlps_version": "nlps(z):6.1.100.12.2-B359;Version: nlps-base-GMT20151130193521;"}},

Firstly, I am searching for transcriptions and interpretations word in the .txt file (So I am using regex) then I want to compare the FIRST value of transcriptions ("Drive me to a charging station") with the Interpreations value ("Drive me to a charging station"). If I give as below in my program, it is just printing as Recognition is INVALID

directory =os.path.join("C:\Users\hemanth_venkatappa\Desktop\Working\pcm-audio\English")
for subdir, dirs, files in os.walk(directory): for file in files:if file.endswith(".txt"): content=json.load(file)if "status_code" in content:if content["status_code"]==0:print("valid")
Answer

You can take a look at difflib for text comparison using Python.

The difflib module contains tools for computing and working withdifferences between sequences. It is especially useful for comparingtext, and includes functions that produce reports using several commondifference formats.

difflib tutorial

Using this module you can evaluate differences between two strings or .txt files this way:

import diffliba = ["Drive me to a charging station", "Drive me to charging station", "Drive me to a charging Station"]
correct = ["Drive me to a charging station"]print difflib.SequenceMatcher(None, a[0], correct[0]).ratio()
>> 1.0print difflib.SequenceMatcher(None, a[1], correct[0]).ratio()
>> 0.965517241379print difflib.SequenceMatcher(None, a[2], correct[0]).ratio()
>> 0.966666666667

As you can see, the .ratio() between a[0] and correct is 1.0 or 100%. This means they're the same string.

You can use a loop to evaluate the ratios and if ratio == 1.0 then print "Recognition is VALID "

Also if you don't wanna use the .ratio() between the strings, you can check the differences using:

d = difflib.Differ()
diff = d.compare(a, correct)
print '\n'.join(diff)

And this block of code gives me:

  Drive me to a charging          # no signal at the start means it's the same string
- Drive me to charging station    # this string has less chars than the expected string
- Drive me to a charging Station  # same here

Then you'll have to figure a way to print Recognition is VALID or INVALID according to your expectations.

https://en.xdnf.cn/q/117830.html

Related Q&A

How to perform an HTTP/XML authentication with requests

I am trying to authenticate to Docushare with Python 3.4 using requests 2.7. I am relatively new to Python and to the requests module but Ive done a lot of reading and am not able to make any more prog…

webPy Sessions - Concurrent users use same session and session timeout

I have a webPy app using sessions for user authentication. Sessions are initiated like so:web.config.debug=Falsestore = web.session.DiskStore(/path_to_app/sessions) if web.config.get(_session) is None:…

get text content from p tag

I am trying to get description text content of each block on this page https://twitter.com/search?q=data%20mining&src=typd&vertical=default&f=users. html for p tag looks like<p class=&q…

Python - making a function that would add - between letters

Im trying to make a function, f(x), that would add a "-" between each letter:For example:f("James")should output as:J-a-m-e-s-I would love it if you could use simple python function…

python script keeps converting dates to utc

I have the following:import psycopg2 from openpyxl import Workbook wb = Workbook() wb.active =0 ws = wb.active ws.title = "Repair" ws.sheet_properties.tabColor = "CCFFCC"print(wb.sh…

sklearn tsne with sparse matrix

Im trying to display tsne on a very sparse matrix with precomputed distances values but Im having trouble with it.It boils down to this:row = np.array([0, 2, 2, 0, 1, 2]) col = np.array([0, 0, 1, 2, 2,…

Removing a sublist from a list

I have a list e.g. l1 = [1,2,3,4] and another list: l2 = [1,2,3,4,5,6,7,1,2,3,4]. I would like to check if l1 is a subset in l2 and if it is, then I want to delete these elements from l2 such that l2 …

Python double FOR loops without threading

Basically, I want to make a grid with school subjects and all the test results I got from it, and I want to display about 10 results for every subject.Like this:... ------------------------------------…

Challenging way of counting entries of a file dynamically

I am facing a strange question, which despite of trying many times, i am not able to find the logic and proper code to the problem. I have a file in the format below: aa:bb:cc dd:ee:ff 100 ---------…

NSException on import of matplotlib, kivy in OSX

Im working on some kivy code thats working fine on windows 10, but crashes on osx sierra, Ive isolated that the crash happens when I import kivy.core.window along side matplotlib: import matplotlib mat…