Skip to content

Commit

Permalink
added dynamic headers for kwic-view based on selected structs
Browse files Browse the repository at this point in the history
  • Loading branch information
lmoertl committed Sep 12, 2024
1 parent c2b24ad commit 9d5b5dc
Show file tree
Hide file tree
Showing 7 changed files with 210 additions and 145 deletions.
94 changes: 2 additions & 92 deletions src/components/DataDisplay/DataDisplayKeywordInContext.vue
Original file line number Diff line number Diff line change
@@ -1,83 +1,10 @@
<script lang="ts" setup>
import { useQueries } from "@tanstack/vue-query";
import { storeToRefs } from "pinia";
import type { Ref } from "vue";
import { getKWICColumns } from "@/utils/kwic";
import type { Type06Concordance } from "~/lib/api-client";
import KWICDetailDialog from "./KWICDetailDialog.vue";
const t = useTranslations("Corpsum");
const queryStore = useQueryStore();
const { queries } = storeToRefs(queryStore);
const api = useApiClient();
const showViewOptionsMode = ref(false);
const KWICresults: Ref<Array<Array<KeywordInContext>>> = ref([]);
const KWICresultsLoading: Ref<Array<boolean>> = ref([]);
const q = computed(() =>
queries.value.map((query, index) => {
return {
queryKey: [
"get-concordance",
query.corpus,
query.subCorpus,
JSON.stringify(queryStore.getKWICqueryAttrStrcs(query)),
JSON.stringify(queryStore.getQueryWithFacetting(query)),
] as const,
queryFn: async () => {
KWICresultsLoading.value[index] = true;
const response = await api.search.getConcordance({
corpname: query.corpus,
usesubcorp: query.subCorpus,
viewmode: "kwic",
...queryStore.getKWICqueryAttrStrcs(query),
refs: "=doc.id,=doc.datum,=doc.region,=doc.ressort2,=doc.docsrc_name",
pagesize: 1000,
json: JSON.stringify({ concordance_query: queryStore.getQueryWithFacetting(query) }),
format: "json",
});
return response.data;
},
select: (data: Type06Concordance) => {
KWICresults.value[index] = (data.Lines?.map(({ Tbl_refs, Left, Kwic, toknum, Right }) => {
// this mapping is directly taken from the ancient code
return {
refs: Tbl_refs,
date: Tbl_refs![1] ?? "",
source: Tbl_refs![4] ?? "",
region: Tbl_refs![2] ?? "",
// @ts-expect-error wrong types in api lib
left: Left!.map(({ str, strc }) => str || strc).join(" "),
word: typeof Kwic![0] !== "undefined" ? Kwic![0].str : "",
// @ts-expect-error wrong types in api lib
right: Right!.map(({ str }: { str: string }) => str).join(" "),
docid: Tbl_refs![0] ?? "",
topic: Tbl_refs![3] ?? "",
toknum,
};
}) ?? []) as unknown as Array<KeywordInContext>;
KWICresultsLoading.value[index] = false;
},
};
}),
);
useQueries({ queries: q });
function open(item: KeywordInContext) {
selectedKWIC.value = item;
}
const selectedKWIC: Ref<KeywordInContext | null> = ref(null);
const columns = computed(() => {
return getKWICColumns(t as unknown as (s: string) => string, open);
});
</script>

<template>
Expand All @@ -88,25 +15,8 @@ const columns = computed(() => {
</template>
</VCardItem>
<VCardText class="py-0">
<div v-for="(query, index) of queries" :key="query.id">
<VCheckbox v-model="showViewOptionsMode" :label="t('viewOptions')"></VCheckbox>
<KWICAttributeSelect v-if="showViewOptionsMode" :query="query" />
<div>
<QueryDisplay :loading="KWICresultsLoading[index]" :query="query" />

<CorpsumDataTable
v-if="!KWICresultsLoading[index]"
:columns="columns"
:data="KWICresults[index]!"
/>

<KWICDetailDialog
v-if="selectedKWIC"
:kwic="selectedKWIC"
:query="query"
@close="selectedKWIC = null"
/>
</div>
<div v-for="query of queries" :key="query.id">
<KWICQueryDisplay :query="query" />
</div>
</VCardText>
</VCard>
Expand Down
3 changes: 3 additions & 0 deletions src/components/KWIC/KWICAttributeSelect.vue
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ const structureOptions = computed(() => {
const attributeOptions = computed(
() => _query?.KWICAttrsStructsOptions.attributes.map((structure) => structure.name) ?? [],
);
const fixed = queryStore.fixedKWICStructures;
</script>

<template>
Expand Down Expand Up @@ -73,6 +75,7 @@ const attributeOptions = computed(
checkbox
:class="{ 'ml-4': item.raw.includes('.') }"
density="compact"
:disabled="fixed.includes(item.raw)"
v-bind="props"
></VListItem>
</template>
Expand Down
90 changes: 90 additions & 0 deletions src/components/KWIC/KWICQueryDisplay.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<script lang="ts" setup>
import { useQuery } from "@tanstack/vue-query";
import { getKWICColumns } from "@/utils/kwic";
import type { Type06Concordance } from "~/lib/api-client";
const props = defineProps<{ query: CorpusQuery }>();
const showViewOptionsMode = ref(false);
const queryStore = useQueryStore();
const api = useApiClient();
const { data: KWICresults, status } = useQuery({
queryKey: [
"get-concordance",
props.query.corpus,
props.query.subCorpus,
props.query.KWICAttrsStructs,
queryStore.getQueryWithFacetting(props.query),
],
queryFn: async () => {
const response = await api.search.getConcordance({
corpname: props.query.corpus,
usesubcorp: props.query.subCorpus,
viewmode: "kwic",
...queryStore.getKWICqueryAttrStrcs(props.query),
refs: props.query.KWICAttrsStructs.structures.map((s: string) => `=${s}`).join(","),
pagesize: 1000,
json: JSON.stringify({ concordance_query: queryStore.getQueryWithFacetting(props.query) }),
format: "json",
});
return response.data;
},
select: (data: Type06Concordance) => {
return (data.Lines?.map(({ Tbl_refs, Left, Kwic, toknum, Right }) => {
// this mapping is directly taken from the ancient code
return {
refs: Tbl_refs,
date: Tbl_refs![1] ?? "",
source: Tbl_refs![3] ?? "",
region: Tbl_refs![2] ?? "",
// @ts-expect-error wrong types in api lib
left: Left!.map(({ str, strc }) => str || strc).join(" "),
word: typeof Kwic![0] !== "undefined" ? Kwic![0].str : "",
// @ts-expect-error wrong types in api lib
right: Right!.map(({ str }: { str: string }) => str).join(" "),
docid: Tbl_refs![0] ?? "",
toknum,
};
}) ?? []) as unknown as Array<KeywordInContext>;
},
});
const loading = computed(() => status.value === "pending");
function open(item: KeywordInContext) {
selectedKWIC.value = item;
}
const t = useTranslations("Corpsum");
const columns = computed(() => {
return getKWICColumns(
t as unknown as (s: string) => string,
open,
props.query.KWICAttrsStructs.structures,
queryStore.fixedKWICStructures,
);
});
const selectedKWIC: Ref<KeywordInContext | null> = ref(null);
</script>

<template>
<div v-if="query">
<VCheckbox v-model="showViewOptionsMode" :label="t('viewOptions')"></VCheckbox>
<KWICAttributeSelect v-if="showViewOptionsMode" :query="query" />
<div>
<QueryDisplay :loading="loading" :query="query" />
<CorpsumDataTable v-if="!loading" :columns="columns" :data="KWICresults!" />
<KWICDetailDialog
v-if="selectedKWIC"
:kwic="selectedKWIC"
:query="query"
@close="selectedKWIC = null"
/>
</div>
</div>
</template>
31 changes: 30 additions & 1 deletion src/messages/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
"corpusCreated": "Subcorpus created successfully!",
"Attributes": "Attributes",
"Structures": "Structures",
"Headers": "Table Headers",
"Attributes and Structures": "Attributes and Structures",
"viewOptions": "Show view options",
"Reload KWIC with selected Attributes": "Reload KWIC with selected Attributes",
Expand All @@ -96,7 +97,35 @@
"total-of-n-entries": "Total of {0} entries.",
"regexp-selection-clear-selection-to-select-values": "regexp selection. clear selection to select values.",
"use-search-as-modes-modeindex-for-search-this-clears-the-rest-of-the-selection": "use '{0}' as '{1}' for search (this clears the rest of the selection)",
"clear-selection": "Clear Selection"
"clear-selection": "Clear Selection",
"doc.id": "doc.id",
"doc.datum_full": "doc.datum_full",
"doc.datum": "doc.datum",
"doc.bibl": "doc.bibl",
"doc.docsrc": "doc.docsrc",
"doc.docsrc_name": "doc.docsrc_name",
"doc.region": "doc.region",
"doc.province": "doc.province",
"doc.tokens": "doc.tokens",
"doc.ressort2": "doc.ressort2",
"doc.autor": "doc.autor",
"doc.deskriptor": "doc.deskriptor",
"doc.mutation": "doc.mutation",
"doc.keys": "doc.keys",
"doc.mediatype": "doc.mediatype",
"doc.wordcount": "doc.wordcount",
"doc.year": "doc.year",
"doc.yymm": "doc.yymm",
"doc.dupl": "doc.dupl",
"doc.txttype": "doc.txttype",
"field.name": "field.name",
"field.dupl": "field.dupl",
"p.dupl": "p.dupl",
"fmt.type": "fmt.type",
"meta_info.type": "meta_info.type",
"meta_info.speaker": "meta_info.speaker",
"meta_info.affil": "meta_info.affil",
"g": "g"
},
"DefaultLayout": {
"meta": {
Expand Down
14 changes: 9 additions & 5 deletions src/stores/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,20 @@ const keyToKey = {
wordrow: "word",
};

const emptySelectedCorpusKWICViewInfo: KWICAttrsStructs = {
const _fixedKWICStructures = ["doc.id", "doc.datum", "doc.region", "doc.docsrc_name"];

const newSelectedCorpusKWICViewInfo: KWICAttrsStructs = {
attributes: [],
structures: [],
structures: _fixedKWICStructures,
};

export const useQueryStore = defineStore(
"QueryStoreWithNewStructure",
"QueryStoreWithNewStructureadsf",
() => {
const nextQueryId = ref(0);
const queries = ref([]) as Ref<Array<CorpusQuery>>;
const corporaStore = useCorporaStore();

const fixedKWICStructures = _fixedKWICStructures;
function addQuery(userInput: string, type: CorpusQueryType) {
let finalQuery = "";

Expand Down Expand Up @@ -61,13 +63,14 @@ export const useQueryStore = defineStore(
concordance_query: concordance_query as ConcordanceQuery,
preparedQuery: `aword,${finalQuery}`, // note: this is done in the old project, so we do it here too
showPicker: false,
KWICAttrsStructs: { ...emptySelectedCorpusKWICViewInfo },
KWICAttrsStructs: { ...newSelectedCorpusKWICViewInfo },
KWICAttrsStructsOptions: {
attributes: (corporaStore.corpInfoResponse.attributes ??
([] as unknown)) as Array<KWICAttribute>,
structures: (corporaStore.corpInfoResponse.structures ??
([] as unknown)) as Array<KWICStructure>,
},
KWICAdditionalViewHeaders: [],
facettingValues: {},
loading: {
yearlyFrequencies: false,
Expand Down Expand Up @@ -104,6 +107,7 @@ export const useQueryStore = defineStore(
};
return {
nextQueryId,
fixedKWICStructures,
queries,
addQuery,
getKWICqueryAttrStrcs,
Expand Down
3 changes: 3 additions & 0 deletions src/types/query.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ interface KeywordInContext {
docid: string;
topic: string;
toknum: number;
refs: Array<string>;
}
interface QueryData {
yearlyFrequencies: Array<{
Expand Down Expand Up @@ -70,6 +71,7 @@ interface KWICAttrsStructsOptions {
attributes: Array<KWICAttribute>;
structures: Array<KWICStructure>;
}

type ConcordanceQuery = Record<"queryselector", CorpusQueryType> &
Record<CorpusQueryTypeValue, string>;

Expand All @@ -91,6 +93,7 @@ interface CorpusQuery {
subCorpus: string;
concordance_query: ConcordanceQuery;
KWICAttrsStructs: KWICAttrsStructs;
KWICAdditionalViewHeaders: Array<string>;
KWICAttrsStructsOptions: KWICAttrsStructsOptions; // ToDo: this might be refactored to come via requesat whenever needed.
facettingValues: FacettingValues;
loading: {
Expand Down
Loading

0 comments on commit 9d5b5dc

Please sign in to comment.