Moved more to Map library

This commit is contained in:
2022-08-29 08:45:01 -07:00
parent 674555b4fc
commit c1d30b5bbc
46 changed files with 1631 additions and 697 deletions

View File

@ -6,38 +6,33 @@ namespace View_by_Distance.Shared.Models;
public class Closest : Properties.IClosest
{
protected readonly double? _Average;
protected readonly int? _NormalizedPixelPercentage;
protected readonly int _Average;
protected readonly bool? _IsWrongYear;
protected readonly double? _Minimum;
protected Mapping _Mapping;
protected readonly double _Minimum;
protected readonly DateTime _MinimumDateTime;
protected readonly PersonBirthday? _PersonBirthday;
public double? Average => _Average;
public int? NormalizedPixelPercentage => _NormalizedPixelPercentage;
protected readonly int _NormalizedPixelPercentage;
protected readonly long? _TicksDelta;
public double Average => _Average;
public bool? IsWrongYear => _IsWrongYear;
public double? Minimum => _Minimum;
public Mapping Mapping => _Mapping;
public double Minimum => _Minimum;
public DateTime MinimumDateTime => _MinimumDateTime;
public PersonBirthday? PersonBirthday => _PersonBirthday;
public int NormalizedPixelPercentage => _NormalizedPixelPercentage;
public long? TicksDelta => _TicksDelta;
[JsonConstructor]
public Closest(double? average, int? normalizedPixelPercentage, bool? isWrongYear, double? minimum, DateTime minimumDateTime, PersonBirthday? personBirthday)
public Closest(int average, int normalizedPixelPercentage, bool? isWrongYear, Mapping mapping, double minimum, DateTime minimumDateTime, long? ticksDelta)
{
_Average = average;
_NormalizedPixelPercentage = normalizedPixelPercentage;
_IsWrongYear = isWrongYear;
_Mapping = mapping;
_Minimum = minimum;
_MinimumDateTime = minimumDateTime;
_PersonBirthday = personBirthday;
_TicksDelta = ticksDelta;
}
public Closest(int? normalizedPixelPercentage, DateTime minimumDateTime, bool? isWrongYear) :
this(null, normalizedPixelPercentage, isWrongYear, null, minimumDateTime, null)
{ }
public Closest(int? normalizedPixelPercentage, DateTime minimumDateTime, bool? isWrongYear, PersonBirthday? personBirthday, List<double> faceDistances) :
this(faceDistances.Average(), normalizedPixelPercentage, isWrongYear, faceDistances.Min(), minimumDateTime, personBirthday)
{ }
public override string ToString()
{
string result = JsonSerializer.Serialize(this, new JsonSerializerOptions() { WriteIndented = true });

View File

@ -7,6 +7,7 @@ public class Face : Properties.IFace
{
protected DateTime _DateTime;
protected List<FaceDistance> _FaceDistances;
protected FaceEncoding? _FaceEncoding;
protected Dictionary<Stateless.FacePart, FacePoint[]>? _FaceParts;
protected readonly OutputResolution? _OutputResolution;
@ -14,6 +15,7 @@ public class Face : Properties.IFace
protected readonly int? _LocationIndex;
protected readonly string _RelativePath;
public DateTime DateTime => _DateTime;
public List<FaceDistance> FaceDistances => _FaceDistances;
public FaceEncoding? FaceEncoding => _FaceEncoding;
public Dictionary<Stateless.FacePart, FacePoint[]>? FaceParts => _FaceParts;
public Location? Location => _Location;
@ -22,9 +24,12 @@ public class Face : Properties.IFace
public string RelativePath => _RelativePath;
[JsonConstructor]
public Face(DateTime dateTime, FaceEncoding? faceEncoding, Dictionary<Stateless.FacePart, FacePoint[]>? faceParts, Location? location, int? locationIndex, OutputResolution? outputResolution, string relativePath)
public Face(DateTime dateTime, List<FaceDistance> faceDistances, FaceEncoding? faceEncoding, Dictionary<Stateless.FacePart, FacePoint[]>? faceParts, Location? location, int? locationIndex, OutputResolution? outputResolution, string relativePath)
{
if (faceDistances is null)
faceDistances = new();
_DateTime = dateTime;
_FaceDistances = faceDistances;
_FaceEncoding = faceEncoding;
_FaceParts = faceParts;
_Location = location;
@ -34,29 +39,29 @@ public class Face : Properties.IFace
}
public Face() :
this(DateTime.MinValue, null, null, null, null, null, string.Empty)
this(DateTime.MinValue, new(), null, null, null, null, null, string.Empty)
{ }
public Face(Location location) :
this(DateTime.MinValue, null, null, location, null, null, string.Empty)
this(DateTime.MinValue, new(), null, null, location, null, null, string.Empty)
{ }
public Face(int facesCount, Face face) :
this(face.DateTime, face.FaceEncoding, face.FaceParts, face.Location, face.LocationIndex, face.OutputResolution, face.RelativePath)
this(face.DateTime, new(), face.FaceEncoding, face.FaceParts, face.Location, face.LocationIndex, face.OutputResolution, face.RelativePath)
{
if (face.Location?.Confidence is not null && face.OutputResolution is not null)
_Location = new(face.Location.Confidence, face.OutputResolution.Height, face.Location, face.OutputResolution.Width, facesCount);
}
public Face(int outputResolutionWidth, int outputResolutionHeight, int outputResolutionOrientation, Face face) :
this(face.DateTime, face.FaceEncoding, face.FaceParts, face.Location, face.LocationIndex, null, face.RelativePath)
this(face.DateTime, new(), face.FaceEncoding, face.FaceParts, face.Location, face.LocationIndex, null, face.RelativePath)
{
if (outputResolutionHeight > 0)
_OutputResolution = new(outputResolutionHeight, outputResolutionOrientation, outputResolutionWidth);
}
public Face(Property property, int outputResolutionWidth, int outputResolutionHeight, int outputResolutionOrientation, string relativePath, int? i, Location? location) :
this(DateTime.MinValue, null, null, location, i, null, relativePath)
this(DateTime.MinValue, new(), null, null, location, i, null, relativePath)
{
DateTime?[] dateTimes;
_OutputResolution = new(outputResolutionHeight, outputResolutionOrientation, outputResolutionWidth);

View File

@ -0,0 +1,36 @@
using System.Text.Json;
using System.Text.Json.Serialization;
namespace View_by_Distance.Shared.Models;
public class FaceDistance : Properties.IFaceDistance
{
protected readonly List<double> _Distances;
protected readonly bool? _IsWrongYear;
protected readonly string _Key;
protected readonly Mapping _Mapping;
protected readonly DateTime _MinimumDateTime;
public List<double> Distances => _Distances;
public bool? IsWrongYear => _IsWrongYear;
public string Key => _Key;
public Mapping Mapping => _Mapping;
public DateTime MinimumDateTime => _MinimumDateTime;
[JsonConstructor]
public FaceDistance(List<double> distances, bool? isWrongYear, string key, Mapping mapping, DateTime minimumDateTime)
{
_Distances = distances;
_IsWrongYear = isWrongYear;
_Key = key;
_Mapping = mapping;
_MinimumDateTime = minimumDateTime;
}
public override string ToString()
{
string result = JsonSerializer.Serialize(this, new JsonSerializerOptions() { WriteIndented = true });
return result;
}
}

View File

@ -11,8 +11,8 @@ public class Item : Properties.IItem
protected List<Closest> _Closest;
protected List<Face> _Faces;
protected readonly FileHolder? _ImageFileHolder;
protected List<Mapping> _Mapping;
protected bool? _Moved;
protected List<Named> _Named;
protected readonly bool? _NoJson;
protected Property? _Property;
protected readonly string _RelativePath;
@ -24,9 +24,9 @@ public class Item : Properties.IItem
public List<Closest> Closest => _Closest;
public List<Face> Faces => _Faces;
public FileHolder? ImageFileHolder => _ImageFileHolder;
public List<Mapping> Mapping => _Mapping;
public bool? Moved => _Moved;
public bool? NoJson => _NoJson;
public List<Named> Named => _Named;
public Property? Property => _Property;
public string RelativePath => _RelativePath;
public FileHolder? ResizedFileHolder => _ResizedFileHolder;
@ -34,15 +34,15 @@ public class Item : Properties.IItem
public bool ValidImageFormatExtension => _ValidImageFormatExtension;
[JsonConstructor]
public Item(bool? abandoned, bool? changed, List<Closest> closest, List<Face> faces, FileHolder? imageFileHolder, bool? moved, List<Named> named, bool? noJson, Property? property, string relativePath, FileHolder? resizedFileHolder, string sourceDirectoryFile, bool validImageFormatExtension)
public Item(bool? abandoned, bool? changed, List<Closest> closest, List<Face> faces, FileHolder? imageFileHolder, List<Mapping> mapping, bool? moved, bool? noJson, Property? property, string relativePath, FileHolder? resizedFileHolder, string sourceDirectoryFile, bool validImageFormatExtension)
{
_Abandoned = abandoned;
_Changed = changed;
_Closest = closest;
_Faces = faces;
_ImageFileHolder = imageFileHolder;
_Mapping = mapping;
_Moved = moved;
_Named = named;
_NoJson = noJson;
_Property = property;
_RelativePath = relativePath;
@ -54,7 +54,7 @@ public class Item : Properties.IItem
public Item(string sourceDirectoryFile, string relativePath, FileHolder? imageFileInfo, bool isValidImageFormatExtension, Property? property, bool? abandoned, bool? changed)
{
_Faces = new();
_Named = new();
_Mapping = new();
_Closest = new();
_Changed = changed;
_Property = property;

View File

@ -116,7 +116,7 @@ public class Location : Properties.ILocation, IEquatable<Location>
value = at / total;
if (value < 0)
value = 3;
result = (int)(Math.Round(value, Stateless.ILocation.Decimals) * Stateless.ILocation.Factor);
result = (int)(Math.Round(value, Stateless.ILocation.Digits) * Stateless.ILocation.Factor);
return result;
}

49
Shared/Models/Mapping.cs Normal file
View File

@ -0,0 +1,49 @@
using System.Text.Json;
using System.Text.Json.Serialization;
namespace View_by_Distance.Shared.Models;
public class Mapping : Properties.IMapping
{
protected readonly int? _ApproximateYears;
protected readonly string _DisplayDirectoryName;
protected bool? _Filtered;
protected readonly int? _NormalizedPixelPercentage;
protected readonly PersonBirthday _PersonBirthday;
protected readonly string _PersonKey;
public int? ApproximateYears => _ApproximateYears;
public string DisplayDirectoryName => _DisplayDirectoryName;
public bool? Filtered => _Filtered;
public int? NormalizedPixelPercentage => _NormalizedPixelPercentage;
public PersonBirthday PersonBirthday => _PersonBirthday;
public string PersonKey => _PersonKey;
[JsonConstructor]
public Mapping(int? approximateYears, string displayDirectoryName, bool? filtered, int? normalizedPixelPercentage, PersonBirthday personBirthday, string personKey)
{
_ApproximateYears = approximateYears;
_DisplayDirectoryName = displayDirectoryName;
_Filtered = filtered;
_NormalizedPixelPercentage = normalizedPixelPercentage;
_PersonBirthday = personBirthday;
_PersonKey = personKey;
}
public Mapping(int? approximateYears, string displayDirectoryName, int? normalizedPixelPercentage, PersonBirthday personBirthday, string personKey) :
this(approximateYears, displayDirectoryName, null, normalizedPixelPercentage, personBirthday, personKey)
{ }
public Mapping(int? approximateYears, string displayDirectoryName, PersonBirthday personBirthday, string personKey) :
this(approximateYears, displayDirectoryName, null, null, personBirthday, personKey)
{ }
public override string ToString()
{
string result = JsonSerializer.Serialize(this, new JsonSerializerOptions() { WriteIndented = true });
return result;
}
public void SetFiltered() => _Filtered = true;
}

View File

@ -0,0 +1,48 @@
using System.Text.Json;
using System.Text.Json.Serialization;
namespace View_by_Distance.Shared.Models;
public class MappingContainer : Properties.IMappingContainer
{
protected double? _Distance;
protected readonly Face? _Face;
protected readonly int _Id;
protected readonly bool? _IsWrongYear;
protected readonly string _Key;
protected readonly Mapping _Mapping;
protected readonly DateTime _MinimumDateTime;
public double? Distance => _Distance;
public Face? Face => _Face;
public int Id => _Id;
public bool? IsWrongYear => _IsWrongYear;
public string Key => _Key;
public Mapping Mapping => _Mapping;
public DateTime MinimumDateTime => _MinimumDateTime;
[JsonConstructor]
public MappingContainer(double? distance, Face? face, int id, bool? isWrongYear, string key, Mapping mapping, DateTime minimumDateTime)
{
_Distance = distance;
_Face = face;
_Id = id;
_IsWrongYear = isWrongYear;
_Key = key;
_Mapping = mapping;
_MinimumDateTime = minimumDateTime;
}
public MappingContainer(Face face, int id, bool? isWrongYear, string key, Mapping mapping, DateTime minimumDateTime) :
this(null, face, id, isWrongYear, key, mapping, minimumDateTime)
{ }
public override string ToString()
{
string result = JsonSerializer.Serialize(this, new JsonSerializerOptions() { WriteIndented = true });
return result;
}
public void SetDistance(double v) => _Distance = v;
}

View File

@ -1,6 +1,6 @@
namespace View_by_Distance.Shared.Models.Methods;
public interface INamed : Stateless.Methods.INamed
public interface INamed : Stateless.Methods.IMapping
{ // ...
//

View File

@ -1,37 +0,0 @@
using System.Text.Json;
using System.Text.Json.Serialization;
namespace View_by_Distance.Shared.Models;
public class Named : Properties.INamed
{
protected readonly bool? _IsWrongYear;
protected readonly DateTime _MinimumDateTime;
protected readonly int? _NormalizedPixelPercentage;
protected readonly PersonBirthday? _PersonBirthday;
public bool? IsWrongYear => _IsWrongYear;
public DateTime MinimumDateTime => _MinimumDateTime;
public int? NormalizedPixelPercentage => _NormalizedPixelPercentage;
public PersonBirthday? PersonBirthday => _PersonBirthday;
[JsonConstructor]
public Named(bool? isWrongYear, DateTime minimumDateTime, int? normalizedPixelPercentage, PersonBirthday? personBirthday)
{
_IsWrongYear = isWrongYear;
_MinimumDateTime = minimumDateTime;
_NormalizedPixelPercentage = normalizedPixelPercentage;
_PersonBirthday = personBirthday;
}
public Named(bool? isWrongYear, DateTime minimumDateTime, PersonBirthday? personBirthday) :
this(isWrongYear, minimumDateTime, null, personBirthday)
{ }
public override string ToString()
{
string result = JsonSerializer.Serialize(this, new JsonSerializerOptions() { WriteIndented = true });
return result;
}
}

View File

@ -1,3 +1,4 @@
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
@ -53,4 +54,18 @@ public class Person : Properties.IPerson
return result;
} // ...
public string GetFullName()
{
StringBuilder result = new();
if (_Name?.First is not null && !string.IsNullOrEmpty(_Name.First.Value))
_ = result.Append(_Name.First.Value);
if (_Name?.Middle is not null && !string.IsNullOrEmpty(_Name.Middle.Value))
_ = result.Append(' ').Append(_Name.Middle.Value);
if (_Name?.Last is not null && !string.IsNullOrEmpty(_Name.Last.Value))
_ = result.Append(' ').Append(_Name.Last.Value);
if (_Name?.Alias is not null && !string.IsNullOrEmpty(_Name.Alias.Value))
_ = result.Append(" (").Append(_Name.Alias.Value).Append(')');
return result.ToString();
}
}

View File

@ -3,11 +3,12 @@ namespace View_by_Distance.Shared.Models.Properties;
public interface IClosest
{
public double? Average { get; }
public int? NormalizedPixelPercentage { get; }
public double Average { get; }
public bool? IsWrongYear { get; }
public double? Minimum { get; }
public Mapping Mapping { get; }
public double Minimum { get; }
public DateTime MinimumDateTime { get; }
public PersonBirthday? PersonBirthday { get; }
public int NormalizedPixelPercentage { get; }
public long? TicksDelta { get; }
}

View File

@ -4,6 +4,7 @@ public interface IFace
{
public DateTime DateTime { get; }
public List<FaceDistance> FaceDistances { get; }
public FaceEncoding? FaceEncoding { get; }
public Dictionary<Stateless.FacePart, FacePoint[]>? FaceParts { get; }
public Location? Location { get; }

View File

@ -1,11 +1,12 @@
namespace View_by_Distance.Shared.Models.Properties;
public interface INamed
public interface IFaceDistance
{
public List<double> Distances { get; }
public bool? IsWrongYear { get; }
public string Key { get; }
public Mapping Mapping { get; }
public DateTime MinimumDateTime { get; }
public int? NormalizedPixelPercentage { get; }
public PersonBirthday? PersonBirthday { get; }
}

View File

@ -8,9 +8,9 @@ public interface IItem
public List<Closest> Closest { get; }
public List<Face> Faces { get; }
public FileHolder? ImageFileHolder { get; }
public List<Mapping> Mapping { get; }
public bool? Moved { get; }
public bool? NoJson { get; }
public List<Named> Named { get; }
public Property? Property { get; }
public string RelativePath { get; }
public FileHolder? ResizedFileHolder { get; }

View File

@ -0,0 +1,13 @@
namespace View_by_Distance.Shared.Models.Properties;
public interface IMapping
{
public int? ApproximateYears { get; }
public string DisplayDirectoryName { get; }
public bool? Filtered { get; }
public int? NormalizedPixelPercentage { get; }
public PersonBirthday PersonBirthday { get; }
public string PersonKey { get; }
}

View File

@ -0,0 +1,14 @@
namespace View_by_Distance.Shared.Models.Properties;
public interface IMappingContainer
{
public double? Distance { get; }
public Face? Face { get; }
public int Id { get; }
public bool? IsWrongYear { get; }
public string Key { get; }
public Mapping Mapping { get; }
public DateTime MinimumDateTime { get; }
}

View File

@ -3,9 +3,14 @@
public interface IClosest
{
// 637972153144596958
// const int Digits = 3;
// const int Factor = 1000;
// const int MaximumPer = 50;
// const bool SkipIsWrongYear = false;
const int Digits = 3;
const int Factor = 1000;
const int MaximumPer = 50;
const float MaximumMinimum = 0.50f;
const bool SkipIsWrongYear = true;
const float MinimumMinimum = 0.05f;
}

View File

@ -0,0 +1,11 @@
namespace View_by_Distance.Shared.Models.Stateless;
public interface IFaceDistance
{
// 637972153144596958
// const int MaximumPer = 999;
const int MaximumPer = 9999;
const double Tolerance = 0.6d;
}

View File

@ -3,7 +3,7 @@
public interface ILocation
{
const int Decimals = 6;
const int Digits = 6;
const int Factor = 1000000;
}

View File

@ -0,0 +1,14 @@
namespace View_by_Distance.Shared.Models.Stateless;
public interface IMapping
{
// 637972153144596958
// const bool UseDeterministicHashCodeUnknownFaceKeyValuePairsForAddToNamed = true;
// const bool OnlyUseNamedWithNormalizedPixelPercentagePopulatedForGetKeyValuePairs = false;
const bool UseDeterministicHashCodeUnknownFaceKeyValuePairsForAddToMapping = false;
const bool UseDeterministicHashCodeUnknownFaceKeyValuePairsForSaveMapping = false;
const bool SaveFaceEncoding = false;
}

View File

@ -0,0 +1,8 @@
namespace View_by_Distance.Shared.Models.Stateless;
public interface IPersonBirthday
{
const string Format = "yyyy-MM-dd_HH";
}

View File

@ -0,0 +1,22 @@
namespace View_by_Distance.Shared.Models.Stateless.Methods;
internal abstract class Age
{
internal static (int, TimeSpan) GetAge(DateTime minuend, DateTime subtrahend)
{
TimeSpan result;
int years = 0;
DateTime check = new(subtrahend.Ticks);
for (int i = 0; i < int.MaxValue; i++)
{
check = check.AddYears(1);
if (check > minuend)
break;
years += 1;
}
result = new(minuend.Ticks - check.AddYears(-1).Ticks);
return (years, result);
}
}

View File

@ -3,6 +3,33 @@ namespace View_by_Distance.Shared.Models.Stateless.Methods;
internal abstract class Closest
{
internal static Models.Closest[] Get(List<Models.Closest> collection) => (from l in collection orderby l.Minimum < Stateless.IClosest.MinimumMinimum, l.Average select l).ToArray();
private static int Get(List<double> faceDistances) => (int)(Math.Round(faceDistances.Average(), Stateless.IClosest.Digits) * Stateless.ILocation.Factor);
private static Models.Closest Get(Models.Face face, DateTime minimumDateTime, FaceDistance faceDistance)
{
Models.Closest result;
int average = Get(faceDistance.Distances);
double minimum = faceDistance.Distances.Min();
long? ticksDelta;
if (faceDistance.IsWrongYear is null || faceDistance.IsWrongYear.Value)
ticksDelta = null;
else
{
ticksDelta = Math.Abs(faceDistance.MinimumDateTime.Ticks - minimumDateTime.Ticks);
if (faceDistance.MinimumDateTime < faceDistance.Mapping.PersonBirthday.Value)
ticksDelta *= 2;
}
if (face.Location?.NormalizedPixelPercentage is null)
throw new NullReferenceException(nameof(face.Location.NormalizedPixelPercentage));
result = new(average, face.Location.NormalizedPixelPercentage.Value, faceDistance.IsWrongYear, faceDistance.Mapping, minimum, faceDistance.MinimumDateTime, ticksDelta);
return result;
}
internal static Models.Closest[] GetCollection(Models.Face face, DateTime minimumDateTime, List<FaceDistance> faceDistances)
{
Models.Closest[] results;
Models.Closest[] closestCollection = (from l in faceDistances select Get(face, minimumDateTime, l)).ToArray();
results = (from l in closestCollection orderby l.Average, l.TicksDelta.HasValue, l.TicksDelta select l).ToArray();
return results;
}
}

View File

@ -0,0 +1,9 @@
namespace View_by_Distance.Shared.Models.Stateless.Methods;
public interface IAge
{ // ...
(int, TimeSpan) TestStatic_GetAge(DateTime minuend, DateTime subtrahend);
static (int, TimeSpan) GetAge(DateTime minuend, DateTime subtrahend) => Age.GetAge(minuend, subtrahend);
}

View File

@ -3,8 +3,8 @@ namespace View_by_Distance.Shared.Models.Stateless.Methods;
public interface IClosest
{ // ...
Models.Closest[] TestStatic_Get(List<Models.Closest> collection);
Models.Closest[] TestStatic_Get(List<FaceDistance> faceDistances);
static Models.Closest[] Get(List<Models.Closest> collection) => Closest.Get(collection);
static Models.Closest[] GetCollection(Models.Face face, DateTime minimumDateTime, List<FaceDistance> faceDistances) => Closest.GetCollection(face, minimumDateTime, faceDistances);
}

View File

@ -1,18 +1,18 @@
namespace View_by_Distance.Shared.Models.Stateless.Methods;
public interface INamed
public interface IMapping
{ // ...
double? TestStatic_GetReversedDeterministicHashCodeKey(string file);
static double? GetReversedDeterministicHashCodeKey(bool keyValuePairsAny, Dictionary<int, List<Models.Face>> keyValuePairs, string file) =>
Named.GetReversedDeterministicHashCodeKey(keyValuePairsAny, keyValuePairs, file);
Mapping.GetReversedDeterministicHashCodeKey(keyValuePairsAny, keyValuePairs, file);
double TestStatic_GetDeterministicHashCodeKey(Models.Item item, Models.Face face);
static double GetDeterministicHashCodeKey(Models.Item item, Models.Face face) =>
Named.GetDeterministicHashCodeKey(item, face);
Mapping.GetDeterministicHashCodeKey(item, face);
double TestStatic_GetDeterministicHashCodeKey(Models.Item item, Models.Closest closest);
static double GetDeterministicHashCodeKey(Models.Item item, Models.Closest closest) =>
Named.GetDeterministicHashCodeKey(item, closest);
Mapping.GetDeterministicHashCodeKey(item, closest);
}

View File

@ -3,37 +3,65 @@ namespace View_by_Distance.Shared.Models.Stateless.Methods;
public interface IPersonBirthday
{
DateTime TestStatic_GetDefaultValue() => PersonBirthday.GetDefaultValue(); // {{1}}SingletonValue
DateTime TestStatic_GetDefaultValue() =>
PersonBirthday.GetDefaultValue(); // {{1}}SingletonValue
static DateTime GetDefaultValue() => PersonBirthday.GetDefaultValue(); // {{1}}SingletonValue
static DateTime GetDefaultValue() =>
PersonBirthday.GetDefaultValue(); // {{1}}SingletonValue
// ...
string TestStatic_GetFormat() => PersonBirthday.GetFormat();
static string GetFormat() => PersonBirthday.GetFormat();
double? TestStatic_GetAge(Models.PersonBirthday birthday);
static double? GetAge(Models.PersonBirthday birthday) =>
PersonBirthday.GetAge(birthday);
DateTime? TestStatic_GetDateTime(string personKey) => PersonBirthday.GetDateTime(personKey);
static DateTime? GetDateTime(string personKey) => PersonBirthday.GetDateTime(personKey);
DateTime? TestStatic_GetDateTime(string personKey) =>
PersonBirthday.GetDateTime(personKey);
static DateTime? GetDateTime(string personKey) =>
PersonBirthday.GetDateTime(personKey);
string TestStatic_GetFileName(Models.PersonBirthday personBirthday) => PersonBirthday.GetFileName(personBirthday);
static string GetFileName(Models.PersonBirthday personBirthday) => PersonBirthday.GetFileName(personBirthday);
string TestStatic_GetFileName(Models.PersonBirthday personBirthday) =>
PersonBirthday.GetFileName(personBirthday);
static string GetFileName(Models.PersonBirthday personBirthday) =>
PersonBirthday.GetFileName(personBirthday);
Models.PersonBirthday? TestStatic_GetPersonBirthday(string personKey) => PersonBirthday.GetPersonBirthday(personKey);
static Models.PersonBirthday? GetPersonBirthday(string personKey) => PersonBirthday.GetPersonBirthday(personKey);
(int, TimeSpan) TestStatic_GetAge(DateTime dateTime, Models.PersonBirthday birthday);
static (int, TimeSpan) GetAge(DateTime dateTime, Models.PersonBirthday birthday) =>
PersonBirthday.GetAge(dateTime, birthday);
string TestStatic_GetFormatted(Models.PersonBirthday personBirthday) => PersonBirthday.GetFormatted(personBirthday);
static string GetFormatted(Models.PersonBirthday personBirthday) => PersonBirthday.GetFormatted(personBirthday);
string TestStatic_GetFormatted(Models.PersonBirthday personBirthday) =>
PersonBirthday.GetFormatted(personBirthday);
static string GetFormatted(Models.PersonBirthday personBirthday) =>
PersonBirthday.GetFormatted(personBirthday);
Models.PersonBirthday TestStatic_GetNextBirthDate(Properties.IStorage storage) => PersonBirthday.GetNextBirthDate(storage);
static Models.PersonBirthday GetNextBirthDate(Properties.IStorage storage) => PersonBirthday.GetNextBirthDate(storage);
Models.PersonBirthday? TestStatic_GetPersonBirthday(string personKey) =>
PersonBirthday.GetPersonBirthday(personKey);
static Models.PersonBirthday? GetPersonBirthday(string personKey) =>
PersonBirthday.GetPersonBirthday(personKey);
bool TestStatic_DoesBirthDateExits(Properties.IStorage storage, Models.PersonBirthday personBirthday) => DoesBirthDateExits(storage, personBirthday);
static bool DoesBirthDateExits(Properties.IStorage storage, Models.PersonBirthday personBirthday) => DoesBirthDateExits(storage, personBirthday);
Models.PersonBirthday TestStatic_GetNextBirthDate(Properties.IStorage storage) =>
PersonBirthday.GetNextBirthDate(storage);
static Models.PersonBirthday GetNextBirthDate(Properties.IStorage storage) =>
PersonBirthday.GetNextBirthDate(storage);
string TestStatic_GetFileFullName(Properties.IStorage storage, Models.PersonBirthday personBirthday) => PersonBirthday.GetFileFullName(storage, personBirthday);
static string GetFileFullName(Properties.IStorage storage, Models.PersonBirthday personBirthday) => PersonBirthday.GetFileFullName(storage, personBirthday);
TimeSpan? TestStatic_Get(DateTime now, Models.PersonBirthday personBirthday) =>
PersonBirthday.GetTimeSpan(now, isWrongYear: false, personBirthday);
static TimeSpan? GetTimeSpan(DateTime minimumDateTime, Models.PersonBirthday personBirthday) =>
PersonBirthday.GetTimeSpan(minimumDateTime, isWrongYear: false, personBirthday);
TimeSpan? TestStatic_Get(DateTime minimumDateTime, bool? isWrongYear, Models.PersonBirthday personBirthday) => PersonBirthday.GetTimeSpan(minimumDateTime, isWrongYear, personBirthday);
static TimeSpan? GetTimeSpan(DateTime minimumDateTime, bool? isWrongYear, Models.PersonBirthday personBirthday) => PersonBirthday.GetTimeSpan(minimumDateTime, isWrongYear, personBirthday);
string TestStatic_GetFileFullName(Properties.IStorage storage, Models.PersonBirthday personBirthday) =>
PersonBirthday.GetFileFullName(storage, personBirthday);
static string GetFileFullName(Properties.IStorage storage, Models.PersonBirthday personBirthday) =>
PersonBirthday.GetFileFullName(storage, personBirthday);
bool TestStatic_DoesBirthDateExits(Properties.IStorage storage, Models.PersonBirthday personBirthday) =>
DoesBirthDateExits(storage, personBirthday);
static bool DoesBirthDateExits(Properties.IStorage storage, Models.PersonBirthday personBirthday) =>
DoesBirthDateExits(storage, personBirthday);
TimeSpan? TestStatic_Get(DateTime minimumDateTime, bool? isWrongYear, Models.PersonBirthday personBirthday) =>
PersonBirthday.GetTimeSpan(minimumDateTime, isWrongYear, personBirthday);
static TimeSpan? GetTimeSpan(DateTime minimumDateTime, bool? isWrongYear, Models.PersonBirthday personBirthday) =>
PersonBirthday.GetTimeSpan(minimumDateTime, isWrongYear, personBirthday);
}

View File

@ -2,18 +2,18 @@ using System.Text.Json;
namespace View_by_Distance.Shared.Models.Stateless.Methods;
internal abstract class Named
internal abstract class Mapping
{
private static double GetDeterministicHashCodeKey(int id, int normalizedPixelPercentage)
=> Math.Round(double.Parse($"{id}.{normalizedPixelPercentage}"), Stateless.ILocation.Decimals);
=> Math.Round(double.Parse($"{id}.{normalizedPixelPercentage}"), Stateless.ILocation.Digits);
internal static double GetDeterministicHashCodeKey(Models.Item item, Models.Closest closest)
{
double result;
if (item.Property?.Id is null || item.ImageFileHolder is null || closest.NormalizedPixelPercentage is null)
if (item.Property?.Id is null || item.ImageFileHolder is null)
throw new NullReferenceException();
result = GetDeterministicHashCodeKey(item.Property.Id.Value, closest.NormalizedPixelPercentage.Value);
result = GetDeterministicHashCodeKey(item.Property.Id.Value, closest.NormalizedPixelPercentage);
return result;
}

View File

@ -1,4 +1,5 @@
using System.Text.Json;
using System.Text.RegularExpressions;
namespace View_by_Distance.Shared.Models.Stateless.Methods;
@ -166,9 +167,9 @@ internal abstract class Person
Models.PersonName name;
List<Models.PersonURL> urls;
Models.PersonBirthday birthday;
List<Models.PersonComment> comments;
List<Models.PersonEmail> emails = new();
List<Models.PersonNumber> numbers = new();
List<Models.PersonComment> comments = new();
List<Models.PersonAddress> addresses = new();
Dictionary<DateTime, PersonImport> keyValuePairs = GetPersonCollection(localKnownPeopleFile);
foreach (KeyValuePair<DateTime, PersonImport> keyValuePair in keyValuePairs)
@ -176,6 +177,7 @@ internal abstract class Person
if (string.IsNullOrEmpty(keyValuePair.Value.Name))
continue;
urls = new();
comments = new();
birthday = new(keyValuePair.Key);
name = PersonName.Create(keyValuePair.Value.Name);
if (name.First is null || string.IsNullOrEmpty(name.First.Value))
@ -242,7 +244,54 @@ internal abstract class Person
results = GetPeopleFromText(storage, localKnownPeopleFile);
}
}
SaveToDirectory(storage, results);
return results.ToArray();
}
private static void SaveToDirectory(Properties.IStorage storage, List<Models.Person> people)
{
int years;
TimeSpan? timeSpan;
string personDirectory;
string? personFullName;
DateTime createdDateTime;
string birthdayDirectory;
string personJsonFileName;
string personDirectoryName;
string? peopleDirectory = null;
DateTime dateTime = DateTime.Now;
string? personJsonFileNameWithoutExtension;
const string pattern = @"[\\,\/,\:,\*,\?,\"",\<,\>,\|]";
foreach (Models.Person person in people)
{
personJsonFileName = IPerson.GetFileFullName(storage, person);
if (string.IsNullOrEmpty(peopleDirectory))
peopleDirectory = Path.GetDirectoryName(personJsonFileName);
if (string.IsNullOrEmpty(peopleDirectory))
break;
personJsonFileNameWithoutExtension = Path.GetFileNameWithoutExtension(personJsonFileName);
if (string.IsNullOrEmpty(personJsonFileNameWithoutExtension))
break;
personFullName = Regex.Replace(person.GetFullName(), pattern, string.Empty);
timeSpan = IPersonBirthday.GetTimeSpan(dateTime, person.Birthday);
if (timeSpan is null || timeSpan.Value.Ticks < 0)
personDirectoryName = $"{personFullName}~";
else
{
createdDateTime = new FileInfo(personJsonFileName).CreationTime;
(years, timeSpan) = IPersonBirthday.GetAge(createdDateTime, person.Birthday);
personDirectoryName = $"{personFullName}^{years}-{Math.Floor(timeSpan.Value.TotalDays):000}";
}
personDirectory = Path.Combine(peopleDirectory, personDirectoryName);
if (!Directory.Exists(personDirectory))
_ = Directory.CreateDirectory(personDirectory);
birthdayDirectory = Path.Combine(personDirectory, personJsonFileNameWithoutExtension);
if (!Directory.Exists(birthdayDirectory))
{
_ = Directory.CreateDirectory(birthdayDirectory);
File.Copy(personJsonFileName, Path.Combine(birthdayDirectory, $"{personJsonFileNameWithoutExtension}.json"));
}
}
}
}

View File

@ -8,14 +8,12 @@ internal abstract class PersonBirthday
internal static DateTime GetDefaultValue() => DateTime.MinValue; // {{1}}SingletonValue
// ...
internal static string GetFormat() => "yyyy-MM-dd_HH";
internal static string GetFormatted(Models.PersonBirthday personBirthday) => personBirthday.Value.ToString(GetFormat());
internal static string GetFileName(Models.PersonBirthday personBirthday) => $"{personBirthday.Value.ToString(GetFormat())}.json";
internal static string GetFormatted(Models.PersonBirthday personBirthday) => personBirthday.Value.ToString(Stateless.IPersonBirthday.Format);
internal static string GetFileName(Models.PersonBirthday personBirthday) => $"{personBirthday.Value.ToString(Stateless.IPersonBirthday.Format)}.json";
internal static bool DoesBirthDateExits(Properties.IStorage storage, Models.PersonBirthday personBirthday) => File.Exists(GetFileFullName(storage, personBirthday));
internal static Models.PersonBirthday GetNextBirthDate(Properties.IStorage storage) => throw new Exception(storage.ToString()); // Person.GetNextBirthDate(storage);
internal static string GetFileFullName(Properties.IStorage storage, Models.PersonBirthday personBirthday) => Path.Combine(storage.PeopleRootDirectory, "{}", GetFileName(personBirthday));
internal static DateTime? GetDateTime(string personKey) => DateTime.TryParseExact(personKey, GetFormat(), CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime dateTime) ? dateTime : null;
internal static DateTime? GetDateTime(string personKey) => DateTime.TryParseExact(personKey, Stateless.IPersonBirthday.Format, CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime dateTime) ? dateTime : null;
internal static Models.PersonBirthday? GetPersonBirthday(string personKey)
{
@ -37,4 +35,41 @@ internal abstract class PersonBirthday
timeSpan = new(minimumDateTime.Ticks - personBirthday.Value.Ticks);
return timeSpan;
}
internal static (int, TimeSpan) GetAge(DateTime dateTime, Models.PersonBirthday birthday)
{
TimeSpan result;
int years;
if (birthday?.Value is null)
throw new NullReferenceException(nameof(birthday.Value));
(years, result) = Age.GetAge(dateTime, birthday.Value);
return (years, result);
}
internal static (int, double) GetAge(DateTime dateTime, DateTime dayBeforeLeapDate, Models.PersonBirthday birthday)
{
double result;
(int years, TimeSpan timeSpan) = GetAge(dateTime, birthday);
if (!DateTime.IsLeapYear(dateTime.Year) || dateTime < dayBeforeLeapDate.AddDays(1))
result = timeSpan.TotalDays / 365;
else
result = timeSpan.TotalDays / 366;
return (years, result);
}
internal static double? GetAge(Models.PersonBirthday birthday)
{
double? result;
if (birthday is null)
result = null;
else
{
DateTime dateTime = DateTime.Now;
DateTime dayBeforeLeapDate = new(dateTime.Year, 2, 28);
(int years, double r) = GetAge(dateTime, dayBeforeLeapDate, birthday);
result = years + r;
}
return result;
}
}

View File

@ -62,10 +62,10 @@ internal abstract class Property
in segments
where l?.Length > 2
&& (
l[..2] is "19" or "20"
|| (l.Length == 5 && l.Substring(1, 2) is "19" or "20" && (l[0] is '~' or '=' or '-' or '^' or '#'))
|| (l.Length == 6 && l[..2] is "19" or "20" && l[4] == '.')
|| (l.Length == 7 && l.Substring(1, 2) is "19" or "20" && l[5] == '.')
l[..2] is "18" or "19" or "20"
|| (l.Length == 5 && l.Substring(1, 2) is "18" or "19" or "20" && (l[0] is '~' or '=' or '-' or '^' or '#'))
|| (l.Length == 6 && l[..2] is "18" or "19" or "20" && l[4] == '.')
|| (l.Length == 7 && l.Substring(1, 2) is "18" or "19" or "20" && l[5] == '.')
)
select l
).ToArray();