Created
December 4, 2023 08:47
-
-
Save Cruxial0/ec843e31bdd1dbb318e1c403bea1c034 to your computer and use it in GitHub Desktop.
A basic file-specific parser used to parse baby names. Used for my latest game "Our Consequences".
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Diagnostics; | |
using System.Net; | |
using Microsoft.VisualBasic.FileIO; | |
using Newtonsoft.Json; | |
static void Main() { | |
Stopwatch sw = new Stopwatch(); | |
sw.Start(); | |
var url = "https://raw.githubusercontent.com/hadley/data-baby-names/master/baby-names.csv"; | |
var outPath = "/Users/cruxial/Documents/School/GL3/OurConsequences/Assets/Resources/Names.json"; | |
using var client = new WebClient(); | |
var data = client.DownloadString(url); | |
StringReader sr = new StringReader(data); | |
List<NameData> nameData = new List<NameData>(); | |
using (TextFieldParser parser = new TextFieldParser(sr)) | |
{ | |
parser.TextFieldType = FieldType.Delimited; | |
parser.SetDelimiters(","); | |
while (!parser.EndOfData) | |
{ | |
//Process row | |
string[] fields = parser.ReadFields(); | |
var currName = ""; | |
for (int i = 0; i < fields.Length; i++) { | |
if (i % 2 == 0 && i % 4 != 0) { | |
if (fields[i].Contains('e')) i++; | |
continue; | |
} | |
else { | |
if (int.TryParse(fields[i], out int e)) { | |
if (e < 1920) { | |
i += 4; | |
continue; | |
} | |
} | |
} | |
if(i % 4 == 0) continue; | |
if (fields[i] == "boy" || fields[i] == "girl") { | |
nameData.Add(new NameData(currName, fields[i] == "boy" ? Gender.Male : Gender.Female)); | |
continue; | |
} | |
currName = fields[i]; | |
} | |
} | |
nameData = nameData.DistinctBy(x => x.Name).ToList(); | |
var json = JsonConvert.SerializeObject(nameData, Formatting.Indented); | |
File.WriteAllText(outPath, json); | |
} | |
Console.WriteLine($"Time elapsed: {sw.ElapsedMilliseconds}ms.\n" + | |
$"Processed {nameData.Count} names (male: {nameData.Count(x => x.Gender == Gender.Male)}, female: {nameData.Count(x => x.Gender == Gender.Female)})"); | |
sw.Stop(); | |
} | |
Main(); | |
public class NameData | |
{ | |
public string Name; | |
public Gender Gender; | |
public NameData(string name, Gender gender) { | |
this.Name = name; | |
this.Gender = gender; | |
} | |
} | |
public enum Gender | |
{ | |
None, | |
Male, | |
Female | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment