private static double ScoreTokens(TokenNode tokenNode, IList<Token> tokens, int index) { Token codeToken = tokens[index]; TokenNode nextToken = tokenNode.NextTokens.FirstOrDefault(nt => nt.Kind == codeToken.Kind); if (nextToken != null) { // Token exists in match tree => points !!! double score = nextToken.Examples.Contains(codeToken.Value) ? SCORE_MULTIPLIER_FOR_EXACT_MATCH: SCORE_MULTIPLIER_PER_LEVEL; if (index < tokens.Count() - 1) { return score * ScoreTokens(nextToken, tokens, index + 1); } return score; } // Token did not exist => no points return 1; }
private static double AddTokens(TokenNode tokenNode, IList<Token> tokens, int index) { double totalScore = 0; while (index < tokens.Count && tokenNode.Level < 10) { Token codeToken = tokens[index]; TokenNode nextTreeToken = tokenNode.NextTokens.FirstOrDefault(nt => nt.Kind == codeToken.Kind); if (nextTreeToken == null) { // Token doesn't exist on this tree level yet var newToken = new TokenNode(codeToken.Kind, tokenNode.Level + 1, tokenNode.Score * SCORE_MULTIPLIER_PER_LEVEL, codeToken.Value); totalScore += tokenNode.Score * SCORE_MULTIPLIER_PER_LEVEL; tokenNode.NextTokens.Add(newToken); tokenNode = newToken; } else { // Token already exists on this level nextTreeToken.Examples.Add(codeToken.Value); tokenNode = nextTreeToken; } index++; } return totalScore; }
private static TokenNode BuildMatchTree(string trainingCode, out double totalScorePossible) { List<Token> tokens = GetAllTokens(trainingCode); // Recursivly build the tree TokenNode root = new TokenNode(TokenKind.Unknown, 0, 1, null); double totalScore = 0; for (int index = 0; index < tokens.Count-1; index++) { totalScore += AddTokens(root, tokens, index); } totalScorePossible = totalScore; return root; }