You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
TrustPilotChallenge/dotnet/WhiteRabbit/StringsProcessor.cs

135 lines
4.9 KiB

8 years ago
namespace WhiteRabbit
{
using System;
8 years ago
using System.Collections.Generic;
using System.Linq;
internal sealed class StringsProcessor
8 years ago
{
private const byte SPACE = 32;
// Ensure that permutations are precomputed prior to main run, so that processing times will be correct
static StringsProcessor()
{
PrecomputedPermutationsGenerator.HamiltonianPermutations(1, 0);
}
public StringsProcessor(byte[] sourceString, int maxWordsCount, IEnumerable<byte[]> words)
8 years ago
{
var filteredSource = sourceString.Where(ch => ch != SPACE).ToArray();
this.NumberOfCharacters = filteredSource.Length;
8 years ago
this.VectorsConverter = new VectorsConverter(filteredSource);
var allWordsAndVectors = words
.Where(word => word != null && word.Length > 0)
.Select(word => new { word, vector = this.VectorsConverter.GetVector(word) })
.Where(tuple => tuple.vector != null)
.Select(tuple => tuple.word)
.Distinct(new ByteArrayEqualityComparer())
.Select(word => word)
.ToArray();
// Dictionary of vectors to array of words represented by this vector
var vectorsToWords = allWordsAndVectors
.Select((word, index) => new { word, index, vector = this.VectorsConverter.GetVector(word).Value })
.GroupBy(tuple => tuple.vector)
.Select(group => new { vector = group.Key, words = group.Select(tuple => tuple.index).ToArray() })
.ToList();
this.WordsDictionary = vectorsToWords.Select(tuple => tuple.words).ToArray();
this.AllWords = allWordsAndVectors.Select(word => new Word(word)).ToArray();
8 years ago
this.VectorsProcessor = new VectorsProcessor(
this.VectorsConverter.GetVector(filteredSource).Value,
maxWordsCount,
vectorsToWords.Select(tuple => tuple.vector).ToArray());
8 years ago
}
private VectorsConverter VectorsConverter { get; }
private Word[] AllWords { get; }
/// <summary>
/// WordsDictionary[vectorIndex] = [word1index, word2index, ...]
/// </summary>
private int[][] WordsDictionary { get; }
8 years ago
private VectorsProcessor VectorsProcessor { get; }
private int NumberOfCharacters { get; }
#if SINGLE_THREADED
8 years ago
public IEnumerable<PhraseSet> GeneratePhrases()
#else
public ParallelQuery<PhraseSet> GeneratePhrases()
#endif
8 years ago
{
8 years ago
// task of finding anagrams could be reduced to the task of finding sequences of dictionary vectors with the target sum
var sums = this.VectorsProcessor.GenerateSequences();
8 years ago
8 years ago
// converting sequences of vectors to the sequences of words...
return from sum in sums
let filter = ComputeFilter(sum)
let wordsVariants = this.ConvertVectorsToWordIndexes(sum)
from wordsArray in Flattener.Flatten(wordsVariants)
from phraseSet in this.ConvertWordsToPhrases(wordsArray, filter)
select phraseSet;
}
public long GetPhrasesCount()
{
return this.VectorsProcessor.GenerateSequences()
.Select(this.ConvertVectorsToWordsNumber)
.Sum(tuple => tuple.Item2 * PrecomputedPermutationsGenerator.GetPermutationsNumber(tuple.Item1));
}
private static uint ComputeFilter(int[] vectors)
{
uint result = 0;
for (var i = 1; i < vectors.Length; i++)
{
if (vectors[i] == vectors[i - 1])
{
result |= (uint)1 << (i - 1);
}
}
return result;
}
private int[][] ConvertVectorsToWordIndexes(int[] vectors)
{
var length = vectors.Length;
var words = new int[length][];
for (var i = 0; i < length; i++)
{
words[i] = this.WordsDictionary[vectors[i]];
}
return words;
}
private Tuple<int, long> ConvertVectorsToWordsNumber(int[] vectors)
{
long result = 1;
for (var i = 0; i < vectors.Length; i++)
{
result *= this.WordsDictionary[vectors[i]].Length;
}
return Tuple.Create(vectors.Length, result);
}
private IEnumerable<PhraseSet> ConvertWordsToPhrases(int[] wordIndexes, uint filter)
{
var permutations = PrecomputedPermutationsGenerator.HamiltonianPermutations(wordIndexes.Length, filter);
var permutationsLength = permutations.Length;
for (var i = 0; i < permutationsLength; i += Constants.PhrasesPerSet)
{
yield return new PhraseSet(this.AllWords, wordIndexes, permutations, i, this.NumberOfCharacters);
}
}
8 years ago
}
}