-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtermennetwerk.ts
151 lines (135 loc) · 4.15 KB
/
termennetwerk.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import { GraphQLClient, gql } from "graphql-request";
import { ColumnRefinement } from "@pldn/ldwizard/types/WizardConfig";
const endpoint = "https://termennetwerk-api.netwerkdigitaalerfgoed.nl/graphql";
interface Term {
uri: string;
prefLabel: string[];
altLabel: string[];
}
interface Result {
__typename: string;
terms: Term[];
message: string;
};
interface TermsResponse {
terms: {
source: {
name: string;
};
result: Result;
}[];
}
interface SourcesResponse {
sources: {
uri: string,
name: string,
description: string
}[];
}
const queryTerms = gql`
query Terms($sources: [ID]! $query: String!) {
terms(
sources: $sources
query: $query
queryMode: OPTIMIZED
) {
source {
name
}
result {
__typename
... on Terms {
terms {
uri
prefLabel
altLabel
}
}
... on Error {
message
}
}
}
}
`;
const querySources = gql`
query Sources {
sources {
uri
name
}
}
`;
const cache: { [query: string]: string } = {};
const client = new GraphQLClient(endpoint);
function selectTerm(result: Result, searchTerm: string, source: string): Term | undefined {
if (result.__typename === 'ServerError') { // catch server errors
console.log("Termennetwerk refinement: server error for source",source);
return undefined;
}
if (result.__typename === 'TimeoutError') { // catch timeouts errors
console.log("Termennetwerk refinement: time out for source", source);
return undefined;
}
if (result.terms.length === 0 ) { // no results found
//console.log("Termennetwerk refinement: no results for",searchTerm,"in",source);
return undefined;
}
var countMatches = 0;
var matchedTerm:Term = <Term>{};
for( const nr in result.terms ) {
const term = result.terms[nr];
const containsSearchTerm = (label: string) => label.toLowerCase() === searchTerm.toLowerCase();
const inPrefLabel = term.prefLabel.some(containsSearchTerm); // exact match for prefLabel?
const inAltLabel = term.altLabel.some(containsSearchTerm); // exact match for altLabel?
if ( inPrefLabel || inAltLabel ) {
countMatches=+1;
matchedTerm=term;
}
}
if(countMatches==1){
//console.log("Termennetwerk refinement: found single exact match for",searchTerm,"in",source);
return matchedTerm;
}
else {
//console.log("Termennetwerk refinement: no single exact match found for",searchTerm,"in",source);
return undefined; // no matches found
}
}
export async function getUriOfSearchTerm(sources: string[], searchTerm: string): Promise<string | undefined> {
if (searchTerm.trim() === "") return undefined;
const cacheKey = searchTerm.toLowerCase();
if (cache[cacheKey]) return cache[cacheKey];
const response: TermsResponse = await client.request(queryTerms, { sources, query: searchTerm });
for( const term in response){
for( const sourcenr in response[term]) {
const source = response[term][sourcenr].source.name;
const result = response[term][sourcenr].result;
const selectedTerm = selectTerm(result, searchTerm, source);
if( selectedTerm ) {
cache[cacheKey] = selectedTerm.uri;
return selectedTerm.uri;
}
}
}
return undefined;
}
export async function getRefinementList():Promise<ColumnRefinement[]> {
const response: SourcesResponse = await client.request(querySources);
var refinementList: ColumnRefinement[] = [];
for( const sourcenr in response.sources ){
//console.log("Source loop index",sourcenr);
const refinement:ColumnRefinement = {
label: "Termennetwerk: "+response.sources[sourcenr].name,
type: "single",
description: response.sources[sourcenr].description,
transformation: async (searchTerm: string) => {
const sources = [response.sources[sourcenr].uri];
return getUriOfSearchTerm(sources, searchTerm);
},
yieldsIri: true
}
refinementList.push(refinement)
}
return refinementList;
}