Skip to content

Commit 4cda994

Browse files
committed
Review fixes: defaultConfig, typo, use pathJoin...
1 parent ba015a0 commit 4cda994

File tree

9 files changed

+61
-107
lines changed

9 files changed

+61
-107
lines changed

src/api/analysis.ts

Lines changed: 6 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,7 @@
11
import { once } from "es-toolkit";
2-
import { ensureTrailingSlash } from "@/util";
2+
import { pathJoin } from "@/util";
33
import type { ByLang } from "@/util.types";
44

5-
// TODO Remove?
6-
export type AnnotationGroup =
7-
| "saldo"
8-
| "msd"
9-
| "syntax"
10-
| "readability"
11-
| "wsd"
12-
| "sensaldo"
13-
| "lexicalClasses"
14-
| "swener";
15-
16-
// TODO Remove?
17-
export const baseAnalyses = [
18-
"sbx-swe-sentence-sparv-storsuc",
19-
"sbx-swe-tokenization-sparv-betterword",
20-
];
21-
22-
// TODO Remove?
23-
export const analysisListing: Record<AnnotationGroup, string[]> = {
24-
saldo: [
25-
"sbx-swe-compound-sparv-saldolemgram",
26-
"sbx-swe-compound-sparv-saldowords",
27-
"sbx-swe-lemgram-sparv-saldo",
28-
"sbx-swe-lemmatization-sparv-saldo2",
29-
],
30-
msd: [
31-
"sbx-swe-msd-stanza-stanzamorph-suc3",
32-
"sbx-swe-msd-stanza-stanzamorph-ufeats",
33-
"sbx-swe-pos-stanza-stanzamorph",
34-
],
35-
syntax: ["sbx-swe-dependency-stanza-stanzasynt"],
36-
readability: [
37-
"sbx-swe-readability-sparv-lix",
38-
"sbx-swe-readability-sparv-nk",
39-
"sbx-swe-readability-sparv-ovix",
40-
],
41-
wsd: ["sbx-swe-sense-sparv"],
42-
sensaldo: ["sbx-swe-sentiment-sparv-sensaldo"],
43-
lexicalClasses: [
44-
"sbx-swe-lexical_classes_text-sparv-blingbring",
45-
"sbx-swe-lexical_classes_text-sparv-swefn",
46-
"sbx-swe-lexical_classes_token-sparv-blingbring",
47-
"sbx-swe-lexical_classes_token-sparv-swefn",
48-
],
49-
swener: ["sbx-swe-namedentity-swener", "sbx-swe-geotagcontext-sparv"],
50-
};
51-
525
export type AnalysisId = string;
536

547
/** Suggested annotation definitions for each known analysis id. */
@@ -103,13 +56,12 @@ export const annotationAnalyses: Readonly<Record<string, AnalysisId>> =
10356
),
10457
);
10558

106-
export const loadAnalysisMetdata = once(async () => {
107-
const urlRaw = import.meta.env.VITE_METADATA_URL;
108-
if (!urlRaw) throw new Error("Missing VITE_METADATA_URL");
109-
110-
const url = ensureTrailingSlash(urlRaw);
59+
export const loadAnalysisMetadata = once(async () => {
60+
const url = import.meta.env.VITE_METADATA_URL;
61+
if (!url) throw new Error("Missing VITE_METADATA_URL");
11162

112-
const response = await fetch(url + "analyses");
63+
console.log(pathJoin(url, "analyses"));
64+
const response = await fetch(pathJoin(url, "analyses"));
11365
const data = (await response.json()) as AnalysisMetadataResponse;
11466
return data.resources.filter(
11567
(resource) =>

src/api/corpusConfig.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,22 +72,22 @@ describe("makeConfig", () => {
7272
});
7373

7474
describe("parseConfig", () => {
75-
test("handle minimal info", async () => {
75+
test("handle minimal info", () => {
7676
const configYaml = Yaml.dump({
7777
import: { importer: "text_import:parse" },
7878
});
79-
const config = await parseConfig(configYaml);
79+
const config = parseConfig(configYaml);
8080
expect(config.format).toBe("txt");
8181
});
8282

8383
test("requires format", () => {
8484
const configYaml = Yaml.dump({
8585
metadata: { name: { swe: "Nyheter", eng: "News" } },
8686
});
87-
expect(() => parseConfig(configYaml)).rejects.toThrowError();
87+
expect(() => parseConfig(configYaml)).toThrowError();
8888
});
8989

90-
test("handle full info", async () => {
90+
test("handle full info", () => {
9191
const configYaml = Yaml.dump({
9292
metadata: {
9393
name: { swe: "Nyheter", eng: "News" },
@@ -106,7 +106,7 @@ describe("parseConfig", () => {
106106
annotations: ["<text>:readability.lix", "swener.ne"],
107107
},
108108
});
109-
const config = await parseConfig(configYaml);
109+
const config = parseConfig(configYaml);
110110
const expected: ConfigOptions = {
111111
format: "xml",
112112
name: { swe: "Nyheter", eng: "News" },

src/api/corpusConfig.ts

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import Yaml from "js-yaml";
22
import {
33
analysisAnnotations,
44
annotationAnalyses,
5-
loadAnalysisMetdata,
5+
loadAnalysisMetadata,
66
type AnalysisId,
77
} from "./analysis";
88
import type {
@@ -149,27 +149,33 @@ export function makeConfig(id: string, options: ConfigOptions): string {
149149
return Yaml.dump(config as SparvConfig, { noArrayIndent: true });
150150
}
151151

152-
/** Default values */
153-
export async function emptyConfig(): Promise<ConfigOptions> {
154-
// Let all analyses be enabled by default, except NER because it is heavy, and Geo because it depends on NER.
155-
const disabledAnalyses = [
156-
"sbx-swe-namedentity-swener",
157-
"sbx-swe-geotagcontext-sparv",
158-
];
152+
/** A minimal config */
153+
export function emptyConfig(): ConfigOptions {
159154
return {
160155
name: { swe: "", eng: "" },
161156
description: { swe: "", eng: "" },
162157
format: "txt",
163158
datetime: undefined,
164-
analyses: Object.fromEntries(
165-
(await loadAnalysisMetdata()).map((analysis) => [
166-
analysis.id,
167-
!disabledAnalyses.includes(analysis.id),
168-
]),
169-
),
159+
analyses: {},
170160
};
171161
}
172162

163+
/** Default config */
164+
export async function defaultConfig(): Promise<ConfigOptions> {
165+
// Let all analyses be enabled by default, except NER because it is heavy, and Geo because it depends on NER.
166+
const disabledAnalyses = [
167+
"sbx-swe-namedentity-swener",
168+
"sbx-swe-geotagcontext-sparv",
169+
];
170+
171+
const config = emptyConfig();
172+
173+
for (const { id } of await loadAnalysisMetadata())
174+
if (!disabledAnalyses.includes(id)) config.analyses[id] = true;
175+
176+
return config;
177+
}
178+
173179
/**
174180
* Parse a Sparv config YAML string.
175181
*
@@ -178,7 +184,7 @@ export async function emptyConfig(): Promise<ConfigOptions> {
178184
*
179185
* May throw all kinds of errors, the sky is the limit (:
180186
*/
181-
export async function parseConfig(configYaml: string): Promise<ConfigOptions> {
187+
export function parseConfig(configYaml: string): ConfigOptions {
182188
const config = Yaml.load(configYaml) as unknown as Partial<SparvConfig>;
183189

184190
if (!config)
@@ -199,7 +205,7 @@ export async function parseConfig(configYaml: string): Promise<ConfigOptions> {
199205

200206
// Build options object
201207
const options = {
202-
...(await emptyConfig()),
208+
...emptyConfig(),
203209
format,
204210
name,
205211
description: config.metadata?.description,

src/auth/sbAuth.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,13 @@
44

55
import { jwtDecode } from "jwt-decode";
66
import { computed, ref } from "vue";
7-
import {
8-
deduplicateRequest,
9-
ensureTrailingSlash,
10-
pathJoin,
11-
progressiveTimeout,
12-
} from "@/util";
7+
import { deduplicateRequest, pathJoin, progressiveTimeout } from "@/util";
138
import api from "@/api/api";
149

15-
const AUTH_URL: string = ensureTrailingSlash(import.meta.env.VITE_AUTH_URL);
10+
const AUTH_URL: string = import.meta.env.VITE_AUTH_URL;
1611
const LOGOUT_URL: string = import.meta.env.VITE_LOGOUT_URL;
17-
const JWT_URL: string = import.meta.env.VITE_JWT_URL || AUTH_URL + "jwt";
12+
const JWT_URL: string =
13+
import.meta.env.VITE_JWT_URL || pathJoin(AUTH_URL, "jwt");
1814

1915
export type JwtSbPayload = {
2016
name: string;
@@ -53,7 +49,7 @@ export function getLoginUrl(redirectLocation = "") {
5349
import.meta.env.BASE_URL,
5450
redirectLocation,
5551
);
56-
return AUTH_URL + `login?redirect=${redirectLocation}`;
52+
return pathJoin(AUTH_URL, `login?redirect=${redirectLocation}`);
5753
}
5854

5955
/** Checks cached JWT and updates it if needed. */

src/corpus/config/ConfigPanel.vue

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { useCorpus } from "../corpus.composable";
44
import useLocale from "@/i18n/locale.composable";
55
import PendingContent from "@/spin/PendingContent.vue";
66
import TerminalOutput from "@/components/TerminalOutput.vue";
7-
import { loadAnalysisMetdata } from "@/api/analysis";
7+
import { loadAnalysisMetadata } from "@/api/analysis";
88
99
const props = defineProps<{
1010
corpusId: string;
@@ -19,7 +19,7 @@ const analyses = computedAsync(async () => {
1919
const map = configOptions.value.analyses;
2020
const ids = Object.keys(map).filter((id) => map[id]);
2121
// Get metadata for selected analyses
22-
const metadata = await loadAnalysisMetdata();
22+
const metadata = await loadAnalysisMetadata();
2323
return metadata
2424
.filter((analysis) => ids.includes(analysis.id))
2525
.sort(thCompare((x) => x.name));
@@ -117,7 +117,10 @@ const analyses = computedAsync(async () => {
117117
<ul class="list-disc list-outside pl-5 mt-2">
118118
<li v-for="analysis of analyses" :key="analysis.id">
119119
{{ th(analysis.name) }}
120-
(<a :href="$t('analyses.url', analysis.id)" target="_blank">
120+
(<a
121+
:href="$t('config.analyses.url', analysis.id)"
122+
target="_blank"
123+
>
121124
{{ analysis.id }}</a
122125
>)
123126
</li>

src/corpus/config/CorpusConfiguration.vue

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import LayoutBox from "@/components/LayoutBox.vue";
3131
import TerminalOutput from "@/components/TerminalOutput.vue";
3232
import {
3333
analysisAnnotations,
34-
loadAnalysisMetdata,
34+
loadAnalysisMetadata,
3535
type AnalysisId,
3636
} from "@/api/analysis";
3737
import useLocale from "@/i18n/locale.composable";
@@ -62,7 +62,7 @@ const tabSelected = ref<TabKey>("metadata");
6262
6363
/** List of metadata for relevant analyses */
6464
const analyses = computedAsync(async () => {
65-
const analyses = await loadAnalysisMetdata();
65+
const analyses = await loadAnalysisMetadata();
6666
// Skip analyses that do not have annotations
6767
// Sort by most significant property last
6868
const filtered = analyses
@@ -78,7 +78,7 @@ const analyses = computedAsync(async () => {
7878
});
7979
});
8080
81-
const configOptions = computedAsync(getParsedConfig);
81+
const configOptions = computed(getParsedConfig);
8282
8383
const formatOptions = computed<FormKitOptionsList>(() =>
8484
FORMATS_EXT.map((ext) => ({
@@ -111,10 +111,10 @@ const segmenterOptions = computed<SegmenterOptions>(() => {
111111
});
112112
113113
// Like `getParsedConfig` in `corpus.composable.ts` but also alerts on error.
114-
async function getParsedConfig() {
114+
function getParsedConfig() {
115115
if (!config.value) return undefined;
116116
try {
117-
const parsed = await parseConfig(config.value);
117+
const parsed = parseConfig(config.value);
118118
return parsed;
119119
} catch (error) {
120120
alert(t("corpus.config.parse.error"), "error");
@@ -124,7 +124,7 @@ async function getParsedConfig() {
124124
125125
async function submit(fields: Form) {
126126
// If there is no previous config file, start from a minimal one.
127-
const configOld = configOptions.value || (await emptyConfig());
127+
const configOld = configOptions.value || emptyConfig();
128128
129129
// Merge new form values with existing config.
130130
const configNew: ConfigOptions = {

src/corpus/corpus.composable.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { computed, watch } from "vue";
22
import { attempt, uniq } from "es-toolkit";
3-
import { computedAsync, useInterval } from "@vueuse/core";
3+
import { useInterval } from "@vueuse/core";
44
import { useCorpusStore } from "@/store/corpus.store";
55
import {
66
makeConfig,
@@ -36,7 +36,7 @@ export function useCorpus(corpusId: string) {
3636
corpusStore.loadConfig(corpusId);
3737
return corpus.value?.config || undefined;
3838
});
39-
const configOptions = computedAsync(getParsedConfig);
39+
const configOptions = computed(getParsedConfig);
4040
const isConfigValid = computed(
4141
() => !attempt(() => validateConfig(configOptions.value!))[0],
4242
);
@@ -89,10 +89,10 @@ export function useCorpus(corpusId: string) {
8989
await corpusStore.uploadConfig(corpusId, configYaml);
9090
}
9191

92-
async function getParsedConfig() {
92+
function getParsedConfig() {
9393
if (!config.value) return undefined;
9494
try {
95-
const parsed = await parseConfig(config.value);
95+
const parsed = parseConfig(config.value);
9696
return parsed;
9797
} catch (error) {
9898
console.error(`Error parsing config for "${corpusId}":`, error);

src/corpus/createCorpus.composable.ts

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
makeConfig,
99
type FileFormat,
1010
type ConfigOptions,
11-
emptyConfig,
11+
defaultConfig,
1212
} from "@/api/corpusConfig";
1313
import type { MinkResponse } from "@/api/api.types";
1414
import useCreateResource from "@/resource/createResource.composable";
@@ -36,14 +36,11 @@ export default function useCreateCorpus() {
3636
const corpusId = await createCorpus().catch(alertError);
3737
if (!corpusId) return;
3838

39-
// Get file extension of first file, assuming all are using the same extension.
40-
const format = getFilenameExtension(files[0].name) as FileFormat;
39+
// Create default config.
40+
const config = await defaultConfig();
4141

42-
// Create a minimal config.
43-
const config = {
44-
...(await emptyConfig()),
45-
format,
46-
};
42+
// Get file extension of first file, assuming all are using the same extension.
43+
config.format = getFilenameExtension(files[0].name) as FileFormat;
4744

4845
// Wait for sources and config to be uploaded in parallel.
4946
const results = await Promise.allSettled([
@@ -89,7 +86,7 @@ export default function useCreateCorpus() {
8986
textAnnotation?: string,
9087
): Promise<string | undefined> {
9188
const config = {
92-
...(await emptyConfig()),
89+
...(await defaultConfig()),
9390
name: { swe: name, eng: name },
9491
description: { swe: description, eng: description },
9592
format,

src/page/title.composable.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export default function usePageTitle() {
3535
const { parseConfig } = await import("@/api/corpusConfig");
3636
let parsedConfig: ConfigOptions;
3737
try {
38-
parsedConfig = await parseConfig(config);
38+
parsedConfig = parseConfig(config);
3939
} catch (error) {
4040
console.error(`Error parsing config for "${corpusId}":`, error);
4141
return;

0 commit comments

Comments
 (0)