在将.csv文件附加到该列表后,如何从列表中选择关键词?

时间:2016-06-09 08:32:37

标签: python list keyword

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

import {Injectable} from '@angular/core';
import {Http, Response, Headers, RequestOptions} from '@angular/http';

import {Observable} from 'rxjs/Observable';

@Injectable ()
export class StructureRequestService {
result: Object;
//private myUrl = 'http://manny.herokuapp.com/audit/get/structure';
private myUrl = './app/home/nodes.json';  // local URL to structure APi
constructor (private http: Http) {
    //use XHR object
    let _build = (<any> http)._backend._browserXHR.build;
    (<any> http)._backend._browserXHR.build = () => {
        let _xhr =  _build();
        _xhr.withCredentials = true;
        return _xhr;
    };
}

sendRequest() {
    let body = JSON.stringify({});
    let headers = new Headers({ 'Content-Type': 'application/json'});
    let options = new RequestOptions({
        headers: headers
    });
     this.http.get(this.myUrl, options)
        .map((res: Response) => res.json())
        .subscribe(res => {this.result = res;
            return this.result; });
}
}

1 个答案:

答案 0 :(得分:0)

您是否正在尝试构建关键字字典或重新发现句子问题? 在这两种情况下,您都需要将问题与关键字相关联。

获取关键字的基本方法是将单词分成单词(使用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))

结果:

  

{&#39;电池&#39;:50.0},我无法打开手机

     

{&#39;屏幕&#39;:25.0},屏幕无法启动

     

{&#39;屏幕&#39;:50.0,&#39;电池&#39;:50.0},手机显示屏空白

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

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