public static bool CalculateManhattanMetricForOneTestSet(Reuter testSet, List <Reuter> TrainingVectors, int k) { double xn = 0; double yn = 0; double underSqrt = 0; TestVectorAndTrainingVectorsCollection result = new TestVectorAndTrainingVectorsCollection(); for (int i = 0; i < TrainingVectors.Count; i++) //wykonujemy petle dla kazdego wzorca treningowego { foreach (var word in testSet.VectorFeatures) //sprawdzamy dla kazdego slowa z wektora testowego czy istnieje takie slowo w wektorze treningowym { if (TrainingVectors.ElementAt(i).VectorFeatures.ContainsKey(word.Key)) { yn = TrainingVectors.ElementAt(i).VectorFeatures[word.Key]; } else { yn = 0; } xn = word.Value; underSqrt += Math.Abs(xn - yn); } TrainingVectors.ElementAt(i).HowFar = underSqrt; result.TestReuter = testSet; underSqrt = 0; } result.TrainingReuters = TrainingVectors; return(KnnAlgorithm.Calculate(result, k)); }
//Team frequency public static void HowManyWordsExtractor(Reuter reut) { int howManyTimeWordOccur = 0; Dictionary <string, double> vectorFeature = new Dictionary <string, double>(); for (int i = 0; i < reut.Text.Count; i++) { for (int j = 0; j < reut.Text.Count; j++) { if (reut.Text.ElementAt(i) == reut.Text.ElementAt(j)) { howManyTimeWordOccur++; } } if (vectorFeature.ContainsKey(reut.Text.ElementAt(i))) { continue; } if (reut.Text.ElementAt(i).Equals("")) { continue; } vectorFeature.Add(reut.Text.ElementAt(i), (double)howManyTimeWordOccur / reut.Text.Count); howManyTimeWordOccur = 0; } reut.VectorFeatures = vectorFeature; }
public static bool ChoosePlace(List <Reuter> neighbours, Reuter TestReuter) { List <Reuter> tempneighbours = neighbours.ToList(); int howManyTimesOccur = 0; Dictionary <string, int> howManyPlaces = new Dictionary <string, int>(); for (int i = 0; i < tempneighbours.Count; i++) { for (int j = 0; j < tempneighbours.Count; j++) { if (tempneighbours.ElementAt(i).Places.First() == tempneighbours.ElementAt(j).Places.First()) { howManyTimesOccur++; } } if (howManyPlaces.ContainsKey(tempneighbours.ElementAt(i).Places.First())) { howManyTimesOccur = 0; continue; } howManyPlaces.Add(tempneighbours.ElementAt(i).Places.First(), howManyTimesOccur); howManyTimesOccur = 0; } howManyPlaces = howManyPlaces.OrderByDescending(x => x.Value) .ToDictionary(pair => pair.Key, pair => pair.Value); List <KeyValuePair <string, int> > result = howManyPlaces.ToList(); var FoundPlace = result.First().Key; if (TestReuter.Places.First().Equals(FoundPlace)) { return(true); } else { return(false); } }
public static Task HowManyWordsExtractorAsync(Reuter reut) { return(Task.Factory.StartNew(() => HowManyWordsExtractor(reut))); }