blob: b4ec124dad5a1493ba05c92d4cbda087b49072fd [file] [log] [blame]
Eliza Margaretha67a88572014-11-04 14:38:56 +00001package de.ids_mannheim.korap.highlight;
2
3import static org.junit.Assert.assertEquals;
4import static org.junit.Assert.fail;
5
6import java.io.BufferedReader;
7import java.io.FileReader;
8import java.io.IOException;
9
Eliza Margarethab698f822014-11-12 10:14:21 +000010import org.apache.lucene.search.spans.SpanQuery;
Eliza Margaretha67a88572014-11-04 14:38:56 +000011import org.junit.Test;
12
Nils Diewalda14ecd62015-02-26 21:00:20 +000013import de.ids_mannheim.korap.KrillIndex;
Nils Diewald392bcf32015-02-26 20:01:17 +000014import de.ids_mannheim.korap.response.Match;
Nils Diewald0339d462015-02-26 14:53:56 +000015import de.ids_mannheim.korap.KrillQuery;
Nils Diewald884dbcf2015-02-27 17:02:28 +000016import de.ids_mannheim.korap.response.Result;
Nils Diewaldbbd39a52015-02-23 19:56:57 +000017import de.ids_mannheim.korap.Krill;
Eliza Margaretha67a88572014-11-04 14:38:56 +000018import de.ids_mannheim.korap.query.SpanNextQuery;
19import de.ids_mannheim.korap.query.wrap.SpanQueryWrapper;
20import de.ids_mannheim.korap.util.QueryException;
21
22
23public class TestClass {
Nils Diewaldbb33da22015-03-04 16:24:25 +000024 KrillIndex ki;
25 Result kr;
26 Krill ks;
Eliza Margaretha67a88572014-11-04 14:38:56 +000027
Nils Diewald11e91862014-11-12 16:29:18 +000028
Nils Diewald7d320642014-11-12 17:39:42 +000029 @Test
Nils Diewaldbb33da22015-03-04 16:24:25 +000030 public void queryJSONpoly1 () throws QueryException, IOException {
Nils Diewald7d320642014-11-12 17:39:42 +000031
Nils Diewaldbb33da22015-03-04 16:24:25 +000032 String jsonPath = getClass().getResource("/queries/poly1.json")
33 .getFile();
34 String jsonQuery = readFile(jsonPath);
35 SpanQueryWrapper sqwi = new KrillQuery("tokens").fromJson(jsonQuery);
36
37 SpanNextQuery sq = (SpanNextQuery) sqwi.toQuery();
38 //System.out.println(sq.toString());
39
40 ki = new KrillIndex();
41 ki.addDoc(getClass().getResourceAsStream("/wiki/JJJ-00785.json.gz"),
42 true);
43 ki.addDoc(getClass().getResourceAsStream("/wiki/DDD-01402.json.gz"),
44 true);
45 ki.commit();
46 kr = ki.search(sq, (short) 10);
47
48 assertEquals(61, kr.getMatch(0).getStartPos());
49 assertEquals(64, kr.getMatch(0).getEndPos());
50 assertEquals(
51 "... Bruckner (Wien) und Mathis Lussy (Paris). [{1:Inspiriert} "
52 + "{2:durch die}] additiven Modelle arabischer Rhythmik (er half ...",
53 kr.getMatch(0).getSnippetBrackets());
54
55 assertEquals(31, kr.getMatch(1).getStartPos());
56 assertEquals(34, kr.getMatch(1).getEndPos());
57 assertEquals(
58 "... des Sendens wird ein unhörbarer Unterton [{1:mitgesendet}, "
59 + "{2:auf den}] das angesprochene Funkgerät reagiert. Die Abkürzung ...",
60 kr.getMatch(1).getSnippetBrackets());
61 }
62
63
64 @Test
65 public void queryJSONpoly4 () throws QueryException, IOException {
66
67 String jsonPath = getClass().getResource("/queries/poly4.json")
68 .getFile();
69 String jsonQuery = readFile(jsonPath);
70 SpanQueryWrapper sqwi = new KrillQuery("tokens").fromJson(jsonQuery);
71 SpanQuery sq = sqwi.toQuery();
72
73 // System.out.println(sq.toString());
74
75
76 ki = new KrillIndex();
77 ki.addDoc(getClass().getResourceAsStream("/wiki/SSS-09803.json.gz"),
78 true);
79
80 ki.commit();
81 kr = ki.search(sq, (short) 10);
82
83 /*
84 for (Match km : kr.getMatches()){
85 System.out.println(km.getStartPos() +","+km.getEndPos()+" "
86 +km.getSnippetBrackets()
87 );
88 }
89 */
90 assertEquals((long) 495, kr.getTotalResults());
91 assertEquals(3, kr.getMatch(0).getStartPos());
92 assertEquals(5, kr.getMatch(0).getEndPos());
93
94 //fail("Tests have to be updated");
95 }
96
97
98 private String readFile (String path) {
99 StringBuilder sb = new StringBuilder();
100 try {
101 BufferedReader in = new BufferedReader(new FileReader(path));
102 String str;
103 while ((str = in.readLine()) != null) {
104 sb.append(str);
105 };
106 in.close();
107 }
108 catch (IOException e) {
109 fail(e.getMessage());
110 }
111 return sb.toString();
112 }
Eliza Margaretha67a88572014-11-04 14:38:56 +0000113}