-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2fe20e1
commit 4039939
Showing
8 changed files
with
14,568 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
lib/output.json |
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,12 @@ | ||
include: package:lints/recommended.yaml | ||
|
||
linter: | ||
rules: | ||
annotate_overrides: true | ||
prefer_void_to_null: false | ||
|
||
analyzer: | ||
language: | ||
strict-casts: true | ||
strict-inference: true | ||
strict-raw-types: true |
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,130 @@ | ||
import 'dart:convert'; | ||
import 'dart:io'; | ||
|
||
import 'package:fpdart/fpdart.dart'; | ||
|
||
import './word.dart'; | ||
|
||
typedef Env = ({ | ||
String wordPairCsv, | ||
String wordsCsv, | ||
String outputPath, | ||
}); | ||
|
||
final wordPairs = Effect<Env, String, List<WordPair>>.gen(($) async { | ||
final env = $.sync(Effect.env()); | ||
final inputFile = File(env.wordPairCsv); | ||
|
||
final lines = inputFile.openRead().transform(utf8.decoder).transform( | ||
LineSplitter(), | ||
); | ||
|
||
final wordCollection = <WordPair>[]; | ||
await for (var line in lines) { | ||
final split = line.split(','); | ||
if (split.length != 3) { | ||
return $.sync(Effect.fail("Missing word-pair info at: '$line'")); | ||
} | ||
|
||
final wordInfo = $ | ||
.sync(Effect.all([ | ||
Effect.fromNullable( | ||
int.tryParse(split[0]), | ||
onNull: () => "Missing id collection", | ||
), | ||
Effect.fromNullable( | ||
int.tryParse(split[1]), | ||
onNull: () => "Missing id word1", | ||
), | ||
Effect.fromNullable( | ||
int.tryParse(split[2]), | ||
onNull: () => "Missing id word2", | ||
), | ||
])) | ||
.toList(); | ||
|
||
wordCollection.add(WordPair(wordInfo[0], wordInfo[1], wordInfo[2])); | ||
} | ||
|
||
return wordCollection; | ||
}); | ||
|
||
final words = Effect<Env, String, List<WordFull>>.gen(($) async { | ||
final env = $.sync(Effect.env()); | ||
final wordCollection = await $.async(wordPairs); | ||
|
||
final inputFile = File(env.wordsCsv); | ||
final lines = inputFile.openRead().transform(utf8.decoder).transform( | ||
LineSplitter(), | ||
); | ||
|
||
final wordMap = <int, String>{}; | ||
await for (var line in lines) { | ||
final split = line.split(','); | ||
if (split.length < 2) { | ||
return $.sync(Effect.fail("Missing word info at: '$line'")); | ||
} | ||
|
||
final idWord = $.sync(Effect.fromNullable( | ||
int.tryParse(split[0]), | ||
onNull: () => "Missing id word", | ||
)); | ||
final word = split[1]; | ||
|
||
wordMap[idWord] = word; | ||
} | ||
|
||
final wordFullList = <WordFull>[]; | ||
for (var entry in wordCollection) { | ||
final word1 = $.sync(Effect.fromNullable( | ||
wordMap[entry.idWord1], | ||
onNull: () => "Missing word 1 at: $entry", | ||
)); | ||
final word2 = $.sync(Effect.fromNullable( | ||
wordMap[entry.idWord2], | ||
onNull: () => "Missing word 2 at: $entry", | ||
)); | ||
|
||
wordFullList.add( | ||
WordFull( | ||
entry.idCollection, | ||
Word(entry.idWord1, word1), | ||
Word(entry.idWord2, word2), | ||
), | ||
); | ||
} | ||
|
||
return wordFullList.toSet().toList(); | ||
}); | ||
|
||
final program = Effect<Env, String, Unit>.gen(($) async { | ||
final env = $.sync(Effect.env()); | ||
final wordFullList = await $.async(words); | ||
|
||
final outputFile = File(env.outputPath); | ||
await $.async( | ||
Effect.tryCatch( | ||
execute: () => outputFile.writeAsString( | ||
"[${wordFullList.map((e) => e.toJson()).join(",\n")}]", | ||
), | ||
onError: (_, __) => "Error while writing output file", | ||
), | ||
); | ||
|
||
return unit; | ||
}); | ||
|
||
void main() async { | ||
await program.provideEnv(( | ||
wordPairCsv: "./lib/word_pairs.csv", | ||
wordsCsv: "./lib/words.csv", | ||
outputPath: "./lib/output.json", | ||
)).matchCause( | ||
onFailure: (cause) { | ||
print(cause); | ||
}, | ||
onSuccess: (_) { | ||
print("Success"); | ||
}, | ||
).runFutureExit(); | ||
} |
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,53 @@ | ||
import 'package:equatable/equatable.dart'; | ||
|
||
class WordPair { | ||
final int idCollection; | ||
final int idWord1; | ||
final int idWord2; | ||
const WordPair(this.idCollection, this.idWord1, this.idWord2); | ||
|
||
@override | ||
String toString() { | ||
return "($idCollection)$idWord1|$idWord2"; | ||
} | ||
} | ||
|
||
class Word extends Equatable { | ||
final int idWord; | ||
final String word; | ||
const Word(this.idWord, this.word); | ||
|
||
@override | ||
String toString() { | ||
return "($idWord)$word"; | ||
} | ||
|
||
@override | ||
List<Object?> get props => [idWord]; | ||
|
||
Map<String, dynamic> toJson() => { | ||
'"idWord"': idWord, | ||
'"word"': '"$word"', | ||
}; | ||
} | ||
|
||
class WordFull extends Equatable { | ||
final int idCollection; | ||
final Word word1; | ||
final Word word2; | ||
const WordFull(this.idCollection, this.word1, this.word2); | ||
|
||
@override | ||
String toString() { | ||
return "👉 $idCollection\n $word1\n $word2"; | ||
} | ||
|
||
@override | ||
List<Object?> get props => [word1, word2]; | ||
|
||
Map<String, dynamic> toJson() => { | ||
'"idCollection"': idCollection, | ||
'"word1"': word1.toJson(), | ||
'"word2"': word2.toJson(), | ||
}; | ||
} |
Oops, something went wrong.