public ActionResult Index( long matterId, short characteristicLinkId, Notation notation, int step, int initialLength, string accuracy) { return(CreateTask(() => { string characteristicName; string matterName; double[] characteristics; Chain sequence; IFullCalculator calculator; Link link; using (var db = new LibiadaWebEntities()) { var commonSequenceRepository = new CommonSequenceRepository(db); matterName = Cache.GetInstance().Matters.Single(m => matterId == m.Id).Name; var sequenceId = db.CommonSequence.Single(c => matterId == c.MatterId && c.Notation == notation).Id; sequence = commonSequenceRepository.GetLibiadaChain(sequenceId); var characteristicTypeLinkRepository = FullCharacteristicRepository.Instance; characteristicName = characteristicTypeLinkRepository.GetCharacteristicName(characteristicLinkId, notation); FullCharacteristic characteristic = characteristicTypeLinkRepository.GetCharacteristic(characteristicLinkId); calculator = FullCalculatorsFactory.CreateCalculator(characteristic); link = characteristicTypeLinkRepository.GetLinkForCharacteristic(characteristicLinkId); } // characteristics = SequencesCharacteristicsCalculator.Calculate( new[] { sequenceId }, characteristicLinkId); AverageRemoteness averageRemotenessCalc = new AverageRemoteness(); double averageRemoteness = averageRemotenessCalc.Calculate(sequence, Link.Start); Alphabet alphabet = sequence.Alphabet; var doubleAccuracy = double.Parse(accuracy); List <SequencePredictionData> sequencePredictionResult; Chain chain; (sequencePredictionResult, chain) = Predict(averageRemotenessCalc, sequence, initialLength, alphabet, averageRemoteness, doubleAccuracy); var matching = FindPercentageOfMatching(sequence, chain, initialLength) * 100; var result = new Dictionary <string, object> { { "result", sequencePredictionResult }, { "matterName", matterName }, { "matching", matching } }; return new Dictionary <string, string> { { "data", JsonConvert.SerializeObject(result) } }; })); }
private (List <SequencePredictionData>, Chain) Predict( AverageRemoteness averageRemotenessCalc, Chain sequence, int initialLength, Alphabet alphabet, double averageRemoteness, double accuracy) { var sequencePredictionResult = new List <SequencePredictionData>(); int wordPositionStart = initialLength; Chain currentPredicion = null; Chain predicted = Copy(sequence, new Chain(initialLength)); for (int i = initialLength; i < sequence.Length; i++) { currentPredicion = Copy(predicted, new Chain(i + 1)); Dictionary <double, ContenderValue> contenderValues = new Dictionary <double, ContenderValue>(); bool isFound = false; foreach (IBaseObject element in alphabet) { currentPredicion.Set(element, wordPositionStart); double currentAvgRemoteness = averageRemotenessCalc.Calculate(currentPredicion, Link.Start); double delta = Math.Abs(currentAvgRemoteness - averageRemoteness); if (delta < accuracy) { contenderValues.Add(delta, new ContenderValue { CurrentAverageRemoteness = currentAvgRemoteness, PredictedWord = SubChain(currentPredicion, wordPositionStart, i) }); isFound = true; } } if (isFound) { ContenderValue contenderValue = contenderValues[contenderValues.Keys.Min()]; sequencePredictionResult.Add(new SequencePredictionData { Fragment = SubChain(sequence, wordPositionStart, i).ToString(), Predicted = contenderValue.PredictedWord.ToString(), ActualCharacteristic = contenderValue.CurrentAverageRemoteness, TheoreticalCharacteristic = averageRemotenessCalc.Calculate(SubChain(sequence, 0, i), Link.Start) //PercentageOfMatched = FindPercentageOfMatching(sequence, currentPredicion) }); predicted = Concat(predicted, contenderValue.PredictedWord, wordPositionStart); wordPositionStart = i + 1; } else { throw new Exception($"Couldn't predict with given accuracy. Position: {i}"); //currentPredicion = IncrementNextCharacter(currentPredicion, wordPositionStart, alphabet); } } return(sequencePredictionResult, currentPredicion); }