hrdk108 /
hsbot
| 1 | /** |
||
| 2 | * hsBot: Supply template and your bot is ready. |
||
| 3 | * |
||
| 4 | * Hardik Shah <[email protected]> |
||
| 5 | * |
||
| 6 | * MIT License |
||
| 7 | */ |
||
| 8 | |||
| 9 | function Analysis(){ |
||
| 10 | |||
| 11 | } |
||
| 12 | |||
| 13 | Analysis.prototype._analyzed = function(userData){ |
||
| 14 | var analyzedData = {}; |
||
| 15 | var loggedInTime = userData.loggedIn; |
||
| 16 | var lastActivity = userData.activities[userData.activities.length-1].ut; |
||
| 17 | analyzedData.timeSpent = lastActivity - loggedInTime; |
||
| 18 | var frequentArr = this.getFrequentText(userData.activities); |
||
| 19 | frequentArr.forEach(function(obj){ |
||
| 20 | if(obj.type == "question"){ |
||
| 21 | analyzedData.frequentBotText = obj.text; |
||
| 22 | } |
||
| 23 | if(obj.type == "command"){ |
||
| 24 | analyzedData.frequentUserText = obj.text; |
||
| 25 | } |
||
| 26 | }); |
||
| 27 | return analyzedData; |
||
| 28 | }; |
||
| 29 | |||
| 30 | Analysis.prototype.getFrequentText = function(activities){ |
||
| 31 | var countArr = { |
||
| 32 | question : {}, |
||
| 33 | command: {} |
||
| 34 | }; |
||
| 35 | activities.forEach(function(act){ |
||
| 36 | countArr.question[act.preQ] = (countArr.question[act.preQ]||0) + 1; |
||
| 37 | countArr.command[act.pattern] = (countArr.command[act.pattern]||0) + 1; |
||
| 38 | }); |
||
| 39 | |||
| 40 | var max = 0, t = "", frequentArr = []; |
||
| 41 | for (var key in countArr) { |
||
|
0 ignored issues
–
show
|
|||
| 42 | for (var key1 in countArr[key]) { |
||
|
0 ignored issues
–
show
A for in loop automatically includes the property of any prototype object, consider checking the key using
hasOwnProperty.
When iterating over the keys of an object, this includes not only the keys of the object, but also keys contained in the prototype of that object. It is generally a best practice to check for these keys specifically: var someObject;
for (var key in someObject) {
if ( ! someObject.hasOwnProperty(key)) {
continue; // Skip keys from the prototype.
}
doSomethingWith(key);
}
Loading history...
|
|||
| 43 | if(max <= parseInt(countArr[key][key1])){ |
||
| 44 | max = countArr[key][key1]; |
||
| 45 | t = key1; |
||
| 46 | } else { |
||
| 47 | max = max; |
||
| 48 | } |
||
| 49 | } |
||
| 50 | frequentArr.push({"text": t, type: key}); |
||
| 51 | max = 0; |
||
| 52 | t= ""; |
||
| 53 | } |
||
| 54 | |||
| 55 | return frequentArr; |
||
| 56 | }; |
||
| 57 | |||
| 58 | module.exports = Analysis; |
When iterating over the keys of an object, this includes not only the keys of the object, but also keys contained in the prototype of that object. It is generally a best practice to check for these keys specifically: