-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: use native fetch in Karp client
- Loading branch information
Showing
4 changed files
with
53 additions
and
73 deletions.
There are no files selected for viewing
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
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
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
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") |
This file was deleted.
Oops, something went wrong.