如何将.csv文件附加到列表中后从列表中选择关键字



我需要能够从Excel CSV文件中选择关键字,我已经将该文件附加到列表中。该程序是电话故障排除,我需要输入("屏幕无法打开")具有与我输入的输出相同的输出("显示屏为空白")。

"Troubleshooting Program to give the user a solution to a trouble they've     encountered based on inputted key words."
phoneprob=input("What problem are you having with your phone? ")
prob=open("phone.csv","r")
phone=prob.read()
prob.close()
eachProb=phone.split("n")
print(eachProb)
problist=[eachProb]
print (problist)

您是在尝试构建关键字字典还是检索句子问题?在这两种情况下,您都需要将问题与关键字相关联。

获取关键字的基本方法是将句子拆分为单词(使用 s.split())并使用最常用的关键字更新关键字列表......difflib 可以在这里提供帮助。

由于我们不知道给定的文件模式,我假设它只是一个句子列表,您在其他地方提供了关键字/问题(情况 Dict)。

例如:

csv_ret = ["I can't turn on my phone", "The screen won't turn on", "phone The display is blank"]
situations = {
    "screen": ["turn on", "blank", "display", "screen"],
    "battery": ["turn on", "phone"]
}

def get_situation_from_sentence(sentence):
    occurences = {}
    for word in sentence.split():
        for key, value in situations.items():
            if word in value:
                if occurences.get(key) is None:
                    occurences[key] = [word]
                elif word not in occurences.get(key):
                    occurences[key].append(word)
    averages = {k: ((len(v) * 100) / len(situations[k])) for k, v in occurences.items()}
    return "{}, {}".format(averages, sentence)
for sentence in csv_ret:
    print(get_situation_from_sentence(sentence))

结果:

{'电池':50.0},我无法打开手机

{'screen': 25.0}, 屏幕无法打开

{"屏幕": 50.0,

"电池": 50.0}, 手机 显示屏空白

此代码以百分比评估句子问题和相关关键字匹配。

再一次,这是一个非常基本的解决方案,您可能需要更强大的东西(词法分析器/解析器,机器学习......),但有时更简单更好:)

最新更新