Created
April 10, 2012 05:51
-
-
Save mahmoud/2348538 to your computer and use it in GitHub Desktop.
effing j. there's not even a syntax highlighter for it.
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
coli=: 4 : '({. x) i. < y' NB. {data w/header} coli {column name} => column index | |
col=: 4 : '}. (x coli y) {"1 x' NB. {data w/header} coli {column name} => boxed column data | |
colu=: 4 : '> x col y' NB. {data w/header} colu {column name} => unboxed column data | |
coln=: 4 : 'makenum x col y' NB. {data w/header} coli {column name} => numeric column data | |
colsel=: 4 : 'y , |: > x&col each y' NB. {data w/header} colsel {column name} [{column name} ...] | |
mean=: +/ % # | |
nps_avg=: 13 : 'mean y #~ +./ (i._11) ="0 _ y' | |
nps_n=: 13 : '# y #~ +./ (i._11) ="0 _ y' | |
nps_freqn=: 13 : '+/"1 (<:i._12) ="0 _ y' NB. nps_freqs {scores} => {freq counts (in <:i._12 order)} | |
nps_freqp=: (] % +/) @ nps_freqn | |
freqn2ppd=: 13 : '3 {. ''++??-------x'' +//. nps_freqp y' | |
freqn2nps=: 13 : '(+/y) %~ (+/(0 1{y)) - +/(4 5 6 7 8 9 10{y)' NB. {nps freqs in <:i._12 order} freqs2nps => {nps in [0,1]} | |
nps=: freqn2nps @ nps_freqn NB. nps {scores} => {nps in [0,100]} | |
NB. getyq=: 13 : '({. x) , (}. x) #~ *./"(1) y ="_ 1 > }. 0 1 {"1 x' NB. {ar|ur} getyq {year} {quarter} | |
getyq=: 13 : '({. x) , (}. x) #~ *./"(1) y ="_ 1 > }. (i.#y) {"1 x' NB. {ar|ur} getyq {year} [quarter] | |
json=: 3 : 0 NB. json {data} | |
if. 0 = #$y do. s=. (',';'""') makedsv y | |
elseif. 1 = #$y do. s=. '['&, ,&']' }: (',';'""') makedsv y | |
elseif. 2 = #$y do. | |
keys=. (dblquote @ ":) each {."1 y | |
vals=. ([: '['&, [: ,&']' [: }: (',';'""')&makedsv) each {:"1 y | |
s=. '{' , (> ((4 : '< (>x),'','',(>y)')/ keys (13 : '(< (>x) , '':'', (>y))')"0 vals)) , '}' | |
end. | |
s | |
) | |
json2=: 3 : 0 | |
if. 0 = #$y do. | |
if. 32 = 3!:0 y do. s=. json2 >y | |
elseif. 2 = 3!:0 y do. s=. '"',y,'"' | |
NB. elseif. 1 do. s=. ":y | |
elseif. 1 do. s=. }: makedsv y | |
end. | |
elseif. 1 = #$y do. | |
if. 32 = 3!:0 y do. s=. '[' , (> (4 : '<(>x),'','',(>y)')/ json2 each y) , ']' | |
elseif. 0 = #y do. s=. '""' NB. but what about [], null? | |
elseif. 1 do. | |
if. 2 = 3!:0 y do. s=. '"',y,'"' else. s=. '[' , (}: (',';'""') makedsv y) , ']' end. NB. doesn't escape specials | |
end. | |
elseif. 2 = #$y do. | |
keys=. json2 each <"0 {. y | |
vals=. json2 each <"0 {: y | |
s=. '{' , (> ((4 : '<(>x),'','',(>y)')/ keys (4 : '<(>x),'':'',(>y)')"0 vals)) , '}' | |
elseif. 1 do. s=. 'ERROR' | |
end. | |
) | |
jsdata_append=: 4 : 0 NB. {varname} jsdata_append {data} | |
s=. 'var ' , x , ' = ' , (json y) , ';' | |
s fappends 'Chartings\data.js' | |
'' | |
) | |
jsdata_append2=: 4 : 0 NB. {varname} jsdata_append {data} | |
s=. 'var ' , x , ' = ' , (json2 y) , ';' | |
s fappends 'Chartings\data.js' | |
'' | |
) | |
dblquote=: '"','"',~] | |
ppd_tbl_col=: 4 : 0 NB. {col label} ppd_tbl_col {year} {quarter} | |
'col_lbl yr'=. x;y | |
col_dat=. (ur getyq yr) colu col_lbl | |
t_ratings=. num2per each <"0 nps_freqp col_dat | |
t_nps=. num2per each <"0 nps col_dat | |
t_n=. < # col_dat | |
(":yr) ; t_ratings , t_nps , t_n | |
) | |
ppd_pie_segs=: 4 : 0 NB. {col label} ppd_pie_segs {year} {quarter} | |
'col_lbl yr'=. x;y | |
col_dat=. (ur getyq yr) colu col_lbl | |
freqn2ppd col_dat | |
) | |
ppd_bar_segs=: 4 : 0 NB. {col label} ppd_bar_segs {year} {quarter} | |
'col_lbl yr'=. x;y | |
col_dat=. (ur getyq yr) colu col_lbl | |
('year';'data';'n') ,: (<":y) , (<@freqn2ppd , <@#) col_dat | |
) | |
nps_bar_segs=: 4 : 0 NB. {col label} nps_bar_segs {year} {quarter} | |
'col_lbl yr'=. x;y | |
col_dat=. (ur getyq yr) colu col_lbl | |
('year';'data';'n') ,: (<":y) , (<@nps , <@#) col_dat | |
) | |
NB. num2per=: ,&'%' @ (0j2&":) @ (100&*) | |
num2per=: ,&'%' @ > @ ('0.2'&(8!:0)) @ (100&*) | |
scores_by=: 4 : 0 NB. {score col label} {key col label} scores_by {year} {quarter} | |
'scolbl keylbl'=. x | |
yr=. y | |
scodat=. (ur getyq yr) colu scolbl | |
keydat=. (ur getyq yr) col keylbl | |
grade=. /: keydat | |
scodat=. grade { scodat | |
keydat=. grade { keydat | |
('key_nub' ; < ~. keydat) , ('overall_avg' ; < nps_avg scodat) , ('key_avg' ; < keydat nps_avg/. scodat) , ('overall_nps' ; < nps scodat) , ('key_nps' ; < keydat nps/. scodat) ,: ('n' ; < keydat #/. scodat) | |
) | |
intersection=: ~. @ (e. # [)/ @ (] , {.) | |
trend_scores_by=: 4 : 0 NB. {score col label} {key col label} scores_by {{year} {quarter}) | |
'scolbl keylbl'=: x | |
yr=. y | |
scodat=. (ur getyq yr) colu scolbl | |
keydat=. (ur getyq yr) col keylbl | |
(< ~. keydat) , (<<"0 keydat nps_avg/. scodat) , (<<"0 keydat nps/. scodat) , (<<"0 keydat #/. scodat) | |
) | |
trend_scores_by_hack=: 3 : 0 " 1 | |
len=. #>{.y | |
exploded=. (i. len) (4 : '> x&{ each y')"0 _ y | |
([: < ('group';'avg';'nps';'n';'year') ,: ])"1 exploded | |
) | |
tblize_trend_scores_by=: 3 : 0 NB. tblize_trend_scores_by {data};{periods} | |
'data periods'=. y | |
universal_groups=. /:~ intersection >>{.each data | |
to_copy=. universal_groups&(e.~) each >{. each data | |
trimmed=. to_copy (4 : '(>x)&# each >y')"0 data | |
unlabeled=. 3 : '((/:@>@{.) y)&{ each y' " 1 trimmed | |
matrix=. unlabeled ,. <"1 |: ((# universal_groups),(# periods)) $ (": each periods) | |
, trend_scores_by_hack matrix | |
) | |
open_ended_non_pcs_tbl=: 4 : 0 NB. {e.g. ar} open_ended_non_pcs_tbl ({code column label};{cat col lbl};{pni};{desc}) | |
cr=. x colsel y | |
c=. (] #~ a: ~: {."1) }.cr | |
data=. \:~ ({."1 (<@# , (,@~.))/.]) c NB. with counts (sorted descending) | |
data=. data ,. <"0 (] % +/) (>@{."1) data NB. with percentages | |
('n';'code';'category';'pni';'desc';'percent') , data | |
) | |
headed_tbl_json_prep=: ({. ([: < ,:)"_ 1 }.) | |
open_ended_all_prep=: 3 : 0 NB. open_ended_all_prep {open_ended_non_pcs_tbl result} | |
rollup=. (([:<[:+/[:>{."1) , (3 4&{"1 @ {.) , ([:<[:+/[:>{:"1)) | |
data=. \:~ (3 4&{"1 (rollup)/. ]) }. y | |
('n';'pni';'desc';'percent') , data | |
) | |
open_ended_pcs_tbl=: 4 : 0 NB. {e.g. ar} open_ended_pcs_tbl ({pcs code column label};{pcs desc};{pni}) | |
cr=. x colsel y | |
c=. (] #~ a: ~: {."1) }.cr | |
data=. (0 _1&{"1 (<@# , (,@~.))/.]) c NB. with counts | |
data=. /:~ (1{"1 data) open_ended_pcs_rollup/. data NB. rolled up with percentages, sorted descending | |
('n';'pcs_code';'desc';'data') , data | |
) | |
open_ended_pcs_rollup=: 3 : 0 | |
code=. > (<0 1) { y | |
desc=. > (<0 2) { y | |
nlut=. 0 ,~ > {."1 y | |
pni=. {:"1 y | |
ns=. nlut {~ pni i. ('Positive';'Indifferent';'Negative') | |
nper=. ns % +/ ns | |
(+/ ns);code;desc;nper | |
) | |
pnirows=: 4 : 0 NB. ({ar};{ur}) pnirows ({unique id};{rating col};{comment};{code};{cat};{pni};{desc};{pcs_code};{pcs_desc}) | |
'all uniq'=. x | |
negrows=. (({. @ [) , (}. @ [) #~ (<'Negative') = [ col ]) NB. {ar} negrows {pni col} | |
r=. all negrows >5{y | |
r=. }. r {"1~ > r&coli each y | |
ratings=. <"0 (all colu >1{"1 y) {~ (all colu >{.y) i. (>{."1 r) | |
r=. ratings (1 } &. |:) r | |
('id';'rating';'comment';'code';'cat';'pni';'desc';'pcs_code';'pcs_desc') , r | |
) | |
negcnts=: (3 : '\:~ |: (~. (y col ''code'')) ,:~ (y col ''code'') (<@#)/. }.y') NB. negcnts {nr} | |
topneg=: (] {. {:"1@[) NB. {nc} topneg {n} | |
coderows=: 13 : '({. x) , (}. x) #~ y = x col ''code''' NB. {nr} coderows {code} | |
negoverall=: 4 : 0 NB. {nr} negoverall {code} | |
cr=. x coderows y | |
n=. <:#cr | |
desc=. {. cr col 'desc' | |
nps=. nps cr colu 'rating' | |
('n';'desc';'nps') ,: (n;(>desc);nps) | |
) | |
neg_ppd_tbl_col=: 4 : 0 NB. {neg row data} ppd_tbl_col {code} | |
'nr code'=. x;(<y) | |
col_dat=. (nr colu 'rating') #~ code = nr col 'code' | |
t_ratings=. num2per each <"0 nps_freqp col_dat | |
t_nps=. num2per each <"0 nps col_dat | |
t_n=. < # col_dat | |
tbl_data=. (":cyq) ; t_ratings , t_nps , t_n | |
('code';'data') ,: (code;<tbl_data) | |
) | |
neg_graphs=: 4 : 0 NB. {nr} neg_graphs {code} | |
select. y | |
case. 'G2' do. ,: < x neg_graph (y;'Branch Code';'Counts by Branch') | |
case. 'J2' do. ,: < x neg_graph (y;'pcs_desc';'Counts by Product/Channel/Service Code') | |
case. 'J1' do. ,: < x neg_graph (y;'pcs_desc';'Counts by Product/Channel/Service Code') | |
case. 'E1A' do. ,: < x neg_graph (y;'pcs_desc';'Counts by Product/Channel/Service Code') | |
case. 'A3' do. ,: < x neg_graph (y;'Branch Code';'Counts by Branch') | |
end. | |
) | |
neg_graph=: 4 : 0 NB. {nr} neg_graphs ({code};{col};{chart name}) | |
nr=: x | |
'code gp_col chart_name'=: y | |
cr=: ({. nr) , (}. nr) #~ (<code) = nr col 'code' | |
r=: ({. ycqcu) , (}. ycqcu) {~ (ycqcu colu 'Unique Customer ID') i. (cr colu 'id') | |
dat=: cr ,. r | |
('chart_name';chart_name) ,. (<'chart_data') , < headed_tbl_json_prep ('data';'label') , \:~ (<@# , {.)/.~ (dat col gp_col) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment