Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions build/vs19/freq01.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>x64</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>..\..\bin\</OutputPath>
<DefineConstants>
</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<AllowUnsafeBlocks>false</AllowUnsafeBlocks>
<Prefer32Bit>false</Prefer32Bit>
<BaseOutputPath>$(SolutionDir)\junk\vs19\$(Configuration)\$(ProjectName)\bin\</BaseOutputPath>
<BaseIntermediateOutputPath>$(SolutionDir)\junk\vs19\$(Configuration)\$(ProjectName)\obj\</BaseIntermediateOutputPath>
<IntermediateOutputPath>$(SolutionDir)\junk\vs19\$(Configuration)\$(ProjectName)\tmp\</IntermediateOutputPath>
</PropertyGroup>
<PropertyGroup>
<ProjectGuid>{B61A5E2F-D44A-4ED0-B058-208149DCFC79}</ProjectGuid>
<TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
<TargetFrameworkProfile />
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="..\..\src\freq01.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
38 changes: 38 additions & 0 deletions build/vs19/freq02.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>x64</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>..\..\bin\</OutputPath>
<DefineConstants>
</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<AllowUnsafeBlocks>false</AllowUnsafeBlocks>
<Prefer32Bit>false</Prefer32Bit>
<BaseOutputPath>$(SolutionDir)\junk\vs19\$(Configuration)\$(ProjectName)\bin\</BaseOutputPath>
<BaseIntermediateOutputPath>$(SolutionDir)\junk\vs19\$(Configuration)\$(ProjectName)\obj\</BaseIntermediateOutputPath>
<IntermediateOutputPath>$(SolutionDir)\junk\vs19\$(Configuration)\$(ProjectName)\tmp\</IntermediateOutputPath>
</PropertyGroup>
<PropertyGroup>
<ProjectGuid>{B61A5E2F-D44A-4ED0-B058-208149DCFC79}</ProjectGuid>
<TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
<TargetFrameworkProfile />
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="..\..\src\freq02.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
38 changes: 38 additions & 0 deletions build/vs19/freq05.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>x64</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>..\..\bin\</OutputPath>
<DefineConstants>
</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<AllowUnsafeBlocks>false</AllowUnsafeBlocks>
<Prefer32Bit>false</Prefer32Bit>
<BaseOutputPath>$(SolutionDir)\junk\vs19\$(Configuration)\$(ProjectName)\bin\</BaseOutputPath>
<BaseIntermediateOutputPath>$(SolutionDir)\junk\vs19\$(Configuration)\$(ProjectName)\obj\</BaseIntermediateOutputPath>
<IntermediateOutputPath>$(SolutionDir)\junk\vs19\$(Configuration)\$(ProjectName)\tmp\</IntermediateOutputPath>
</PropertyGroup>
<PropertyGroup>
<ProjectGuid>{B61A5E2F-D44A-4ED0-B058-208149DCFC79}</ProjectGuid>
<TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
<TargetFrameworkProfile />
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="..\..\src\freq05.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
38 changes: 38 additions & 0 deletions build/vs19/freq06.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>x64</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>..\..\bin\</OutputPath>
<DefineConstants>
</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<AllowUnsafeBlocks>false</AllowUnsafeBlocks>
<Prefer32Bit>false</Prefer32Bit>
<BaseOutputPath>$(SolutionDir)\junk\vs19\$(Configuration)\$(ProjectName)\bin\</BaseOutputPath>
<BaseIntermediateOutputPath>$(SolutionDir)\junk\vs19\$(Configuration)\$(ProjectName)\obj\</BaseIntermediateOutputPath>
<IntermediateOutputPath>$(SolutionDir)\junk\vs19\$(Configuration)\$(ProjectName)\tmp\</IntermediateOutputPath>
</PropertyGroup>
<PropertyGroup>
<ProjectGuid>{B61A5E2F-D44A-4ED0-B058-208149DCFC79}</ProjectGuid>
<TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
<TargetFrameworkProfile />
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="..\..\src\freq06.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
59 changes: 59 additions & 0 deletions src/freq01.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace freq01
{
class freq01
{
private static readonly Dictionary<string, int> dict = new Dictionary<string, int>();

private static void AddWord(StringBuilder word)
{
var strWord = word.ToString().ToLowerInvariant();

if (dict.ContainsKey(strWord))
dict[strWord]++;
else
dict[strWord] = 1;
}

static void Main(string[] args)
{
if (args.Length != 2)
{
Console.WriteLine("Usage: freq <input_file> <output_file>");
return;
}

using (StreamReader sr = new StreamReader(args[0]))
{
var word = new StringBuilder();
while (sr.Peek() >= 0)
{
var ch = (char)sr.Read();
if (Char.IsLetter(ch))
{
word.Append(ch);
continue;
}

if (word.Length == 0)
continue;

AddWord(word);
word = new StringBuilder();
}

if (word.Length > 0)
AddWord(word);
}

File.WriteAllLines(args[1], dict.OrderByDescending(kvp => kvp.Value)
.ThenBy(kvp => kvp.Key)
.Select(kvp => $"{kvp.Value} {kvp.Key}"));
}
}
}
107 changes: 107 additions & 0 deletions src/freq02.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;

namespace freq02
{
class trie
{
public node Root = new node() { Word = "" };

public IEnumerable<node> Enumerate(node root)
{
for (int i = 0; i < 26; i++)
{
var child = root.Children[i];
if (child != null)
{
if (child.Count > 0)
yield return root.Children[i];

foreach (var child_node in Enumerate(child))
{
yield return child_node;
}
}
}
}

public class node
{
public node[] Children = new node[26];
public int Count;
public string Word;

public node WeNeedToGoDeeper(int index)
{
return Children[index] ?? (Children[index] = new node() {Word = Word + (char) (index + 97)});
}
}
}

class freq02
{
private static readonly trie _trie = new trie();
private static void Count(string filename)
{
var text = File.ReadAllBytes(filename);

trie.node node = _trie.Root;
foreach (var ch in text)
{
if (ch >= 'a' && ch <= 'z')
{
var index = ch - 97;
node = node.WeNeedToGoDeeper(index);
continue;
}

if (ch >= 'A' && ch <= 'Z')
{
var index = ch - 65;
node = node.WeNeedToGoDeeper(index);
continue;
}

if (node == _trie.Root)
continue;

node.Count++;
node = _trie.Root;
}

if (node != _trie.Root)
{
node.Count++;
}
}

private static void SortAndDump(string filename)
{
var enumerable = _trie.Enumerate(_trie.Root);
var sorted = enumerable.OrderByDescending(n => n.Count); //.ThenBy(n => n.Word); words are already sorted during trie traversal
File.WriteAllLines(filename, sorted.Select(n => $"{n.Count} {n.Word}"));
}

static void Main(string[] args)
{
if (args.Length != 2)
{
Console.WriteLine("Usage: freq <input_file> <output_file>");
return;
}

var stopwatch = new Stopwatch();
stopwatch.Start();

Count(args[0]);
SortAndDump(args[1]);

stopwatch.Stop();
Console.WriteLine($"Elapsed time: {(decimal) stopwatch.ElapsedMilliseconds / 1000}s");
}
}
}
Loading