I have an incoming filename and I want to be able to glean and pass along helpful information to the user.
The filename will come through as something resembling: XYZStatsSyllabus2023.docx
I’m trying to analyze incoming filenames to figure out 1) what class it might belong to and 2) what document type it may be.
I have a JSON file with just about 150 classes, each with a short name (ex: Stats) as the key, a long name (ex: Statistics), a course code (ex: MT105), and an acronym (ex: STAT). The JSON information is as shown below.
{
"Course":{
"Stats":{
"name": "Statistics",
"code": "MT105",
"acronym": "STAT"
},
"Lit":{
"name": "Literature",
"code": "EN102",
"acronym" : "LITR"
},
"Hist":{
"name": "History",
"code": "HT101",
"acronym": "HIST"
},
"Gov":{
"name": "Government",
"code": "PS101",
"acronym": "GOVT"
}
}
}
The information contained in the filename will always contain one of the three entries. I want to be able to regex the filename to see if it matches any of the name, code, or acronym and if it does, then I want to be able to pass to the user the key of the total course information. If a filename contains MT105, I want to tell the user that it belongs to “Stats”, if it contains “History”, I want to return “Hist”.
I have experience iterating through an object like “Course” and then picking a match and iterating through that, but how would I go about matching a string to JSON when I essentially need to iterate through “Course” and then I’d have to iterate through its children and if I find a match, then I return the key value. Surely there has to be a better way?
I have tried iterating through “Course” and then through each child’s children. It is a very round about way to go about it and it doesn’t always work.