forked from mooniak/textual-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjsonFormulate.py
46 lines (38 loc) · 1.5 KB
/
jsonFormulate.py
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
#
# jsonFormulate.py
#
# Copyright (c) 2015,
# Mooniak <[email protected]>
# Ayantha Randika <[email protected]>
# Improvements: https://github.com/mooniak/textual-tools
# Released under the GNU General Public License version 3 or later.
# See accompanying LICENSE file for details.
import json
class CharObj:
def __init__(self, character, count):
self.character = character
self.count = count
def json_formulate(counter):
ledgerSingle = []
ledgerDouble = []
ledgerTriple = []
for key, val in counter.ledgerSingle.items():
ledgerSingle.append(CharObj(key, val).__dict__)
for key, val in counter.ledgerDouble.items():
ledgerDouble.append(CharObj(key, val).__dict__)
for key, val in counter.ledgerTriple.items():
ledgerTriple.append(CharObj(key, val).__dict__)
ledgerSingle = sorted(ledgerSingle, key=lambda x: x['count'], reverse=True)
ledgerDouble = sorted(ledgerDouble, key=lambda x: x['count'], reverse=True)
ledgerTriple = sorted(ledgerTriple, key=lambda x: x['count'], reverse=True)
out = {}
out['ledgerSingle'] = ledgerSingle
out['ledgerDouble'] = ledgerDouble
out['ledgerTriple'] = ledgerTriple
out['charCount'] = counter.charCount
out['charCountNoWhiteSpace'] = counter.charCountNoWhiteSpace
out['wordCount'] = counter.wordCount
out['unicodeRange'] = counter.unicodeRange
out['name'] = counter.name
out['source'] = counter.source
return "textReport(" + json.dumps(out, ensure_ascii=False) + ")"