bchavez / Bogus

:card_index: A simple fake data generator for C#, F#, and VB.NET. Based on and ported from the famed faker.js.
Other
8.66k stars 495 forks source link

How to generate a middleName? #381

Closed bloodgang94 closed 3 years ago

bloodgang94 commented 3 years ago

How will the middle name be generated? I use the ru locale. In the class name I did not find a property

digitalcoyote commented 3 years ago

I don't believe the dataset has a middle name. I've been using a second FirstName instead. For my locale that works out well enough. Are Middle names in ru similar to first names?

On Wed, Jul 14, 2021, 12:42 AM bloodgang94 @.***> wrote:

How will the middle name be generated? I use the ru locale. In the class name I did not find a property

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/bchavez/Bogus/issues/381, or unsubscribe https://github.com/notifications/unsubscribe-auth/AEAWF7MCFROBR2DF4JHUWF3TXUPVDANCNFSM5AKXDXUA .

salixzs commented 3 years ago

If you do not need it to be 100% correct, you can reuse FirstName and if it is Fathers` name, you can modify rule to add "ич" or "ович" to the end, so "Иван" would become "Иванович".

.RuleFor(u => u.MiddleName, f => f.Person.FirstName() + "ович")

Then again if you want it gender safe - you need to build some more logic to add "овна" in case person gender is woman.

bloodgang94 commented 3 years ago

If you do not need it to be 100% correct, you can reuse FirstName and if it is Fathers` name, you can modify rule to add "ич" or "ович" to the end, so "Иван" would become "Иванович".

.RuleFor(u => u.MiddleName, f => f.Person.FirstName() + "ович")

Then again if you want it gender safe - you need to build some more logic to add "овна" in case person gender is woman.

Thank you. I need 100% correct reporting. Solved the problem like this : image

bchavez commented 3 years ago

It's super great to see the community help with these questions. Great work!

To add to the solutions in this issue; Bogus does contain an "ru" set of gendered middle names. However, the "ru" middle names are not exposed at a top level by C# APIs. The raw data can be found here: https://github.com/bchavez/Bogus/blob/9fc7614274f7f80b889151aa31a5fd2f2d0e610d/Source/Bogus/data/ru.locale.json#L1368-L1372

If you want to access this raw data in C#, you'll need to use lower-level APIs via the raw DB as shown below:

void Main()
{
   var f = new Faker("ru");
   Enumerable.Range(0, 5)
      .Select(_ => f.Name.GetRuMiddleName(Name.Gender.Male))
      .Dump();

   Enumerable.Range(0, 5)
      .Select(_ => f.Name.GetRuMiddleName(Name.Gender.Female))
      .Dump();
}

public static class RuNameExtensions
{
   public static string GetRuMiddleName(this Bogus.DataSets.Name name, Name.Gender gender)
   {
      BArray middleNameData;
      if( gender == Bogus.DataSets.Name.Gender.Male ){
         middleNameData = (BArray)Bogus.Database.Get("name", "male_middle_name", "ru");
      }
      else
      {
         middleNameData = (BArray)Bogus.Database.Get("name", "female_middle_name", "ru");
      }

      return name.Random.ArrayElement(middleNameData);
   }
}

image

I have no idea if this middle name data for the "ru" locale is correct, but it's there.