|
|
@ -4,7 +4,7 @@ |
|
|
|
using System.Collections.Immutable; |
|
|
|
using System.Collections.Immutable; |
|
|
|
using System.Linq; |
|
|
|
using System.Linq; |
|
|
|
using System.Numerics; |
|
|
|
using System.Numerics; |
|
|
|
|
|
|
|
using System.Threading.Tasks.Dataflow; |
|
|
|
internal class StringsProcessor |
|
|
|
internal class StringsProcessor |
|
|
|
{ |
|
|
|
{ |
|
|
|
public StringsProcessor(string sourceString, int maxWordsCount, IEnumerable<string> words) |
|
|
|
public StringsProcessor(string sourceString, int maxWordsCount, IEnumerable<string> words) |
|
|
@ -28,24 +28,26 @@ |
|
|
|
this.VectorsConverter.GetString); |
|
|
|
this.VectorsConverter.GetString); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private VectorsConverter VectorsConverter { get; } |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private VectorsProcessor VectorsProcessor { get; } |
|
|
|
private VectorsProcessor VectorsProcessor { get; } |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private VectorsConverter VectorsConverter { get; } |
|
|
|
|
|
|
|
|
|
|
|
private Dictionary<Vector<byte>, string[]> VectorsToWords { get; } |
|
|
|
private Dictionary<Vector<byte>, string[]> VectorsToWords { get; } |
|
|
|
|
|
|
|
|
|
|
|
public IEnumerable<string> GeneratePhrases() |
|
|
|
public void PostUnorderedSequences(ITargetBlock<Vector<byte>[]> target) => this.VectorsProcessor.PostUnorderedSequences(target); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public IPropagatorBlock<Vector<byte>[], string> CreateUnorderedSequencesToPhrasesTransform() |
|
|
|
{ |
|
|
|
{ |
|
|
|
// task of finding anagrams could be reduced to the task of finding sequences of dictionary vectors with the target sum |
|
|
|
var unorderedSequencesToOrderedSequences = this.VectorsProcessor.CreateUnorderedSequencesToOrderedSequencesTransform(); |
|
|
|
var sums = this.VectorsProcessor.GenerateSequences(); |
|
|
|
var orderedSequencesToWordVariants = this.CreateOrderedSequencesToWordVariantsTransform(); |
|
|
|
|
|
|
|
var wordVariantsToFlatWords = this.CreateWordVariantsToFlatWordsTransform(); |
|
|
|
|
|
|
|
var flatWordsToPhrases = this.CreateFlatWordsToPhrasesTransform(); |
|
|
|
|
|
|
|
|
|
|
|
// converting sequences of vectors to the sequences of words... |
|
|
|
unorderedSequencesToOrderedSequences.LinkForever(orderedSequencesToWordVariants); |
|
|
|
var anagramsWords = sums |
|
|
|
orderedSequencesToWordVariants.LinkForever(wordVariantsToFlatWords); |
|
|
|
.Select(sum => ImmutableStack.Create(sum.Select(vector => this.VectorsToWords[vector]).ToArray())) |
|
|
|
wordVariantsToFlatWords.LinkForever(flatWordsToPhrases); |
|
|
|
.SelectMany(this.Flatten) |
|
|
|
|
|
|
|
.Select(stack => stack.ToArray()); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return anagramsWords.Select(list => string.Join(" ", list)); |
|
|
|
return DataflowBlock.Encapsulate(unorderedSequencesToOrderedSequences, flatWordsToPhrases); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Converts e.g. pair of variants [[a, b, c], [d, e]] into all possible pairs: [[a, d], [a, e], [b, d], [b, e], [c, d], [c, e]] |
|
|
|
// Converts e.g. pair of variants [[a, b, c], [d, e]] into all possible pairs: [[a, d], [a, e], [b, d], [b, e], [c, d], [c, e]] |
|
|
@ -60,5 +62,29 @@ |
|
|
|
var newStack = phrase.Pop(out wordVariants); |
|
|
|
var newStack = phrase.Pop(out wordVariants); |
|
|
|
return this.Flatten(newStack).SelectMany(remainder => wordVariants.Select(word => remainder.Push(word))); |
|
|
|
return this.Flatten(newStack).SelectMany(remainder => wordVariants.Select(word => remainder.Push(word))); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private IPropagatorBlock<Vector<byte>[], ImmutableStack<string[]>> CreateOrderedSequencesToWordVariantsTransform() |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
return new TransformBlock<Vector<byte>[], ImmutableStack<string[]>>(sum => |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
return ImmutableStack.CreateRange(sum.Select(vector => this.VectorsToWords[vector])); |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private IPropagatorBlock<ImmutableStack<string[]>, ImmutableStack<string>> CreateWordVariantsToFlatWordsTransform() |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
return new TransformManyBlock<ImmutableStack<string[]>, ImmutableStack<string>>(wordVariants => |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
return this.Flatten(wordVariants); |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private IPropagatorBlock<ImmutableStack<string>, string> CreateFlatWordsToPhrasesTransform() |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
return new TransformBlock<ImmutableStack<string>, string>(words => |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
return string.Join(" ", words); |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|