cheapestSegmentSplitStrategy: function(words, accumulatedClasses, explain, explanations) { //for (var start=0; start<=words.length; ++start) { // for (var end=start+1; end<=words.length; ++end) { // var segment = words.slice(start,end).join(" "); // var bestClassAndProbability = this.bestClassOfSegment(segment, explain); // if (bestClassAndProbability[1] != Infinity) // { // var bestClass = bestClassAndProbability[0]; // var bestClassProbability = bestClassAndProbability[1]; // digraph.add(start, end, -bestClassProbability); // } // } //} var cheapest_paths = require("graph-paths").cheapest_paths; var mini = Infinity _(words.length).times(function(nn){ cheapestSegmentClassificationCosts = cheapest_paths(segmentClassificationCosts, nn); _.each(cheapestSegmentClassificationCosts, function(value, key, list){ if (value.cost<mini) { mini = value.cost cheapestSentenceClassificationCost = value } }, this) }, this) cheapestSegmentClassificationCosts = cheapest_paths(segmentClassificationCosts, 0); cheapestSentenceClassificationCost = cheapestSegmentClassificationCosts[words.length]; var path = cheapestSentenceClassificationCost.path; for (var i=0; i<path.length-1; ++i) { // var segment = words.slice(cheapestClassificationPath[i],cheapestClassificationPath[i+1]).join(" "); var segment = words.slice(path[i],path[i+1]).join(" "); //HERE var segmentClassesWithExplain = this.classifySegment(segment, explain); var segmentClasses = (segmentClassesWithExplain.classes? segmentClassesWithExplain.classes: segmentClassesWithExplain); if (segmentClasses.length>0) accumulatedClasses[segmentClasses[0]] = true; // explanations = [] if (explain>0) { if (segmentClasses.length>0) explanations.push([segmentClasses[0], segment, [path[i], path[i+1]],segmentClassesWithExplain['explanation']['positive']]) }; } },
_(words.length).times(function(nn){ cheapestSegmentClassificationCosts = cheapest_paths(segmentClassificationCosts, nn); _.each(cheapestSegmentClassificationCosts, function(value, key, list){ if (value.cost<mini) { mini = value.cost cheapestSentenceClassificationCost = value } }, this) }, this)
cheapestSegmentSplitStrategy: function(words, accumulatedClasses, explain, explanations) { // Calculate the cost of classification of the segment from i to j. // (Cost = - log probability). var segmentClassificationCosts = []; // best cost to classify segment [i,j] for (var start=0; start<=words.length; ++start) { segmentClassificationCosts[start] = []; for (var end=0; end<start; ++end) segmentClassificationCosts[start][end]=Infinity; segmentClassificationCosts[start][start]=0; for (var end=start+1; end<=words.length; ++end) { var segment = words.slice(start,end).join(" "); var classification = this.bestClassOfSegment(segment); segmentClassificationCosts[start][end] = -Math.log(classification.probability); } } //console.log(words+": "); console.log("segmentClassificationCosts"); console.dir(segmentClassificationCosts); var cheapest_paths = require("graph-paths").cheapest_paths; cheapestSegmentClassificationCosts = cheapest_paths(segmentClassificationCosts, 0); cheapestSentenceClassificationCost = cheapestSegmentClassificationCosts[words.length]; if (!cheapestSentenceClassificationCost) throw new Error("cheapestSegmentClassificationCosts["+words.length+"] is empty"); //console.log("cheapestSentenceClassificationCost"); console.dir(cheapestSentenceClassificationCost); var cheapestClassificationPath = cheapestSentenceClassificationCost.path; explanations.push(cheapestSentenceClassificationCost); for (var i=0; i<cheapestClassificationPath.length-1; ++i) { var segment = words.slice(cheapestClassificationPath[i],cheapestClassificationPath[i+1]).join(" "); //console.log(segment+": "); var segmentCategoryWithExplain = this.classifySegment(segment, explain); //console.dir(segmentCategoryWithExplain); var segmentCategory = (segmentCategoryWithExplain.category? segmentCategoryWithExplain.category: segmentCategoryWithExplain); accumulatedClasses[segmentCategory]=true; if (explain>0) { explanations.push(segment); explanations.push(segmentCategoryWithExplain.explanation); }; } },