-
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.
Replace lexicons service with plain async functions
- Loading branch information
Showing
8 changed files
with
88 additions
and
104 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
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 |
---|---|---|
@@ -1,70 +1,46 @@ | ||
/** @format */ | ||
import _ from "lodash" | ||
import angular, { IHttpService, IPromise } from "angular" | ||
import settings from "@/settings" | ||
import { getAuthorizationHeader } from "@/components/auth/auth" | ||
import { httpConfAddMethod } from "@/util" | ||
import { getLemgrams, getSenseId, getSenses, getSwefnFrame } from "@/karp" | ||
import * as karp from "@/karp" | ||
import { korpRequest } from "./common" | ||
|
||
export type LexiconsService = { | ||
relatedWordSearch: (lemgram: string) => IPromise<LexiconsRelatedWordsResponse> | ||
getLemgrams: (wf: string, resources: string[], corporaIDs: string[]) => IPromise<LemgramCount[]> | ||
getSenses: (wf: string) => IPromise<SenseResult[]> | ||
} | ||
export type LexiconsRelatedWordsResponse = LexiconsRelatedWordsItem[] | ||
export type LexiconsRelatedWordsItem = { | ||
export type LexiconsRelatedWords = { | ||
label: string | ||
words: string[] | ||
} | ||
|
||
/** @see https://ws.spraakbanken.gu.se/docs/korp#tag/Statistics/paths/~1lemgram_count/get */ | ||
type KorpLemgramCountResponse = { | ||
time: number | ||
[lemgram: string]: number | ||
} | ||
|
||
export type LemgramCount = { lemgram: string; count: number } | ||
|
||
export type SenseResult = { sense: string; desc: string } | ||
|
||
angular.module("korpApp").factory("lexicons", [ | ||
"$http", | ||
function ($http: IHttpService): LexiconsService { | ||
return { | ||
async getLemgrams(wf: string, resources: string[], corporaIDs: string[]) { | ||
const lemgrams = await getLemgrams(wf, resources).then((data) => data.hits) | ||
if (lemgrams.length == 0) return [] | ||
/** Look up lemgrams matching a given wordform and count them in selected corpora. */ | ||
export async function getLemgrams(wf: string, resources: string[], corporaIDs: string[]): Promise<LemgramCount[]> { | ||
const lemgrams = (await karp.getLemgrams(wf, resources)).hits | ||
if (lemgrams.length == 0) return [] | ||
|
||
const counts = await $http<KorpLemgramCountResponse>( | ||
httpConfAddMethod({ | ||
url: settings["korp_backend_url"] + "/lemgram_count", | ||
method: "GET", | ||
params: { | ||
lemgram: lemgrams.join(","), | ||
count: "lemgram", | ||
corpus: corporaIDs.join(","), | ||
}, | ||
headers: getAuthorizationHeader(), | ||
}) | ||
).then(({ data }) => _.omit(data, "time")) | ||
const data = await korpRequest("lemgram_count", { | ||
lemgram: lemgrams.join(","), | ||
count: "lemgram", | ||
corpus: corporaIDs.join(","), | ||
}) | ||
const counts = _.omit(data, "time") | ||
|
||
return lemgrams.map((lemgram) => ({ lemgram, count: counts[lemgram] || 0 })) | ||
}, | ||
return lemgrams.map((lemgram) => ({ lemgram, count: counts[lemgram] || 0 })) | ||
} | ||
|
||
async getSenses(wf: string) { | ||
const lemgrams = await getLemgrams(wf, ["saldom"]).then((data) => data.hits) | ||
if (lemgrams.length == 0) return [] | ||
/** Look up SALDO senses of lemgrams of a given wordform. */ | ||
export async function getSenses(wf: string): Promise<SenseResult[]> { | ||
const lemgrams = (await karp.getLemgrams(wf, ["saldom"])).hits | ||
if (lemgrams.length == 0) return [] | ||
|
||
const senses = await getSenses(lemgrams).then((data) => data.hits) | ||
return senses.map(({ senseID, primary }) => ({ sense: senseID, desc: primary })) | ||
}, | ||
const senses = (await karp.getSenses(lemgrams)).hits | ||
return senses.map(({ senseID, primary }) => ({ sense: senseID, desc: primary })) | ||
} | ||
|
||
async relatedWordSearch(lemgram: string) { | ||
const senses = await getSenseId(lemgram).then((data) => data.hits) | ||
if (senses.length == 0) return [] | ||
/** Look up SweFN frames matching a given lemgram and get their other lexical units (LUs) */ | ||
export async function relatedWordSearch(lemgram: string): Promise<LexiconsRelatedWords[]> { | ||
const senses = (await karp.getSenseId(lemgram)).hits | ||
if (senses.length == 0) return [] | ||
|
||
const frames = await getSwefnFrame(senses).then((data) => data.hits) | ||
return frames.map((entry) => ({ label: entry.swefnID, words: entry.LUs })) | ||
}, | ||
} | ||
}, | ||
]) | ||
const frames = (await karp.getSwefnFrame(senses)).hits | ||
return frames.map((entry) => ({ label: entry.swefnID, words: entry.LUs })) | ||
} |
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,13 @@ | ||
/** @format */ | ||
/** @see https://ws.spraakbanken.gu.se/docs/korp#tag/Statistics/paths/~1lemgram_count/get */ | ||
|
||
export type LemgramCountParams = { | ||
lemgram: string | ||
count: "lemgram" | ||
corpus: string | ||
} | ||
|
||
export type LemgramCountResponse = { | ||
// The key type is given as more specific than `string` only to make TS distinguish it from "ERROR". | ||
[lemgram: string]: number | ||
} |
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