Projekt

Obecné

Profil

« Předchozí | Další » 

Revize 3ef07892

Přidáno uživatelem Jitka Poubová před téměř 5 roky(ů)

Re #7805: Vracení nalezených dokumentů na query
- předěláno dle struktury na Wiki (stránka Architektura FE)

Zobrazit rozdíly:

be/fulltextsearch/src/main/java/cz/zcu/kiv/aswi/fulltextsearch/IndexController.java
2 2

  
3 3
import cz.zcu.kiv.aswi.fulltextsearch.model.DocumentResponse;
4 4
import cz.zcu.kiv.aswi.fulltextsearch.model.Query;
5
import cz.zcu.kiv.aswi.fulltextsearch.model.QueryDocumentResponse;
5
import cz.zcu.kiv.aswi.fulltextsearch.model.SearchResponse;
6 6
import cz.zcu.kiv.aswi.fulltextsearch.model.ResponseMessage;
7 7
import org.springframework.http.HttpStatus;
8 8
import org.springframework.http.ResponseEntity;
......
20 20
    private SolrService solrService = new SolrService();
21 21

  
22 22
    @PostMapping("/")
23
    public List<QueryDocumentResponse> index(@RequestBody Query query) {
23
    public SearchResponse index(@RequestBody Query query) {
24 24
        try {
25 25
            return solrService.query(query.getQuery());
26 26
        } catch (Exception e) {
be/fulltextsearch/src/main/java/cz/zcu/kiv/aswi/fulltextsearch/SolrService.java
1 1
package cz.zcu.kiv.aswi.fulltextsearch;
2 2

  
3
import cz.zcu.kiv.aswi.fulltextsearch.document.PcGts;
4
import cz.zcu.kiv.aswi.fulltextsearch.document.TextLine;
5
import cz.zcu.kiv.aswi.fulltextsearch.document.TextRegion;
6
import cz.zcu.kiv.aswi.fulltextsearch.model.QueryDocumentResponse;
3
import cz.zcu.kiv.aswi.fulltextsearch.document.*;
4
import cz.zcu.kiv.aswi.fulltextsearch.model.SearchResponse;
7 5
import org.apache.solr.client.solrj.SolrQuery;
8 6
import org.apache.solr.client.solrj.SolrServerException;
9 7
import org.apache.solr.client.solrj.impl.HttpSolrClient;
......
119 117
        solrClient.commit();
120 118
    }
121 119

  
122
    public List<QueryDocumentResponse> query(String query) throws IOException, SolrServerException  {
120
    public SearchResponse query(String query) throws IOException, SolrServerException  {
123 121
        SolrQuery solrQuery = new SolrQuery();
124 122
        solrQuery.set("q", FIELD_TEXT_REGION + ":" + query);
125 123
        solrQuery.setHighlight(true);
......
129 127
        SolrDocumentList docList = response.getResults();
130 128
        Map<String, Map<String, List<String>>> highlight = response.getHighlighting();
131 129

  
132
        List<QueryDocumentResponse> results = new ArrayList<>();
130
        SearchResponse searchResponse = new SearchResponse();
131
        searchResponse.setExpression(query);
132

  
133
        List<TextRegion> textRegions = new LinkedList<>();
133 134
        for (SolrDocument solrDocument: docList) {
134
            QueryDocumentResponse documentResponse = new QueryDocumentResponse();
135
            TextRegion textRegion = new TextRegion();
136

  
137
            // text, coords
138
            textRegion.setRegionText((String) solrDocument.getFieldValue(FIELD_TEXT_REGION));
139
            String pointsString = (String) solrDocument.getFieldValue(FIELD_TEXT_REGION_COORDS);
140
            Point[] points = Coords.parsePointString(pointsString);
141
            textRegion.setRegionCoords(new Coords(points));
135 142

  
136
            // filename, text region
137
            documentResponse.setDoc_filename((String) solrDocument.getFieldValue(FIELD_DOC_FILENAME));
138
            documentResponse.setText_region((String) solrDocument.getFieldValue(FIELD_TEXT_REGION));
139
            documentResponse.setText_region_coords((String) solrDocument.getFieldValue(FIELD_TEXT_REGION_COORDS));
143
            // filename
144
            String documentName = ((String) solrDocument.getFieldValue(FIELD_DOC_FILENAME)).replaceAll(".xml", "");
145
            textRegion.setDocumentName(documentName);
140 146

  
141 147
            // text lines
142 148
            Map<String, List<String>> map = highlight.get(solrDocument.getFieldValue("id").toString());
143 149
            Set<String> linesNames = map.keySet();
144
            Map<String, String> linesMap = new HashMap<>();
150
            List<TextLine> textLines = new LinkedList<>();
145 151
            for (String lineName: linesNames) {
146
                String value = (String) solrDocument.getFieldValue(lineName);
152
                TextLine textLine = new TextLine();
153

  
154
                textLine.setLineText((String) solrDocument.getFieldValue(lineName));
155

  
147 156
                String coordsValue = (String) solrDocument.getFieldValue(lineName + SUFFIX_COORDS);
148
                linesMap.put(lineName, value);
149
                linesMap.put(lineName + SUFFIX_COORDS, coordsValue);
157
                Point[] linePoints = Coords.parsePointString(coordsValue);
158
                textLine.setLineCoords(new Coords(linePoints));
159

  
160
                textLines.add(textLine);
150 161
            }
151
            documentResponse.setText_lines(linesMap);
162

  
163
            textRegion.setTextLines(textLines.toArray(new TextLine[0]));
152 164

  
153 165
            // image
154 166
            String imgFilename = (String) solrDocument.getFieldValue(FIELD_IMG_FILENAME);
155 167
            InputStream is = getClass().getClassLoader().getResourceAsStream(SolrService.DATA_DIR + imgFilename);
156 168
            String encodedImage = Base64.getEncoder().encodeToString(is.readAllBytes());
157
            documentResponse.setImage(encodedImage);
169
            textRegion.setImageCut(encodedImage); // todo - only temporary - setting imageCut to whole document image
158 170

  
159
            results.add(documentResponse);
171
            textRegions.add(textRegion);
160 172
        }
161 173

  
162
        return results;
174
        searchResponse.setTextRegions(textRegions.toArray(new TextRegion[0]));
175

  
176
        return searchResponse;
163 177
    }
164 178

  
165 179
    private void addDocument(PcGts document) throws IOException, SolrServerException {
......
173 187
            solrInputDocument.addField(FIELD_DOC_FILENAME, document.getFilename());
174 188
            solrInputDocument.addField(FIELD_IMG_FILENAME, document.getFilename().replaceAll(".xml", ".png")); // todo
175 189
            solrInputDocument.addField(FIELD_TEXT_REGION, textRegion.getTextEquiv().getUnicode());
176
            solrInputDocument.addField(FIELD_TEXT_REGION_COORDS, textRegion.getCoords().getPoints());
190
            solrInputDocument.addField(FIELD_TEXT_REGION_COORDS, textRegion.getRegionCoords().getPointsString());
177 191

  
178
            List<TextLine> textLines = textRegion.getTextLines();
179
            if (textLines == null || textLines.size() == 0) continue;
180
            for (int j = 0; j < textLines.size(); j++) {
181
                TextLine textLine = textLines.get(j);
192
            TextLine[] textLines = textRegion.getTextLines();
193
            if (textLines == null || textLines.length == 0) continue;
194
            for (int j = 0; j < textLines.length; j++) {
195
                TextLine textLine = textLines[j];
182 196
                String prefix = PREFIX_TEXT_LINE + j;
183 197

  
184 198
                addFieldToSolr(prefix);
185 199
                solrInputDocument.addField(prefix, textLine.getTextEquiv().getUnicode());
186 200

  
187 201
                addFieldToSolr(prefix + SUFFIX_COORDS);
188
                solrInputDocument.addField(prefix + SUFFIX_COORDS, textLine.getCoords().getPoints());
202
                solrInputDocument.addField(prefix + SUFFIX_COORDS, textLine.getLineCoords().getPointsString());
189 203
            }
190 204

  
191 205
            solrClient.add(solrInputDocument);
be/fulltextsearch/src/main/java/cz/zcu/kiv/aswi/fulltextsearch/document/Coords.java
1 1
package cz.zcu.kiv.aswi.fulltextsearch.document;
2 2

  
3
import com.fasterxml.jackson.annotation.JsonIgnore;
4

  
3 5
import javax.xml.bind.annotation.XmlAccessType;
4 6
import javax.xml.bind.annotation.XmlAccessorType;
5 7
import javax.xml.bind.annotation.XmlAttribute;
8
import java.util.LinkedList;
9
import java.util.List;
6 10

  
7 11
@XmlAccessorType(XmlAccessType.FIELD)
8 12
public class Coords {
9 13

  
10 14
    @XmlAttribute(name = "points")
11
    private String points;
15
    @JsonIgnore
16
    private String pointsString;
17

  
18
    private Point[] points;
19

  
20
    public Coords() {
21

  
22
    }
23

  
24
    public Coords(Point[] points) {
25
        this.points = points;
26
    }
27

  
28
    public String getPointsString() {
29
        return pointsString;
30
    }
31

  
32
    public void setPointsString(String pointsString) {
33
        this.pointsString = pointsString;
34
    }
12 35

  
13
    public String getPoints() {
36
    public Point[] getPoints() {
14 37
        return points;
15 38
    }
16 39

  
17
    public void setPoints(String points) {
40
    public void setPoints(Point[] points) {
18 41
        this.points = points;
19 42
    }
20 43

  
21 44
    @Override
22 45
    public String toString() {
23
        return this.points;
46
        return this.pointsString;
47
    }
48

  
49
    public static Point[] parsePointString(String pointsString) {
50
        List<Point> pointList = new LinkedList<>();
51
        if (pointsString == null || pointsString.isEmpty()) return pointList.toArray(new Point[0]);
52

  
53
        String[] simplePoints = pointsString.split(" ");
54
        for (String pointString: simplePoints) {
55
            String[] numbers = pointString.split(",");
56

  
57
            if (numbers.length < 2) continue;
58
            int x = Integer.parseInt(numbers[0]);
59
            int y = Integer.parseInt(numbers[1]);
60

  
61
            Point p = new Point(x, y);
62
            pointList.add(p);
63
        }
64

  
65
        return pointList.toArray(new Point[0]);
24 66
    }
25 67
}
be/fulltextsearch/src/main/java/cz/zcu/kiv/aswi/fulltextsearch/document/Page.java
31 31

  
32 32
        for (int i = 0; i < textRegions.size(); i++) {
33 33
            TextRegion textRegion = textRegions.get(i);
34
            textRegion.setFilename(filename);
34
            textRegion.setDocumentName(filename);
35 35
        }
36 36
    }
37 37
}
be/fulltextsearch/src/main/java/cz/zcu/kiv/aswi/fulltextsearch/document/Point.java
1
package cz.zcu.kiv.aswi.fulltextsearch.document;
2

  
3
public class Point {
4

  
5
    private int x;
6
    private int y;
7

  
8
    public Point(int x, int y) {
9
        this.x = x;
10
        this.y = y;
11
    }
12

  
13
    public int getX() {
14
        return x;
15
    }
16

  
17
    public void setX(int x) {
18
        this.x = x;
19
    }
20

  
21
    public int getY() {
22
        return y;
23
    }
24

  
25
    public void setY(int y) {
26
        this.y = y;
27
    }
28
}
be/fulltextsearch/src/main/java/cz/zcu/kiv/aswi/fulltextsearch/document/TextLine.java
1 1
package cz.zcu.kiv.aswi.fulltextsearch.document;
2 2

  
3
import com.fasterxml.jackson.annotation.JsonIgnore;
4

  
3 5
import javax.xml.bind.annotation.XmlAccessType;
4 6
import javax.xml.bind.annotation.XmlAccessorType;
5 7
import javax.xml.bind.annotation.XmlElement;
6
import java.util.List;
7 8

  
8 9
@XmlAccessorType(XmlAccessType.PROPERTY)
9 10
public class TextLine {
10 11

  
11
    private Coords coords;
12
    private Coords lineCoords;
13
    private String lineText;
14
    private TextWord[] textWords;
15

  
16
    @JsonIgnore
12 17
    private TextEquiv textEquiv;
13
    private List<Word> words;
14 18

  
15
    public Coords getCoords() {
16
        return coords;
19
    public Coords getLineCoords() {
20
        return lineCoords;
17 21
    }
18 22

  
19 23
    @XmlElement(name = "Coords")
20
    public void setCoords(Coords coords) {
21
        this.coords = coords;
24
    public void setLineCoords(Coords lineCoords) {
25
        this.lineCoords = lineCoords;
22 26
    }
23 27

  
24 28
    public TextEquiv getTextEquiv() {
......
30 34
        this.textEquiv = textEquiv;
31 35
    }
32 36

  
33
    public List<Word> getWords() {
34
        return words;
37
    public TextWord[] getTextWords() {
38
        return textWords;
35 39
    }
36 40

  
37 41
    @XmlElement(name = "Word")
38
    public void setWords(List<Word> words) {
39
        this.words = words;
42
    public void setTextWords(TextWord[] textWords) {
43
        this.textWords = textWords;
40 44
    }
41 45

  
46
    public String getLineText() {
47
        return lineText;
48
    }
49

  
50
    public void setLineText(String lineText) {
51
        this.lineText = lineText;
52
    }
42 53
}
be/fulltextsearch/src/main/java/cz/zcu/kiv/aswi/fulltextsearch/document/TextRegion.java
1 1
package cz.zcu.kiv.aswi.fulltextsearch.document;
2 2

  
3
import com.fasterxml.jackson.annotation.JsonIgnore;
4

  
3 5
import javax.xml.bind.annotation.XmlAccessType;
4 6
import javax.xml.bind.annotation.XmlAccessorType;
5 7
import javax.xml.bind.annotation.XmlElement;
6
import java.util.List;
7 8

  
8 9
@XmlAccessorType(XmlAccessType.PROPERTY)
9 10
public class TextRegion {
10 11

  
11
    private Coords coords;
12
    private TextEquiv textEquiv;
13
    private List<TextLine> textLines;
12
    private String imageCut;
13
    private Coords regionCoords;
14
    private String regionText;
15
    private TextLine[] textLines;
16
    private String documentName;
14 17

  
15
    private String filename;
18
    @JsonIgnore
19
    private TextEquiv textEquiv;
16 20

  
17
    public Coords getCoords() {
18
        return coords;
21
    public Coords getRegionCoords() {
22
        return regionCoords;
19 23
    }
20 24

  
21 25
    @XmlElement(name = "Coords")
22
    public void setCoords(Coords coords) {
23
        this.coords = coords;
26
    public void setRegionCoords(Coords regionCoords) {
27
        this.regionCoords = regionCoords;
24 28
    }
25 29

  
26 30
    public TextEquiv getTextEquiv() {
......
32 36
        this.textEquiv = textEquiv;
33 37
    }
34 38

  
35
    public List<TextLine> getTextLines() {
39
    public TextLine[] getTextLines() {
36 40
        return textLines;
37 41
    }
38 42

  
39 43
    @XmlElement(name = "TextLine")
40
    public void setTextLines(List<TextLine> textLines) {
44
    public void setTextLines(TextLine[] textLines) {
41 45
        this.textLines = textLines;
42 46
    }
43 47

  
44
    public String getFilename() {
45
        return filename;
48
    public String getDocumentName() {
49
        return documentName;
50
    }
51

  
52
    public void setDocumentName(String documentName) {
53
        this.documentName = documentName;
46 54
    }
47 55

  
48
    public void setFilename(String filename) {
49
        this.filename = filename;
56
    public String getImageCut() {
57
        return imageCut;
50 58
    }
51 59

  
60
    public void setImageCut(String imageCut) {
61
        this.imageCut = imageCut;
62
    }
63

  
64
    public String getRegionText() {
65
        return regionText;
66
    }
67

  
68
    public void setRegionText(String regionText) {
69
        this.regionText = regionText;
70
    }
52 71
}
be/fulltextsearch/src/main/java/cz/zcu/kiv/aswi/fulltextsearch/document/TextWord.java
1
package cz.zcu.kiv.aswi.fulltextsearch.document;
2

  
3
import javax.xml.bind.annotation.XmlAccessType;
4
import javax.xml.bind.annotation.XmlAccessorType;
5
import javax.xml.bind.annotation.XmlElement;
6

  
7
@XmlAccessorType(XmlAccessType.PROPERTY)
8
public class TextWord {
9

  
10
    private Coords wordCoords;
11
    private TextEquiv textEquiv;
12

  
13
    public Coords getWordCoords() {
14
        return wordCoords;
15
    }
16

  
17
    @XmlElement(name = "Coords")
18
    public void setWordCoords(Coords wordCoords) {
19
        this.wordCoords = wordCoords;
20
    }
21

  
22
    public TextEquiv getTextEquiv() {
23
        return textEquiv;
24
    }
25

  
26
    @XmlElement(name = "TextEquiv")
27
    public void setTextEquiv(TextEquiv textEquiv) {
28
        this.textEquiv = textEquiv;
29
    }
30

  
31
}
be/fulltextsearch/src/main/java/cz/zcu/kiv/aswi/fulltextsearch/document/Word.java
1
package cz.zcu.kiv.aswi.fulltextsearch.document;
2

  
3
import javax.xml.bind.annotation.XmlAccessType;
4
import javax.xml.bind.annotation.XmlAccessorType;
5
import javax.xml.bind.annotation.XmlElement;
6

  
7
@XmlAccessorType(XmlAccessType.PROPERTY)
8
public class Word {
9

  
10
    private Coords coords;
11
    private TextEquiv textEquiv;
12

  
13
    public Coords getCoords() {
14
        return coords;
15
    }
16

  
17
    @XmlElement(name = "Coords")
18
    public void setCoords(Coords coords) {
19
        this.coords = coords;
20
    }
21

  
22
    public TextEquiv getTextEquiv() {
23
        return textEquiv;
24
    }
25

  
26
    @XmlElement(name = "TextEquiv")
27
    public void setTextEquiv(TextEquiv textEquiv) {
28
        this.textEquiv = textEquiv;
29
    }
30

  
31
}
be/fulltextsearch/src/main/java/cz/zcu/kiv/aswi/fulltextsearch/model/QueryDocumentResponse.java
1
package cz.zcu.kiv.aswi.fulltextsearch.model;
2

  
3
import java.util.HashMap;
4
import java.util.Map;
5

  
6
public class QueryDocumentResponse {
7

  
8
    private String doc_filename;
9
    private String image; // PNG in Base64
10
    private String text_region;
11
    private String text_region_coords;
12
    private Map<String, String> text_lines = new HashMap<>();
13

  
14
    public String getDoc_filename() {
15
        return doc_filename;
16
    }
17

  
18
    public void setDoc_filename(String doc_filename) {
19
        this.doc_filename = doc_filename;
20
    }
21

  
22
    public String getText_region() {
23
        return text_region;
24
    }
25

  
26
    public void setText_region(String text_region) {
27
        this.text_region = text_region;
28
    }
29

  
30
    public String getText_region_coords() {
31
        return text_region_coords;
32
    }
33

  
34
    public void setText_region_coords(String text_region_coords) {
35
        this.text_region_coords = text_region_coords;
36
    }
37

  
38
    public Map<String, String> getText_lines() {
39
        return text_lines;
40
    }
41

  
42
    public void setText_lines(Map<String, String> text_lines) {
43
        this.text_lines = text_lines;
44
    }
45

  
46
    public String getImage() {
47
        return image;
48
    }
49

  
50
    public void setImage(String image) {
51
        this.image = image;
52
    }
53
}
be/fulltextsearch/src/main/java/cz/zcu/kiv/aswi/fulltextsearch/model/SearchResponse.java
1
package cz.zcu.kiv.aswi.fulltextsearch.model;
2

  
3
import cz.zcu.kiv.aswi.fulltextsearch.document.TextRegion;
4

  
5
public class SearchResponse {
6

  
7
    private String expression;
8
    private TextRegion[] textRegions;
9

  
10
    public String getExpression() {
11
        return expression;
12
    }
13

  
14
    public void setExpression(String expression) {
15
        this.expression = expression;
16
    }
17

  
18
    public TextRegion[] getTextRegions() {
19
        return textRegions;
20
    }
21

  
22
    public void setTextRegions(TextRegion[] textRegions) {
23
        this.textRegions = textRegions;
24
    }
25
}

Také k dispozici: Unified diff