Esempio n. 1
0
        public static IEnumerable <R> JoinSimilar <T, S, R>(this List <T> outer, List <S> inner,
                                                            Func <T, string> outerKeySelector,
                                                            Func <S, string> innerKeySelector,
                                                            Func <T, S, int, R> resultSelector)
            where T : notnull
            where S : notnull
        {
            StringDistance sd = new StringDistance();
            Dictionary <Tuple <T, S>, int> distances = (from o in outer
                                                        from i in inner
                                                        select KVP.Create(Tuple.Create(o, i),
                                                                          sd.LevenshteinDistance(outerKeySelector(o), innerKeySelector(i)))).ToDictionary();

            while (distances.Count > 0)
            {
                var kvp   = distances.WithMin(a => a.Value);
                var tuple = kvp.Key;

                distances.RemoveRange(distances.Keys.Where(a => a.Item1.Equals(tuple.Item1) || a.Item2.Equals(tuple.Item2)).ToList());
                outer.Remove(tuple.Item1);
                inner.Remove(tuple.Item2);

                yield return(resultSelector(tuple.Item1, tuple.Item2, kvp.Value));
            }
        }
Esempio n. 2
0
        T TryGetValueInternal(Type type)
        {
            T result;

            if (cached.TryGetValue(type, out result))
            {
                return(result);
            }

            var baseValue = type.BaseType == null || !IsAllowed(type.BaseType) ? null : TryGetValue(type.BaseType);

            var currentValue = definitions.TryGetC(type);

            if (minimumType != null && !minimumType.IsInterface)
            {
                return(merger(KVP.Create(type, currentValue), KVP.Create(type.BaseType, baseValue), null));
            }

            IEnumerable <Type> interfaces = type.GetInterfaces().Where(IsAllowed);

            if (type.BaseType != null)
            {
                interfaces = interfaces.Except(type.BaseType.GetInterfaces());
            }

            return(merger(KVP.Create(type, currentValue), KVP.Create(type.BaseType, baseValue), interfaces.Select(inter => KVP.Create(inter, TryGetValue(inter))).ToList()));
        }
Esempio n. 3
0
        internal static LocalizedType ImportXml(Type type, DescriptionOptions opts, LocalizedAssembly assembly, XElement x)
        {
            string description = !opts.IsSetAssert(DescriptionOptions.Description, type) ? null :
                                 (x == null || x.Attribute("Name").Value != type.Name ? null : x.Attribute("Description")?.Value) ??
                                 (!assembly.IsDefault ? null : DescriptionManager.DefaultTypeDescription(type));

            var xMembers = x == null ? null : x.Elements("Member")
                           .Select(m => KVP.Create(m.Attribute("Name").Value, m.Attribute("Description").Value))
                           .Distinct(m => m.Key)
                           .ToDictionary();

            LocalizedType result = new LocalizedType
            {
                Type     = type,
                Options  = opts,
                Assembly = assembly,

                Description       = description,
                PluralDescription = !opts.IsSetAssert(DescriptionOptions.PluralDescription, type) ? null :
                                    ((x == null || x.Attribute("Name").Value != type.Name ? null : x.Attribute("PluralDescription")?.Value) ??
                                     (!assembly.IsDefault ? null : type.GetCustomAttribute <PluralDescriptionAttribute>()?.PluralDescription) ??
                                     (description == null ? null : NaturalLanguageTools.Pluralize(description, assembly.Culture))),

                Gender = !opts.IsSetAssert(DescriptionOptions.Gender, type) ? null :
                         ((x == null ? null : x.Attribute("Gender")?.Value.Single()) ??
                          (!assembly.IsDefault ? null : type.GetCustomAttribute <GenderAttribute>()?.Gender) ??
                          (description == null ? null : NaturalLanguageTools.GetGender(description, assembly.Culture))),

                Members = !opts.IsSetAssert(DescriptionOptions.Members, type) ? null :
                          (from m in GetMembers(type)
                           where DescriptionManager.OnShouldLocalizeMember(m)
                           let value = xMembers?.TryGetC(m.Name) ?? (!assembly.IsDefault ? null : DescriptionManager.DefaultMemberDescription(m))
                                       where value != null
                                       select KVP.Create(m.Name, value))
                          .ToDictionary()
            };

            return(result);
        }
Esempio n. 4
0
        public static LocalizedAssembly FromXml(Assembly assembly, CultureInfo cultureInfo, XDocument doc, Dictionary <string, string> replacements /*new -> old*/)
        {
            Dictionary <string, XElement> file = doc == null ? null : doc.Element("Translations").Elements("Type")
                                                 .Select(x => KVP.Create(x.Attribute("Name").Value, x))
                                                 .Distinct(x => x.Key)
                                                 .ToDictionary();

            var result = new LocalizedAssembly
            {
                Assembly  = assembly,
                Culture   = cultureInfo,
                IsDefault = GetDefaultAssemblyCulture(assembly) == cultureInfo.Name
            };

            result.Types = (from t in assembly.GetTypes()
                            let opts = GetDescriptionOptions(t)
                                       where opts != DescriptionOptions.None
                                       let x = file?.TryGetC(replacements?.TryGetC(t.Name) ?? t.Name)
                                               select LocalizedType.ImportXml(t, opts, result, x))
                           .ToDictionary(lt => lt.Type);

            return(result);
        }