Moved more to Map library
This commit is contained in:
22
Shared/Models/Stateless/Methods/Age.cs
Normal file
22
Shared/Models/Stateless/Methods/Age.cs
Normal 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);
|
||||
}
|
||||
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
9
Shared/Models/Stateless/Methods/IAge.cs
Normal file
9
Shared/Models/Stateless/Methods/IAge.cs
Normal 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);
|
||||
|
||||
}
|
@ -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);
|
||||
|
||||
}
|
@ -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);
|
||||
|
||||
}
|
@ -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);
|
||||
|
||||
}
|
@ -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;
|
||||
}
|
||||
|
@ -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"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
@ -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();
|
||||
|
Reference in New Issue
Block a user