Skip to content

Commit 5dba40c

Browse files
committed
Added thematic area scores and rankings
1 parent bb3be19 commit 5dba40c

File tree

1 file changed

+39
-28
lines changed

1 file changed

+39
-28
lines changed

utils/generate-country-files.ts

Lines changed: 39 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,57 +2,55 @@ import fs from 'fs-extra'
22
import { parse } from 'csv-parse/sync'
33
import path from 'path'
44

5-
const file1Path = './files/source/GIRAI_2024_Edition_Data - Rankings and Scores.csv'
6-
const file2Path = './files/source/GIRAI_2024_regional_rankings - Regional rankings.csv'
7-
const file3Path = './files/source/GIRAI_2024_Edition_Data_Links_fix - frameworks.csv'
8-
const file4Path = './files/source/GIRAI_2024_Edition_Data_Links_fix - gov. actions.csv'
95
const outputDir = './files/output/'
106

117
type Row = Record<string, string>
128
type LinkEntry = { Title: string; URL: string }
139

14-
function mergeRows(row1?: Row, row2?: Row): Row {
15-
return { ...(row2 || {}), ...(row1 || {}) }
10+
function mergeRows(rankingsScores?: Row, regionalRankings?: Row): Row {
11+
return { ...(regionalRankings || {}), ...(rankingsScores || {}) }
1612
}
1713

1814
async function main() {
19-
const file1Content = await fs.readFile(file1Path, 'utf8')
20-
const file2Content = await fs.readFile(file2Path, 'utf8')
21-
const file3Content = await fs.readFile(file3Path, 'utf8')
22-
const file4Content = await fs.readFile(file4Path, 'utf8')
15+
const countryRankingsScoresContent = await fs.readFile('./files/source/Country_Rankings_Scores.csv', 'utf8')
16+
const countryRegionalRankingsContent = await fs.readFile('./files/source/Country_Regional_Rankings.csv', 'utf8')
17+
const countryFrameworksContent = await fs.readFile('./files/source/Country_Frameworks.csv', 'utf8')
18+
const countryGovActionsContent = await fs.readFile('./files/source/Country_Gov_Actions.csv', 'utf8')
19+
const countryThematicRankingContent = await fs.readFile('./files/source/Country_Thematic_Scores_Ranking.csv', 'utf8')
2320

24-
const records1: Row[] = parse(file1Content, { columns: true, skip_empty_lines: true })
25-
const records2: Row[] = parse(file2Content, { columns: true, skip_empty_lines: true })
26-
const records3: Row[] = parse(file3Content, { columns: true, skip_empty_lines: true })
27-
const records4: Row[] = parse(file4Content, { columns: true, skip_empty_lines: true })
21+
const records1: Row[] = parse(countryRankingsScoresContent, { columns: true, skip_empty_lines: true })
22+
const records2: Row[] = parse(countryRegionalRankingsContent, { columns: true, skip_empty_lines: true })
23+
const records3: Row[] = parse(countryFrameworksContent, { columns: true, skip_empty_lines: true })
24+
const records4: Row[] = parse(countryGovActionsContent, { columns: true, skip_empty_lines: true })
25+
const records5: Row[] = parse(countryThematicRankingContent, { columns: true, skip_empty_lines: true })
26+
27+
console.log(records5)
2828

2929
const countryMap = new Map<
3030
string,
3131
{
32-
row1?: Row
33-
row2?: Row
32+
rankingsScores?: Row
33+
regionalRankings?: Row
3434
frameworks?: LinkEntry[]
3535
govActions?: LinkEntry[]
36+
thematicData?: Row
3637
}
3738
>()
3839

39-
// Load file 1
4040
for (const row of records1) {
4141
const country = row['Country']
4242
if (country) {
43-
countryMap.set(country, { ...(countryMap.get(country) || {}), row1: row })
43+
countryMap.set(country, { ...(countryMap.get(country) || {}), rankingsScores: row })
4444
}
4545
}
4646

47-
// Load file 2
4847
for (const row of records2) {
4948
const country = row['Country']
5049
if (country) {
51-
countryMap.set(country, { ...(countryMap.get(country) || {}), row2: row })
50+
countryMap.set(country, { ...(countryMap.get(country) || {}), regionalRankings: row })
5251
}
5352
}
5453

55-
// Load file 3 (frameworks)
5654
for (const row of records3) {
5755
const country = row['Country']
5856
const title = row['Title']
@@ -65,7 +63,6 @@ async function main() {
6563
}
6664
}
6765

68-
// Load file 4 (government actions)
6966
for (const row of records4) {
7067
const country = row['Country']
7168
const title = row['Title']
@@ -74,19 +71,33 @@ async function main() {
7471
const current = countryMap.get(country) || {}
7572
const govActions = current.govActions || []
7673
govActions.push({ Title: title, URL: url })
77-
console.log(govActions)
7874
countryMap.set(country, { ...current, govActions })
7975
}
8076
}
8177

82-
await fs.ensureDir(outputDir)
78+
for (const row of records5) {
79+
console.log(row)
80+
const country = row['Country']
81+
if (country) {
82+
countryMap.set(country, { ...(countryMap.get(country) || {}), thematicData: row })
83+
}
84+
}
8385

84-
for (const [country, { row1, row2, frameworks, govActions }] of countryMap.entries()) {
85-
console.log(govActions)
86-
const merged = mergeRows(row1, row2)
86+
await fs.ensureDir(outputDir)
8787

88+
for (const [country, { rankingsScores, regionalRankings, frameworks, govActions, thematicData }] of countryMap.entries()) {
89+
const merged = mergeRows(rankingsScores, regionalRankings)
8890
const lines = Object.entries(merged).map(([key, value]) => `${key}: ${value}`)
8991

92+
if (thematicData) {
93+
lines.push('\nThematic Scores And Rankings:')
94+
for (const [key, value] of Object.entries(thematicData)) {
95+
if (key !== 'Country') {
96+
lines.push(`- ${key}: ${value}`)
97+
}
98+
}
99+
}
100+
90101
if (frameworks?.length) {
91102
lines.push('\nFrameworks:')
92103
for (const fw of frameworks) {
@@ -105,7 +116,7 @@ async function main() {
105116
const safeCountryName = country.replace(/[^a-z0-9]/gi, '_')
106117
const filePath = path.join(outputDir, `${safeCountryName}.txt`)
107118
await fs.writeFile(filePath, content)
108-
console.log('Generated:', filePath)
119+
//console.log('Generated:', filePath)
109120
}
110121
}
111122

0 commit comments

Comments
 (0)