-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.dart
62 lines (54 loc) · 1.72 KB
/
main.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import 'src/huffman.dart';
import 'package:args/args.dart';
runHuffman(String message) {
Map<String, int> chars = char_frequency(message);
Map<String, double> pd = d_sort_map(prob_dist(chars));
List<Node> nodes = generateNodeFromMap(pd);
Node root = generateHuffmanTree(nodes);
Map<String, String> table = generateHuffmanTable(node: root);
Map<String, dynamic> encoded_message = compressor(table, message);
String original_message = decompressor(encoded_message);
print("table: ${encoded_message['table']}");
print("encoded message: ${encoded_message['message']}");
print("Retrived Message: ${original_message}");
var len_enc = encoded_message['message'].length;
var len_mess = message.length * 8; // Assume each char takes 8 bits
var comp_perctange = ((len_mess - len_enc) / len_mess) * 100;
print("-" * 30);
print("${dp(comp_perctange, places: 2)} % compressed ");
}
int main(List<String> args) {
String message;
var help = {
"message": "enter the message you want to compress",
"help": "-m: message [String] ",
"char_count": 'Enter character count. Format: {"word": count}',
"prob_dist":
'Enter probability distribution of chars. Format: {"word": count}'
};
var parser = ArgParser();
parser.addMultiOption(
'message',
abbr: 'm',
help: help['message'],
);
parser.addFlag(
'help',
abbr: 'h',
help: help['help'],
);
var argsResult = parser.parse(args);
if (argsResult['help']) {
print("""
Huffman Compression
--------------------
${parser.usage}
""");
} else if (argsResult['message'] != null) {
message = argsResult['message'][0];
runHuffman(message);
} else {
print("[Error] Incorrect Input Format");
}
return 0;
}