private IEnumerable DoCreateGramms(Contact contact) { string[] types = contact.ContactType == ContactTypesAttribute.BAccountProperty ? AccountTypes : ContactTypes; foreach (string validationType in types) { PXCache cache = graph.Caches[contact.GetType()]; PXCache lCache = graph.Caches[typeof(Location)]; Location defLocation = null; if (contact.BAccountID != null) { BAccount bAccount = PXSelect <BAccount, Where <BAccount.bAccountID, Equal <Required <Contact.bAccountID> > > > .Select(graph, contact.BAccountID); if (bAccount != null && bAccount.DefLocationID != null) { defLocation = PXSelect <Location, Where <Location.bAccountID, Equal <Required <Location.bAccountID> >, And <Location.locationID, Equal <Required <BAccount.defLocationID> > > > > .Select(graph, bAccount.BAccountID, bAccount.DefLocationID); } } string type = validationType; // TODO: remove duplicate? if (!Definition.TypeRules.ContainsKey(validationType)) { continue; } decimal total = 0, totalZero = 0; foreach (var rule in Definition.TypeRules[type]) { if (type == ValidationTypesAttribute.Account && defLocation != null) { if (lCache.GetValue(defLocation, rule.MatchingField) == null && cache.GetValue(contact, rule.MatchingField) == null) { totalZero += rule.ScoreWeight.GetValueOrDefault(); } else { total += rule.ScoreWeight.GetValueOrDefault(); } } else { if (cache.GetValue(contact, rule.MatchingField) == null) { totalZero += rule.ScoreWeight.GetValueOrDefault(); } else { total += rule.ScoreWeight.GetValueOrDefault(); } } } if (total == 0) { continue; } foreach (CRValidationRules rule in Definition.TypeRules[type]) { string fieldName = rule.MatchingField; string transformRule = rule.TransformationRule; Decimal sw = rule.ScoreWeight ?? 0; if (sw == 0) { continue; } if (sw > 0 && totalZero > 0) { sw += totalZero * (sw / total); } var value = cache.GetValue(contact, fieldName); if (type == ValidationTypesAttribute.Account && value == null) { value = lCache.GetValue(defLocation, fieldName); } if (value == null) { continue; } string stringValue = value.ToString().ToLower(); if (transformRule.Equals(TransformationRulesAttribute.SplitWords)) { int? id = contact.ContactID; string[] words = stringValue.Split(); foreach (string word in words) { Decimal score = Decimal.Round(sw / words.Length, 4); if (score <= 0) { continue; } CRGrams gram = new CRGrams(); gram.EntityID = id; gram.ValidationType = validationType; gram.FieldName = fieldName; gram.FieldValue = word; gram.Score = score; yield return(gram); } } else { if (transformRule.Equals(TransformationRulesAttribute.DomainName)) { if (stringValue.Contains('@')) { stringValue = stringValue.Segment('@', 1); } else { try { stringValue = new UriBuilder(stringValue).Host; int index = stringValue.IndexOf('.'); string firstSegment = index < 0 ? stringValue : stringValue.Substring(0, index); if (firstSegment.Equals("www")) { stringValue = stringValue.Substring(index + 1); } } catch (UriFormatException) { //Do nothing } } } CRGrams gram = new CRGrams(); gram.FieldValue = stringValue; gram.EntityID = contact.ContactID; gram.ValidationType = validationType; gram.FieldName = fieldName; gram.Score = Decimal.Round(sw, 4); yield return(gram); } } } }
private IEnumerable DoCreateGramms(Contact contact) { string[] types = contact.ContactType == ContactTypesAttribute.BAccountProperty ? AccountTypes : ContactTypes; foreach (string validationType in types) { PXCache cache = graph.Caches[typeof(Contact)]; string type = validationType; decimal total = 0, totalZero = 0; foreach (var rule in Definition.TypeRules[type]) { if (cache.GetValue(contact, rule.MatchingField) == null) { totalZero += rule.ScoreWeight.GetValueOrDefault(); } else { total += rule.ScoreWeight.GetValueOrDefault(); } } if (total == 0) { continue; } foreach (CRValidationRules rule in Definition.TypeRules[type]) { string fieldName = rule.MatchingField; string transformRule = rule.TransformationRule; Decimal sw = rule.ScoreWeight ?? 0; if (sw == 0) { continue; } if (sw > 0 && totalZero > 0) { sw += totalZero * (sw / total); } var value = cache.GetValue(contact, fieldName); if (value == null) { continue; } if (transformRule.Equals(TransformationRulesAttribute.SplitWords)) { int?id = contact.ContactID; value = value.ToString().ToLower(); string[] words = value.ToString().Split(); foreach (string word in words) { Decimal score = Decimal.Round(sw / words.Length, 4); if (score <= 0) { continue; } CRGrams gram = new CRGrams(); gram.EntityID = id; gram.ValidationType = validationType; gram.FieldName = fieldName; gram.FieldValue = word; gram.Score = score; yield return(gram); } } else { value = value.ToString().ToLower(); if (transformRule.Equals(TransformationRulesAttribute.DomainName)) { if (value.ToString().Contains('@')) { value = value.ToString().Split('@')[1]; } else { try { value = new UriBuilder(value.ToString()).Host; if (value.ToString().Split('.')[0].Equals("www")) { value = value.ToString().Substring(value.ToString().IndexOf('.') + 1); } } catch (UriFormatException) { //Do nothing } } } CRGrams gram = new CRGrams(); gram.FieldValue = value.ToString(); gram.EntityID = contact.ContactID; gram.ValidationType = validationType; gram.FieldName = fieldName; gram.Score = Decimal.Round(sw, 4); yield return(gram); } } } }