All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.anserini.search.topicreader.DprJsonlTopicReader Maven / Gradle / Ivy

/*
 * Anserini: A Lucene toolkit for reproducible information retrieval research
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

 package io.anserini.search.topicreader;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.BufferedReader;
import java.io.IOException;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;

public class DprJsonlTopicReader extends TopicReader {
    private static final String QUESTION_KEY = "question";
    private static final String ANSWERS_KEY = "answer";

    public DprJsonlTopicReader(Path topicFile) throws IOException {
        super(topicFile);
    }

    @Override
    public SortedMap> read(BufferedReader reader) throws IOException {
        SortedMap> map = new TreeMap<>();
        String line;
        Integer topicID = 0;
        ObjectMapper mapper = new ObjectMapper();
        while ((line = reader.readLine()) != null) {
            line = line.trim();
            JsonNode lineNode = mapper.readerFor(JsonNode.class).readTree(line);
            Map fields = new HashMap<>();
            fields.put("title", lineNode.get(QUESTION_KEY).asText());
            fields.put("answers", lineNode.get(ANSWERS_KEY).toString());
            map.put(topicID, fields);
            topicID += 1;
        }

        return map;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy