-
Notifications
You must be signed in to change notification settings - Fork 0
/
indexer.java
executable file
·763 lines (708 loc) · 27.2 KB
/
indexer.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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.*;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.xml.sax.Attributes;
import java.time.Duration;
import java.time.Instant;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import java.util.regex.Pattern;
public class indexer {
public static HashMap<String,Integer> tf=new HashMap<String,Integer>();
public static HashMap<String,Integer> idf=new HashMap<String,Integer>();
public static HashMap<String,Integer> mat=new HashMap<String,Integer>();
public static TreeMap<String, SortedSet<Pair<Integer, String>>> indexer;
public static String valcategory="$1";
public static StringBuffer bodyText = new StringBuffer("");;
public static void main(String[] args) throws Exception {
try {
Instant start = Instant.now();
File inputFile = new File(args[0]);
SAXParserFactory factory = SAXParserFactory.newInstance();
indexer = new TreeMap<String, SortedSet<Pair<Integer, String>>>();
SAXParser saxParser = factory.newSAXParser();
UserHandler userhandler = new UserHandler();
userhandler.sample(tf, idf, indexer, bodyText);
saxParser.parse(inputFile, userhandler);
Map<String,Integer> idf_sorted = sortByValue(idf);
Map<String,Integer> tf_sorted = sortByValue(tf);
for(Map.Entry m:idf.entrySet()){
Integer s1=(Integer) tf.get(m.getKey());
Integer s2 =(Integer) m.getValue();
Integer s3=s1*s2;
mat.put((String)m.getKey(),s3);
}
Map<String,Integer> mat_sorted = sortByValue(mat);
File statText = new File(args[1]);
FileOutputStream is = new FileOutputStream(statText);
OutputStreamWriter osw = new OutputStreamWriter(is);
Writer w = new BufferedWriter(osw);
//for(Map.Entry m:mat_sorted.entrySet()){
//
////System.out.println(m.getKey()+" "+m.getValue());
//}
if(indexer.size()!=0)
{
try {
SPIMI_ALGO.createblock(indexer);
} catch (IOException e) {
e.printStackTrace();
}
indexer.clear();
}
// SPIMI_ALGO.mergeblocks();
String key2 = "%$%";
boolean firstline = true;
for(Map.Entry<String, SortedSet<Pair<Integer, String>>> entry : indexer.entrySet()) {
String key = entry.getKey();
valcategory = key;
//if(!firstline && keycheck(key,key2)==false)
if(!firstline)
w.write("\n");
firstline=false;
SortedSet<Pair<Integer, String>> pr2 = entry.getValue();
w.write(valcategory + ":");
//for(Map.Entry<Integer, SortedSet<Pair<Integer, String>>> entry2 : value.entrySet())
//{
//Integer doc = entry2.getKey();
//w.write(doc.toString() + ":");
// SortedSet<Pair<Integer, String>> pr2 = entry.getValue();
boolean ft=true;
String dq="";
for (Pair<Integer, String> pr:pr2)
{
Integer in = pr.getFirst();
String qr = pr.getSecond();
if(!ft)
dq=",";
w.write(dq+in.toString()+"-"+qr);
ft=false;
}
key2=key;
// w.write(";");
//}
}
w.close();
Instant end = Instant.now();
Duration timeElapsed = Duration.between(start, end);
System.out.println("Time taken: "+ timeElapsed.toMillis() +" milliseconds");
////System.out.println(indexer);
} catch (Exception e) {
e.printStackTrace();
}
}
public static boolean keycheck(String key,String key2){
String PATTERN_TOKEN = "$";
//String s1 = s2.replaceAll("[-:()/^[!|=,?._'{}@+\\[\\]]]", " ");
boolean fd=true;
String tg=".";
String fkey=".",fkey2="..";
HashSet<String> parts2 = new HashSet<String>();
final StringTokenizer normalTokenizer = new StringTokenizer(key,PATTERN_TOKEN);
while(normalTokenizer.hasMoreTokens()){
String sr= normalTokenizer.nextToken().trim();
if(fd)
{
fkey=sr;
}
else{
tg=sr;
}
fd=false;
}
fd=true;
final StringTokenizer normalTokenizer2 = new StringTokenizer(key2,PATTERN_TOKEN);
while(normalTokenizer2.hasMoreTokens()){
String sr= normalTokenizer2.nextToken().trim();
if(fd)
{
fkey2=sr;
}
fd=false;
}
if(fkey.equalsIgnoreCase(fkey2))
{
valcategory="|$" + tg;
return true;
}
else
{
return false;
}
}
private static Map<String, Integer> sortByValue(Map<String, Integer> unsortMap) {
// 1. Convert Map to List of Map
List<Map.Entry<String, Integer>> list =
new LinkedList<Map.Entry<String, Integer>>(unsortMap.entrySet());
// 2. Sort list with Collections.sort(), provide a custom Comparator
// Try switch the o1 o2 position for a different order
Collections.sort(list, new Comparator<Map.Entry<String, Integer>>() {
public int compare(Map.Entry<String, Integer> o1,
Map.Entry<String, Integer> o2) {
return (o1.getValue()).compareTo(o2.getValue());
}
});
// 3. Loop the sorted list and put it into a new insertion order Map LinkedHashMap
Map<String, Integer> sortedMap = new LinkedHashMap<String, Integer>();
for (Map.Entry<String, Integer> entry : list) {
sortedMap.put(entry.getKey(), entry.getValue());
}
return sortedMap;
}
}
class UserHandler extends DefaultHandler {
boolean isTitle = false;
boolean isBody = false;
boolean isRevision = false;
boolean isId = false;
Pattern ptn = Pattern.compile("(d+|[w]+)");
HashSet<String> set=new HashSet<String>();
HashMap<String,Integer> tf;
HashMap<String,Integer> idf;
StringBuffer buffer;
TreeMap<String, SortedSet<Pair<Integer, String>>> index;
Stemmer stemf;
Integer id=-1;
Integer numberword = 0;
GenerateStopwords stopw = new GenerateStopwords();
int counter=1;
public void sample(HashMap<String,Integer> tf,HashMap<String,Integer> idf,TreeMap<String, SortedSet<Pair<Integer, String>>> indexer, StringBuffer bfr){
this.tf=tf;
this.idf=idf;
this.index=indexer;
this.buffer=bfr;
stemf = new Stemmer();
stopw.generatefromfile("stopwords.txt");
}
@Override
public void startElement(String uri,
String localName, String qName, Attributes attributes) throws SAXException {
// ////System.out.println("Roll No : " + qName);
if (qName.equalsIgnoreCase("revision")) {
isRevision = true;
} else if (qName.equalsIgnoreCase("title")) {
buffer = new StringBuffer();
isTitle = true;
} else if (qName.equalsIgnoreCase("text")) {
buffer = new StringBuffer();
isBody = true;
numberword = 0;
}
else if (qName.equalsIgnoreCase("id")) {
// String rollNo = attributes.getValue("id");
// ////System.out.println("Roll No : " + rollNo);
counter++;
if(counter%500==0)
System.out.println(String.valueOf(counter));
isId = true;
}
}
@Override
public void endElement(String uri,
String localName, String qName) throws SAXException {
final String tag = qName.toLowerCase();
if(tag.equalsIgnoreCase("revision"))
{
isRevision = false;
}
else if(tag.equalsIgnoreCase("id"))
{
isId = false;
}
else if(tag.equalsIgnoreCase("title"))
{
isTitle = false;
String s2 = buffer.toString();
s2=s2.toLowerCase();
extractLinks(s2);
extractCategories(s2);
extractReferences(s2);
extractInfoBox(s2);
s2=deleteCitation(s2);
String PATTERN_TOKEN = "\\$%{}[]()`<>='&:,;/.~ ;*\n|\"^_-+!?#\t@";
//String s1 = s2.replaceAll("[-:()/^[!|=,?._'{}@+\\[\\]]]", " ");
HashSet<String> parts2 = new HashSet<String>();
HashMap<String, Integer> countw= new HashMap<String,Integer>();
final StringTokenizer normalTokenizer = new StringTokenizer(s2,PATTERN_TOKEN);
while(normalTokenizer.hasMoreTokens()){
String nk=normalTokenizer.nextToken().trim();
parts2.add(nk);
if(countw.get(nk)==null){
countw.put(nk,1);
}
else{
countw.put(nk,countw.get(nk)+1);
}
}
Pattern rp = Pattern.compile("^[a-zA-Z0-9]*$");
List<String> parts = new ArrayList<String>();
for(String p:parts2)
{
Matcher m = rp.matcher(p);
if (m.find())
parts.add(p);
}
////System.out.println(parts.toString());
for(String p:parts){
String p2=p;
stemf.add(p.toCharArray(), p.length());
stemf.stem();
p = stemf.toString();
if(p.length()>0 && !stopw.checkStopWord(p) && !stopw.checkStopWord(p2))
{
String p3=p;
p = p + "$T";
//System.out.println(p+ " ");
if(!index.containsKey(p))
{
Pair<Integer,String> r = new Pair<Integer,String>(id,String.valueOf(countw.get(p2)));
SortedSet<Pair<Integer,String>> ls = new TreeSet<Pair<Integer,String>>();
ls.add(r);
// index.put(id,ls);
index.put(p,ls);
}
else{
SortedSet<Pair<Integer,String>> ls = index.get(p);
Pair<Integer,String> r = new Pair<Integer,String>(id,String.valueOf(countw.get(p2)));
ls.add(r);
// f.put(id,ls);
index.put(p,ls);
}
}
numberword++;
}
if(index.size()>=1000000)
{
try {
SPIMI_ALGO.createblock(index);
} catch (Exception e) {
e.printStackTrace();
}
index.clear();
}
}
else if(tag.equalsIgnoreCase("text"))
{
//System.out.println(String.valueOf(index.size()));
isBody = false;
String s2 = buffer.toString();
s2=s2.toLowerCase();
extractLinks(s2);
extractCategories(s2);
extractReferences(s2);
extractInfoBox(s2);
s2=deleteCitation(s2);
String PATTERN_TOKEN = "\\$%{}[]()`<>='&:,;/.~ ;*\n|\"^_-+!?#\t@";
//String s1 = s2.replaceAll("[-:()/^[!|=,?._'{}@+\\[\\]]]", " ");
HashSet<String> parts2 = new HashSet<String>();
HashMap<String, Integer> countw= new HashMap<String,Integer>();
final StringTokenizer normalTokenizer = new StringTokenizer(s2,PATTERN_TOKEN);
while(normalTokenizer.hasMoreTokens()){
String nk=normalTokenizer.nextToken().trim();
parts2.add(nk);
if(countw.get(nk)==null){
countw.put(nk,1);
}
else{
countw.put(nk,countw.get(nk)+1);
}
}
Pattern rp = Pattern.compile("^[a-zA-Z0-9]*$");
List<String> parts = new ArrayList<String>();
for(String p:parts2)
{
Matcher m = rp.matcher(p);
if (m.find())
parts.add(p);
}
////System.out.println(parts.toString());
for(String p:parts){
String p2=p;
stemf.add(p.toCharArray(), p.length());
stemf.stem();
p = stemf.toString();
if(p.length()>0 && !stopw.checkStopWord(p) && !stopw.checkStopWord(p2) && p.charAt(0)!='0')
{
String p3=p;
p = p + "$B";
//System.out.println(p+ " ");
if(!index.containsKey(p))
{
Pair<Integer,String> r = new Pair<Integer,String>(id,String.valueOf(countw.get(p2)));
SortedSet<Pair<Integer,String>> ls = new TreeSet<Pair<Integer,String>>();
ls.add(r);
// index.put(id,ls);
index.put(p,ls);
}
else{
SortedSet<Pair<Integer,String>> ls = index.get(p);
Pair<Integer,String> r = new Pair<Integer,String>(id,String.valueOf(countw.get(p2)));
ls.add(r);
// f.put(id,ls);
index.put(p,ls);
}
}
numberword++;
}
if(index.size()>=500000)
{
try {
SPIMI_ALGO.createblock(index);
} catch (Exception e) {
e.printStackTrace();
}
index.clear();
}
////System.out.println(String.valueOf(id));
}
}
@Override
public void characters(char ch[], int start, int length) throws SAXException {
set=new HashSet<String>();
if (isTitle) {
String s2 = new String(ch, start, length);
buffer.append(s2);
// ////System.out.println("Title: "
// + new String(ch, start, length));
} else if (isBody) {
String s2 = new String(ch, start, length);
buffer.append(s2);
} else if (isRevision) {
// ////System.out.println("Revision: " + new String(ch, start, length));
} else if (isId && !isRevision) {
String s2 = new String(ch, start, length);
s2=s2.toLowerCase();
id=Integer.valueOf(s2);
//////System.out.println(s2);
}
}
public void extractLinks(final String content){
//Tokenize the link. [[user innovation]]
final Pattern linkPattern = Pattern.compile("\\[\\[(.*?)\\]\\]", Pattern.MULTILINE);
final Matcher matcher = linkPattern.matcher(content);
while(matcher.find()) {
final String [] match = matcher.group(1).split("\\|");
if(match == null || match.length == 0) continue;
final String link = match[0];
if(link.contains(":") == false) {
String s2=link.toLowerCase();
//System.out.println(s2);
String PATTERN_TOKEN = "\\$%{}[]()`<>='&:,;/.~ ;*\n|\"^_-+!?#\t@";
//String s1 = s2.replaceAll("[-:()/^[!|=,?._'{}@+\\[\\]]]", " ");
HashSet<String> parts2 = new HashSet<String>();
HashMap<String, Integer> countw= new HashMap<String,Integer>();
final StringTokenizer normalTokenizer = new StringTokenizer(s2,PATTERN_TOKEN);
while(normalTokenizer.hasMoreTokens()){
String nk=normalTokenizer.nextToken().trim();
parts2.add(nk);
if(countw.get(nk)==null){
countw.put(nk,1);
}
else{
countw.put(nk,countw.get(nk)+1);
}
}
Pattern rp = Pattern.compile("^[a-zA-Z0-9]*$");
List<String> parts = new ArrayList<String>();
for(String p:parts2)
{
Matcher m = rp.matcher(p);
if (m.find())
parts.add(p);
}
////System.out.println(parts.toString());
for(String p:parts){
String p2=p;
stemf.add(p.toCharArray(), p.length());
stemf.stem();
p = stemf.toString();
if(p.length()>0 && !stopw.checkStopWord(p) && !stopw.checkStopWord(p2) && p.charAt(0)!='0')
{
String p3=p;
p = p + "$L";
//System.out.println(p+ " ");
if(!index.containsKey(p))
{
Pair<Integer,String> r = new Pair<Integer,String>(id,String.valueOf(countw.get(p2)));
SortedSet<Pair<Integer,String>> ls = new TreeSet<Pair<Integer,String>>();
ls.add(r);
// index.put(id,ls);
index.put(p,ls);
}
else{
SortedSet<Pair<Integer,String>> ls = index.get(p);
Pair<Integer,String> r = new Pair<Integer,String>(id,String.valueOf(countw.get(p2)));
ls.add(r);
// f.put(id,ls);
index.put(p,ls);
}
}
}
}
}
}
public void extractCategories(final String content){
//Tokenize the categories
final Pattern categoryPattern = Pattern.compile("\\[\\[category:(.*?)\\]\\]", Pattern.MULTILINE);
final Matcher matcher = categoryPattern.matcher(content);
while(matcher.find()) {
final String [] match = matcher.group(1).split("\\|");
String s2=match[0].toLowerCase();
//System.out.println(s2);
String PATTERN_TOKEN = "\\$%{}[]()`<>='&:,;/.~ ;*\n|\"^_-+!?#\t@";
//String s1 = s2.replaceAll("[-:()/^[!|=,?._'{}@+\\[\\]]]", " ");
HashSet<String> parts2 = new HashSet<String>();
HashMap<String, Integer> countw= new HashMap<String,Integer>();
final StringTokenizer normalTokenizer = new StringTokenizer(s2,PATTERN_TOKEN);
while(normalTokenizer.hasMoreTokens()){
String nk=normalTokenizer.nextToken().trim();
parts2.add(nk);
if(countw.get(nk)==null){
countw.put(nk,1);
}
else{
countw.put(nk,countw.get(nk)+1);
}
}
Pattern rp = Pattern.compile("^[a-zA-Z0-9]*$");
List<String> parts = new ArrayList<String>();
for(String p:parts2)
{
Matcher m = rp.matcher(p);
if (m.find())
parts.add(p);
}
////System.out.println(parts.toString());
for(String p:parts){
String p2=p;
stemf.add(p.toCharArray(), p.length());
stemf.stem();
p = stemf.toString();
if(p.length()>0 && !stopw.checkStopWord(p) && !stopw.checkStopWord(p2) && p.charAt(0)!='0')
{
String p3=p;
p = p + "$C";
//System.out.println(p+ " ");
if(!index.containsKey(p))
{
Pair<Integer,String> r = new Pair<Integer,String>(id,String.valueOf(countw.get(p2)));
SortedSet<Pair<Integer,String>> ls = new TreeSet<Pair<Integer,String>>();
ls.add(r);
// index.put(id,ls);
index.put(p,ls);
}
else{
SortedSet<Pair<Integer,String>> ls = index.get(p);
Pair<Integer,String> r = new Pair<Integer,String>(id,String.valueOf(countw.get(p2)));
ls.add(r);
// f.put(id,ls);
index.put(p,ls);
}
}
}
// extractToken(match[0],'C');
}
}
public void extractInfoBox(final String content){
//Tokenize the infobox.
final String infoBoxPatterm = "{{infobox";
//Find the start pos and end pos of info box.
int startPos = content.indexOf(infoBoxPatterm);
if(startPos < 0) return ;
int bracketCount = 2;
int endPos = startPos + infoBoxPatterm.length();
for(; endPos < content.length(); endPos++) {
switch(content.charAt(endPos)) {
case '}':
bracketCount--;
break;
case '{':
bracketCount++;
break;
default:
}
if(bracketCount == 0) break;
}
if(endPos+1 >= content.length()) return;
//Filter the infobox
String infoBoxText = content.substring(startPos, endPos+1);
infoBoxText = deleteCitation(infoBoxText);
infoBoxText = infoBoxText.replaceAll(">", ">");
infoBoxText = infoBoxText.replaceAll("<", "<");
infoBoxText = infoBoxText.replaceAll("<ref.*?>.*?</ref>", " ");
infoBoxText = infoBoxText.replaceAll("</?.*?>", " ");
String s2=infoBoxText.toLowerCase();
//System.out.println(s2);
String PATTERN_TOKEN = "\\$%{}[]()`<>='&:,;/.~ ;*\n|\"^_-+!?#\t@";
//String s1 = s2.replaceAll("[-:()/^[!|=,?._'{}@+\\[\\]]]", " ");
HashSet<String> parts2 = new HashSet<String>();
HashMap<String, Integer> countw= new HashMap<String,Integer>();
final StringTokenizer normalTokenizer = new StringTokenizer(s2,PATTERN_TOKEN);
while(normalTokenizer.hasMoreTokens()){
String nk=normalTokenizer.nextToken().trim();
parts2.add(nk);
if(countw.get(nk)==null){
countw.put(nk,1);
}
else{
countw.put(nk,countw.get(nk)+1);
}
}
Pattern rp = Pattern.compile("^[a-zA-Z0-9]*$");
List<String> parts = new ArrayList<String>();
for(String p:parts2)
{
Matcher m = rp.matcher(p);
if (m.find())
parts.add(p);
}
////System.out.println(parts.toString());
for(String p:parts){
String p2=p;
stemf.add(p.toCharArray(), p.length());
stemf.stem();
p = stemf.toString();
if(p.length()>0 && !stopw.checkStopWord(p) && !stopw.checkStopWord(p2))
{
String p3=p;
p = p + "$I";
//System.out.println(p+ " ");
if(!index.containsKey(p))
{
Pair<Integer,String> r = new Pair<Integer,String>(id,String.valueOf(countw.get(p2)));
SortedSet<Pair<Integer,String>> ls = new TreeSet<Pair<Integer,String>>();
ls.add(r);
// index.put(id,ls);
index.put(p,ls);
}
else{
SortedSet<Pair<Integer,String>> ls = index.get(p);
Pair<Integer,String> r = new Pair<Integer,String>(id,String.valueOf(countw.get(p2)));
ls.add(r);
// f.put(id,ls);
index.put(p,ls);
}
}
}
}
public String deleteCitation(final String content) {
//Deletes the citation from the content.
final String CITE_PATTERN = "{{cite";
//Find the start pos and end pos of citation.
int startPos = content.indexOf(CITE_PATTERN);
if(startPos < 0) return content;
int bracketCount = 2;
int endPos = startPos + CITE_PATTERN.length();
for(; endPos < content.length(); endPos++) {
switch(content.charAt(endPos)) {
case '}':
bracketCount--;
break;
case '{':
bracketCount++;
break;
default:
}
if(bracketCount == 0) break;
}
String soltxt = "";
if(content!=null && startPos>=1 && endPos>=0)
{
final String text = content.substring(0, startPos-1) + content.substring(endPos);
soltxt = text;
}
//Discard the citation and search for remaining citations.
return deleteCitation(soltxt);
}
public String extractReferences(final String content) {
//Extracts the citation from the content.
final String CITE_PATTERN = "{{cite";
//Find the start pos and end pos of citation.
int startPos = content.indexOf(CITE_PATTERN);
if(startPos < 0) return content;
int bracketCount = 2;
int endPos = startPos + CITE_PATTERN.length();
for(; endPos < content.length(); endPos++) {
switch(content.charAt(endPos)) {
case '}':
bracketCount--;
break;
case '{':
bracketCount++;
break;
default:
}
if(bracketCount == 0) break;
}
//Extract the citation and search for remaining citations.
//extractToken(content.substring(startPos, endPos),'R');
String s2=content.substring(startPos, endPos);
//System.out.println(s2);
String PATTERN_TOKEN = "\\$%{}[]()`<>='&:,;/.~ ;*\n|\"^_-+!?#\t@";
//String s1 = s2.replaceAll("[-:()/^[!|=,?._'{}@+\\[\\]]]", " ");
HashSet<String> parts2 = new HashSet<String>();
HashMap<String, Integer> countw= new HashMap<String,Integer>();
final StringTokenizer normalTokenizer = new StringTokenizer(s2,PATTERN_TOKEN);
while(normalTokenizer.hasMoreTokens()){
String nk=normalTokenizer.nextToken().trim();
parts2.add(nk);
if(countw.get(nk)==null){
countw.put(nk,1);
}
else{
countw.put(nk,countw.get(nk)+1);
}
}
Pattern rp = Pattern.compile("^[a-zA-Z0-9]*$");
List<String> parts = new ArrayList<String>();
for(String p:parts2)
{
Matcher m = rp.matcher(p);
if (m.find())
parts.add(p);
}
////System.out.println(parts.toString());
for(String p:parts){
String p2=p;
stemf.add(p.toCharArray(), p.length());
stemf.stem();
p = stemf.toString();
if(p.length()>0 && !stopw.checkStopWord(p) && !stopw.checkStopWord(p2) && p.charAt(0)!='0')
{
String p3=p;
p = p + "$R";
//System.out.println(p+ " ");
if(!index.containsKey(p))
{
Pair<Integer,String> r = new Pair<Integer,String>(id,String.valueOf(countw.get(p2)));
SortedSet<Pair<Integer,String>> ls = new TreeSet<Pair<Integer,String>>();
ls.add(r);
// index.put(id,ls);
index.put(p,ls);
}
else{
SortedSet<Pair<Integer,String>> ls = index.get(p);
Pair<Integer,String> r = new Pair<Integer,String>(id,String.valueOf(countw.get(p2)));
ls.add(r);
// f.put(id,ls);
index.put(p,ls);
}
}
}
String soltxt = "";
if(content!=null && startPos>=1 && endPos>=0)
{
final String text = content.substring(0, startPos-1) + content.substring(endPos);
soltxt = text;
}
return extractReferences(soltxt);
}
}