example 7

This commit is contained in:
Alexander Zhirov 2021-11-09 23:43:59 +03:00
parent 3f11e9e2df
commit 97bb5df912
4 changed files with 4555 additions and 0 deletions

15
src/example_7/.gitignore vendored Normal file
View File

@ -0,0 +1,15 @@
.dub
docs.json
__dummy.html
docs/
/example_7
example_7.so
example_7.dylib
example_7.dll
example_7.a
example_7.lib
example_7-test-*
*.exe
*.o
*.obj
*.lst

9
src/example_7/dub.json Normal file
View File

@ -0,0 +1,9 @@
{
"authors": [
"alexander"
],
"description": "Основные структуры данных.",
"license": "proprietary",
"name": "example_7",
"targetPath": "bin"
}

View File

@ -0,0 +1,68 @@
import std.algorithm, std.regex, std.range, std.stdio, std.string, std.ascii;
struct PersonaData
{
ulong totalWordsSpoken;
ulong[string] wordCount;
}
void addParagraph(string line, ref PersonaData[string] info)
{
line = strip(line);
auto sentence = line.find(". ");
if (sentence.empty)
{
return;
}
auto persona = line[0 .. $ - sentence.length];
sentence = toLower(strip(sentence[2 .. $]));
auto words = split(sentence, regex("[ \t,.;:?]+"));
auto data = persona in info;
if (data)
{
data.totalWordsSpoken += words.length;
foreach (word; words)
++data.wordCount[word];
}
else
{
PersonaData newData;
newData.totalWordsSpoken = words.length;
foreach (word; words)
newData.wordCount[word] = 1;
info[persona] = newData;
}
}
void printResults(PersonaData[string] info)
{
foreach (persona, data; info)
{
writefln("%20s %6u %6u", persona, data.totalWordsSpoken,
data.wordCount.length);
}
}
void main()
{
PersonaData[string] info;
string currentParagraph;
auto f = File("./source/hamlet.txt", "r");
foreach (line; f.byLine)
{
if (line.startsWith(" ")
&& line.length > 4
&& isAlpha(line[4]))
{
currentParagraph ~= line[3 .. $];
}
else if (line.startsWith(" ")
&& line.length > 2
&& isAlpha(line[2]))
{
addParagraph(currentParagraph, info);
currentParagraph = line[2 .. $].idup;
}
}
printResults(info);
}

File diff suppressed because it is too large Load Diff