Skip to content

Commit

Permalink
+ Uçbirim uygulaması eklendi.
Browse files Browse the repository at this point in the history
  • Loading branch information
anezih committed Jul 13, 2024
1 parent 8b09c3c commit 8b38911
Show file tree
Hide file tree
Showing 13 changed files with 244 additions and 42 deletions.
2 changes: 2 additions & 0 deletions FonoSozlukNet/FonoBlazor/FonoBlazor.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk.BlazorWebAssembly">

<PropertyGroup>
<Authors>/~https://github.com/anezih</Authors>
<Version>1.0.0</Version>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
Expand Down
55 changes: 47 additions & 8 deletions FonoSozlukNet/FonoConverter/Converter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,40 @@ public async Task<MemoryStream> ToStardict()
return stardictMs;
}

public async Task ToStardict(string outFolder)
public void ToStardict(string outFolder)
{
List<OutputEntry> outputEntries = new(fonoFormat.Length);
if (wordForms != null)
{
foreach (var item in fonoFormat)
{
var wf = wordForms.GetWordForms(item.Headword, NoPFX:true, NoCross:true).SFX;
outputEntries.Add(
new OutputEntry(item.Headword, item.Definition, wf)
);
}
}
else
{
foreach (var item in fonoFormat)
{
outputEntries.Add(
new OutputEntry(item.Headword, item.Definition)
);
}
}
var stardictMs = StarDictNet.StarDictNet.Write(
entries:outputEntries,
fileName:dictInfo.FileName,
title:dictInfo.Title,
author:dictInfo.Author,
description:dictInfo.Description
);

string outPath = Path.Combine(outFolder, $"{dictInfo.FileName}.zip");
await using (var fs = new FileStream(outPath, FileMode.Create))
using (var fs = new FileStream(outPath, FileMode.Create))
{
var stardict = await ToStardict();
await stardict.CopyToAsync(fs);
stardictMs.CopyTo(fs);
}
}

Expand All @@ -84,13 +111,25 @@ public async Task<MemoryStream> ToTsv()
return ms;
}

public async Task ToTsv(string outFolder)
public void ToTsv(string outFolder)
{
MemoryStream ms = new();
UTF8Encoding utf8NoBom = new(false);
if (fonoFormat != null && fonoFormat.Length > 0)
{
if (!string.IsNullOrEmpty(fonoFormat.Abbreviations))
ms.Write(utf8NoBom.GetBytes($"Abbreviations\t{fonoFormat.Abbreviations}\n"));
foreach (var entry in fonoFormat)
{
ms.Write(utf8NoBom.GetBytes($"{entry.Headword}\t{entry.Definition}\n"));
}
ms.Seek(0, SeekOrigin.Begin);
}

string outPathTsv = Path.Combine(outFolder, $"{dictInfo.FileName}.tsv");
await using (var fs = new FileStream(outPathTsv, FileMode.Create))
using (var fs = new FileStream(outPathTsv, FileMode.Create))
{
var tsv = await ToTsv();
await tsv.CopyToAsync(fs);
ms.CopyTo(fs);
}
}
}
2 changes: 2 additions & 0 deletions FonoSozlukNet/FonoConverter/FonoConverter.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
</ItemGroup>

<PropertyGroup>
<Authors>/~https://github.com/anezih</Authors>
<Version>1.0.0</Version>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
Expand Down
2 changes: 1 addition & 1 deletion FonoSozlukNet/FonoFileFormats/BaseFonoFormat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,5 @@ protected async Task<string> Rtf2HtmlAsync(string rtf)
return html;
}

protected abstract void ReadEntries();
protected abstract void ReadEntries(Progress progress);
}
2 changes: 2 additions & 0 deletions FonoSozlukNet/FonoFileFormats/FonoFileFormats.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<Authors>/~https://github.com/anezih</Authors>
<Version>1.0.0</Version>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
Expand Down
12 changes: 6 additions & 6 deletions FonoSozlukNet/FonoFileFormats/FonoFormat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,22 @@ public static FormatType GuessTypeFromFileExtension(string filename)
return FormatType.UNKNOWN;
}

public static BaseFonoFormat? GetFonoFormat(string path, FormatType fileFormat)
public static BaseFonoFormat? GetFonoFormat(string path, Progress progress, FormatType fileFormat)
{
return fileFormat switch
{
FormatType.KDD => new KddReader(path),
FormatType.XML => new FonoXmlReader(path),
FormatType.KDD => new KddReader(path, progress),
FormatType.XML => new FonoXmlReader(path, progress),
_ => null,
};
}

public static BaseFonoFormat? GetFonoFormat(Stream stream, FormatType fileFormat)
public static BaseFonoFormat? GetFonoFormat(Stream stream, Progress progress, FormatType fileFormat)
{
return fileFormat switch
{
FormatType.KDD => new KddReader(stream),
FormatType.XML => new FonoXmlReader(stream),
FormatType.KDD => new KddReader(stream, progress),
FormatType.XML => new FonoXmlReader(stream, progress),
_ => null,
};
}
Expand Down
10 changes: 5 additions & 5 deletions FonoSozlukNet/FonoFileFormats/FonoXmlReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,23 @@ public class FonoXmlReader : BaseFonoFormat
{
private XmlDocument fonoXml = new XmlDocument();

public FonoXmlReader(string path)
public FonoXmlReader(string path, Progress progress)
{
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
var streamReader = new StreamReader(path, Encoding.GetEncoding(1254));
fonoXml.Load(streamReader);
ReadEntries();
ReadEntries(progress);
}

public FonoXmlReader(Stream stream)
public FonoXmlReader(Stream stream, Progress progress)
{
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
var streamReader = new StreamReader(stream, Encoding.GetEncoding(1254));
fonoXml.Load(streamReader);
ReadEntries();
ReadEntries(progress);
}

protected override void ReadEntries()
protected override void ReadEntries(Progress progress)
{
var kelimeler = fonoXml.GetElementsByTagName("KELIME");
if (kelimeler.Count == 0)
Expand Down
2 changes: 1 addition & 1 deletion FonoSozlukNet/FonoFileFormats/FonoXmlReaderAsync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,5 @@ protected async Task ReadEntriesAsync(Progress progress)
}
}

protected override void ReadEntries(){}
protected override void ReadEntries(Progress progress){}
}
17 changes: 9 additions & 8 deletions FonoSozlukNet/FonoFileFormats/KddReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,19 @@ public class KddReader : BaseFonoFormat
private static readonly byte[] Utf16Newline = [0x0A, 0x00];
private static readonly byte[] ZlibLastThreeBytes = [0x00, 0x78, 0xDA];

public KddReader(string path)
public KddReader(string path, Progress progress)
{
kddFileStream = File.Open(path, FileMode.Open);
kddStream = new BinaryReader(kddFileStream);
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
ReadEntries();
ReadEntries(progress);
}

public KddReader(Stream stream)
public KddReader(Stream stream, Progress progress)
{
kddStream = new BinaryReader(stream);
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
ReadEntries();
ReadEntries(progress);
}
~KddReader()
{
Expand Down Expand Up @@ -94,7 +94,7 @@ private string ReadZlib(BinaryReader binaryReader, int length, int skipFromBegin
return result;
}

protected override void ReadEntries()
protected override void ReadEntries(Progress progress)
{
if (!CheckHeader() | !kddStream.BaseStream.CanSeek)
return;
Expand All @@ -110,6 +110,7 @@ protected override void ReadEntries()
totalWords += length;
kddStream.BaseStream.Seek(6, SeekOrigin.Current);
}
progress.Total = totalWords;
entries = new List<Entry>(totalWords);
kddStream.BaseStream.Seek(headwordsOffset, SeekOrigin.Begin);
List<byte> headword = new();
Expand All @@ -128,7 +129,7 @@ protected override void ReadEntries()
var hw = HandleHeadword(Encoding.Unicode.GetString(headword.Skip(skip).ToArray()));
entries.Add(new Entry());
entries[headwordCnt].Headword = hw;
headwordCnt += 1;
headwordCnt++;
headword.Clear();
}
else
Expand Down Expand Up @@ -160,9 +161,9 @@ protected override void ReadEntries()
kddStream.BaseStream.Seek(2, SeekOrigin.Current);
var definition = Rtf2Html(ReadZlib(kddStream, definitionLength, 4));
entries[definitionCnt].Definition = definition;
definitionCnt += 1;
definitionCnt++;
progress.Step = definitionCnt;
kddStream.BaseStream.Seek(4, SeekOrigin.Current);
Console.WriteLine(definitionCnt);
}
}
}
2 changes: 1 addition & 1 deletion FonoSozlukNet/FonoFileFormats/KddReaderAsync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,5 +158,5 @@ protected async Task ReadEntriesAsync(Progress progress)
}
}

protected override void ReadEntries(){}
protected override void ReadEntries(Progress progress){}
}
11 changes: 11 additions & 0 deletions FonoSozlukNet/FonoSozlukCli/FonoSozlukCli.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,24 @@

<ItemGroup>
<ProjectReference Include="..\FonoFileFormats\FonoFileFormats.csproj" />
<ProjectReference Include="..\FonoConverter\FonoConverter.csproj" />
<ProjectReference Include="..\..\HunspellWordForms\src\HunspellWordForms.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="CommandLineParser" Version="2.9.1" />
</ItemGroup>

<PropertyGroup>
<Authors>/~https://github.com/anezih</Authors>
<Version>1.0.0</Version>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<PublishSingleFile>true</PublishSingleFile>
<SelfContained>true</SelfContained>
<RuntimeIdentifiers>win-x64;win-arm64;linux-arm64;linux-x64;osx-x64;osx-arm64</RuntimeIdentifiers>
</PropertyGroup>

</Project>
Loading

0 comments on commit 8b38911

Please sign in to comment.