Search in sources :

Example 1 with KeywordInContext

use of de.catma.document.source.KeywordInContext in project catma by forTEXT.

the class KwicListJSONSerializer method toJSON.

public String toJSON(List<KeywordInContext> kwicList, boolean caseSensitive) {
    JsonNodeFactory factory = JsonNodeFactory.instance;
    ObjectNode kwicListJson = factory.objectNode();
    ArrayNode prefixArraysJson = factory.arrayNode();
    ArrayNode tokenArray = factory.arrayNode();
    ArrayNode postfixArraysJson = factory.arrayNode();
    kwicListJson.set(KwicSerializationField.prefixArrays.name(), prefixArraysJson);
    kwicListJson.set(KwicSerializationField.tokenArray.name(), tokenArray);
    kwicListJson.set(KwicSerializationField.postfixArrays.name(), postfixArraysJson);
    kwicListJson.put(KwicSerializationField.caseSensitive.name(), Boolean.toString(caseSensitive));
    int rtlCount = 0;
    for (KeywordInContext kwic : kwicList) {
        if (kwic instanceof KeywordInSpanContext) {
            KeywordInSpanContext spanKwic = (KeywordInSpanContext) kwic;
            ArrayNode prefixArrayJson = factory.arrayNode();
            prefixArraysJson.add(prefixArrayJson);
            for (TermInfo ti : spanKwic.getSpanContext().getBackwardTokens()) {
                prefixArrayJson.add(ti.getTerm());
            }
            tokenArray.add(spanKwic.getKeyword());
            ArrayNode postfixArrayJson = factory.arrayNode();
            postfixArraysJson.add(postfixArrayJson);
            for (TermInfo ti : spanKwic.getSpanContext().getForwardTokens()) {
                postfixArrayJson.add(ti.getTerm());
            }
            if (kwic.isRightToLeft()) {
                rtlCount++;
            }
        }
    }
    // rightToLeftLanaguage->true if more than half of the kwics stem from RTL documents
    kwicListJson.put(KwicSerializationField.rightToLeftLanguage.name(), Boolean.toString(rtlCount > (BigDecimal.valueOf(kwicList.size()).divide(BigDecimal.valueOf(2), BigDecimal.ROUND_HALF_UP).intValue())));
    return kwicListJson.toString();
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) TermInfo(de.catma.indexer.TermInfo) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) KeywordInSpanContext(de.catma.indexer.KeywordInSpanContext) KeywordInContext(de.catma.document.source.KeywordInContext) JsonNodeFactory(com.fasterxml.jackson.databind.node.JsonNodeFactory)

Aggregations

ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)1 JsonNodeFactory (com.fasterxml.jackson.databind.node.JsonNodeFactory)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 KeywordInContext (de.catma.document.source.KeywordInContext)1 KeywordInSpanContext (de.catma.indexer.KeywordInSpanContext)1 TermInfo (de.catma.indexer.TermInfo)1