Skip to content

Commit

Permalink
Set format from file extension when creating corpus
Browse files Browse the repository at this point in the history
  • Loading branch information
arildm committed Nov 17, 2023
1 parent 090a8c8 commit 3ea2bea
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ As this project is a user-facing application, the places in the semantic version

### Changed

- A corpus created by upload now gets its format set to match the uploaded files
- A corpus created by upload (not through the creation form) now gets its name set to the id, instead of remaining empty

### Fixed
Expand Down
23 changes: 17 additions & 6 deletions src/api/corpusConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,13 @@ export async function makeConfig(id, options) {
],
};

if (format === "xml") {
if (!textAnnotation) {
throw new TypeError("Text annotation setting is required for XML");
if (format == "xml") {
// The text annotation setting is required if XML, but it may be set later
if (textAnnotation) {
config.import.text_annotation = textAnnotation;
config.export.source_annotations = [`${textAnnotation} as text`, "..."];
}
config.import.text_annotation = textAnnotation;
config.export.source_annotations = [`${textAnnotation} as text`, "..."];
} else if (format === "pdf") {
} else if (format == "pdf") {
config.export.source_annotations = ["text", "page:number"];
}

Expand Down Expand Up @@ -141,3 +141,14 @@ export async function parseConfig(configYaml) {
).params.value,
};
}

/** Check if the config looks ready to run. May throw anything. */
export function validateConfig(config) {
if (!config.format) {
throw new TypeError("Format missing");
}

if (config.format == "xml" && !config.textAnnotation) {
throw new TypeError("Text annotation setting is required for XML");
}
}
5 changes: 3 additions & 2 deletions src/corpus/corpusState.composable.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { computed } from "vue";
import { useI18n } from "vue-i18n";
import { FORMATS_EXT } from "@/api/corpusConfig";
import { validateConfig } from "@/api/corpusConfig";
import useConfig from "./config/config.composable";
import useJob from "./job/job.composable";
import useSources from "./sources/sources.composable";
import { getException } from "@/util";

/** The "corpus state" is related to the job status, but is more about predicting what action the user needs to take. */
export function useCorpusState(corpusId) {
Expand Down Expand Up @@ -38,7 +39,7 @@ export function useCorpusState(corpusId) {
});

const isConfigValid = computed(
() => config.value && FORMATS_EXT.includes(config.value.format)
() => !getException(() => validateConfig(config.value))
);

const hasMetadata = computed(
Expand Down
4 changes: 4 additions & 0 deletions src/corpus/createCorpus.composable.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,13 @@ export default function useCreateCorpus() {
const corpusId = await createCorpus().catch(alertError);
if (!corpusId) return;

// Get file extension of first file, assuming all are using the same extension.
const format = files[0]?.name.split(".").pop();

// Create a minimal config.
const config = {
name: { swe: corpusId, eng: corpusId },
format,
};

const results = await Promise.allSettled([
Expand Down
2 changes: 1 addition & 1 deletion src/i18n/locales/en.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ api.code.failed_uploading_sources: Failed to upload source files
api.code.failed_uploading_sources_file_size: Some files could not be uploaded, because they exceed the maximum file size of {max_file_size} MB
api.code.failed_uploading_sources_invalid_file_extension: 'File "{file}" could not be uploaded, because it has an invalid filename extension. Please use any of the supported file formats: xml, txt, odt, docx, pdf.'
api.code.failed_uploading_sources_invalid_xml: File "{file}" could not be uploaded, because it contains invalid XML
api.code.failed_uploading_sources_incompatible_file_extension: File "{file}" could not be uploaded, because it has an different filename extension. Please check that the file format of this corpus is configured to agree with your source files.
api.code.failed_uploading_sources_incompatible_file_extension: File "{file}" could not be uploaded, because it has a different filename extension. Please check that the file format of this corpus is configured to agree with your source files.
api.code.uploaded_sources: The source files were successfully added
api.code.data_too_large: Request data too large (max {max_content_length} MB per upload)
api.code.listing_sources: Listing current source files for the corpus "{corpus_id}"
Expand Down
10 changes: 10 additions & 0 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,13 @@ export function setKeys(obj, keys, defaultValue = null) {

/** Create a random string of around 11 chars in the [0-9a-z] range. */
export const randomString = () => Math.random().toString(36).slice(2);

/** Execute callback, catch and return any exception, otherwise return undefined. */
export const getException = (f) => {
try {
f();
} catch (e) {
return e;
}
return undefined;
};
17 changes: 17 additions & 0 deletions src/util.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
ensureExtension,
formatDate,
formatSeconds,
getException,
pathJoin,
randomString,
setKeys,
Expand Down Expand Up @@ -64,3 +65,19 @@ describe("randomString", () => {
expect(fails).toEqual([]);
});
});

describe("getException", () => {
test("translate success to undefined", () => {
const f = () => "foobar";
const exception = getException(f);
expect(exception).toBeUndefined();
});
test("reflect exception", () => {
const f = () => {
throw new EvalError("Leverpastej");
};
const exception = getException(f);
expect(exception.name).toBe("EvalError");
expect(exception.message).toBe("Leverpastej");
});
});

0 comments on commit 3ea2bea

Please sign in to comment.