Buscar contenidos

lunes, 26 de marzo de 2018

Cálculo porcentaje de similitud entre Strings


C#: Calculating Percentage Similarity of 2 strings

double CalculateSimilarity(string source, string target)
{
    if ((source == null) || (target == null)) return 0.0;
    if ((source.Length == 0) || (target.Length == 0)) return 0.0;
    if (source == target) return 1.0;
   
    int stepsToSame = ComputeLevenshteinDistance(source, target);
    return (1.0 - ((double)stepsToSame / (double)Math.Max(source.Length, target.Length)));
}
int ComputeLevenshteinDistance(string source, string target)
{
    if ((source == null) || (target == null)) return 0;
    if ((source.Length == 0) || (target.Length == 0)) return 0;
    if (source == target) return source.Length;
   
    int sourceWordCount = source.Length;
    int targetWordCount = target.Length;
   
    // Step 1
    if (sourceWordCount == 0)
        return targetWordCount;
   
    if (targetWordCount == 0)
        return sourceWordCount;
   
    int[,] distance = new int[sourceWordCount + 1, targetWordCount + 1];
   
    // Step 2
    for (int i = 0; i <= sourceWordCount; distance[i, 0] = i++);
    for (int j = 0; j <= targetWordCount; distance[0, j] = j++);
   
    for (int i = 1; i <= sourceWordCount; i++)
    {
        for (int j = 1; j <= targetWordCount; j++)
        {
            // Step 3
            int cost = (target[j - 1] == source[i - 1]) ? 0 : 1;
   
            // Step 4
            distance[i, j] = Math.Min(Math.Min(distance[i - 1, j] + 1, distance[i, j - 1] + 1), distance[i - 1, j - 1] + cost);
        }
    }
   
    return distance[sourceWordCount, targetWordCount];
}

jueves, 15 de marzo de 2018

Método Parallel.ForEach

Procesamiento paralelo
Mejor rendimiento
Número controlado de Threads por ejecución


        var task1 = Task.Factory.StartNew(() =>
            {
             

            });

            var task2 = Task.Factory.StartNew(() =>
            {


            });

            Task.WaitAll(task1, task2);

Link artículo

string[] lines = File.ReadAllLines(txtProxyListPath.Text);
List<string> list_lines = new List<string>(lines);
Parallel.ForEach(list_lines, line =>
{
    //Your stuff
});
You can specify a MaxDegreeOfParallelism in a ParallelOptions parameter
Parallel.ForEach(
    listOfWebpages,
    new ParallelOptions { MaxDegreeOfParallelism = 4 },
    webpage => { Download(webpage); }
);
------------
        var tests = new[]
            {
                Task.Factory.StartNew(() => LectorBatch1()),              
Task.Factory.StartNew(() => LectorBatch2()),            
Task.Factory.StartNew(() => LectorBatch3())
            };
            Task.WaitAll(tests);
--
public static class MyExtensions
{
    public static IEnumerable<IEnumerable<T>> Batch<T>(this IEnumerable<T> items,
                                                       int maxItems)
    {
        return items.Select((item, inx) => new { item, inx })
                    .GroupBy(x => x.inx / maxItems)
                    .Select(g => g.Select(x => x.item));
    }
}
and the usage would be:
List<int> list = new List<int>() { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

foreach(var batch in list.Batch(3))
{
    Console.WriteLine(String.Join(",",batch));
}
OUTPUT:
0,1,2
3,4,5
6,7,8
9
Task Run queda corriendo en segundo plano
    public Task<long> InsertLog(string tracking)
        {            
            var task = Task<long>.Run(() => {                
                return 0;
            });

            return task;
        }
...
 taskInsertLog.Wait();
taskInsertLog.Result



class Program
    {
        static void Main(string[] args)
        {
            List<Minion> lMinions = GetMonions();
            List<Task> lTask;
            foreach (var item in Batch<Minion>(lMinions, 50))//recorre una lista en lotes de 50*
            {               
                lTask = new List<Task>();
                foreach (var CurrentMinion in item)
                {                   
                    lTask.Add(Task.Factory.StartNew(() => Proceso(CurrentMinion)));
                }
                Task.WaitAll(lTask.ToArray());
            }
            Console.ReadKey();
        }
        public static void Proceso(Minion minion)
        {
            Console.WriteLine("Id:"+ minion.Id + " Nombre:" + minion.Nombre + " Nacimiento:" + minion.FechaNacimiento);
        }
        public static IEnumerable<IEnumerable<T>> Batch<T>(IEnumerable<T> items, int maxItems)
        {
            return items.Select((item, inx) => new { item, inx })
                        .GroupBy(x => x.inx / maxItems)
                        .Select(g => g.Select(x => x.item));
        }
        public static List<Minion> GetMonions()
        {
            List<Minion> lMinions = new List<Minion>();
            for (int i = 0; i < 10000; i++)
            {
                lMinions.Add(new Minion() { Id = i, Nombre = "nombre"+ i +"", FechaNacimiento = DateTime.Now  });
            }
            return lMinions;
        }
    }