forked from neosyon/SimpTextAlign
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAlignNewselaDataset.java
233 lines (201 loc) · 10.4 KB
/
AlignNewselaDataset.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package simplifiedTextAlignment.DatasetAlignment;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.tools.ant.DirectoryScanner;
import simplifiedTextAlignment.Representations.ModelContainer;
import simplifiedTextAlignment.Representations.EmbeddingModel;
import simplifiedTextAlignment.Representations.NgramModel;
import simplifiedTextAlignment.Representations.Text2abstractRepresentation;
import simplifiedTextAlignment.Representations.TextAlignment;
import simplifiedTextAlignment.Utils.DefinedConstants;
import simplifiedTextAlignment.Utils.MyIOutils;
import simplifiedTextAlignment.Utils.TextProcessingUtils;
import simplifiedTextAlignment.Utils.VectorUtils;
public class AlignNewselaDataset {
public static void main(String args[]) throws IOException{
//BEGINNING OF CONFIG PARAMETERS
// String baseDir = "/path/to/your/newsela/parent/folder/";
// TODO: Update this
String baseDir = "/Users/schan/Desktop/wm-revision-analysis/aligners/ngram_aligner/";
// String inFolder = baseDir+"newsela/sample/";
// String inFolder = baseDir+"SimplifiedTextAlignment/newsela_article_corpus_2016-01-29/articles/";
// String inFolder = baseDir+"SimplifiedTextAlignment/newsela_article_corpus_2016-01-29/testArticles/";
String inFolder = baseDir + "test_input_dir/";
String language = DefinedConstants.EnglishLanguage;
// String language = DefinedConstants.SpanishLanguage;
// String alignmentLevel = DefinedConstants.ParagraphSepEmptyLineLevel;
String alignmentLevel = DefinedConstants.SentenceLevel;
// String alignmentLevel = DefinedConstants.ParagraphSepEmptyLineAndSentenceLevel;
int nGramSize = 3;
// String similarityStrategy = DefinedConstants.WAVGstrategy;
// String similarityStrategy = DefinedConstants.CWASAstrategy;
String similarityStrategy = DefinedConstants.CNGstrategy;
String alignmentStrategy = DefinedConstants.closestSimStrategy;
// String alignmentStrategy = DefinedConstants.closestSimKeepingSeqStrategy;
String subLvAlignmentStrategy = DefinedConstants.closestSimStrategy;
// String subLvAlignmentStrategy = DefinedConstants.closestSimKeepingSeqStrategy;
// String outFolder = baseDir+"newsela/output/"+language+"/"+alignmentLevel+
// String outFolder = baseDir+"newsela_article_corpus_2016-01-29/output/"+language+"/"+alignmentLevel+
// "_"+(!alignmentLevel.equals(DefinedConstants.ParagraphSepEmptyLineAndSentenceLevel) ? alignmentStrategy : alignmentStrategy+"_"+subLvAlignmentStrategy)
// +"_"+(similarityStrategy.equals(DefinedConstants.CNGstrategy) ? similarityStrategy.replace("N", nGramSize+"") : similarityStrategy)+"/";
String outFolder = baseDir + "test_output_dir/";
String embeddingsFile = null;
if(language.equals(DefinedConstants.EnglishLanguage))
// embeddingsFile = baseDir+"w2v_collections/Wikipedia/vectors/EN_Wikipedia_w2v_input_format.txtUTF8.vec";
embeddingsFile = baseDir + "wiki-news-300d-1M.vec";
else if(language.equals(DefinedConstants.SpanishLanguage))
embeddingsFile = baseDir+"w2v_collections/SBW-vectors-300-min5.txt";
if (args.length > 0) {
inFolder = outFolder = null;
nGramSize = 0;
Map<String, String> param2value = MyIOutils.parseOptions(args);
if (param2value == null) {
System.out.println("Error: invalid input options. ");
MyIOutils.showNewselaUsageMessage();
System.exit(1);
}
inFolder = param2value.get("input");
outFolder = param2value.get("output");
language = param2value.get("language");
alignmentLevel = param2value.get("aLv");
similarityStrategy = param2value.get("similarity");
alignmentStrategy = param2value.get("aSt");
subLvAlignmentStrategy = param2value.get("aSt2");
embeddingsFile = param2value.get("emb");
if (similarityStrategy != null && similarityStrategy.length() == 3 && similarityStrategy.charAt(0) == 'C' && similarityStrategy.charAt(2) == 'G') {
nGramSize = Integer.parseInt(similarityStrategy.charAt(1) + "");
similarityStrategy = DefinedConstants.CNGstrategy;
}
} else {
System.out.println("Using parameters by default. ");
MyIOutils.showNewselaUsageMessage();
}
//END CONFIG PARAMETERS
boolean isCWASA = false;
ModelContainer model = null;
if((isCWASA=similarityStrategy.equals(DefinedConstants.CWASAstrategy)) || similarityStrategy.equals(DefinedConstants.WAVGstrategy)){
System.out.println("Reading embeddings...");
Set<String> vocab = MyIOutils.readNewselaEmbeddingVocabulary(inFolder,language);
model = new ModelContainer(new EmbeddingModel(embeddingsFile,vocab));
if(isCWASA){
model.em.precomputeW2VcosDist();
model.em.createSimilarityMatrix();
}
}
else if(similarityStrategy.equals(DefinedConstants.CNGstrategy)){
System.out.println("Calculating IDF...");
NgramModel aux;
model = new ModelContainer(aux = new NgramModel(true, nGramSize));
aux.buildNewselaNgramModel(inFolder,language, alignmentLevel);
}
// create output folder if it does not exists
boolean success = (new File(outFolder)).mkdirs();
if(!success) {
if(!Files.exists(Paths.get(outFolder))){
System.out.println("Failed at creating the output folder: " + outFolder);
System.exit(0);
}
}
else
System.out.println("Output folder successfully created: " + outFolder);
System.out.println("Aligning...");
long ini = System.currentTimeMillis();
alignNewselaDataset(inFolder,language,outFolder,alignmentLevel, similarityStrategy, alignmentStrategy, subLvAlignmentStrategy, model);
long end = System.currentTimeMillis();
System.out.println("Alignment done in " + ((double) ((end-ini)/ 1000) / 60) + " minutes.");
}
private static void alignNewselaDataset(String inFolder, String language, String outFolder,
String alignmentLevel, String similarityStrategy, String alignmentStrategy, String subLvAlignmentStrategy, ModelContainer model) throws IOException {
DirectoryScanner scanner = new DirectoryScanner();
System.out.println("inFolder " + inFolder);
System.out.println("outFolder " + outFolder);
scanner.setIncludes(new String[]{"*_1.txt"});
scanner.setBasedir(inFolder);
scanner.setCaseSensitive(false);
scanner.scan();
String[] files = scanner.getIncludedFiles();
int k = 0;
for(String fileProto : files){
System.out.println("fileProto: " + fileProto);
Map<String, List<Text2abstractRepresentation>> file2clean = new HashMap<String, List<Text2abstractRepresentation>>();
for (int i = 1; i <= 2; i++) {
String file1 = fileProto.replace("_1.txt","_" + i + ".txt");
String text1 = MyIOutils.readTextFile(inFolder+file1);
if (text1 != null)
file2clean.put(file1, TextProcessingUtils.getCleanText(text1,alignmentLevel, similarityStrategy, model));
}
List<Text2abstractRepresentation> cleanSubtexts1;
List<Text2abstractRepresentation> cleanSubtexts2;
for (int i = 0; i < 2; i++) {
String file1 = fileProto.replace("_1.txt","_" + i + ".txt");
if ((cleanSubtexts1 = file2clean.get(file1)) != null) {
for (int j = i + 1; j <= 2; j++) {
String file2 = fileProto.replace("_1.txt", "_" + j + ".txt");
if ((cleanSubtexts2 = file2clean.get(file2)) != null) {
System.out.println("file1: " + file1);
System.out.println("file2: " + file2);
// System.out.println(cleanSubtexts1);
// System.out.println(cleanSubtexts2);
List<TextAlignment> alignments = VectorUtils.alignUsingStrategy(cleanSubtexts1, cleanSubtexts2, similarityStrategy, alignmentStrategy, model);
// MyIOutils.displayAlignments(alignments,false);
// System.in.read();
if (alignmentLevel.equals(DefinedConstants.ParagraphSepEmptyLineAndSentenceLevel))
alignments = VectorUtils.getSubLevelAlignments(alignments, cleanSubtexts1, cleanSubtexts2, similarityStrategy, subLvAlignmentStrategy, model);
MyIOutils.saveAlignments(alignments, outFolder + file2 + "_ALIGNED_WITH_" + file1);
}
}
}
}
// String text1 = MyIOutils.readTextFile(inFolder+file1);
// List<Text2abstractRepresentation> cleanSubtexts1 = TextProcessingUtils.getCleanText(text1,alignmentLevel, similarityStrategy, model);
// String fileAux = null;
// List<Text2abstractRepresentation> cleanSubtextsAux = null;
// for (int i = 1; i <= 5; i++) {
// String file2 = file1.replace("." + language + ".0.txt","." + language + "." + i + ".txt");
// String text2 = MyIOutils.readTextFile(inFolder + file2);
// if (text2 != null) {
// List<Text2abstractRepresentation> cleanSubtexts2 = processCompareAndSave(cleanSubtexts1, text2, alignmentLevel, alignmentStrategy, subLvAlignmentStrategy, similarityStrategy, model, outFolder + file2 + "_ALIGNED_WITH_" + file1);
// if(i == 1){
// fileAux = file2;
// cleanSubtextsAux = cleanSubtexts2;
// }
// }
// }
// file1 = fileAux;
// cleanSubtexts1 = cleanSubtextsAux;
// if(file1 != null)
// for (int i = 2; i <= 5; i++) {
// String file2 = file1.replace("." + language + "." + (i - 1) + ".txt","." + language + "." + i + ".txt");
// String text2 = MyIOutils.readTextFile(inFolder + file2);
// if (text2 != null) {
// List<Text2abstractRepresentation> cleanSubtexts2 = processCompareAndSave(cleanSubtexts1, text2, alignmentLevel, alignmentStrategy, subLvAlignmentStrategy, similarityStrategy, model, outFolder + file2 + "_ALIGNED_WITH_" + file1);
// file1 = file2;
// cleanSubtexts1 = cleanSubtexts2;
// }
// }
k++;
if(k%10 == 0)
System.out.println(k + "/" + files.length);
}
}
private static List<Text2abstractRepresentation> processCompareAndSave(List<Text2abstractRepresentation> cleanSubtexts1, String text2,
String alignmentLevel, String alignmentStrategy, String subLvAlignmentStrategy, String similarityStrategy, ModelContainer model,
String outFile) throws IOException {
List<Text2abstractRepresentation> cleanSubtexts2 = TextProcessingUtils.getCleanText(text2,alignmentLevel, similarityStrategy,model);
List<TextAlignment> alignments = VectorUtils.alignUsingStrategy(cleanSubtexts1, cleanSubtexts2,similarityStrategy, alignmentStrategy, model);
// MyIOutils.displayAlignments(alignments,false);
// System.in.read();
if(alignmentLevel.equals(DefinedConstants.ParagraphSepEmptyLineAndSentenceLevel))
alignments = VectorUtils.getSubLevelAlignments(alignments, cleanSubtexts1, cleanSubtexts2, similarityStrategy, subLvAlignmentStrategy, model);
MyIOutils.saveAlignments(alignments, outFile);
// MyIOutils.displayAlignments(alignments,false);
// System.in.read();
return cleanSubtexts2;
}
}