Merge pull request #49 from LD-Reborn/35-implement-enums-for-probmethods-in-the-shared-models
Added enums to JSONEntity and JSONDatapoint
This commit is contained in:
@@ -1,7 +1,9 @@
|
|||||||
|
using Shared.Models;
|
||||||
|
|
||||||
namespace Server.Exceptions;
|
namespace Server.Exceptions;
|
||||||
|
|
||||||
public class ProbMethodNotFoundException(string probMethod) : Exception($"Unknown probMethod name {probMethod}") { }
|
public class ProbMethodNotFoundException(ProbMethodEnum probMethod) : Exception($"Unknown probMethod name {probMethod}") { }
|
||||||
|
|
||||||
public class SimilarityMethodNotFoundException(string similarityMethod) : Exception($"Unknown similarityMethod name \"{similarityMethod}\"") { }
|
public class SimilarityMethodNotFoundException(SimilarityMethodEnum similarityMethod) : Exception($"Unknown similarityMethod name \"{similarityMethod}\"") { }
|
||||||
|
|
||||||
public class JSONPathSelectionException(string path, string testedContent) : Exception($"Unable to select tokens using JSONPath {path} for string: {testedContent}.") { }
|
public class JSONPathSelectionException(string path, string testedContent) : Exception($"Unable to select tokens using JSONPath {path} for string: {testedContent}.") { }
|
||||||
@@ -38,12 +38,12 @@ public class DatabaseHelper(ILogger<DatabaseHelper> logger)
|
|||||||
return helper.ExecuteSQLCommandGetInsertedID("INSERT INTO searchdomain (name, settings) VALUES (@name, @settings)", parameters);
|
return helper.ExecuteSQLCommandGetInsertedID("INSERT INTO searchdomain (name, settings) VALUES (@name, @settings)", parameters);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static int DatabaseInsertEntity(SQLHelper helper, string name, string probmethod, int id_searchdomain)
|
public static int DatabaseInsertEntity(SQLHelper helper, string name, ProbMethodEnum probmethod, int id_searchdomain)
|
||||||
{
|
{
|
||||||
Dictionary<string, dynamic> parameters = new()
|
Dictionary<string, dynamic> parameters = new()
|
||||||
{
|
{
|
||||||
{ "name", name },
|
{ "name", name },
|
||||||
{ "probmethod", probmethod },
|
{ "probmethod", probmethod.ToString() },
|
||||||
{ "id_searchdomain", id_searchdomain }
|
{ "id_searchdomain", id_searchdomain }
|
||||||
};
|
};
|
||||||
return helper.ExecuteSQLCommandGetInsertedID("INSERT INTO entity (name, probmethod, id_searchdomain) VALUES (@name, @probmethod, @id_searchdomain)", parameters);
|
return helper.ExecuteSQLCommandGetInsertedID("INSERT INTO entity (name, probmethod, id_searchdomain) VALUES (@name, @probmethod, @id_searchdomain)", parameters);
|
||||||
@@ -60,13 +60,13 @@ public class DatabaseHelper(ILogger<DatabaseHelper> logger)
|
|||||||
return helper.ExecuteSQLCommandGetInsertedID("INSERT INTO attribute (attribute, value, id_entity) VALUES (@attribute, @value, @id_entity)", parameters);
|
return helper.ExecuteSQLCommandGetInsertedID("INSERT INTO attribute (attribute, value, id_entity) VALUES (@attribute, @value, @id_entity)", parameters);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static int DatabaseInsertDatapoint(SQLHelper helper, string name, string probmethod_embedding, string similarityMethod, string hash, int id_entity)
|
public static int DatabaseInsertDatapoint(SQLHelper helper, string name, ProbMethodEnum probmethod_embedding, SimilarityMethodEnum similarityMethod, string hash, int id_entity)
|
||||||
{
|
{
|
||||||
Dictionary<string, dynamic> parameters = new()
|
Dictionary<string, dynamic> parameters = new()
|
||||||
{
|
{
|
||||||
{ "name", name },
|
{ "name", name },
|
||||||
{ "probmethod_embedding", probmethod_embedding },
|
{ "probmethod_embedding", probmethod_embedding.ToString() },
|
||||||
{ "similaritymethod", similarityMethod },
|
{ "similaritymethod", similarityMethod.ToString() },
|
||||||
{ "hash", hash },
|
{ "hash", hash },
|
||||||
{ "id_entity", id_entity }
|
{ "id_entity", id_entity }
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -179,13 +179,13 @@ public class SearchdomainHelper(ILogger<SearchdomainHelper> logger, DatabaseHelp
|
|||||||
preexistingEntity.datapoints.Add(newDatapoint);
|
preexistingEntity.datapoints.Add(newDatapoint);
|
||||||
|
|
||||||
}
|
}
|
||||||
if (newEntityDatapoint is not null && (newEntityDatapoint.Probmethod_embedding != datapoint.probMethod.name || newEntityDatapoint.SimilarityMethod != datapoint.similarityMethod.name))
|
if (newEntityDatapoint is not null && (newEntityDatapoint.Probmethod_embedding != datapoint.probMethod.probMethodEnum || newEntityDatapoint.SimilarityMethod != datapoint.similarityMethod.similarityMethodEnum))
|
||||||
{
|
{
|
||||||
// Datapoint - Updated (probmethod or similaritymethod)
|
// Datapoint - Updated (probmethod or similaritymethod)
|
||||||
Dictionary<string, dynamic> parameters = new()
|
Dictionary<string, dynamic> parameters = new()
|
||||||
{
|
{
|
||||||
{ "probmethod", newEntityDatapoint.Probmethod_embedding },
|
{ "probmethod", newEntityDatapoint.Probmethod_embedding.ToString() },
|
||||||
{ "similaritymethod", newEntityDatapoint.SimilarityMethod },
|
{ "similaritymethod", newEntityDatapoint.SimilarityMethod.ToString() },
|
||||||
{ "datapointName", datapoint.name },
|
{ "datapointName", datapoint.name },
|
||||||
{ "entityId", preexistingEntityID}
|
{ "entityId", preexistingEntityID}
|
||||||
};
|
};
|
||||||
@@ -227,7 +227,7 @@ public class SearchdomainHelper(ILogger<SearchdomainHelper> logger, DatabaseHelp
|
|||||||
}
|
}
|
||||||
|
|
||||||
var probMethod = Probmethods.GetMethod(jsonEntity.Probmethod) ?? throw new ProbMethodNotFoundException(jsonEntity.Probmethod);
|
var probMethod = Probmethods.GetMethod(jsonEntity.Probmethod) ?? throw new ProbMethodNotFoundException(jsonEntity.Probmethod);
|
||||||
Entity entity = new(jsonEntity.Attributes, probMethod, jsonEntity.Probmethod, datapoints, jsonEntity.Name)
|
Entity entity = new(jsonEntity.Attributes, probMethod, jsonEntity.Probmethod.ToString(), datapoints, jsonEntity.Name)
|
||||||
{
|
{
|
||||||
id = id_entity
|
id = id_entity
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,37 +1,29 @@
|
|||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
using Server.Exceptions;
|
using Server.Exceptions;
|
||||||
|
using Shared.Models;
|
||||||
|
|
||||||
namespace Server;
|
namespace Server;
|
||||||
|
|
||||||
public class ProbMethod
|
public class ProbMethod
|
||||||
{
|
{
|
||||||
public Probmethods.probMethodDelegate method;
|
public Probmethods.probMethodDelegate method;
|
||||||
|
public ProbMethodEnum probMethodEnum;
|
||||||
public string name;
|
public string name;
|
||||||
|
|
||||||
public ProbMethod(string name, ILogger logger)
|
public ProbMethod(ProbMethodEnum probMethodEnum, ILogger logger)
|
||||||
{
|
{
|
||||||
this.name = name;
|
this.probMethodEnum = probMethodEnum;
|
||||||
|
this.name = probMethodEnum.ToString();
|
||||||
Probmethods.probMethodDelegate? probMethod = Probmethods.GetMethod(name);
|
Probmethods.probMethodDelegate? probMethod = Probmethods.GetMethod(name);
|
||||||
if (probMethod is null)
|
if (probMethod is null)
|
||||||
{
|
{
|
||||||
logger.LogError("Unable to retrieve probMethod {name}", [name]);
|
logger.LogError("Unable to retrieve probMethod {name}", [name]);
|
||||||
throw new ProbMethodNotFoundException(name);
|
throw new ProbMethodNotFoundException(probMethodEnum);
|
||||||
}
|
}
|
||||||
method = probMethod;
|
method = probMethod;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public enum ProbMethodEnum
|
|
||||||
{
|
|
||||||
Mean,
|
|
||||||
HarmonicMean,
|
|
||||||
QuadraticMean,
|
|
||||||
GeometricMean,
|
|
||||||
EVEWAvg,
|
|
||||||
HVEWAvg,
|
|
||||||
LVEWAvg,
|
|
||||||
DictionaryWeightedAverage
|
|
||||||
}
|
|
||||||
|
|
||||||
public static class Probmethods
|
public static class Probmethods
|
||||||
{
|
{
|
||||||
@@ -54,6 +46,11 @@ public static class Probmethods
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static probMethodDelegate? GetMethod(ProbMethodEnum probMethodEnum)
|
||||||
|
{
|
||||||
|
return GetMethod(probMethodEnum.ToString());
|
||||||
|
}
|
||||||
|
|
||||||
public static probMethodDelegate? GetMethod(string name)
|
public static probMethodDelegate? GetMethod(string name)
|
||||||
{
|
{
|
||||||
string methodName = name;
|
string methodName = name;
|
||||||
|
|||||||
@@ -8,12 +8,19 @@ using Server.HealthChecks;
|
|||||||
using Server.Helper;
|
using Server.Helper;
|
||||||
using Server.Models;
|
using Server.Models;
|
||||||
using Server.Services;
|
using Server.Services;
|
||||||
|
using System.Text.Json.Serialization;
|
||||||
|
|
||||||
var builder = WebApplication.CreateBuilder(args);
|
var builder = WebApplication.CreateBuilder(args);
|
||||||
|
|
||||||
// Add services to the container.
|
// Add services to the container.
|
||||||
|
|
||||||
builder.Services.AddControllersWithViews();
|
builder.Services.AddControllersWithViews()
|
||||||
|
.AddJsonOptions(options =>
|
||||||
|
{
|
||||||
|
options.JsonSerializerOptions.Converters.Add(
|
||||||
|
new JsonStringEnumConverter()
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
// Add Localization
|
// Add Localization
|
||||||
builder.Services.AddLocalization(options => options.ResourcesPath = "Resources");
|
builder.Services.AddLocalization(options => options.ResourcesPath = "Resources");
|
||||||
|
|||||||
@@ -96,8 +96,16 @@ public class Searchdomain
|
|||||||
string probmethodString = datapointReader.GetString(3);
|
string probmethodString = datapointReader.GetString(3);
|
||||||
string similarityMethodString = datapointReader.GetString(4);
|
string similarityMethodString = datapointReader.GetString(4);
|
||||||
string hash = datapointReader.GetString(5);
|
string hash = datapointReader.GetString(5);
|
||||||
ProbMethod probmethod = new(probmethodString, _logger);
|
ProbMethodEnum probmethodEnum = (ProbMethodEnum)Enum.Parse(
|
||||||
SimilarityMethod similarityMethod = new(similarityMethodString, _logger);
|
typeof(ProbMethodEnum),
|
||||||
|
probmethodString
|
||||||
|
);
|
||||||
|
SimilarityMethodEnum similairtyMethodEnum = (SimilarityMethodEnum)Enum.Parse(
|
||||||
|
typeof(SimilarityMethodEnum),
|
||||||
|
similarityMethodString
|
||||||
|
);
|
||||||
|
ProbMethod probmethod = new(probmethodEnum, _logger);
|
||||||
|
SimilarityMethod similarityMethod = new(similairtyMethodEnum, _logger);
|
||||||
if (embedding_unassigned.TryGetValue(id, out Dictionary<string, float[]>? embeddings) && probmethod is not null)
|
if (embedding_unassigned.TryGetValue(id, out Dictionary<string, float[]>? embeddings) && probmethod is not null)
|
||||||
{
|
{
|
||||||
embedding_unassigned.Remove(id);
|
embedding_unassigned.Remove(id);
|
||||||
|
|||||||
@@ -1,16 +1,18 @@
|
|||||||
using System.Numerics.Tensors;
|
using System.Numerics.Tensors;
|
||||||
using System.Text.Json;
|
using Shared.Models;
|
||||||
|
|
||||||
namespace Server;
|
namespace Server;
|
||||||
|
|
||||||
public class SimilarityMethod
|
public class SimilarityMethod
|
||||||
{
|
{
|
||||||
public SimilarityMethods.similarityMethodDelegate method;
|
public SimilarityMethods.similarityMethodDelegate method;
|
||||||
|
public SimilarityMethodEnum similarityMethodEnum;
|
||||||
public string name;
|
public string name;
|
||||||
|
|
||||||
public SimilarityMethod(string name, ILogger logger)
|
public SimilarityMethod(SimilarityMethodEnum similarityMethodEnum, ILogger logger)
|
||||||
{
|
{
|
||||||
this.name = name;
|
this.similarityMethodEnum = similarityMethodEnum;
|
||||||
|
this.name = similarityMethodEnum.ToString();
|
||||||
SimilarityMethods.similarityMethodDelegate? probMethod = SimilarityMethods.GetMethod(name);
|
SimilarityMethods.similarityMethodDelegate? probMethod = SimilarityMethods.GetMethod(name);
|
||||||
if (probMethod is null)
|
if (probMethod is null)
|
||||||
{
|
{
|
||||||
@@ -21,14 +23,6 @@ public class SimilarityMethod
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public enum SimilarityMethodEnum
|
|
||||||
{
|
|
||||||
Cosine,
|
|
||||||
Euclidian,
|
|
||||||
Manhattan,
|
|
||||||
Pearson
|
|
||||||
}
|
|
||||||
|
|
||||||
public static class SimilarityMethods
|
public static class SimilarityMethods
|
||||||
{
|
{
|
||||||
public delegate float similarityMethodProtoDelegate(float[] vector1, float[] vector2);
|
public delegate float similarityMethodProtoDelegate(float[] vector1, float[] vector2);
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ namespace Shared.Models;
|
|||||||
public class JSONEntity
|
public class JSONEntity
|
||||||
{
|
{
|
||||||
public required string Name { get; set; }
|
public required string Name { get; set; }
|
||||||
public required string Probmethod { get; set; }
|
public required ProbMethodEnum Probmethod { get; set; }
|
||||||
public required string Searchdomain { get; set; }
|
public required string Searchdomain { get; set; }
|
||||||
public required Dictionary<string, string> Attributes { get; set; }
|
public required Dictionary<string, string> Attributes { get; set; }
|
||||||
public required JSONDatapoint[] Datapoints { get; set; }
|
public required JSONDatapoint[] Datapoints { get; set; }
|
||||||
@@ -13,7 +13,27 @@ public class JSONDatapoint
|
|||||||
{
|
{
|
||||||
public required string Name { get; set; }
|
public required string Name { get; set; }
|
||||||
public required string? Text { get; set; }
|
public required string? Text { get; set; }
|
||||||
public required string Probmethod_embedding { get; set; }
|
public required ProbMethodEnum Probmethod_embedding { get; set; }
|
||||||
public required string SimilarityMethod { get; set; }
|
public required SimilarityMethodEnum SimilarityMethod { get; set; }
|
||||||
public required string[] Model { get; set; }
|
public required string[] Model { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public enum ProbMethodEnum
|
||||||
|
{
|
||||||
|
Mean,
|
||||||
|
HarmonicMean,
|
||||||
|
QuadraticMean,
|
||||||
|
GeometricMean,
|
||||||
|
EVEWAvg,
|
||||||
|
HVEWAvg,
|
||||||
|
LVEWAvg,
|
||||||
|
DictionaryWeightedAverage
|
||||||
|
}
|
||||||
|
|
||||||
|
public enum SimilarityMethodEnum
|
||||||
|
{
|
||||||
|
Cosine,
|
||||||
|
Euclidian,
|
||||||
|
Manhattan,
|
||||||
|
Pearson
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user