'use strict';


// 将totalstring的文本切分为句
function splitString(totalstring) {
    var ret;
    ret = totalstring.split(/([。！？；.!?])/);  // 正则表达式：/([content])/

    for (var i = 0; i < ret.length; i++) {
        if (ret[i] === "。" || ret[i] === "！" || ret[i] === "？" || ret[i] === "；"|| ret[i] === "."|| ret[i] === "!"|| ret[i] === "?") {
            ret.splice(i, 1);
            i--;
        }
    }
    return ret;
}


// 匹配mystring里每一项的完整句子
function searchMaxSentence(mystring, contents) {
    var tosearch = mystring.split(/([。！？；.!?])/);
    for (var i = 0; i < tosearch.length; i++) {
        if (tosearch[i] === "。" || tosearch[i] === "！" || tosearch[i] === "？" || tosearch[i] === "；"|| tosearch[i] === "."|| tosearch[i] === "!"|| tosearch[i] === "?") {
            tosearch.splice(i, 1);
            i--;
        }
    }
    var senLen = tosearch.length;
    var ret = new Array();
    if (senLen == 1) {

        for (var i = 0; i < contents.length; i++) {
            if (contents[i].indexOf(tosearch[0]) != -1) {  // 注意不要用search函数
                ret[0] = contents[i];
                break;
            }
        }
    } else {
        for (var i = 0; i < contents.length - senLen + 1; i++) {
            var flag = 1;
            ret = new Array();
            for (var j = 0; j < senLen; j++) {
                ret[j] = contents[i + j];
                if (contents[i + j].indexOf(tosearch[j]) != 1) flag = 0;
            }
            if (flag) return ret;
        }
    }
    return ret;
}


function freqSentence(transfermsg, contents) {
    var dic = new Array();  // 定义一个字典
    for (var i = 0; i < transfermsg.length; i++) {
        var msg = searchMaxSentence(transfermsg[i]["items"], contents);
        var freq = transfermsg[i]["freq"];
        for (var j = 0; j < msg.length; j++) {
            // console.log(j, msg[j])
            // console.log(dic.keys())
            if (msg[j] in dic.keys()) {
            // if (dic.hasOwnProperty(msg[j])) {
                dic[msg[j]] += freq;
            } else {
                dic[msg[j]] = freq;
            }
            console.log(dic[msg[j]])
        }
    }
    return dic;
}