Skip to content

Commit

Permalink
refactor(ts): statistics_config
Browse files Browse the repository at this point in the history
  • Loading branch information
arildm committed Nov 7, 2024
1 parent 3dd8cc9 commit a85151d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 21 deletions.
41 changes: 22 additions & 19 deletions app/config/statistics_config.js → app/config/statistics_config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,28 @@ import _ from "lodash"
import settings from "@/settings"
import { lemgramToHtml, regescape, saldoToHtml } from "@/util"
import { locAttribute } from "@/i18n"
import { Token } from "@/backend/kwic-proxy"
import { Attribute } from "@/settings/config.types"
import { JQueryStaticExtended } from "@/jquery.types"

let customFunctions = {}
type Stringifier = (tokens: string[], ignoreCase?: boolean) => string

let customFunctions: Record<string, Stringifier> = {}

try {
customFunctions = require("custom/statistics.js").default
} catch (error) {
console.log("No module for statistics functions available")
}

export function getCqp(hitValues, ignoreCase) {
export function getCqp(hitValues: Token[], ignoreCase: boolean): string {
const positionalAttributes = ["word", ...Object.keys(settings.corpusListing.getCurrentAttributes())]
let hasPositionalAttributes = false

var tokens = []
var tokens: string[] = []
for (var i = 0; i < hitValues.length; i++) {
var token = hitValues[i]
var andExpr = []
var andExpr: string[] = []
for (var attribute in token) {
if (token.hasOwnProperty(attribute)) {
var values = token[attribute]
Expand All @@ -39,11 +44,11 @@ export function getCqp(hitValues, ignoreCase) {
return `<match> ${tokens.join(" ")} </match>`
}

function reduceCqp(type, tokens, ignoreCase) {
function reduceCqp(type: string, tokens: string[], ignoreCase: boolean): string {
let attrs = settings.corpusListing.getCurrentAttributes()
if (attrs[type] && attrs[type].stats_cqp) {
// A stats_cqp function should call regescape for the value as appropriate
return customFunctions[attrs[type].stats_cqp](tokens, ignoreCase)
return customFunctions[attrs[type].stats_cqp!](tokens, ignoreCase)
}
tokens = _.map(tokens, (val) => regescape(val))
switch (type) {
Expand All @@ -55,11 +60,11 @@ function reduceCqp(type, tokens, ignoreCase) {
case "sense":
case "transformer-neighbour":
if (tokens[0] === "") return "ambiguity(" + type + ") = 0"
else var res
let res: string
if (tokens.length > 1) {
var key = tokens[0].split(":")[0]

var variants = []
const variants: string[][] = []
_.map(tokens, function (val) {
const parts = val.split(":")
if (variants.length == 0) {
Expand All @@ -68,15 +73,12 @@ function reduceCqp(type, tokens, ignoreCase) {
for (var idx = 1; idx < parts.length; idx++) variants[idx - 1].push(parts[idx])
})

variants = _.map(variants, function (variant) {
return ":(" + variant.join("|") + ")"
})

res = key + variants.join("")
const variantsJoined = variants.map((variant) => ":(" + variant.join("|") + ")")
res = key + variantsJoined.join("")
} else {
res = tokens[0]
}
return type + " contains '" + res + "'"
return `${type} contains '${res}'`
case "word":
let s = 'word="' + tokens[0] + '"'
if (ignoreCase) s = s + " %c"
Expand All @@ -101,11 +103,11 @@ function reduceCqp(type, tokens, ignoreCase) {
}

// Get the html (no linking) representation of the result for the statistics table
export function reduceStringify(type, values, structAttributes) {
export function reduceStringify(type: string, values: string[], structAttributes: Attribute): string {
let attrs = settings.corpusListing.getCurrentAttributes()

if (attrs[type] && attrs[type].stats_stringify) {
return customFunctions[attrs[type].stats_stringify](values)
return customFunctions[attrs[type].stats_stringify!](values)
}

switch (type) {
Expand All @@ -123,15 +125,16 @@ export function reduceStringify(type, values, structAttributes) {
case "lex":
case "lemma":
case "sense":
let stringify: (value: string, appendIndex?: boolean) => string
if (type == "saldo" || type == "sense") {
var stringify = saldoToHtml
stringify = saldoToHtml
} else if (type == "lemma") {
stringify = (lemma) => lemma.replace(/_/g, " ")
} else {
stringify = lemgramToHtml
}

var html = _.map(values, function (token) {
const html = _.map(values, function (token) {
if (token === "") return "–"
return stringify(token.replace(/:.*/g, ""), true)
})
Expand All @@ -148,7 +151,7 @@ export function reduceStringify(type, values, structAttributes) {
return output
case "msd_orig": // TODO: OMG this is corpus specific, move out to config ASAP (ASU corpus)
var output = _.map(values, function (token) {
return $("<span>").text(token).outerHTML()
return ($("<span>").text(token) as any).outerHTML()
}).join(" ")
return output
default:
Expand Down
2 changes: 1 addition & 1 deletion app/scripts/components/statistics.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import CSV from "comma-separated-values/csv"
import settings from "@/settings"
import { html } from "@/util"
import { loc, locObj } from "@/i18n"
import { getCqp } from "../../config/statistics_config.js"
import { getCqp } from "../../config/statistics_config"
import { expandOperators } from "@/cqp_parser/cqp"
import { requestMapData } from "@/backend/backend"
import "@/backend/backend"
Expand Down
2 changes: 1 addition & 1 deletion app/scripts/statistics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const createStatisticsService = function () {
sortable: true,
formatter(row, cell, value, columnDef, dataContext) {
if (dataContext["rowId"] !== 0) {
const formattedValue = reduceStringify(reduceVal, dataContext[reduceVal!], attrObj[reduceVal!])
const formattedValue = reduceStringify(reduceVal!, dataContext[reduceVal!], attrObj[reduceVal!])
dataContext["formattedValue"][reduceVal] = formattedValue
return `<span class="statistics-link" data-row=${dataContext["rowId"]}>${formattedValue}</span>`
} else {
Expand Down

0 comments on commit a85151d

Please sign in to comment.