Currently my task is to get all the user's input inside the .aiml file, and append it to a list using Python3. Below is the code:
topic.aiml
<?xml version = "1.0" encoding = "UTF-8"?>
<aiml version = "1.0.1" encoding = "UTF-8"?>
<category>
<pattern>HELLO ALICE</pattern>
<template>
Hello User
</template>
</category>
<category>
<pattern>HOW ARE YOU</pattern>
<template>
I'm fine
</template>
</category>
<category>
<pattern>WHAT ARE YOU DOING</pattern>
<template>
Coding
</template>
</category>
<category>
<pattern>WHERE YOU FROM</pattern>
<template>
Manila
</template>
</category>
</aiml>
test.py
@extract.route('/')
def index_page():
folder = 'templates/topic.aiml'
data_set = []
with open(folder, 'r') as myfile:
data = myfile.read().replace('\n', '')
return data
Return value is: HELLO ALICE HOW ARE YOU WHAT ARE YOU DOING WHERE YOU FROM, which is correct. But I don't know how will I append it to data_set
I'm just to new to this kind of task that the team gave to me. Much appreciated if someone know how to manipulate this one. Thanks!
Below is the result.
If the output you want truly is HELLO ALICE HOW ARE YOU WHAT ARE YOU DOING WHERE YOU FROM
, your result is Misleading you.
I am going to take some of the ambiguous information in your question with my interpretation of it.
What you are currently doing is just taking your file, removing the line-breaks, and returning everything else.
I ran this program;
from flask import Flask
extract = Flask(__name__)
@extract.route('/')
def index_page():
folder = 'templates/topic.aiml'
data_set = []
with open(folder, 'r') as myfile:
data = myfile.read().replace('\n', '')
return data
extract.run()
And this is how it looks in the browser;
What you instead need to do is to parse the text and find what's inside the pattern
tags. For that I recommend using BeautifulSoup;
from flask import Flask
from bs4 import BeautifulSoup
extract = Flask(__name__)
@extract.route('/')
def index_page():
folder = 'templates/topic.aiml'
with open(folder, 'r') as myfile:
soup = BeautifulSoup(myfile.read(), 'html.parser')
data_set = [match.text for match in soup.find_all("pattern")]
data = " ".join(data_set)
return data
extract.run()