static IEnumerable<string> GetFileNames(string path)
{
foreach (var fileName in Directory.EnumerateFiles(path, "*.txt"))
{
yield return fileName;
}
}
static IEnumerable<string> LoadLines(IEnumerable<string> fileNames)
{
foreach (var fileName in fileNames)
{
using (FileStream stream = File.OpenRead(fileName))
{
var reader = new StreamReader(stream,Encoding.Default);
string line = null;
while ((line = reader.ReadLine()) != null) yield return line;
}
}
}
static IEnumerable<string> GetWords(IEnumerable<string> lines)
{
foreach (var line in lines)
{
string[] words = line.Split(' ', ';', '.', ',');
foreach (var word in words)
{
if (!string.IsNullOrEmpty(word)) yield return word;
}
}
}
static ITargetBlock<string> SetupPipeline()
{
var fileNamesForPath = new TransformBlock<string, IEnumerable<string>>(path => GetFileNames(path));
var lines = new TransformBlock<IEnumerable<string>, IEnumerable<string>>(fileNames => LoadLines(fileNames));
var words = new TransformBlock<IEnumerable<string>, IEnumerable<string>>(lines2 => GetWords(lines2));
var display = new ActionBlock<IEnumerable<string>>(coll =>
{
foreach (var s in coll) Console.WriteLine(s);
});
fileNamesForPath.LinkTo(lines);
lines.LinkTo(words);
words.LinkTo(display);
return fileNamesForPath;
}
//调用
var target = SetupPipeline();
target.Post(@"C:\Users\chenss\Desktop");
//案例二
var downloadString = new TransformBlock<string, string>(uri =>
{
string strResult = string.Empty;
using (var reader = new StreamReader(uri, Encoding.Default))
{
while (reader.ReadLineAsync().Result != null)
{
strResult += reader.ReadLineAsync().Result + " ";
}
}
Console.WriteLine($"下载 {strResult} ...");
return strResult;
});
var createWordList = new TransformBlock<string, string[]>(text =>
{
Console.WriteLine("创建单词列表 ...");
char[] tokens = text.Select(c => char.IsLetter(c) ? c : ' ').ToArray();
text = new string(tokens);
return text.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
});
var filterWordList = new TransformBlock<string[], string[]>(words =>
{
Console.WriteLine("筛选单词列表 ...");
return words.Where(word => word.Length > 3).Distinct().ToArray();
});
var findReversedWords = new TransformManyBlock<string[], string>(words =>
{
Console.WriteLine("查找颠倒的单词 ...");
var wordsSet = new HashSet<string>(words);
var query = from word in words.AsParallel() select word;
return query;
});
var printReversedWords = new ActionBlock<string>(reversedWord =>
{
Console.WriteLine("Found reversed words {0}/{1}", reversedWord, new string(reversedWord.Reverse().ToArray()));
});
//var linkOptions = new DataflowLinkOptions { PropagateCompletion = true };
downloadString.LinkTo(createWordList);
createWordList.LinkTo(filterWordList);
filterWordList.LinkTo(findReversedWords);
findReversedWords.LinkTo(printReversedWords);
downloadString.Post(@"C:\Users\chenss\Desktop\Test.txt");
downloadString.Complete();
printReversedWords.Completion.Wait();