From 1994556b7784e890e9a507662dd3abba7940073a Mon Sep 17 00:00:00 2001 From: Arild Matsson Date: Mon, 21 Oct 2024 16:08:20 +0200 Subject: [PATCH] refactor: use native fetch in Karp client --- CHANGELOG.md | 2 +- app/scripts/backend/lexicons.ts | 17 ++++----- app/scripts/karp.ts | 45 ++++++++++++++++++++++++ app/scripts/karp/service.ts | 62 --------------------------------- 4 files changed, 53 insertions(+), 73 deletions(-) create mode 100644 app/scripts/karp.ts delete mode 100644 app/scripts/karp/service.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index 74505040..e66d41da 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,7 +13,7 @@ - Font is now a dependency, not checked-in files (and the font looks slightly different) - New loading spinners in result tabs - Undo override of Tailwind classname separator for Pug [#376](https://github.com/spraakbanken/korp-frontend/issues/376) -- Extracted Karp backend usage into `app/scripts/karp/service.ts` +- Extracted Karp backend usage into `app/scripts/karp.ts` - `stringifyFunc(key)` was renamed to `getStringifier(key)` - `stringify(key, x)` was removed, use `getStringifier(key)(x)` instead diff --git a/app/scripts/backend/lexicons.ts b/app/scripts/backend/lexicons.ts index 99959e2b..b011e894 100644 --- a/app/scripts/backend/lexicons.ts +++ b/app/scripts/backend/lexicons.ts @@ -4,8 +4,7 @@ import angular, { IHttpService, IPromise } from "angular" import settings from "@/settings" import { getAuthorizationHeader } from "@/components/auth/auth" import { httpConfAddMethod } from "@/util" -import "@/karp/service" -import { KarpService } from "@/karp/service" +import { getLemgrams, getSenseId, getSenses, getSwefnFrame } from "@/karp" export type LexiconsService = { relatedWordSearch: (lemgram: string) => IPromise @@ -29,12 +28,10 @@ export type SenseResult = { sense: string; desc: string } angular.module("korpApp").factory("lexicons", [ "$http", - "karp", - function ($http: IHttpService, karp: KarpService): LexiconsService { + function ($http: IHttpService): LexiconsService { return { async getLemgrams(wf: string, resources: string[], corporaIDs: string[]) { - const lemgrams = await karp.getLemgrams(wf, resources).then(({ data }) => data.hits) - + const lemgrams = await getLemgrams(wf, resources).then((data) => data.hits) if (lemgrams.length == 0) return [] const counts = await $http( @@ -54,18 +51,18 @@ angular.module("korpApp").factory("lexicons", [ }, async getSenses(wf: string) { - const lemgrams = await karp.getLemgrams(wf, ["saldom"]).then(({ data }) => data.hits) + const lemgrams = await getLemgrams(wf, ["saldom"]).then((data) => data.hits) if (lemgrams.length == 0) return [] - const senses = await karp.getSenses(lemgrams).then(({ data }) => data.hits) + const senses = await getSenses(lemgrams).then((data) => data.hits) return senses.map(({ senseID, primary }) => ({ sense: senseID, desc: primary })) }, async relatedWordSearch(lemgram: string) { - const senses = await karp.getSenseId(lemgram).then(({ data }) => data.hits) + const senses = await getSenseId(lemgram).then((data) => data.hits) if (senses.length == 0) return [] - const frames = await karp.getSwefnFrame(senses).then(({ data }) => data.hits) + const frames = await getSwefnFrame(senses).then((data) => data.hits) return frames.map((entry) => ({ label: entry.swefnID, words: entry.LUs })) }, } diff --git a/app/scripts/karp.ts b/app/scripts/karp.ts new file mode 100644 index 00000000..3dc1c633 --- /dev/null +++ b/app/scripts/karp.ts @@ -0,0 +1,45 @@ +/** @format */ +const karpURL = "https://spraakbanken4.it.gu.se/karp/v7" + +export type KarpResponse = { + total: number + hits: T[] + distribution: Record +} + +export type SaldoEntry = { + senseID: string + primary: string + // There's more, but not used here +} + +export type SwefnEntry = { + swefnID: string + LUs: string[] + // There's more, but not used here +} + +/** Query for saldom resource to find all entries that have wf as a non-compound word form */ +const wfQuery = (wf: string) => + `inflectionTable(and(equals|writtenForm|${wf}||not(equals|msd|c||equals|msd|ci||equals|msd|cm||equals|msd|sms)))` + +/** Create a query condition for a field matching any of several values */ +const equals = (field: string, values: string[]) => + `or(${values.map((value) => `equals|${field}|${value}`).join("||")})` + +/** Query lexicons in the Karp API */ +async function query(lexicons: string[], q: string, path: string, params?: object) { + const url = `${karpURL}/query/${lexicons.join(",")}` + const response = await fetch(url + "?" + new URLSearchParams({ q, path, ...params })) + return (await response.json()) as KarpResponse +} + +export const getLemgrams = (wordForm: string, morphologies: string[]) => + query(morphologies, wfQuery(wordForm), "entry.lemgram", { size: 100 }) + +export const getSenseId = (lemgram: string) => query(["saldo"], `equals|lemgrams|${lemgram}`, "entry.senseID") + +export const getSenses = (lemgrams: string[]) => + query(["saldo"], equals("lemgrams", lemgrams), "entry", { size: 500 }) + +export const getSwefnFrame = (senses: string[]) => query(["swefn"], equals("LUs", senses), "entry") diff --git a/app/scripts/karp/service.ts b/app/scripts/karp/service.ts deleted file mode 100644 index 5ea6ad66..00000000 --- a/app/scripts/karp/service.ts +++ /dev/null @@ -1,62 +0,0 @@ -/** @format */ - -import angular, { IHttpPromise, IHttpService } from "angular" - -const karpURL = "https://spraakbanken4.it.gu.se/karp/v7" - -export type KarpService = { - getLemgrams: (wordForm: string, morphologies: string[]) => IHttpPromise> - getSenseId(lemgram: string): IHttpPromise> - getSenses(lemgrams: string[]): IHttpPromise> - getSwefnFrame(senses: string[]): IHttpPromise> -} - -export type KarpResponse = { - total: number - hits: T[] - distribution: Record -} - -export type SaldoEntry = { - senseID: string - primary: string - // There's more, but not used here -} - -export type SwefnEntry = { - swefnID: string - LUs: string[] - // There's more, but not used here -} - -/** Query for saldom resource to find all entries that have wf as a non-compound word form */ -const wfQuery = (wf: string) => - `inflectionTable(and(equals|writtenForm|${wf}||not(equals|msd|c||equals|msd|ci||equals|msd|cm||equals|msd|sms)))` - -/** Create a query condition for a field matching any of several values */ -const equals = (field: string, values: string[]) => - `or(${values.map((value) => `equals|${field}|${value}`).join("||")})` - -angular.module("korpApp").factory("karp", [ - "$http", - function ($http: IHttpService): KarpService { - /** Query lexicons in the Karp API */ - const query = (lexicons: string[], q: string, path: string, params?: object) => - $http>({ - method: "GET", - url: `${karpURL}/query/${lexicons.join(",")}`, - params: { q, path, ...params }, - }) - - return { - getLemgrams: (wordForm, morphologies) => - query(morphologies, wfQuery(wordForm), "entry.lemgram", { size: 100 }), - - getSenseId: (lemgram) => query(["saldo"], `equals|lemgrams|${lemgram}`, "entry.senseID"), - - getSenses: (lemgrams) => query(["saldo"], equals("lemgrams", lemgrams), "entry", { size: 500 }), - - getSwefnFrame: (senses) => query(["swefn"], equals("LUs", senses), "entry"), - } - }, -])