-
Notifications
You must be signed in to change notification settings - Fork 1
/
AbstractPipeline.java
173 lines (146 loc) · 6.21 KB
/
AbstractPipeline.java
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package org.icij.datashare.text.nlp;
import org.icij.datashare.PropertiesProvider;
import org.icij.datashare.text.Document;
import org.icij.datashare.text.Language;
import org.icij.datashare.text.NamedEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.lang.reflect.InvocationTargetException;
import java.nio.charset.Charset;
import java.util.*;
import static org.icij.datashare.function.ThrowingFunctions.*;
import static org.icij.datashare.text.nlp.Pipeline.Type.valueOf;
public abstract class AbstractPipeline implements Pipeline {
public static final String NLP_STAGES_PROP = "nlpStages";
protected final Logger LOGGER = LoggerFactory.getLogger(getClass());
protected final Charset encoding;
protected final Map<NlpStage, List<NlpStage>> stageDependencies;
protected final List<NlpStage> targetStages;
protected final List<NamedEntity.Category> targetEntities;
protected final boolean caching;
protected List<NlpStage> stages;
protected AbstractPipeline(Properties properties) {
targetEntities = getProperty(Property.ENTITIES.getName(), properties,
removeSpaces
.andThen(splitComma)
.andThen(NamedEntity.Category.parseAll))
.orElse(DEFAULT_ENTITIES);
targetStages = getProperty(NLP_STAGES_PROP, properties,
removeSpaces.andThen(splitComma).andThen(NlpStage.parseAll))
.orElse(DEFAULT_TARGET_STAGES);
encoding = getProperty(Property.ENCODING.getName(), properties,
parseCharset.compose(String::trim))
.orElse(DEFAULT_ENCODING);
caching = getProperty(Property.CACHING.getName(), properties,
trim.andThen(Boolean::parseBoolean))
.orElse(DEFAULT_CACHING);
stageDependencies = new HashMap<NlpStage, List<NlpStage>>() {{
Arrays.stream(NlpStage.values())
.forEach( stage ->
put(stage, new ArrayList<>())
);
}};
}
@Override
public Type getType() { return Type.fromClassName(getClass().getSimpleName()).get(); }
@Override
public List<NamedEntity.Category> getTargetEntities() { return targetEntities; }
@Override
public List<NlpStage> getStages() { return stages; }
@Override
public boolean isCaching() { return caching; }
@Override
public Charset getEncoding() { return encoding; }
public static AbstractPipeline create(final String pipelineName, final PropertiesProvider propertiesProvider) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException, ClassNotFoundException {
Class<? extends AbstractPipeline> pipelineClass = (Class<? extends AbstractPipeline>) Class.forName(valueOf(pipelineName).getClassName());
return pipelineClass.getDeclaredConstructor(PropertiesProvider.class).newInstance(propertiesProvider);
}
/**
* Prepare pipeline run
* Check language support for implied stages.
*
* @return false if any stage is not supported in language; true otherwise
*/
public boolean initialize(Language language) throws InterruptedException {
// Pull all dependencies from targeted stages
stages = stagesDependenciesTC(targetStages);
// Check all dependencies for support in language
if ( ! checkStages(language)) {
LOGGER.info("initializing " + getType() + " Skipping... Stage unsupported for " +
language + " " + stages);
return false;
}
LOGGER.info("initializing " + getType() + " " + language + " " + stages);
return true;
}
/**
* Apply all specified stages/annotators on input
* @param doc is the document source to process */
public abstract List<NamedEntity> process(Document doc) throws InterruptedException;
/**
* Post-processing operations
*/
public void terminate(Language language) throws InterruptedException {
LOGGER.info("ending " + getType() + " " + language + " " + stages.toString());
}
/**
* @return Language . NlpStage support matrix
*/
public abstract Map<Language, Set<NlpStage>> supportedStages();
/**
* {@inheritDoc}
*/
@Override
public boolean supports(NlpStage stage, Language language) {
Set<NlpStage> supStagesForLang = supportedStages().get(language);
if (supStagesForLang == null || supStagesForLang.isEmpty())
return false;
return supStagesForLang.contains(stage);
}
/**
* Check every stage supports language
*
* @return true if every stage supports language, false otherwise
*/
private boolean checkStages(Language language) {
for (NlpStage stage : getStages()) {
if ( ! supports(stage, language))
return false;
}
return true;
}
/**
* Transitive closure of stage dependencies
*
* @param coreStages the set of stages to expand
* @return the topological sort of all depending stages
*/
private List<NlpStage> stagesDependenciesTC(List<NlpStage> coreStages) {
Set<NlpStage> visited = new HashSet<>();
List<NlpStage> tc = new ArrayList<>();
for (NlpStage stage : coreStages) {
dfs(stage, visited, tc, stageDependencies);
}
return tc;
}
/**
* Depth-First Search traversal of stage dependencies
*
* @param stage the current stage being traversed
* @param visited keeps the set of already seen stages during traversal
* @param sorted represents the stages, in post-fix DFS traversal order
* @param stagesMatrix holds stages dependencies
*/
private void dfs(NlpStage stage,
Set<NlpStage> visited,
List<NlpStage> sorted,
Map<NlpStage, List<NlpStage>> stagesMatrix) {
visited.add(stage);
stagesMatrix.get(stage)
.forEach( stageDep -> {
if ( ! visited.contains(stageDep))
dfs(stageDep, visited, sorted, stagesMatrix);
});
sorted.add(stage);
}
}