【问题标题】:How to pick key words from a list, after appending a .csv file to that list?将 .csv 文件附加到该列表后,如何从列表中选择关键词?
【发布时间】:2016-06-09 08:32:37
【问题描述】:

我需要能够从 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)

【问题讨论】:

  • 请说明您的期望(输入和输出)

标签: python list keyword


【解决方案1】:

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

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

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

例如:

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))

结果:

{'battery': 50.0},我无法开机

{'screen': 25.0},屏幕不亮

{'screen': 50.0, 'battery': 50.0}, phone 显示空白

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

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

【讨论】:

    猜你喜欢
    • 2014-05-13
    • 2017-06-30
    • 1970-01-01
    • 2016-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-08
    • 2021-11-02
    相关资源
    最近更新 更多