From 559875f63fc0a384ce44ccc01a6280d9e1c7e482 Mon Sep 17 00:00:00 2001 From: Mike Phares Date: Thu, 30 Mar 2023 01:31:50 -0700 Subject: [PATCH] Person Sex --- Instance/DlibDotNet.cs | 23 ++--- Shared/Models/Stateless/Methods/Person.cs | 98 ++++++++++++++----- .../Stateless/Methods/PersonBirthday.cs | 5 +- 3 files changed, 86 insertions(+), 40 deletions(-) diff --git a/Instance/DlibDotNet.cs b/Instance/DlibDotNet.cs index 2170311..49f7f61 100644 --- a/Instance/DlibDotNet.cs +++ b/Instance/DlibDotNet.cs @@ -115,6 +115,7 @@ public partial class DlibDotNet if (rootResultsDirectory is null) throw new Exception(); Storage storage = new(rootDirectory, rootResultsDirectory, peopleRootDirectory); + _ = Shared.Models.Stateless.Methods.IPath.DeleteEmptyDirectories(Path.Combine(peopleRootDirectory, "{}")); _PersonContainers = Shared.Models.Stateless.Methods.IPersonContainer.GetPersonContainers(storage, configuration.PersonBirthdayFormat, configuration.PersonCharacters.ToArray(), _Faces.FileNameExtension); } { @@ -1046,11 +1047,10 @@ public partial class DlibDotNet private void CreateTree(PersonContainer[] personContainers, long ticks, string a2PeopleContentDirectory, Dictionary> personKeyToIds) { string by; - string directory; string[] matches; - string[] segments; const int zero = 0; string[] pGedFiles; + string[] pGedLines; string personKeyFormatted; List distinct = new(); PersonBirthday personBirthday; @@ -1089,15 +1089,16 @@ public partial class DlibDotNet pGedFiles = (from l in matches where l.EndsWith(".pged") select l).ToArray(); if (!pGedFiles.Any()) continue; - lines.AddRange(File.ReadAllLines(pGedFiles[0])); - segments = personContainer.DisplayDirectoryName.Split(_Configuration.PersonCharacters.ToArray()); - if (segments.Length < 2) - directory = Path.Combine(rootDirectory, $"000 {personKeyFormatted} {personContainer.DisplayDirectoryName}"); - else - directory = Path.Combine(rootDirectory, $"{segments[1].PadLeft(3, '0')} {personKeyFormatted} {personContainer.DisplayDirectoryName}"); - if (Directory.Exists(directory)) - continue; - _ = Directory.CreateDirectory(directory); + pGedLines = File.ReadAllLines(pGedFiles[0]); + lines.AddRange(pGedLines); + // segments = personContainer.DisplayDirectoryName.Split(_Configuration.PersonCharacters.ToArray()); + // if (segments.Length < 2) + // directory = Path.Combine(rootDirectory, $"000 {personKeyFormatted} {personContainer.DisplayDirectoryName}"); + // else + // directory = Path.Combine(rootDirectory, $"{segments[1].PadLeft(3, '0')} {personKeyFormatted} {personContainer.DisplayDirectoryName}"); + // if (Directory.Exists(directory)) + // continue; + // _ = Directory.CreateDirectory(directory); } lines.Add("0 TRLR"); File.WriteAllLines(Path.Combine(a2PeopleContentDirectory, $"{ticks}.ged"), lines); diff --git a/Shared/Models/Stateless/Methods/Person.cs b/Shared/Models/Stateless/Methods/Person.cs index 5452e00..99bee7b 100644 --- a/Shared/Models/Stateless/Methods/Person.cs +++ b/Shared/Models/Stateless/Methods/Person.cs @@ -1,5 +1,3 @@ -using System.Text; - namespace View_by_Distance.Shared.Models.Stateless.Methods; internal abstract class Person @@ -17,38 +15,86 @@ internal abstract class Person return new(personBirthday, personKeyFormatted); } - private static void WriteGedFile(string? personKeyFormatted, Models.PersonBirthday personBirthday, Models.PersonName name, string[] matches) + private static void WriteGedFile(string personKeyFormatted, Models.PersonBirthday personBirthday, Models.PersonName name, string[] matches) { string[] pGedFiles = (from l in matches where l.EndsWith(".pged") select l).ToArray(); if (!pGedFiles.Any()) { - StringBuilder stringBuilder = new(); - _ = stringBuilder.Append("0 @I").Append(personKeyFormatted).AppendLine("@ INDI"); - _ = stringBuilder.Append("1 NAME ").Append(name.First.Value).Append(" /").Append(name.Last.Value).AppendLine("/"); - if (!string.IsNullOrEmpty(name.First.Value)) - _ = stringBuilder.Append("2 GIVN ").AppendLine(name.First.Value); - if (!string.IsNullOrEmpty(name.Last.Value)) - _ = stringBuilder.Append("2 SURN ").AppendLine(name.Last.Value); - if (!string.IsNullOrEmpty(name.Alias.Value)) - { - _ = stringBuilder.Append("2 NICK ").AppendLine(name.Alias.Value); - if (name.Alias.Value.Contains(" Jr")) - _ = stringBuilder.Append("2 NSFX ").AppendLine("Jr"); - else if (name.Alias.Value.Contains(" Sr")) - _ = stringBuilder.Append("2 NSFX ").AppendLine("Sr"); - } - _ = stringBuilder.AppendLine("1 SEX U"); - if (!IPersonBirthday.IsCounterPersonBirthday(personBirthday)) - { - _ = stringBuilder.AppendLine("1 BIRT"); - _ = stringBuilder.Append("2 DATE ").AppendLine(personBirthday.Value.ToString("dd MMM yyyy")); - } string? directory = Path.GetDirectoryName(matches[0]); if (directory is null) throw new Exception(); + List pGedLines = new(); + string nameLine = $"1 NAME {name.First.Value} /{name.Last.Value}/"; + pGedLines.Add($"0 @I{personKeyFormatted}@ INDI"); + pGedLines.Add(nameLine); + if (!string.IsNullOrEmpty(name.First.Value)) + pGedLines.Add($"2 GIVN {name.First.Value}"); + if (!string.IsNullOrEmpty(name.Last.Value)) + pGedLines.Add($"2 SURN {name.Last.Value}"); + if (!string.IsNullOrEmpty(name.Alias.Value)) + { + pGedLines.Add($"2 NICK {name.Alias.Value}"); + if (name.Alias.Value.Contains(" Jr")) + pGedLines.Add("2 NSFX Jr"); + else if (name.Alias.Value.Contains(" Sr")) + pGedLines.Add("2 NSFX Sr"); + } + if (personKeyFormatted[^2..] == "06") + pGedLines.Add("1 SEX F"); + else if (personKeyFormatted[^2..] == "13") + pGedLines.Add("1 SEX M"); + else + { + string? sexLine = null; + string[] sourceLines = File.ReadAllLines("D:/1) Images A/Images-9b89679-Results/A2) People/9b89679/([])/FamilyEcho/638157228251558818-RootsMagic-Export.ged"); + for (int i = 0; i < sourceLines.Length; i++) + { + if (sourceLines[i] != nameLine) + continue; + for (int j = i + 1; j < sourceLines.Length; j++) + { + i = j; + if (sourceLines[j].StartsWith("0 @I")) + break; + if (sourceLines[j].StartsWith("1 SEX ")) + { + sexLine = sourceLines[j]; + break; + } + } + if (sexLine is not null) + break; + } + if (sexLine is null) + pGedLines.Add("1 SEX U"); + else + { + pGedLines.Add($"1 SEX {sexLine[6]}"); + string sex = sexLine[6] is 'F' ? "06" : sexLine[6] is 'M' ? "13" : sexLine[6] is 'U' ? "21" : throw new NotImplementedException(); + if (directory.EndsWith("00")) + directory = string.Concat(directory[..^2], sex); + else if (directory.EndsWith("01")) + directory = string.Concat(directory[..^2], sex); + else if (directory.EndsWith("05")) // Daisy + directory = string.Concat(directory[..^2], sex); + else if (directory.EndsWith("18")) // Meghan + directory = string.Concat(directory[..^2], sex); + else if (directory.EndsWith("20")) // Joey + directory = string.Concat(directory[..^2], sex); + else + throw new NotImplementedException(); + personKeyFormatted = $"{personKeyFormatted[..^2]}{sex}"; + } + } + if (!IPersonBirthday.IsCounterPersonBirthday(personBirthday)) + { + pGedLines.Add("1 BIRT"); + pGedLines.Add($"2 DATE {personBirthday.Value:dd MMM yyyy}"); + } if (!Directory.Exists(directory)) _ = Directory.CreateDirectory(directory); - File.WriteAllText(Path.Combine(directory, $"{personKeyFormatted}.pged"), stringBuilder.ToString()); + string text = string.Join(Environment.NewLine, pGedLines); + _ = IPath.WriteAllText(Path.Combine(directory, $"{personKeyFormatted}.pged"), text, updateDateWhenMatches: false, compareBeforeWrite: true); } } @@ -64,7 +110,7 @@ internal abstract class Person List addresses = new(); Models.PersonName name = PersonName.Create(segments[zero]); string[] matches = (from l in personDisplayDirectoryAllFiles where !string.IsNullOrEmpty(personKeyFormatted) && l.Contains(personKeyFormatted) select l).ToArray(); - if (matches.Any()) + if (!string.IsNullOrEmpty(personKeyFormatted) && matches.Any()) WriteGedFile(personKeyFormatted, personBirthday, name, matches); result = new(id, personBirthday, name, comments, urls, numbers, emails, addresses); return result; diff --git a/Shared/Models/Stateless/Methods/PersonBirthday.cs b/Shared/Models/Stateless/Methods/PersonBirthday.cs index f1d083a..7161990 100644 --- a/Shared/Models/Stateless/Methods/PersonBirthday.cs +++ b/Shared/Models/Stateless/Methods/PersonBirthday.cs @@ -124,11 +124,10 @@ internal abstract class PersonBirthday continue; if (!IPersonBirthday.IsCounterPersonBirthday(personBirthday) && ((!personKeyDirectory.Contains('#') && (personDisplayDirectoryName.Contains('~') || personDisplayDirectoryName.Contains('#'))) || (personKeyDirectory.Contains('#') && !personDisplayDirectoryName.Contains('#')))) throw new NotSupportedException(); - results.Add(new(personKeyFormatted, personBirthday)); files = Directory.GetFiles(personKeyDirectory, "*", SearchOption.TopDirectoryOnly); - if (files.Any()) + if (!files.Any()) continue; - File.WriteAllText(Path.Combine(personKeyDirectory, $"{personKeyFormatted}.txt"), string.Empty); + results.Add(new(personKeyFormatted, personBirthday)); } return results; }