需要解析并生成一个好的输入以发送到mediawiki api

2024-05-23 14:23:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用Flask/python3.7进行一个项目,我无法提供一个好的用户输入,这可以从media wiki API获得具体的答案

我试图创建一个stopWords文件,但这还不足以让用户提出的问题变得清晰,得到一个好的答案,并得到用户期望的数据

下面是我用来解决这个问题的两种方法


def __init__(self, ask):
    self.ask = ask
    self.stopwords = []
    self.result = []

def ReadSW(self):
    """ Method to creat a list of the words that 
    must be ignored to have a specific question """

    with open('app/stopwords.txt', 'r') as my_words :
         for line  in my_words:
             line = line.rstrip()
             self.stopwords.append(line)
    return self.stopwords

def SelectWord(self):
    """Select and filter the worlds we need from the user input"""
    response = self.ask.split()

    for elt in response:
        if elt not in self.stopwords:
        self.result.append(elt)
    return ' '.join(self.result)


Tags: theto答案用户inselfformy