Skip to main content

Dictionary

that code/// goes like this///
public Dictionary<string, List<string>> ConsecutiveMatches { get; set; }

public void Load(List<string> dataset)
{
    for (int i = 0; i < dataset.Count; i++)
    {
        var sentence = dataset[i];
        var words = sentence.Split(" ", StringSplitOptions.RemoveEmptyEntries);

        // This loop is used to construct the trie for prefix match
        // it can be ignored for this code review
        for (int j = 0; j < words.Length; j++)
        {
            var currentWord = words[j];

            AddWord(currentWord, sentence);
        }

        BuildConsecutiveMatches(words, sentence);
    }

    RefineConsecutiveMatches();
}

private void BuildConsecutiveMatches(string[] words, string sentence)
{
    BuildConsecutiveMatchesRecursive(words, 0, sentence);
}

private void BuildConsecutiveMatchesRecursive(string[] words, int index, string sentence)
{
    if (index >= words.Length - 1)
    {
        return;
    }

    var builder = new StringBuilder(words[index]);
    builder.Append(' ');

    for (int i = index + 1; i < words.Length; i++)
    {
        builder.Append(words[i]);
        var permutation = builder.ToString();
        if (ConsecutiveMatches.TryGetValue(permutation, out var matches))
        {
            ConsecutiveMatches[permutation].Add(sentence);
        }
        else
        {
            ConsecutiveMatches.Add(permutation, new List<string> { sentence });
        }
        builder.Append(' ');
    }

    BuildConsecutiveMatchesRecursive(words, ++index, sentence);
}

private void RefineConsecutiveMatches()
{
    ConsecutiveMatches = ConsecutiveMatches.Where(pair => pair.Value.Count >= 2)
                            .ToDictionary(pair => pair.Key,
                                        pair => pair.Value);
}

so there's that