Extracting Relations With Created Models

In this section we show how, using a model (download some samples to start) you can build a runnning Relation Extraction System in one step. You can see this implementation in the ClassifierBasedRelationshipExtractor:

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import edu.columbia.cs.ref.algorithm.CandidatesGenerator;
import edu.columbia.cs.ref.model.CandidateSentence;
import edu.columbia.cs.ref.model.Document;
import edu.columbia.cs.ref.model.StructureConfiguration;
import edu.columbia.cs.ref.model.core.structure.OperableStructure;
import edu.columbia.cs.ref.model.re.Model;
import edu.columbia.cs.ref.model.relationship.Relationship;
import edu.columbia.cs.ref.model.relationship.RelationshipType;
import edu.columbia.cs.ref.tool.document.splitter.SentenceSplitter;

public class ClassifierBasedRelationshipExtractorextends Document> implements RelationshipExtractor{
    private Model m;
    private CandidatesGenerator generator;
    private StructureConfiguration structGenerator;

    public ClassifierBasedRelationshipExtractor(Model m, SentenceSplitter s){
        this.m=m;
        this.generator = new CandidatesGenerator(s);
        this.structGenerator = m.getStructureConfiguration();
    }

    public List extractTuples(Document d){
        
        //Get The candidate Sentences for a given Document

        Set sents = generator.generateCandidates(d, m.getRelationshipTypes());
        
        //Generate a map to access the detected relationship types later on

        Map types = new HashMap();
        for(RelationshipType type : m.getRelationshipTypes()){
            types.put(type.getType(), type);
        }
        
        //Create an array where we will store the extracted relationships.

        List results = new ArrayList();
        
        //Iterate over the generated candidate senteces

        for(CandidateSentence s : sents){

            //Enrich the sentence with features

            OperableStructure struc = structGenerator.getOperableStructure(s);
            
            //Get all the labels that were found by the model

            Set labels = m.predictLabel(struc);

            //Iterate over the labels to obtain the relations            

            for(String label : labels){

                //Retrieve the relationship type to be used in the next step

                RelationshipType type = types.get(label);
    
                //Obtain the Relationship corresponding to the given relationship type.

                Relationship r = struc.getCandidateSentence().getRelationship(type);

                //Add a new label to the relationship
                r.setLabel(label);
                
                //add the relationship to the results list
                results.add(r);
            }
        }
        
        //return all found relationships.

        return results;
    }
}