Beispiel #1
0
		/// <summary>
		/// 	мНПЛЮКХГСЕР МЮОХЯЮМХЕ ХЛЕМ
		/// </summary>
		/// <param name="candidate"> </param>
		/// <returns> </returns>
		public static string NormalizeFio(string candidate) {
			candidate = GetNormalizedFio(candidate).ToUpper();
			candidate = candidate.Replace('╗', 'е');
			candidate = candidate.Replace("юдхеб", "юдэеб");
			candidate = candidate.Replace("хебм", "эебм");
			candidate = candidate.Replace("хебхв", "эебхв");
			candidate = candidate.Replace("цемюд", "цеммюд");
			candidate = candidate.Replace("хмнйем", "хммнйем");

	
			
			if(candidate.StartsWith("йюрепхм")) {
				candidate = "е" + candidate;
			}
			candidate = candidate.Replace(" йюрепхм", " ейюрепхм");

			var stub = new FioStruct(candidate);
			if(!string.IsNullOrWhiteSpace(stub.LastName)) {
				stub.LastName = FixUp(stub.LastName);
			}
			if (!string.IsNullOrWhiteSpace(stub.Name))
			{
				stub.Name = FixUp(stub.Name);
			}
			if (!string.IsNullOrWhiteSpace(stub.AddName))
			{
				stub.AddName = FixUp(stub.AddName);
			}

			return stub.ToString(true,false,true);
		}
		/// <summary>
		/// 	Проверяет применимость правила
		/// </summary>
		/// <param name="str"> </param>
		/// <returns> </returns>
		public bool Match(FioStruct str) {
			var item = str.GetPart(Part);
			if (string.IsNullOrWhiteSpace(item)) {
				return false;
			}
			if(IsPrefix) {
				return string.IsNullOrWhiteSpace(Suffix) || item.StartsWith(Suffix);
			}else
				return string.IsNullOrWhiteSpace(Suffix) || item.EndsWith(Suffix);
		}
		/// <summary>
		/// 	Получить пол человека
		/// </summary>
		/// <param name="fioStruct"> </param>
		/// <param name="transformerOptions"> </param>
		/// <returns> </returns>
		public FioSex GetSex(FioStruct fioStruct, FioTransformerOptions transformerOptions) {
		    
		        if (null == fioStruct)
		        {
		            throw new ArgumentNullException("fioStruct");
		        }
                try
                {
		        if (fioStruct.Error != null)
		        {
		            return FioSex.Female;
		        }
		        if (!transformerOptions.AutoSex)
		        {
		            return transformerOptions.Sex;
		        }
		        if (!string.IsNullOrWhiteSpace(fioStruct.AddName))
		        {
		            if (fioStruct.AddName.EndsWith("НА"))
		            {
		                return FioSex.Female;
		            }
		            if (fioStruct.AddName.EndsWith("ВИЧ"))
		            {
		                return FioSex.Male;
		            }
		        }

		        if (_femaleLastEnds.Any(fioStruct.LastName.EndsWith))
		        {
		            return FioSex.Female;
		        }
		        if (_maleLastEnds.Any(fioStruct.LastName.EndsWith))
		        {
		            return FioSex.Male;
		        }
		        if (_femaleNameEnds.Any(fioStruct.Name.EndsWith))
		        {
		            return FioSex.Female;
		        }
		        return FioSex.Male;
		    }
		    catch (Exception e)
		    {
		        if (transformerOptions.IgnoreErrors) return FioSex.Error;
                
		        throw e;
		    }
		}
		/// <summary>
		/// 	Возвращает в указанном падеже
		/// </summary>
		/// <param name="fioStruct"> </param>
		/// <param name="transformerOptions"> </param>
		/// <param name="padezh"> </param>
		/// <returns> </returns>
		public FioStruct GetWithPadezh(FioStruct fioStruct, FioTransformerOptions transformerOptions, Padezh padezh) {
			Rules = Rules ?? FioTransformerRuleSet.Default;
			if (padezh == Padezh.Imenit) {
				return fioStruct;
			}
			
			var result = new FioStruct {Name = fioStruct.Name, LastName = fioStruct.LastName, AddName = fioStruct.AddName};
			var sex = GetSex(fioStruct, transformerOptions?? new FioTransformerOptions
			{
			    AutoSex = true,
			    AutoTranslate = true,
			});
			foreach (var p in new[] {FioStructPart.LastName, FioStructPart.Name, FioStructPart.AddName,}) {
				var rule =
					Rules.Where(
						x => (x.Sex == sex || x.Sex == FioSex.Any) && x.Part == p && (x.Padezh == padezh || x.Padezh == Padezh.Any)
						&&(!x.Rewriter)).
						FirstOrDefault(x => x.Match(result));
				if (null != rule) {
					rule.Apply(result);
				}
				var fixerrules =
					Rules.Where(
						x => (x.Sex == sex || x.Sex == FioSex.Any) && x.Part == p && (x.Padezh == padezh || x.Padezh == Padezh.Any)
						     && (x.Rewriter)).Where(x => x.Match(result));
				foreach (var fixerrule in fixerrules) {
					fixerrule.Apply(result);
				}

			}
			return result;
		}
		/// <summary>
		/// 	Получает все варианты написания ФИО c весами
		/// </summary>
		/// <param name="sourcefio"> </param>
		/// <param name="transformerOptions"> </param>
		/// <returns> </returns>
		public IEnumerable<FioVariant> GetVariants(FioStruct sourcefio, FioTransformerOptions transformerOptions = null) {
		    if (null != sourcefio.Error)
		    {
		        if (transformerOptions.IgnoreErrors)
		        {
		            yield return
		                new FioVariant("ОШИБКА [" + sourcefio.Name + "]", -1, Padezh.Imenit, false, false,
		                    false, false);
		        }
		        else
		        {
		            throw sourcefio.Error;
		        }
		    }
			transformerOptions = transformerOptions ?? FioTransformerOptions.Default;
			foreach (
				var padezh in new[] {Padezh.Imenit, Padezh.Rodit, Padezh.Datel, Padezh.Vinit, Padezh.Tvorit, Padezh.Predlozh}) {
				var transformedfio = GetWithPadezh(sourcefio, transformerOptions, padezh);
				if (transformerOptions.SelfLastNameWeight != 0) {
					yield return
						new FioVariant(transformedfio.LastName, transformerOptions.SelfLastNameWeight, padezh, true, false, false, true);
				}
				if (!string.IsNullOrWhiteSpace(transformedfio.AddName)) {
					yield return new FioVariant(transformedfio.ToString(true, false, true), 100, padezh, false, true, false, true);
					yield return new FioVariant(transformedfio.ToString(true, false, false), 100, padezh, false, true, false, false);

					yield return new FioVariant(transformedfio.ToString(true, true, true), 90, padezh, false, true, true, true);
					yield return new FioVariant(transformedfio.ToString(true, true, false), 90, padezh, false, true, true, false);
					if (transformerOptions.NotDotedAbbrevations) {
						yield return
							new FioVariant(transformedfio.ToString(true, true, true).Replace(".", ""), 90, padezh, false, true, true, true);
						yield return
							new FioVariant(transformedfio.ToString(true, true, false).Replace(".", ""), 90, padezh, false, true, true, false)
							;
					}
				}

				yield return new FioVariant(transformedfio.ToString(false, true, true), 70, padezh, false, false, true, true);
				yield return new FioVariant(transformedfio.ToString(false, true, false), 70, padezh, false, false, true, false);
				if (transformerOptions.NotDotedAbbrevations) {
					yield return
						new FioVariant(transformedfio.ToString(false, true, true).Replace(".", ""), 70, padezh, false, false, true, true);
					yield return
						new FioVariant(transformedfio.ToString(false, true, false).Replace(".", ""), 70, padezh, false, false, true, false)
						;
				}
				yield return new FioVariant(transformedfio.ToString(false, false, true), 80, padezh, false, false, false, true);
				yield return new FioVariant(transformedfio.ToString(false, false, false), 80, padezh, false, false, false, false);
			}
		}
		/// <summary>
		/// </summary>
		/// <param name="other"> </param>
		/// <returns> </returns>
		protected bool Equals(FioStruct other) {
			return string.Equals(Name, other.Name) && string.Equals(LastName, other.LastName) &&
			       string.Equals(AddName, other.AddName);
		}
		/// <summary>
		/// 	Применяет правило к целевой структуре
		/// </summary>
		/// <param name="str"> </param>
		public void Apply(FioStruct str) {
			var item = str.GetPart(Part);
			if (Type == FioTrasformerRuleType.Append) {
				item = item + Changer;
			}
			else {
				item = Regex.Replace(item, Suffix + "$", Changer, RegexOptions.Compiled);
			}
			str.SetPart(Part, item);
		}