-
Notifications
You must be signed in to change notification settings - Fork 0
/
EntityResolution.java
413 lines (380 loc) · 13.9 KB
/
EntityResolution.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
import java.util.*;
import java.io.FileNotFoundException;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.Attributes;
import java.io.File;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
//! Interface for SAXParsing Handler Factory
interface SAXHandlerFactoryInterface {
public void makeSAXParser();
}
//! Factory Interface
/*!
* creates the parsing logic using the handler provided.
*/
class SAXHandlerFactory implements SAXHandlerFactoryInterface {
DefaultHandler handler;
public SAXHandlerFactory(DefaultHandler handler)
{
this.handler = handler;
}
@Override
public void makeSAXParser()
{
try
{
File inputFile = new File("dblp.xml");
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
saxParser.getXMLReader().setFeature("http://xml.org/sax/features/validation", true);
saxParser.parse(inputFile, handler);
}
catch (FileNotFoundException e) {e.printStackTrace();}
catch (Exception e) {e.printStackTrace(); }
}
}
public class EntityResolution {
public Map<String, Author> ourMainMap;
public EntityResolution()
{
this.ourMainMap = new HashMap<String, Author>();
}
public void parseWWW()
{
DefaultHandler handler = new UserHandlerWWW(this.ourMainMap);
SAXHandlerFactory factory = new SAXHandlerFactory(handler);
factory.makeSAXParser();
}
public void parseAllAuthors()
{
DefaultHandler handler = new UserHandlerAuthors(this.ourMainMap);
SAXHandlerFactory factory = new SAXHandlerFactory(handler);
factory.makeSAXParser();
}
public List<Publication> parsePublicationByAuthor(String queryAuthorName)
{
List<Publication> publicationsFound = new ArrayList<Publication>();
Author queryAuthor = this.ourMainMap.get(queryAuthorName);
DefaultHandler handler = new UserHandlerAuthorPublication(queryAuthor, publicationsFound);
SAXHandlerFactory factory = new SAXHandlerFactory(handler);
factory.makeSAXParser();
return publicationsFound;
}
public Map<Integer, List<Publication> > parsePublicationByTitle(List<String> listTitle)
{
Map<Integer, List<Publication> > publMap = new TreeMap<Integer, List<Publication> >(new Comparator<Integer>()
{
public int compare(Integer a, Integer b)
{
return b.compareTo(a);
}
});
DefaultHandler handler = new UserHandlerTitlePublication(listTitle, this.ourMainMap, publMap);
SAXHandlerFactory factory = new SAXHandlerFactory(handler);
factory.makeSAXParser();
return publMap;
}
}
//! WWW Parsing
/*!
* Parsing for multiple names of the same author
*/
class UserHandlerWWW extends DefaultHandler{
private Map<String, Author> nameMap;
boolean bAuthorName = false;
boolean bWWW = false;
Author author;
public UserHandlerWWW(Map<String, Author> givenMap){
this.nameMap = givenMap;
}
public void startElement(String uri,String localname,String qname,Attributes attributes) throws SAXException{
if(qname.equalsIgnoreCase("www")){
author = new Author();
bWWW = true;
}
if(qname.equalsIgnoreCase("author") && bWWW){
bAuthorName = true;
}
}
public void characters(char ch[],int start, int length)throws SAXException{
if(bAuthorName){
author.addAuthorName(new String(ch, start, length));
this.bAuthorName = false;
}
}
public void endElement(String uri,String localname,String qname) throws SAXException {
if(qname.equalsIgnoreCase("www")) {
for(String i : author.getNames())
{
this.nameMap.put(i, author);
}
bWWW = false;
}
}
}
//! Handler class for Authors and Publications
/*!
* Parse for authors and their number of Publications
*/
class UserHandlerAuthors extends DefaultHandler {
private Map<String, Author> nameMap;
boolean bAuthorName = false;
boolean bPubl = false;
Author newAuthor;
//!Constructor
public UserHandlerAuthors(Map<String, Author> givenMap) {
this.nameMap = givenMap;
}
//! callback for start element done by SAX Parser
/*!
* Check the type for start and element and mark the corresponding boolean variable.
*/
public void startElement(String uri,String localname,String qname,Attributes attributes) throws SAXException{
if(qname.equalsIgnoreCase("article") || qname.equalsIgnoreCase("inproceedings") || qname.equalsIgnoreCase("proceedings") || qname.equalsIgnoreCase("book") || qname.equalsIgnoreCase("incollection") || qname.equalsIgnoreCase("masterthesis") || qname.equalsIgnoreCase("phdthesis")) {
bPubl = true;
}
if(qname.equalsIgnoreCase("author") && bPubl){
bAuthorName = true;
}
}
//! callback for characters done by SAX Parser
/*!
* ch stores the text within the opening and ending tags in xml file.
* Match the author of the publication with the query author and create
* the author instance.
*/
public void characters(char ch[],int start, int length)throws SAXException{
if(bAuthorName){
String name = new String(ch, start, length);
if(!this.nameMap.containsKey(name))
{
newAuthor = new Author();
newAuthor.addAuthorName(name);
this.nameMap.put(name, newAuthor);
}
newAuthor = this.nameMap.get(name);
newAuthor.incrementPub();
this.bAuthorName = false;
}
}
//! callback for endelement done by SAX parser
/*!
* When end element is a publication, store the auhtor created in the author map.
*/
public void endElement(String uri,String localname,String qname) throws SAXException {
if(qname.equalsIgnoreCase("article") || qname.equalsIgnoreCase("inproceedings") || qname.equalsIgnoreCase("proceedings") || qname.equalsIgnoreCase("book") || qname.equalsIgnoreCase("incollection") || qname.equalsIgnoreCase("masterthesis") || qname.equalsIgnoreCase("phdthesis")) {
bPubl = false;
}
}
}
//! User Handler for AuthorPublication
/*!
* Parsing for Publications pertaining to the Given author
*/
class UserHandlerAuthorPublication extends DefaultHandler {
private List<Publication> publications;
public Author author;
private boolean AuthorFound;
private boolean bPubl = false;
private boolean bAuthorName = false;
private boolean bTitle = false;
private boolean bPages = false;
private boolean bYear = false;
private boolean bVolume = false;
private boolean bJournal = false;
private boolean bUrl = false;
private Publication newPubl;
private int result = 0;
//! Constructor
public UserHandlerAuthorPublication(Author author, List<Publication> publ){
this.author = author;
this.publications = publ;
this.AuthorFound = false;
}
List<Publication> getPublications(){ return this.publications; }
//! callback for start element done by SAX Parser
/*!
* Check the type for start and element and mark the corresponding boolean variable.
*/
public void startElement(String uri,String localname,String qname,Attributes attributes) throws SAXException{
if(qname.equalsIgnoreCase("article") || qname.equalsIgnoreCase("inproceedings") || qname.equalsIgnoreCase("proceedings") || qname.equalsIgnoreCase("book") || qname.equalsIgnoreCase("incollection") || qname.equalsIgnoreCase("masterthesis") || qname.equalsIgnoreCase("phdthesis")) {
bPubl = true;
}
if(qname.equalsIgnoreCase("author") && bPubl){
bAuthorName = true;
}
if(qname.equalsIgnoreCase("title") && bPubl) bTitle = true;
if(qname.equalsIgnoreCase("pages") && bPubl) bPages = true;
if(qname.equalsIgnoreCase("year") && bPubl) bYear = true;
if(qname.equalsIgnoreCase("volume") && bPubl) bVolume = true;
if((qname.equalsIgnoreCase("journal") | qname.equalsIgnoreCase("booktitle")) && bPubl) bJournal = true;
if(qname.equalsIgnoreCase("url") && bPubl) bUrl = true;
}
//! callback for characters done by SAX Parser
/*!
* ch stores the text within the opening and ending tags in xml file.
* Match the author of the publication with the query author and create
* the publication instance.
*/
public void characters(char ch[],int start, int length)throws SAXException{
if(bAuthorName){
String name = new String(ch, start, length);
for(String i : author.getNames()){
if(i.equalsIgnoreCase(name)){
this.AuthorFound = true;
newPubl = new Publication();
newPubl.addAuthor(this.author);
break;
}
}
this.bAuthorName = false;
}
if(bTitle){
if(this.AuthorFound) newPubl.setTitle(new String(ch, start, length));
this.bTitle = false;
}
if(bPages){
if(this.AuthorFound) newPubl.setPages(new String(ch, start, length));
this.bPages = false;
}
if(bYear){
if(this.AuthorFound) newPubl.setYear(Integer.valueOf(new String(ch, start, length)));
this.bYear = false;
}
if(bVolume){
if(this.AuthorFound) newPubl.setVolume(new String(ch, start, length));
this.bVolume = false;
}
if(bJournal){
if(this.AuthorFound) newPubl.setJournal(new String(ch, start, length));
this.bJournal = false;
}
if(bUrl){
if(this.AuthorFound) newPubl.setURL(new String(ch, start, length));
this.bUrl = false;
}
}
//! callback for endelement done by SAX parser
/*!
* When end element is a publication, store the publication created in the result list.
*/
public void endElement(String uri,String localname,String qName) throws SAXException {
if(qName.equalsIgnoreCase("article") || qName.equalsIgnoreCase("inproceedings") || qName.equalsIgnoreCase("proceedings") || qName.equalsIgnoreCase("book") || qName.equalsIgnoreCase("incollection") || qName.equalsIgnoreCase("masterthesis") || qName.equalsIgnoreCase("phdthesis")) {
if(this.AuthorFound){
if(newPubl != null){
this.publications.add(newPubl);}
this.result += 1;
this.AuthorFound = false;
}
bPubl = false;
}
}
}
/*!
* Parse for Publication pertaining to Title Tags
*/
class UserHandlerTitlePublication extends DefaultHandler {
Map<Integer, List<Publication> > matchedPubl;
Map<String, Author> ourMainMap;
List<String> queryTitle;
int numberOfMatches;
boolean bPubl = false,bAuthorName = false,bTitle = false,bPages = false;
boolean bYear = false,bVolume = false,bJournal = false,bUrl = false;
Publication newPubl;
Author instanceAuthor;
//! Constructor
public UserHandlerTitlePublication(List<String> titleQuery, Map<String, Author> givenMap, Map<Integer, List<Publication> > publMap) {
this.queryTitle = titleQuery;
this.ourMainMap = givenMap;
this.matchedPubl = publMap;
this.numberOfMatches = 0;
}
//! callback for start element done by SAX Parser
/*!
* Check the type for start and element and mark the corresponding boolean variable.
*/
public void startElement(String uri,String localname,String qname,Attributes attributes) throws SAXException{
if(qname.equalsIgnoreCase("article") || qname.equalsIgnoreCase("inproceedings") || qname.equalsIgnoreCase("proceedings") || qname.equalsIgnoreCase("book") || qname.equalsIgnoreCase("incollection") || qname.equalsIgnoreCase("masterthesis") || qname.equalsIgnoreCase("phdthesis")) {
bPubl = true;
}
if(qname.equalsIgnoreCase("author") && bPubl){bAuthorName = true;}
if(qname.equalsIgnoreCase("title") && bPubl) bTitle = true;
if(qname.equalsIgnoreCase("pages") && bPubl) bPages = true;
if(qname.equalsIgnoreCase("year") && bPubl) bYear = true;
if(qname.equalsIgnoreCase("volume") && bPubl) bVolume = true;
if((qname.equalsIgnoreCase("journal") | qname.equalsIgnoreCase("booktitle")) && bPubl) bJournal = true;
if(qname.equalsIgnoreCase("url") && bPubl) bUrl = true;
}
//! callback for characters done by SAX Parser
/*!
* ch stores the text within the opening and ending tags in xml file.
* Match the title of the publication with the
* query title and store the number of matches.
*/
public void characters(char ch[],int start, int length)throws SAXException{
if(bAuthorName){
String gotName = new String(ch, start, length);
this.instanceAuthor = this.ourMainMap.get(gotName);
bAuthorName = false;
}
if(bTitle){
String gotTitle = new String(ch, start, length);
String[] splitTitle = gotTitle.split(" ");
for(String i : this.queryTitle)
{
String other1 = i+"."; String other2 = i+":"; String other3 = i+",";
for(String j : splitTitle) {
if(i.equalsIgnoreCase(j) | other1.equalsIgnoreCase(j) | other2.equalsIgnoreCase(j) | other3.equalsIgnoreCase(j))
this.numberOfMatches += 1;
}
}
if(this.numberOfMatches > 0) {
this.newPubl = new Publication();
this.newPubl.setTitle(gotTitle);
}
bTitle = false;
}
if(bPages){
if(this.numberOfMatches > 0) newPubl.setPages(new String(ch, start, length));
bPages = false;
}
if(bYear){
if(this.numberOfMatches > 0) newPubl.setYear(Integer.valueOf(new String(ch, start, length)));
bYear = false;
}
if(bVolume){
if(this.numberOfMatches > 0) newPubl.setVolume(new String(ch, start, length));
bVolume = false;
}
if(bJournal){
if(this.numberOfMatches > 0) newPubl.setJournal(new String(ch, start, length));
bJournal = false;
}
if(bUrl){
if(this.numberOfMatches > 0) newPubl.setURL(new String(ch, start, length));
bUrl = false;
}
}
//! callback for endelement done by SAX parser
/*!
* When the end element is article, inproceedings etc (publications),
* then store the created publication in the map with the key as number of matches.
*/
public void endElement(String uri,String localname,String qname) throws SAXException {
if(qname.equalsIgnoreCase("article") || qname.equalsIgnoreCase("inproceedings") || qname.equalsIgnoreCase("proceedings") || qname.equalsIgnoreCase("book") || qname.equalsIgnoreCase("incollection") || qname.equalsIgnoreCase("masterthesis") || qname.equalsIgnoreCase("phdthesis")) {
if(this.numberOfMatches > 0){
newPubl.addAuthor(this.instanceAuthor);
if(!this.matchedPubl.containsKey(this.numberOfMatches))
{
this.matchedPubl.put(this.numberOfMatches, new ArrayList<Publication>());
}
List<Publication> gotPublication = this.matchedPubl.get(this.numberOfMatches);
gotPublication.add(newPubl);
}
bPubl = false;
this.numberOfMatches = 0;
}
}
}