Skip to content

Commit 109f97c

Browse files
authored
Merge branch 'development' into dependabot/npm_and_yarn/node-polyfill-webpack-plugin-4.0.0
2 parents 188040c + f81cc39 commit 109f97c

29 files changed

+1640
-207
lines changed

app/api/services/informationextraction/InformationExtraction.ts

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import {
2929
FileWithAggregation,
3030
getFilesForTraining,
3131
getFilesForSuggestions,
32+
propertyTypeIsWithoutExtractedMetadata,
3233
propertyTypeIsSelectOrMultiSelect,
3334
} from 'api/services/informationextraction/getFiles';
3435
import { Suggestions } from 'api/suggestions/suggestions';
@@ -95,6 +96,16 @@ type MaterialsData =
9596
| TextSelectionMaterialsData
9697
| ValuesSelectionMaterialsData;
9798

99+
async function fetchCandidates(property: PropertySchema) {
100+
const defaultLanguageKey = (await settings.getDefaultLanguage()).key;
101+
const query: { template?: ObjectId; language: string } = {
102+
language: defaultLanguageKey,
103+
};
104+
if (property.content !== '') query.template = new ObjectId(property.content);
105+
const candidates = await entities.getUnrestricted(query, ['title', 'sharedId']);
106+
return candidates;
107+
}
108+
98109
class InformationExtraction {
99110
static SERVICE_NAME = 'information_extraction';
100111

@@ -142,9 +153,9 @@ class InformationExtraction {
142153

143154
let data: MaterialsData = { ..._data, language_iso };
144155

145-
const isSelect = propertyTypeIsSelectOrMultiSelect(propertyType);
156+
const noExtractedData = propertyTypeIsWithoutExtractedMetadata(propertyType);
146157

147-
if (!isSelect && propertyLabeledData) {
158+
if (!noExtractedData && propertyLabeledData) {
148159
data = {
149160
...data,
150161
label_text: propertyValue || propertyLabeledData?.selection?.text,
@@ -155,7 +166,7 @@ class InformationExtraction {
155166
};
156167
}
157168

158-
if (isSelect) {
169+
if (noExtractedData) {
159170
if (!Array.isArray(propertyValue)) {
160171
throw new Error('Property value should be an array');
161172
}
@@ -184,7 +195,7 @@ class InformationExtraction {
184195
);
185196
const { propertyValue, propertyType } = file;
186197

187-
const missingData = propertyTypeIsSelectOrMultiSelect(propertyType)
198+
const missingData = propertyTypeIsWithoutExtractedMetadata(propertyType)
188199
? !propertyValue
189200
: type === 'labeled_data' && !propertyLabeledData;
190201

@@ -383,15 +394,22 @@ class InformationExtraction {
383394

384395
const params: TaskParameters = {
385396
id: extractorId.toString(),
386-
multi_value: property.type === 'multiselect',
397+
multi_value: property.type === 'multiselect' || property.type === 'relationship',
387398
};
388399

389-
if (property.type === 'select' || property.type === 'multiselect') {
400+
if (propertyTypeIsSelectOrMultiSelect(property.type)) {
390401
const thesauri = await dictionatiesModel.getById(property.content);
391402

392403
params.options =
393404
thesauri?.values?.map(value => ({ label: value.label, id: value.id as string })) || [];
394405
}
406+
if (property.type === 'relationship') {
407+
const candidates = await fetchCandidates(property);
408+
params.options = candidates.map(candidate => ({
409+
label: candidate.title || '',
410+
id: candidate.sharedId || '',
411+
}));
412+
}
395413

396414
await this.taskManager.startTask({
397415
task: 'create_model',

app/api/services/informationextraction/getFiles.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,16 @@ type FileEnforcedNotUndefined = {
4545
};
4646

4747
const selectProperties: Set<string> = new Set([propertyTypes.select, propertyTypes.multiselect]);
48+
const propertiesWithoutExtractedMetadata: Set<string> = new Set([
49+
...Array.from(selectProperties),
50+
propertyTypes.relationship,
51+
]);
4852

4953
const propertyTypeIsSelectOrMultiSelect = (type: string) => selectProperties.has(type);
5054

55+
const propertyTypeIsWithoutExtractedMetadata = (type: string) =>
56+
propertiesWithoutExtractedMetadata.has(type);
57+
5158
async function getFilesWithAggregations(files: (FileType & FileEnforcedNotUndefined)[]) {
5259
const filesNames = files.filter(x => x.filename).map(x => x.filename);
5360

@@ -98,7 +105,7 @@ async function fileQuery(
98105
propertyType: string,
99106
entitiesFromTrainingTemplatesIds: string[]
100107
) {
101-
const needsExtractedMetadata = !propertyTypeIsSelectOrMultiSelect(propertyType);
108+
const needsExtractedMetadata = !propertyTypeIsWithoutExtractedMetadata(propertyType);
102109
const query: {
103110
type: string;
104111
filename: { $exists: Boolean };
@@ -125,7 +132,7 @@ function entityForTrainingQuery(
125132
const query: {
126133
[key: string]: { $in?: ObjectIdSchema[]; $exists?: Boolean; $ne?: any[] };
127134
} = { template: { $in: templates } };
128-
if (propertyTypeIsSelectOrMultiSelect(propertyType)) {
135+
if (propertyTypeIsWithoutExtractedMetadata(propertyType)) {
129136
query[`metadata.${property}`] = { $exists: true, $ne: [] };
130137
}
131138
return query;
@@ -162,8 +169,8 @@ async function getFilesForTraining(templates: ObjectIdSchema[], property: string
162169
return { ...file, propertyType };
163170
}
164171

165-
if (propertyTypeIsSelectOrMultiSelect(propertyType)) {
166-
const propertyValue = (entity.metadata[property] || []).map(({ value, label }) => ({
172+
if (propertyTypeIsWithoutExtractedMetadata(propertyType)) {
173+
const propertyValue = (entity.metadata?.[property] || []).map(({ value, label }) => ({
167174
value: ensure<string>(value),
168175
label: ensure<string>(label),
169176
}));
@@ -223,5 +230,6 @@ export {
223230
getFilesForSuggestions,
224231
getSegmentedFilesIds,
225232
propertyTypeIsSelectOrMultiSelect,
233+
propertyTypeIsWithoutExtractedMetadata,
226234
};
227235
export type { FileWithAggregation };

app/api/services/informationextraction/ixextractors.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,16 @@ import {
88
createBlankSuggestionsForExtractor,
99
createBlankSuggestionsForPartialExtractor,
1010
} from 'api/suggestions/blankSuggestions';
11+
import { Subset } from 'shared/tsUtils';
12+
import { PropertyTypeSchema } from 'shared/types/commonTypes';
1113
import { IXExtractorModel as model } from './IXExtractorModel';
1214

13-
type AllowedPropertyTypes = 'title' | 'text' | 'numeric' | 'date' | 'select' | 'multiselect';
15+
type AllowedPropertyTypes =
16+
| Subset<
17+
PropertyTypeSchema,
18+
'text' | 'numeric' | 'date' | 'select' | 'multiselect' | 'relationship'
19+
>
20+
| 'title';
1421

1522
const ALLOWED_PROPERTY_TYPES: AllowedPropertyTypes[] = [
1623
'title',
@@ -19,6 +26,7 @@ const ALLOWED_PROPERTY_TYPES: AllowedPropertyTypes[] = [
1926
'date',
2027
'select',
2128
'multiselect',
29+
'relationship',
2230
];
2331

2432
const allowedTypeSet = new Set<string>(ALLOWED_PROPERTY_TYPES);

0 commit comments

Comments
 (0)