de.tudarmstadt.ukp.csniper.webapp.search.cqp.CqpContextProvider.java Source code

Java tutorial

Introduction

Here is the source code for de.tudarmstadt.ukp.csniper.webapp.search.cqp.CqpContextProvider.java

Source

/*******************************************************************************
 * Copyright 2013
 * Ubiquitous Knowledge Processing (UKP) Lab
 * Technische Universitt Darmstadt
 * 
 * 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 de.tudarmstadt.ukp.csniper.webapp.search.cqp;

import java.util.List;

import org.apache.commons.lang.StringUtils;

import de.tudarmstadt.ukp.csniper.webapp.evaluation.model.EvaluationItem;
import de.tudarmstadt.ukp.csniper.webapp.evaluation.model.ItemContext;
import de.tudarmstadt.ukp.csniper.webapp.search.ContextProvider;
import de.tudarmstadt.ukp.csniper.webapp.search.CorpusService;

public class CqpContextProvider implements ContextProvider {
    private CqpEngine engine;

    public void setEngine(CqpEngine aEngine) {
        engine = aEngine;
    }

    public CqpEngine getEngine() {
        return engine;
    }

    @Override
    public ItemContext getContext(EvaluationItem aItem, int aLeftSize, int aRightSize) {
        StringBuilder[] sb = { new StringBuilder(), new StringBuilder() };
        int idx = 0;

        int contextWidth = 1;
        CqpQuery cqpManager = new CqpQuery(engine, aItem.getType(), aItem.getCollectionId());
        List<String> output = cqpManager.getContextAround(aItem, contextWidth);

        String[] context = StringUtils.substringsBetween(output.get(0), "<sentence>", "</sentence>");

        if (context.length < 1 + 2 * contextWidth) {
            // this method of extracting the context is not error prone
            // TODO find a better one...
            for (int i = 0; i < context.length; i++) {
                if (context[i].equals(aItem.getCoveredText())) {
                    idx = 1;
                } else {
                    sb[idx].append(context[i] + " ");
                }
            }
        } else {
            // this can only be used when we have full context
            for (int i = 0; i < (context.length - 1) / 2; i++) {
                sb[0].append(context[i] + " ");
                sb[1].append(context[i + (context.length - 1) / 2 + 1] + " ");
            }
        }

        return null;
        //      return new String[] { sb[0].toString(), sb[1].toString() };
    }

    @Override
    public void setOutputPos(boolean aShowPos) {
        throw new UnsupportedOperationException();
    }

    @Override
    public void setCorpusService(CorpusService aCorpusService) {
        throw new UnsupportedOperationException();
    }
}