-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfind_tarot
executable file
·255 lines (217 loc) · 5.55 KB
/
find_tarot
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
#! /usr/bin/env node
'use strict';
var fs = require('graceful-fs');
var _ = require('lodash');
var commander = require('commander');
// meant to be used like this:
// find 2016/03_March | grep log\.txt$ | xargs ./find_tarot
commander
.usage('[options] file1 file2\n\nTry it like this:\n find 2016/03_March | grep log\\.txt$ | xargs ./find_tarot --single')
.option('-s, --single', 'Include single (daily) readings')
.option('-m, --multiple', 'Include multi-card readings')
.option('-d, --detail', 'Show per-card and entry details')
.parse(process.argv);
var options = commander.opts();
if (!options.single && !options.multiple) {
commander.outputHelp();
process.exit(1);
}
var findSingleCard = /^ Card: (.*?)( - (.*))?$/m;
var cards = Object.create(null);
var makeLookupFromArray = function(array) {
return _.chain(array)
.map(function(value, index) {
return [value, index];
})
.fromPairs()
.value();
};
var suitR = /(\w+) of (\w+)/;
var suits = ['Major Arcana', 'Wands', 'Chalices', 'Swords', 'Pentacles'];
var suitOrderByName = makeLookupFromArray(suits);
var suitRanks = [
'Ace',
'One',
'Two',
'Three',
'Four',
'Five',
'Six',
'Seven',
'Eight',
'Nine',
'Ten',
'Knave',
'Knight',
'Queen',
'King'
];
var suitRankByName = makeLookupFromArray(suitRanks)
var majors = [
'The Fool',
'The Magician',
'The High Priestess',
'The Empress',
'The Emperor',
'The Hierophant',
'The Lovers',
'The Chariot',
'Justice',
'The Hermit',
'The Wheel',
'Strength',
'The Hanged Man',
'Death',
'Temperance',
'The Devil',
'The Tower',
'The Star',
'The Moon',
'The Sun',
'Judgement',
'The World'
];
var majorRankByName = makeLookupFromArray(majors);
var getSuit = function(name) {
var match = suitR.exec(name);
if (match) {
var suit = match[2];
var order = suitOrderByName[suit];
if (!_.isNumber(order)) {
throw new Error(`unrecognized suit: ${suit}`);
}
var rankName = match[1];
var rank = suitRankByName[rankName];
if (!_.isNumber(rank)) {
throw new Error(`unrecognized rank: ${rankName}`);
}
return {
name: suit,
order: order,
rankName: rankName,
rank: rank
}
}
var majorRank = majorRankByName[name];
if (!_.isNumber(majorRank)) {
throw new Error(`unrecognized major arcana: ${name}`);
}
return {
order: 0,
name: 'Major Arcana',
rank: majorRank
}
};
var addCard = function(name, path) {
if (!cards[name]) {
cards[name] = {
count: 0,
paths: [],
suit: getSuit(name)
};
}
var entry = cards[name];
entry.paths.push(path);
entry.count += 1;
};
var sections = [];
commander.args.map(function(path) {
var contents = fs.readFileSync(path).toString();
var findTarot = /(\[[X ]\] )?(.*[tT]arot.*)\n((?: .*\n|\n)+)/gm;
try {
var match;
var foundAny = false;
while (match = findTarot.exec(contents)) {
var found = false;
var title = match[2];
var entry = match[3];
if (options.single) {
var singleMatch = findSingleCard.exec(entry);
if (singleMatch) {
found = true;
sections.push(`${path} - single - ${title}`);
addCard(singleMatch[1], path);
}
}
if (options.multiple) {
var findMultipleCards = /^ [0-9]+\. (.*?)( - (.*))?$/gm;
var multipleMatch;
while (multipleMatch = findMultipleCards.exec(entry)) {
found = true;
sections.push(`${path} - multiple - ${title}`);
var description = multipleMatch[1];
var split = description.indexOf(' - ');
if (split < 0) {
split = description.length;
}
var card = description.slice(0, split);
addCard(card, path);
}
}
if (!found) {
sections.push(`${path} - no match - ${title}`);
}
if (found) {
foundAny = true;
}
}
if (!foundAny) {
sections.push(`${path} - no match in file!`);
}
}
catch(err) {
throw new Error(`problem with file ${path}: ${err.message}`);
}
});
var objectsArray = _.chain(cards)
.map(function(card, key) {
card.name = key;
if (card.suit.name == 'Major Arcana') {
card.name = `${card.name} (rank: ${card.suit.rank})`;
}
return card;
});
if (options.detail) {
console.log('** Sections found');
console.log(sections.join('\n'));
var objectsByFrequency = _.chain(objectsArray)
.orderBy(['count', 'suit.order', 'suit.rank'], ['desc', 'asc', 'asc'])
.value();
console.log('\n** Objects by Frequency');
console.log(JSON.stringify(objectsByFrequency, null, ' '));
console.log();
}
var namesByFrequency = _.chain(objectsArray)
.orderBy(['count', 'suit.order', 'suit.rank'], ['desc', 'asc', 'asc'])
.map(function(card) {
return card.count + ' ' + card.name;
})
.join('\n')
.value();
console.log('** By Frequency');
console.log(namesByFrequency);
var namesSorted = _.chain(objectsArray)
.sortBy(['suit.order','suit.rank'])
.map(function(card) {
return `${card.count} ${card.name}`;
})
.join('\n')
.value();
console.log('\n** By Name');
console.log(namesSorted);
var namesSorted = _.chain(objectsArray)
.groupBy(function(card) {
return card.suit.name;
})
.toPairs()
.sortBy('1.0.suit.order')
.map(function(pair) {
const name = pair[0];
const list = pair[1];
const total = _.reduce(list, (result, card) => result + card.count, 0);
return `${total} ${name} (${list.length} unique)`
})
.join('\n')
.value();
console.log('\n** By Suit');
console.log(namesSorted);