Skip to content

Commit

Permalink
refactor: use native fetch in Karp client
Browse files Browse the repository at this point in the history
  • Loading branch information
arildm committed Oct 23, 2024
1 parent a0c7869 commit 1994556
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 73 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
17 changes: 7 additions & 10 deletions app/scripts/backend/lexicons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<LexiconsRelatedWordsResponse>
Expand All @@ -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<KorpLemgramCountResponse>(
Expand All @@ -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 }))
},
}
Expand Down
45 changes: 45 additions & 0 deletions app/scripts/karp.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/** @format */
const karpURL = "https://spraakbanken4.it.gu.se/karp/v7"

export type KarpResponse<T> = {
total: number
hits: T[]
distribution: Record<string, number>
}

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<T>(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<T>
}

export const getLemgrams = (wordForm: string, morphologies: string[]) =>
query<string>(morphologies, wfQuery(wordForm), "entry.lemgram", { size: 100 })

export const getSenseId = (lemgram: string) => query<string>(["saldo"], `equals|lemgrams|${lemgram}`, "entry.senseID")

export const getSenses = (lemgrams: string[]) =>
query<SaldoEntry>(["saldo"], equals("lemgrams", lemgrams), "entry", { size: 500 })

export const getSwefnFrame = (senses: string[]) => query<SwefnEntry>(["swefn"], equals("LUs", senses), "entry")
62 changes: 0 additions & 62 deletions app/scripts/karp/service.ts

This file was deleted.

0 comments on commit 1994556

Please sign in to comment.