com.articulate.sigma.nlp.SimFloodTest.java Source code

Java tutorial

Introduction

Here is the source code for com.articulate.sigma.nlp.SimFloodTest.java

Source

package com.articulate.sigma.nlp;

import com.articulate.sigma.nlp.pipeline.Pipeline;
import semRewrite.Graph;
import com.articulate.sigma.test.JsonReader;
import edu.stanford.nlp.pipeline.*;
import com.google.common.collect.Maps;
import junit.framework.TestCase;
import org.json.simple.JSONObject;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

import java.io.File;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/*
Copyright 2014-2015 IPsoft
    
Author: Adam Pease adam.pease@ipsoft.com
    
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 2 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, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston,
MA  02111-1307 USA
*/
@RunWith(Parameterized.class)
public class SimFloodTest extends TestCase {

    private static Map<String, ArrayList<Graph>> parsedSentences = Maps.newHashMap();

    private static Pipeline pipe = new Pipeline();

    @Parameterized.Parameter(value = 0)
    public String fileName;
    @Parameterized.Parameter(value = 1)
    public String query;
    @Parameterized.Parameter(value = 2)
    public String answer;

    /** ***************************************************************
     */
    @AfterClass
    public static void cleanUp() {

        parsedSentences.clear();
    }

    /** ***************************************************************
     */
    @Parameterized.Parameters(name = "<{0}> {1}")
    public static Collection<Object[]> prepare() {

        return JsonReader.transform("QA/json/IRtests.json", (JSONObject jo) -> {
            String fname = (String) jo.get("file");
            String query = (String) jo.get("query");
            String answer = (String) jo.get("answer");
            return new Object[] { fname, query, answer };
        });
    }

    /** ***************************************************************
     */
    private ArrayList<Graph> getCachedAnnotations(String fileName) {

        ArrayList<Graph> sents = parsedSentences.get(fileName);
        SimFlood sf = new SimFlood();
        if (sents == null) {
            sents = sf.readGraphsFromFile("QA/textfiles" + File.separator + fileName, true, pipe);
            parsedSentences.put(fileName, sents);
        }
        return sents;
    }

    /** ***************************************************************
     */
    @Test
    public void test() {

        ArrayList<Graph> sents = getCachedAnnotations(fileName);
        SimFlood sf = new SimFlood();
        Graph queryG = new Graph(query);
        queryG.fromSentence(query, pipe);
        Graph bestG = sf.match(queryG, sents);
        Pattern p = Pattern.compile(answer.toLowerCase());
        Matcher m = p.matcher(bestG.label.toLowerCase());
        if (!m.find()) {
            assertEquals(answer, bestG.label);
        }
    }
}