1
0

Adaptado para que agrupe los datos

This commit is contained in:
2023-01-19 10:55:47 +01:00
parent 105e2e471e
commit 8b128ceb46
2 changed files with 85 additions and 42 deletions

View File

@@ -10,14 +10,18 @@ using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace testML
{
public static class DictionaryToObjectConverter
{
public static IEnumerable<object> Convert(List<Dictionary<string, object>> data, string toPredict, out Type classType, out Type classPredictionType, out DataViewSchema schema)
public static IEnumerable<object> Convert(List<Dictionary<string, object>> data, string toPredict, string objectFilename, out Type classType, out Type classPredictionType, out DataViewSchema schema)
{
var regexCR = new Regex(@"_CR\d+");
var currentCR = regexCR.Match(toPredict).Groups[0].Value;
var schemaBuilder = new DataViewSchema.Builder();
var definition = new Dictionary<string, Type>();
@@ -27,6 +31,11 @@ namespace testML
{
foreach (var key in sample.Keys)
{
if (!key.Contains(currentCR))
{
continue;
}
//Buscamos el tipo
var sampleValue = (from x in data where x.ContainsKey(key) && x[key] != null select x[key]).FirstOrDefault();
if (sampleValue != null)
@@ -58,44 +67,57 @@ namespace testML
}
}
var converter = new DictionaryToObjectConverterClass()
var className = "OBJ"+ Path.GetFileNameWithoutExtension(objectFilename).Replace(".", "_");
Assembly dllAssembly = null;
if (File.Exists(Path.Combine(Environment.CurrentDirectory, objectFilename)))
{
ClassName = "OBJ" + Guid.NewGuid().ToString("N").ToUpper(),
ToPredict = toPredict,
Definition = definition
};
var classCode = converter.TransformText();
string exDir = System.Runtime.InteropServices.RuntimeEnvironment.GetRuntimeDirectory().Trim(Path.DirectorySeparatorChar);
CompilerParameters compilerParameters = new CompilerParameters
dllAssembly = Assembly.LoadFrom(Path.Combine(Environment.CurrentDirectory, objectFilename));
}
else
{
GenerateExecutable = false,
GenerateInMemory = true,
IncludeDebugInformation = true,
TreatWarningsAsErrors = false,
CompilerOptions = string.Format("/target:library /lib:{0}", exDir)
};
var converter = new DictionaryToObjectConverterClass()
{
ClassName = className,
ToPredict = toPredict,
Definition = definition
};
var mlDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
var classCode = converter.TransformText();
foreach (var item in AppDomain.CurrentDomain.GetAssemblies())
{
compilerParameters.ReferencedAssemblies.Add(item.Location);
File.WriteAllText(Path.Combine(Environment.CurrentDirectory, objectFilename + ".cs"), classCode, Encoding.UTF8);
string exDir = System.Runtime.InteropServices.RuntimeEnvironment.GetRuntimeDirectory().Trim(Path.DirectorySeparatorChar);
CompilerParameters compilerParameters = new CompilerParameters
{
GenerateExecutable = false,
GenerateInMemory = false,
IncludeDebugInformation = true,
TreatWarningsAsErrors = false,
OutputAssembly = Path.Combine(Environment.CurrentDirectory, objectFilename),
CompilerOptions = string.Format("/target:library /lib:{0}", exDir)
};
var mlDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
foreach (var item in AppDomain.CurrentDomain.GetAssemblies())
{
compilerParameters.ReferencedAssemblies.Add(item.Location);
}
// compilerParameters.ReferencedAssemblies.Add(Assembly.GetExecutingAssembly().Location);
CompilerResults compilerResults =
new CSharpCodeProvider(new Dictionary<string, string> { { "CompilerVersion", "v4.0" } }).CompileAssemblyFromSource(compilerParameters,
new string[] { classCode }
);
dllAssembly = compilerResults.CompiledAssembly;
}
// compilerParameters.ReferencedAssemblies.Add(Assembly.GetExecutingAssembly().Location);
CompilerResults compilerResults =
new CSharpCodeProvider(new Dictionary<string, string> { { "CompilerVersion", "v4.0" } }).CompileAssemblyFromSource(compilerParameters,
new string[] { classCode }
);
var dllAssembly = compilerResults.CompiledAssembly;
classType = dllAssembly.GetType("DictionaryToObjectConverterNamespace." + converter.ClassName);
classPredictionType = dllAssembly.GetType("DictionaryToObjectConverterNamespace." + converter.ClassName + "Prediction");
classType = dllAssembly.GetType("DictionaryToObjectConverterNamespace." + className);
classPredictionType = dllAssembly.GetType("DictionaryToObjectConverterNamespace." + className + "Prediction");
Type listType = typeof(List<>);
Type genericType = listType.MakeGenericType(classType);