Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve Table Output #27

Merged
merged 2 commits into from
Feb 27, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -59,25 +59,54 @@ internal override void Execute()
var source = DeContext.Instance;
var entries = source.Entries.ToArray();
var nouns = GetNouns(entries);
var importances = source.Importances.Where(item => item.WrittenRepGuess.IsBaseWord()).ToList();
var importanceDict = importances.ToDictionary(i => i.WrittenRepGuess, i => i);
InsertNounsParameter(nouns, importanceDict);
InsertNounsParameter(nouns);
UpdateNounDBAsync(nouns);
}

internal IList<Noun> GetNouns(IEnumerable<Entry> entries)
internal Dictionary<string, Importance> GetImportanceDictionary()
{
var importances = DeContext.Instance.Importances.ToArray();
var validImportances = importances.Where(item => item.WrittenRepGuess.IsBaseWord()).ToList();
var importanceDict = validImportances.ToDictionary(i => i.WrittenRepGuess, i => i);
return importanceDict;
}

internal List<Noun> GetNouns(IEnumerable<Entry> entries)
{
var nouns = new HashSet<Noun>();
foreach (var entry in entries)
{
var noun = EntryToNoun(entry);
if (noun != null && !nouns.Contains(noun))
if (noun != null)
{
if (nouns.Contains(noun))
{
nouns.Remove(noun);
}
else
{
nouns.Add(noun);
}
}
}

return nouns.ToList();
}

internal Dictionary<string, Translation> GetTranslationDictionary()
{
var translations = DeEnContext.Instance.Translations.ToArray();
var validTranslations = translations.Where(item => item.WrittenRep.IsBaseWord()).ToList();
var translationsDict = new Dictionary<string, Translation>(validTranslations.Count);
foreach (var translation in validTranslations)
{
if (!translationsDict.ContainsKey(translation.WrittenRep))
{
nouns.Add(noun);
translationsDict[translation.WrittenRep] = translation;
}
}

return nouns.ToArray();
return translationsDict;
}

private static async void UpdateNounDBAsync(IList<Noun> nounsSource)
Expand All @@ -97,14 +126,45 @@ private static async void UpdateNounDBAsync(IList<Noun> nounsSource)
await dictionaryDB.SaveChangesAsync().ConfigureAwait(false);
}

private void InsertNounsParameter(IList<Noun> nouns, Dictionary<string, Importance> importanceDict)
private void InsertNounsParameter(IList<Noun> nouns)
{
var importanceDict = GetImportanceDictionary();
var translationDict = GetTranslationDictionary();
var invalidNouns = new List<Noun>();
foreach (var noun in nouns)
{
var remove = false;
if (importanceDict.TryGetValue(noun.Word, out var importance) && importance.Score != null)
{
noun.Importance = importance.Score.Value;
}

if (translationDict.TryGetValue(noun.Word, out var translation))
{
noun.Translation = translation.TransList;
noun.Sense = translation.Sense;
if (string.IsNullOrEmpty(noun.Translation))
{
remove = true;
}
}
else
{
remove = true;
}

if (remove)
{
invalidNouns.Add(noun);
}
}

foreach (var invalidNoun in invalidNouns)
{
if (nouns.Contains(invalidNoun))
{
nouns.Remove(invalidNoun);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,35 @@ public DictionaryContext(DbContextOptions<DictionaryContext> options)

#region Properties

public static DictionaryContext Instance { get; } = new DictionaryContext();
public static DictionaryContext Instance { get; private set; }

public virtual DbSet<Noun> Nouns { get; set; }

internal static string DBName { get; } = $"NounsDict.sqlite3";
internal static string DBPath { get; private set; }

#endregion Properties

#region Methods

internal static void CreateInstance(string directory)
{
if (!Directory.Exists(directory))
{
throw new DirectoryNotFoundException($"Folder {directory} not found");
}

if (Instance != null)
{
return;
}

DBPath = Path.Combine(directory, "NounsDict.sqlite3");
Instance = new DictionaryContext();
}

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
#warning To protect potentially sensitive information in your connection string, you should move it out of source code. You can avoid scaffolding the connection string by using the Name= syntax to read it from configuration - see https://go.microsoft.com/fwlink/?linkid=2131148. For more guidance on storing connection strings, see https://go.microsoft.com/fwlink/?LinkId=723263.
=> optionsBuilder.UseSqlite($"Data Source=NounsDict.sqlite3");
=> optionsBuilder.UseSqlite($"Data Source={DBPath}");

#endregion Methods
}
Expand Down
25 changes: 23 additions & 2 deletions Projects/DerDieDasAI/Src/DerDieDasAICore/Database/Models/Noun.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace DerDieDasAICore.Database.Models
using System.ComponentModel;
using System.ComponentModel.DataAnnotations.Schema;

public class Noun : INotifyPropertyChanged
public class Noun : INotifyPropertyChanged, IEquatable<Noun>
{
#region Events

Expand All @@ -24,13 +24,19 @@ public class Noun : INotifyPropertyChanged
[Column(Order = 0)]
public int Id { get; set; }

[Column(Order = 3)]
public double Importance { get; set; }

[Column(Order = 7)]
public string Information { get; set; }

[Column(Order = 3)]
[Column(Order = 4)]
public string Pronounce { get; set; }

[Column(Order = 6)]
public string Sense { get; set; }

[Column(Order = 5)]
public string Translation { get; set; }

[Column(Order = 1)]
Expand All @@ -40,6 +46,21 @@ public class Noun : INotifyPropertyChanged

#region Methods

public bool Equals(Noun other)
{
if (this is null || other is null)
{
return false;
}

return this.Word == other.Word;
}

public override int GetHashCode()
{
return this.Word.GetHashCode();
}

public override string ToString()
{
var message = $"{this.Word}:Gender:{this.Gender};Pronounce:{this.Pronounce}";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public DeContext(DbContextOptions<DeContext> options)

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
#warning To protect potentially sensitive information in your connection string, you should move it out of source code. You can avoid scaffolding the connection string by using the Name= syntax to read it from configuration - see https://go.microsoft.com/fwlink/?linkid=2131148. For more guidance on storing connection strings, see https://go.microsoft.com/fwlink/?LinkId=723263.
=> optionsBuilder.UseSqlite("Data Source=D:\\Local\\Projects\\GitHub\\Playground\\Projects\\DerDieDasAI\\Data\\de.sqlite3");
=> optionsBuilder.UseSqlite("Data Source=C:\\Temp\\de.sqlite3");

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// ========================================== //
// Developer: Yohanes Wahyu Nurcahyo //
// Website: https://github.com/yoyokits //
// ========================================== //

using Microsoft.EntityFrameworkCore;

namespace DerDieDasAICore.Database.Models.Source;

public partial class DeEnContext : DbContext
{
#region Constructors

public DeEnContext()
{
}

public DeEnContext(DbContextOptions<DeEnContext> options)
: base(options)
{
}

#endregion Constructors

#region Properties

public static DeEnContext Instance { get; } = new();

public virtual DbSet<TranslationGrouped> TranslationGroupeds { get; set; }

public virtual DbSet<Translation> Translations { get; set; }

#endregion Properties

#region Methods

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
#warning To protect potentially sensitive information in your connection string, you should move it out of source code. You can avoid scaffolding the connection string by using the Name= syntax to read it from configuration - see https://go.microsoft.com/fwlink/?linkid=2131148. For more guidance on storing connection strings, see https://go.microsoft.com/fwlink/?LinkId=723263.
=> optionsBuilder.UseSqlite("DataSource=C:\\Temp\\de-en.sqlite3");

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Translation>(entity =>
{
entity
.HasNoKey()
.ToTable("translation");

entity.Property(e => e.IsGood).HasColumnName("is_good");
entity.Property(e => e.Lexentry).HasColumnName("lexentry");
entity.Property(e => e.Score).HasColumnName("score");
entity.Property(e => e.Sense).HasColumnName("sense");
entity.Property(e => e.SenseNum).HasColumnName("sense_num");
entity.Property(e => e.TransList).HasColumnName("trans_list");
entity.Property(e => e.WrittenRep).HasColumnName("written_rep");
});

modelBuilder.Entity<TranslationGrouped>(entity =>
{
entity
.HasNoKey()
.ToView("translation_grouped");

entity.Property(e => e.Lexentry).HasColumnName("lexentry");
entity.Property(e => e.MinSenseNum).HasColumnName("min_sense_num");
entity.Property(e => e.Score).HasColumnName("score");
entity.Property(e => e.SenseList).HasColumnName("sense_list");
entity.Property(e => e.TransList).HasColumnName("trans_list");
entity.Property(e => e.WrittenRep).HasColumnName("written_rep");
});

////OnModelCreatingPartial(modelBuilder);
}

////partial void OnModelCreatingPartial(ModelBuilder modelBuilder);

#endregion Methods
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,29 @@
using System;
using System.Collections.Generic;
// ========================================== //
// Developer: Yohanes Wahyu Nurcahyo //
// Website: https://github.com/yoyokits //
// ========================================== //

namespace DerDieDasAICore.Database.Models.Source;

public partial class Importance
{
public string Vocable { get; set; }
#region Properties

public double? Score { get; set; }

public string Vocable { get; set; }

public string WrittenRepGuess { get; set; }
}

#endregion Properties

#region Methods

public override string ToString()
{
var message = $"{WrittenRepGuess}:Importance:{Score:0.##}";
return message;
}

#endregion Methods
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// ========================================== //
// Developer: Yohanes Wahyu Nurcahyo //
// Website: https://github.com/yoyokits //
// ========================================== //

namespace DerDieDasAICore.Database.Models.Source;

public partial class Translation
{
#region Properties

public int? IsGood { get; set; }

public string Lexentry { get; set; }

public int? Score { get; set; }

public string Sense { get; set; }

public string SenseNum { get; set; }

public string TransList { get; set; }

public string WrittenRep { get; set; }

#endregion Properties
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// ========================================== //
// Developer: Yohanes Wahyu Nurcahyo //
// Website: https://github.com/yoyokits //
// ========================================== //

namespace DerDieDasAICore.Database.Models.Source;

public partial class TranslationGrouped
{
#region Properties

public string Lexentry { get; set; }

public string MinSenseNum { get; set; }

public double? Score { get; set; }

public string SenseList { get; set; }

public byte[] TransList { get; set; }

public string WrittenRep { get; set; }

#endregion Properties
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
</ItemGroup>

<ItemGroup>
<None Update="Database\Models\Source\de-en.sqlite3">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Database\Models\Source\de.sqlite3">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
Expand Down
2 changes: 1 addition & 1 deletion Projects/DerDieDasAI/Src/DerDieDasAICore/Helpers/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public static class Parser

public static bool IsBaseWord(this string word)
{
return !string.IsNullOrEmpty(word) && !word.Contains('-') && !word.Contains(' ');
return !string.IsNullOrEmpty(word) && !word.Contains('-') && !word.Contains('*') && !word.Contains(' ');
}

#endregion Methods
Expand Down
Loading