forked from AIObjectives/talk-to-the-city-reports
-
Notifications
You must be signed in to change notification settings - Fork 0
/
merge_v0.ts
116 lines (99 loc) · 3.08 KB
/
merge_v0.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
import nodes from '$lib/node_register';
import categories from '$lib/node_categories';
import deepCopy from 'deep-copy';
import type { DGNodeInterface, BaseData } from '$lib/node_data_types';
import _ from 'lodash';
export default class MergeNode {
id: string;
data: MergeData;
position: { x: number; y: number };
type: string;
constructor(node_data: MergeNodeInterface) {
const { id, data, position, type } = node_data;
this.id = id;
this.data = data;
this.position = position;
this.type = type;
}
async compute(
inputData: Record<string, any>,
context: string,
info: (arg: string) => void,
error: (arg: string) => void,
success: (arg: string) => void,
slug: string,
Cookies: any
) {
if (!inputData) {
return;
}
let cluster_extraction =
inputData?.cluster_extraction || inputData[this.data.input_ids.cluster_extraction];
let argument_extraction =
inputData?.argument_extraction || inputData[this.data.input_ids.argument_extraction];
if (!cluster_extraction || !argument_extraction || !cluster_extraction.topics) {
this.data.dirty = false;
return;
}
cluster_extraction = deepCopy(cluster_extraction);
argument_extraction = deepCopy(argument_extraction);
_.forOwn(argument_extraction, (v, k) => {
_.forEach(v['claims'], (claim, i) => {
claim['id'] = `${k}-${i}`;
});
});
const lookup: Record<string, any> = {};
_.forOwn(argument_extraction, (v, k) => {
_.forEach(v['claims'], (claim) => {
const combinedClaim = _.assign({}, claim, {
interview: v['interview'],
commentId: v['id']
});
const key = `${combinedClaim['topicName']}::${combinedClaim['subtopicName']}`;
if (!_.has(lookup, key)) {
lookup[key] = [];
}
lookup[key].push(combinedClaim);
});
});
_.forEach(cluster_extraction['topics'], (topic) => {
_.forEach(topic['subtopics'], (subtopic) => {
const key = `${topic['topicName']}::${subtopic['subtopicName']}`;
subtopic['claims'] = _.get(lookup, key, []);
});
});
_.remove(cluster_extraction['topics'], (topic: Record<string, any>) => {
_.remove(topic['subtopics'], (subtopic: Record<string, any>) => {
return _.isEmpty(subtopic['claims']);
});
return _.isEmpty(topic['subtopics']);
});
this.data.output = cluster_extraction;
this.data.dirty = false;
return cluster_extraction;
}
}
interface MergeData extends BaseData {
output: object;
}
type MergeNodeInterface = DGNodeInterface & {
data: MergeData;
};
export const merge_node_data: MergeNodeInterface = {
id: 'merge',
data: {
label: 'merge',
output: {},
dirty: false,
compute_type: 'merge_v0',
input_ids: { cluster_extraction: '', argument_extraction: '' },
category: categories.wrangling.id,
icon: 'merge_v0',
show_in_ui: false,
message: ''
},
position: { x: 0, y: 0 },
type: 'merge_v0'
};
export const merge_node = new MergeNode(merge_node_data);
nodes.register(MergeNode, merge_node_data);