@@ -3,23 +3,28 @@ import _ from "lodash"
3
3
import settings from "@/settings"
4
4
import { lemgramToHtml , regescape , saldoToHtml } from "@/util"
5
5
import { locAttribute } from "@/i18n"
6
+ import { Token } from "@/backend/kwic-proxy"
7
+ import { Attribute } from "@/settings/config.types"
8
+ import { JQueryStaticExtended } from "@/jquery.types"
6
9
7
- let customFunctions = { }
10
+ type Stringifier = ( tokens : string [ ] , ignoreCase ?: boolean ) => string
11
+
12
+ let customFunctions : Record < string , Stringifier > = { }
8
13
9
14
try {
10
15
customFunctions = require ( "custom/statistics.js" ) . default
11
16
} catch ( error ) {
12
17
console . log ( "No module for statistics functions available" )
13
18
}
14
19
15
- export function getCqp ( hitValues , ignoreCase ) {
20
+ export function getCqp ( hitValues : Token [ ] , ignoreCase : boolean ) : string {
16
21
const positionalAttributes = [ "word" , ...Object . keys ( settings . corpusListing . getCurrentAttributes ( ) ) ]
17
22
let hasPositionalAttributes = false
18
23
19
- var tokens = [ ]
24
+ var tokens : string [ ] = [ ]
20
25
for ( var i = 0 ; i < hitValues . length ; i ++ ) {
21
26
var token = hitValues [ i ]
22
- var andExpr = [ ]
27
+ var andExpr : string [ ] = [ ]
23
28
for ( var attribute in token ) {
24
29
if ( token . hasOwnProperty ( attribute ) ) {
25
30
var values = token [ attribute ]
@@ -39,11 +44,11 @@ export function getCqp(hitValues, ignoreCase) {
39
44
return `<match> ${ tokens . join ( " " ) } </match>`
40
45
}
41
46
42
- function reduceCqp ( type , tokens , ignoreCase ) {
47
+ function reduceCqp ( type : string , tokens : string [ ] , ignoreCase : boolean ) : string {
43
48
let attrs = settings . corpusListing . getCurrentAttributes ( )
44
49
if ( attrs [ type ] && attrs [ type ] . stats_cqp ) {
45
50
// A stats_cqp function should call regescape for the value as appropriate
46
- return customFunctions [ attrs [ type ] . stats_cqp ] ( tokens , ignoreCase )
51
+ return customFunctions [ attrs [ type ] . stats_cqp ! ] ( tokens , ignoreCase )
47
52
}
48
53
tokens = _ . map ( tokens , ( val ) => regescape ( val ) )
49
54
switch ( type ) {
@@ -55,11 +60,11 @@ function reduceCqp(type, tokens, ignoreCase) {
55
60
case "sense" :
56
61
case "transformer-neighbour" :
57
62
if ( tokens [ 0 ] === "" ) return "ambiguity(" + type + ") = 0"
58
- else var res
63
+ let res : string
59
64
if ( tokens . length > 1 ) {
60
65
var key = tokens [ 0 ] . split ( ":" ) [ 0 ]
61
66
62
- var variants = [ ]
67
+ const variants : string [ ] [ ] = [ ]
63
68
_ . map ( tokens , function ( val ) {
64
69
const parts = val . split ( ":" )
65
70
if ( variants . length == 0 ) {
@@ -68,15 +73,12 @@ function reduceCqp(type, tokens, ignoreCase) {
68
73
for ( var idx = 1 ; idx < parts . length ; idx ++ ) variants [ idx - 1 ] . push ( parts [ idx ] )
69
74
} )
70
75
71
- variants = _ . map ( variants , function ( variant ) {
72
- return ":(" + variant . join ( "|" ) + ")"
73
- } )
74
-
75
- res = key + variants . join ( "" )
76
+ const variantsJoined = variants . map ( ( variant ) => ":(" + variant . join ( "|" ) + ")" )
77
+ res = key + variantsJoined . join ( "" )
76
78
} else {
77
79
res = tokens [ 0 ]
78
80
}
79
- return type + " contains '" + res + "'"
81
+ return ` ${ type } contains '${ res } '`
80
82
case "word" :
81
83
let s = 'word="' + tokens [ 0 ] + '"'
82
84
if ( ignoreCase ) s = s + " %c"
@@ -101,11 +103,11 @@ function reduceCqp(type, tokens, ignoreCase) {
101
103
}
102
104
103
105
// Get the html (no linking) representation of the result for the statistics table
104
- export function reduceStringify ( type , values , structAttributes ) {
106
+ export function reduceStringify ( type : string , values : string [ ] , structAttributes : Attribute ) : string {
105
107
let attrs = settings . corpusListing . getCurrentAttributes ( )
106
108
107
109
if ( attrs [ type ] && attrs [ type ] . stats_stringify ) {
108
- return customFunctions [ attrs [ type ] . stats_stringify ] ( values )
110
+ return customFunctions [ attrs [ type ] . stats_stringify ! ] ( values )
109
111
}
110
112
111
113
switch ( type ) {
@@ -123,15 +125,16 @@ export function reduceStringify(type, values, structAttributes) {
123
125
case "lex" :
124
126
case "lemma" :
125
127
case "sense" :
128
+ let stringify : ( value : string , appendIndex ?: boolean ) => string
126
129
if ( type == "saldo" || type == "sense" ) {
127
- var stringify = saldoToHtml
130
+ stringify = saldoToHtml
128
131
} else if ( type == "lemma" ) {
129
132
stringify = ( lemma ) => lemma . replace ( / _ / g, " " )
130
133
} else {
131
134
stringify = lemgramToHtml
132
135
}
133
136
134
- var html = _ . map ( values , function ( token ) {
137
+ const html = _ . map ( values , function ( token ) {
135
138
if ( token === "" ) return "–"
136
139
return stringify ( token . replace ( / : .* / g, "" ) , true )
137
140
} )
@@ -148,7 +151,7 @@ export function reduceStringify(type, values, structAttributes) {
148
151
return output
149
152
case "msd_orig" : // TODO: OMG this is corpus specific, move out to config ASAP (ASU corpus)
150
153
var output = _ . map ( values , function ( token ) {
151
- return $ ( "<span>" ) . text ( token ) . outerHTML ( )
154
+ return ( $ ( "<span>" ) . text ( token ) as any ) . outerHTML ( )
152
155
} ) . join ( " " )
153
156
return output
154
157
default :
0 commit comments