-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from helmisatria/feat/support-gephi-format
feat: support gephi format
- Loading branch information
Showing
8 changed files
with
115 additions
and
35 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
#!/usr/bin/env node | ||
|
||
import * as fs from "fs"; | ||
import * as Papa from "papaparse"; | ||
import { program } from "commander"; | ||
import { pick } from "lodash"; | ||
|
||
interface InputRow { | ||
username: string; | ||
in_reply_to_screen_name: string; | ||
} | ||
|
||
interface OutputRow { | ||
source: string; | ||
target: string; | ||
} | ||
|
||
function readCSV(filePath: string): Promise<InputRow[]> { | ||
return new Promise((resolve, reject) => { | ||
const fileContent = fs.readFileSync(filePath, "utf8"); | ||
Papa.parse(fileContent, { | ||
header: true, | ||
complete: (result) => { | ||
const data = (result.data.map((d) => pick(d, ["username", "in_reply_to_screen_name"])) as InputRow[]).filter( | ||
(d) => d.username || d.in_reply_to_screen_name | ||
); | ||
resolve(data); | ||
}, | ||
error: (error) => reject(error), | ||
}); | ||
}); | ||
} | ||
|
||
function writeCSV(filePath: string, data: OutputRow[]): void { | ||
const csv = Papa.unparse(data, { | ||
columns: ["source", "target"], | ||
delimiter: ",", | ||
header: true, | ||
quotes: true, | ||
}); | ||
fs.writeFileSync(filePath, csv, "utf8"); | ||
console.log(`CSV file was written successfully to ${filePath}`); | ||
} | ||
|
||
async function transformCSV(inputFilePath: string, outputFilePath: string) { | ||
try { | ||
const inputData = await readCSV(inputFilePath); | ||
const outputData: OutputRow[] = inputData.map((row) => ({ | ||
source: row.username, | ||
target: row.in_reply_to_screen_name || "", | ||
})); | ||
|
||
writeCSV(outputFilePath, outputData); | ||
} catch (error) { | ||
console.error("Error processing CSV file:", error); | ||
} | ||
} | ||
|
||
program | ||
.requiredOption("-i, --input <path>", "Input CSV file path") | ||
.requiredOption("-o, --output <path>", "Output CSV file path"); | ||
|
||
program.parse(process.argv); | ||
|
||
const options = program.opts(); | ||
transformCSV(options.input, options.output); |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
import { defineConfig } from "tsup"; | ||
|
||
export default defineConfig({ | ||
entry: ["src/bin.ts"], | ||
entry: ["src/bin.ts", "src/scripts/convert-source-target.ts"], | ||
splitting: true, | ||
}); |