Java tutorial
/* * Copyright (c) 2014 Patrick Meyer * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ package com.itemanalysis.jmetrik.data; import com.itemanalysis.psychometrics.data.SpecialDataCodes; import org.apache.commons.csv.CSVFormat; import org.apache.commons.csv.CSVParser; import org.apache.commons.csv.CSVRecord; import org.apache.commons.io.input.BOMInputStream; import java.io.*; import java.util.LinkedHashMap; /** * This class holds item scoring information, which is loaded from a CSV file with a specific format. * The file should have one row for each response option. The only entry needed for an item is the * name, correct answer option, and score assigned to the correct answer. All other options will be * scored as 0 points. * * When reading a scoring file there are certain expected column names. * name = item name * option = response option * score = option score * missing = missing data code. By default it is an empty string. * missing score = score assigned to a missing data value such as 0 or -1. * notreached = code to identify a not-reached response. * notreached score = score assigned to a not-reached response such as 0 or -1. * omitted = code to identify an omitted response. * omitted score = score assigned to an omitted response such as 0 or -1. */ @Deprecated public class Scorer { private LinkedHashMap<String, GenericItemScoring> scoring = null; public Scorer(String fileName) { scoring = new LinkedHashMap<String, GenericItemScoring>(); } private void readScoringFile(String fileName) throws IOException { File f = new File(fileName); CSVParser parser = null; Reader reader = null; GenericItemScoring itemScoring = null; SpecialDataCodes specialCodes = null; String name = ""; String option = ""; int score = 0; try { reader = new InputStreamReader(new BOMInputStream(new FileInputStream(f)), "UTF-8"); parser = new CSVParser(reader, CSVFormat.EXCEL.withHeader()); for (CSVRecord csvRecord : parser) { name = csvRecord.get("name"); option = csvRecord.get("option"); score = Integer.parseInt(csvRecord.get("score")); itemScoring = new GenericItemScoring(name); itemScoring.addCategory(option, score); specialCodes = new SpecialDataCodes(); if (csvRecord.isMapped("missing")) specialCodes.setMissingDataCode(csvRecord.get("missing")); if (csvRecord.isMapped("missing score")) specialCodes.setMissingDataScore(Integer.parseInt(csvRecord.get("missing score"))); if (csvRecord.isMapped("notreached")) specialCodes.setMissingDataCode(csvRecord.get("notreached")); if (csvRecord.isMapped("notreached score")) specialCodes.setMissingDataScore(Integer.parseInt(csvRecord.get("notreached score"))); if (csvRecord.isMapped("omitted")) specialCodes.setMissingDataCode(csvRecord.get("omitted")); if (csvRecord.isMapped("omitted score")) specialCodes.setMissingDataScore(Integer.parseInt(csvRecord.get("omitted score"))); scoring.put(name, itemScoring); } } catch (IOException ex) { throw (ex); } finally { parser.close(); reader.close(); } } public GenericItemScoring getItemScoring(String name) { return scoring.get(name); } /** * Computes the item score. If scoring is not present, it will attempt to parse the * String as a double and return it. If the response is not a double, this method * will throw a NumberFormatException. * * @param name item name * @param response item response * @return item score */ public double computeItemScoreAt(String name, String response) { GenericItemScoring itemScoring = scoring.get(name); if (itemScoring == null) { return Double.parseDouble(response); } return itemScoring.computeItemScore(response); } }