How to write this new file completing with information inside a string in the correct positions?

Question:

If I have this string, it contains several lines separated by newlines.

data_string = """
te gustan los animales?
puede que algunos me gusten pero no se mucho de eso

animales
puede que algunos me gusten pero no se mucho de eso

te gustan las plantas?
no se mucho sobre ellas

carnivoro
segun mis registros son aquellos que se alimentan de carne
"""

# Split the string into individual lines
lines = data_string.splitlines() 

# Open the check.py file for writing
with open("check.py", "w") as f:
    # Write the lines in the file here...
    for line in lines:
        #How to write the code with this lines info?

Using python how can I create a file using the content of the lines inside the data_string variable so that it is thus inside a new file.

This is how the resulting file should look like, note that the lines "te gustan los animales?", "animales", "te gustan las plantas?" and "carnivoro" were left as parameters of SequenceMatcher(None, str1, "here").ratio() and the lines "puede que algunos me gusten pero no se mucho de eso", "no se mucho sobre ellas", "no se mucho sobre ellas" and "segun mis registros son aquellos que se alimentan de carne" were left as response_text = "here"

Output file called check.py :

from difflib import SequenceMatcher

def check_function(str1):
    
    similarity_ratio = 0.0
    response_text = "no coincide con nada"
    threshold = 0.0

    similarity_ratio_to_compare = SequenceMatcher(None, str1, "te gustan los animales?").ratio()
    if similarity_ratio_to_compare > similarity_ratio and similarity_ratio_to_compare > threshold:
        response_text = "puede que algunos me gusten pero no se mucho de eso"
        similarity_ratio = similarity_ratio_to_compare

    similarity_ratio_to_compare = SequenceMatcher(None, str1, "animales").ratio()
    if similarity_ratio_to_compare > similarity_ratio and similarity_ratio_to_compare > threshold:
        response_text = "puede que algunos me gusten pero no se mucho de eso"
        similarity_ratio = similarity_ratio_to_compare

    similarity_ratio_to_compare = SequenceMatcher(None, str1, "te gustan las plantas?").ratio()
    if similarity_ratio_to_compare > similarity_ratio and similarity_ratio_to_compare > threshold:
        response_text = "no se mucho sobre ellas"
        similarity_ratio = similarity_ratio_to_compare

    similarity_ratio_to_compare = SequenceMatcher(None, str1, "carnivoro").ratio()
    if similarity_ratio_to_compare > similarity_ratio and similarity_ratio_to_compare > threshold:
        response_text = "segun mis registros son aquellos que se alimentan de carne"
        similarity_ratio = similarity_ratio_to_compare

    return response_text

input_text = "te gusta saltar la soga bien piolon???"
text = check_function(input_text)
print(text)

In the end this file must be saved with the name check.py, keep in mind that the number of lines inside the data_string variable is not known (and in this case there are only 4 questions with 4 answers to prevent the question from being too long)

Asked By: Matt095

||

Answers:

Here is the code you can save your question and answer pair in a file as the key value pair in .json format:
Code to create .json file containing question and answer pair:

jsonData.py


import json

data_string = {
"te gustan los animales?":
"puede que algunos me gusten pero no se mucho de eso",

"animales":
"puede que algunos me gusten pero no se mucho de eso",

"te gustan las plantas?":
"no se mucho sobre ellas",

"carnivoro":
"segun mis registros son aquellos que se alimentan de carne"
}

with open('result.json', 'w') as fp:
    json.dump(data_string, fp)

from the above code you can store all your question and answer pair in the json file…
First store all you question and answer pair using jsonData.py file and then use the below code to match the question and answer if exis..


And the below code is for matching the question answer pair :

check.py

from difflib import SequenceMatcher
import json

with open('result.json', 'r') as f_in:
    data = json.load(f_in)

def check_function(str1):
    similarity_ratio = 0.0
    response_text = "no coincide con nada"
    threshold = 0.0

    try:
        similarity_ratio_to_compare = SequenceMatcher(None,str1, data[str1]).ratio()
        if similarity_ratio_to_compare > similarity_ratio and similarity_ratio_to_compare > threshold:
            response_text = data[str1]
            similarity_ratio = similarity_ratio_to_compare
    except Exception as e:
        return response_text
    return response_text

input_text = input('Enter text to get answer:')
text = check_function(input_text)
print(text)

Answered By: Manvi