<commit_before>/*******************************************************************************
 * Copyright 2012
 * Ubiquitous Knowledge Processing (UKP) Lab and FG Language Technology
 * Technische Universität 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.clarin.webanno.brat.annotation;

import static org.uimafit.util.JCasUtil.select;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.exception.ExceptionUtils;
import org.apache.uima.UIMAException;
import org.apache.uima.cas.CAS;
import org.apache.uima.cas.TypeSystem;
import org.apache.uima.cas.impl.CASCompleteSerializer;
import org.apache.uima.cas.impl.CASImpl;
import org.apache.uima.cas.impl.Serialization;
import org.apache.uima.cas.text.AnnotationFS;
import org.apache.uima.jcas.JCas;
import org.apache.uima.jcas.tcas.Annotation;
import org.apache.wicket.request.IRequestParameters;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.springframework.http.converter.json.MappingJacksonHttpMessageConverter;
import org.springframework.security.core.context.SecurityContextHolder;
import org.uimafit.factory.JCasFactory;

import de.tudarmstadt.ukp.clarin.webanno.api.AnnotationService;
import de.tudarmstadt.ukp.clarin.webanno.api.RepositoryService;
import de.tudarmstadt.ukp.clarin.webanno.brat.annotation.BratAnnotator.MultipleSentenceCoveredException;
import de.tudarmstadt.ukp.clarin.webanno.brat.controller.AnnotationTypeConstant;
import de.tudarmstadt.ukp.clarin.webanno.brat.controller.BratAjaxCasController;
import de.tudarmstadt.ukp.clarin.webanno.brat.controller.BratAjaxCasUtil;
import de.tudarmstadt.ukp.clarin.webanno.brat.display.model.OffsetsList;
import de.tudarmstadt.ukp.clarin.webanno.model.AnnotationDocument;
import de.tudarmstadt.ukp.clarin.webanno.model.AnnotationDocumentState;
import de.tudarmstadt.ukp.clarin.webanno.model.Mode;
import de.tudarmstadt.ukp.clarin.webanno.model.SourceDocument;
import de.tudarmstadt.ukp.clarin.webanno.model.SourceDocumentState;
import de.tudarmstadt.ukp.clarin.webanno.model.User;
import de.tudarmstadt.ukp.dkpro.core.api.metadata.type.DocumentMetaData;
import de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Sentence;
import de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Token;

/**
 * A helper class for {@link BratAnnotator} and CurationEditor
 *
 * @author <NAME>
 *
 */
public class BratAnnotatorUtility
{

    public static Object getDocument(BratAnnotatorUIData aUIData, RepositoryService repository,
            AnnotationService annotationService, BratAnnotatorModel bratAnnotatorModel)
        throws ClassNotFoundException, IOException, UIMAException
    {
        Object result = null;
        BratAjaxCasController controller = new BratAjaxCasController(repository, annotationService);

        aUIData.setGetDocument(true);
        result = controller.getDocumentResponse(bratAnnotatorModel,
                aUIData.getAnnotationOffsetStart(), aUIData.getjCas(), aUIData.isGetDocument());
        aUIData.setGetDocument(false);

        return result;
    }

    public static Object createSpan(IRequestParameters aRequest, User aUser,
            BratAnnotatorUIData aUIData, RepositoryService repository,
            AnnotationService annotationService, BratAnnotatorModel bratAnnotatorModel,
            MappingJacksonHttpMessageConverter jsonConverter)
        throws ClassNotFoundException, IOException, UIMAException, MultipleSentenceCoveredException
    {

        Object result = null;
        BratAjaxCasController controller = new BratAjaxCasController(repository, annotationService);
        String offsets = aRequest.getParameterValue("offsets").toString();

        OffsetsList offsetLists = jsonConverter.getObjectMapper().readValue(offsets,
                OffsetsList.class);
        int start = offsetLists.get(0).getBegin();
        int end = offsetLists.get(0).getEnd();
        aUIData.setAnnotationOffsetStart(BratAjaxCasUtil.getAnnotationBeginOffset(
                aUIData.getjCas(), bratAnnotatorModel.getSentenceAddress()) + start);
        aUIData.setAnnotationOffsetEnd(BratAjaxCasUtil.getAnnotationBeginOffset(aUIData.getjCas(),
                bratAnnotatorModel.getSentenceAddress()) + end);
        aUIData.setType(aRequest.getParameterValue("type").toString());

        if (!BratAjaxCasUtil.offsetsInOneSentences(aUIData.getjCas(),
                aUIData.getAnnotationOffsetStart(), aUIData.getAnnotationOffsetEnd())) {
            throw new MultipleSentenceCoveredException(
                    "Annotation coveres multiple sentences, limit your annotation to single sentence!");
        }

        AnnotationFS originFs = (AnnotationFS) aUIData.getjCas().getLowLevelCas()
                .ll_getFSForRef(aUIData.getOrigin());
        AnnotationFS targetFs = (AnnotationFS) aUIData.getjCas().getLowLevelCas()
                .ll_getFSForRef(aUIData.getTarget());

        result = controller.createSpanResponse(bratAnnotatorModel,
                aUIData.getAnnotationOffsetStart(), aUIData.getjCas(), aUIData.isGetDocument(),
                aUIData.getAnnotationOffsetEnd(), aUIData.getType(), originFs, targetFs);<commit_msg>#462 - Make Englishes more good <commit_after>/*******************************************************************************
 * Copyright 2012
 * Ubiquitous Knowledge Processing (UKP) Lab and FG Language Technology
 * Technische Universität 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.clarin.webanno.brat.annotation;

import static org.uimafit.util.JCasUtil.select;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.exception.ExceptionUtils;
import org.apache.uima.UIMAException;
import org.apache.uima.cas.CAS;
import org.apache.uima.cas.TypeSystem;
import org.apache.uima.cas.impl.CASCompleteSerializer;
import org.apache.uima.cas.impl.CASImpl;
import org.apache.uima.cas.impl.Serialization;
import org.apache.uima.cas.text.AnnotationFS;
import org.apache.uima.jcas.JCas;
import org.apache.uima.jcas.tcas.Annotation;
import org.apache.wicket.request.IRequestParameters;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.springframework.http.converter.json.MappingJacksonHttpMessageConverter;
import org.springframework.security.core.context.SecurityContextHolder;
import org.uimafit.factory.JCasFactory;

import de.tudarmstadt.ukp.clarin.webanno.api.AnnotationService;
import de.tudarmstadt.ukp.clarin.webanno.api.RepositoryService;
import de.tudarmstadt.ukp.clarin.webanno.brat.annotation.BratAnnotator.MultipleSentenceCoveredException;
import de.tudarmstadt.ukp.clarin.webanno.brat.controller.AnnotationTypeConstant;
import de.tudarmstadt.ukp.clarin.webanno.brat.controller.BratAjaxCasController;
import de.tudarmstadt.ukp.clarin.webanno.brat.controller.BratAjaxCasUtil;
import de.tudarmstadt.ukp.clarin.webanno.brat.display.model.OffsetsList;
import de.tudarmstadt.ukp.clarin.webanno.model.AnnotationDocument;
import de.tudarmstadt.ukp.clarin.webanno.model.AnnotationDocumentState;
import de.tudarmstadt.ukp.clarin.webanno.model.Mode;
import de.tudarmstadt.ukp.clarin.webanno.model.SourceDocument;
import de.tudarmstadt.ukp.clarin.webanno.model.SourceDocumentState;
import de.tudarmstadt.ukp.clarin.webanno.model.User;
import de.tudarmstadt.ukp.dkpro.core.api.metadata.type.DocumentMetaData;
import de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Sentence;
import de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Token;

/**
 * A helper class for {@link BratAnnotator} and CurationEditor
 *
 * @author <NAME>
 *
 */
public class BratAnnotatorUtility
{

    public static Object getDocument(BratAnnotatorUIData aUIData, RepositoryService repository,
            AnnotationService annotationService, BratAnnotatorModel bratAnnotatorModel)
        throws ClassNotFoundException, IOException, UIMAException
    {
        Object result = null;
        BratAjaxCasController controller = new BratAjaxCasController(repository, annotationService);

        aUIData.setGetDocument(true);
        result = controller.getDocumentResponse(bratAnnotatorModel,
                aUIData.getAnnotationOffsetStart(), aUIData.getjCas(), aUIData.isGetDocument());
        aUIData.setGetDocument(false);

        return result;
    }

    public static Object createSpan(IRequestParameters aRequest, User aUser,
            BratAnnotatorUIData aUIData, RepositoryService repository,
            AnnotationService annotationService, BratAnnotatorModel bratAnnotatorModel,
            MappingJacksonHttpMessageConverter jsonConverter)
        throws ClassNotFoundException, IOException, UIMAException, MultipleSentenceCoveredException
    {

        Object result = null;
        BratAjaxCasController controller = new BratAjaxCasController(repository, annotationService);
        String offsets = aRequest.getParameterValue("offsets").toString();

        OffsetsList offsetLists = jsonConverter.getObjectMapper().readValue(offsets,
                OffsetsList.class);
        int start = offsetLists.get(0).getBegin();
        int end = offsetLists.get(0).getEnd();
        aUIData.setAnnotationOffsetStart(BratAjaxCasUtil.getAnnotationBeginOffset(
                aUIData.getjCas(), bratAnnotatorModel.getSentenceAddress()) + start);
        aUIData.setAnnotationOffsetEnd(BratAjaxCasUtil.getAnnotationBeginOffset(aUIData.getjCas(),
                bratAnnotatorModel.getSentenceAddress()) + end);
        aUIData.setType(aRequest.getParameterValue("type").toString());

        if (!BratAjaxCasUtil.offsetsInOneSentences(aUIData.getjCas(),
                aUIData.getAnnotationOffsetStart(), aUIData.getAnnotationOffsetEnd())) {
            throw new MultipleSentenceCoveredException(
                    "You selected a span across multiple sentences. Limit your span annotations to single sentences!");
        }

        AnnotationFS originFs = (AnnotationFS) aUIData.getjCas().getLowLevelCas()
                .ll_getFSForRef(aUIData.getOrigin());
        AnnotationFS targetFs = (AnnotationFS) aUIData.getjCas().getLowLevelCas()
                .ll_getFSForRef(aUIData.getTarget());

        result = controller.createSpanResponse(bratAnnotatorModel,
                aUIData.getAnnotationOffsetStart(), aUIData.getjCas(), aUIData.isGetDocument(),
                aUIData.getAnnotationOffsetEnd(), aUIData.getType(), originFs, targetFs);
{'fraction_non_alphanumeric': 0.11449226630800269, 'fraction_numerical': 0.001933422999327505, 'mean_word_length': 4.582824964805256, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2568357', 'n_tokens_mistral': 3525, 'n_tokens_neox': 3366, 'n_words': 616}