<commit_before>package fr.inria.anhalytics.annotate;

import fr.inria.anhalytics.annotate.properties.AnnotateProperties;
import fr.inria.anhalytics.commons.exceptions.UnreachableNerdServiceException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import org.codehaus.jackson.node.*;
import org.codehaus.jackson.map.ObjectMapper;

/**
 * Call of Nerd process via its REST web services.
 *
 * @author <NAME>
 */
public class NerdService {

    private static final Logger logger = LoggerFactory.getLogger(NerdService.class);

    private String input = null;

    static private String REQUEST = "processERDQuery";

    public NerdService(String input) {
        this.input = input;
    }

    /**
     * Call the NERD full text annotation service on server.
     *
     * @return the resulting annotation in JSON
     */
    public String runNerd() {
        StringBuffer output = new StringBuffer();
        try {
            URL url = new URL("http://" + AnnotateProperties.getNerd_host() + ":" + AnnotateProperties.getNerd_port() + "/" + REQUEST);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setDoOutput(true);
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Content-Type", "application/json; charset=utf8");

            ObjectMapper mapper = new ObjectMapper();
            ObjectNode node = mapper.createObjectNode();
            node.put("text", input);
            ObjectNode dataTable = mapper.createObjectNode();
            dataTable.put("lang", "fr");
            dataTable.put("lang", "de");
            dataTable.put("lang", "en");
            node.put("language", dataTable);
            byte[] postDataBytes = node.toString().getBytes("UTF-8");

            OutputStream os = conn.getOutputStream();
            os.write(postDataBytes);
            os.flush();
            if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) {
                logger.error("Failed annotating text segment: HTTP error code : "
                        + conn.getResponseCode());
                return null;
            }
            BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));
            String line = null;
            while ((line = br.readLine()) != null) {
                output.append(line);
                output.append(" ");
            }
            os.close();
            conn.disconnect();

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return output.toString().trim();
    }

    /**
     * Checks if Nerd service is responding and available.
     * @return boolean
     */
    public static boolean isNerdReady() throws UnreachableNerdServiceException {
        logger.info("Cheking NERD service...");
        int responseCode = 0;
        
            
                System.out.println("sdqsdsdfsd");
        HttpURLConnection conn = null;
        try {
            URL url = new URL("http://" + AnnotateProperties.getNerd_host() + ":" + AnnotateProperties.getNerd_port());
            conn = (HttpURLConnection) url.openConnection();
            conn.setDoOutput(true);
            conn.setRequestMethod("GET");
            responseCode = conn.getResponseCode();
        } catch (Exception e) {
            throw new UnreachableNerdServiceException("NERD service is not reachable, check host and port parameters.");
        }
        if (responseCode != 200) {
            logger.error("NERD service is not alive.");
            throw new UnreachableNerdServiceException("NERD service is not alive.");
        }
        conn.disconnect();<commit_msg>typo.<commit_after>package fr.inria.anhalytics.annotate;

import fr.inria.anhalytics.annotate.properties.AnnotateProperties;
import fr.inria.anhalytics.commons.exceptions.UnreachableNerdServiceException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import org.codehaus.jackson.node.*;
import org.codehaus.jackson.map.ObjectMapper;

/**
 * Call of Nerd process via its REST web services.
 *
 * @author <NAME>
 */
public class NerdService {

    private static final Logger logger = LoggerFactory.getLogger(NerdService.class);

    private String input = null;

    static private String REQUEST = "processERDQuery";

    public NerdService(String input) {
        this.input = input;
    }

    /**
     * Call the NERD full text annotation service on server.
     *
     * @return the resulting annotation in JSON
     */
    public String runNerd() {
        StringBuffer output = new StringBuffer();
        try {
            URL url = new URL("http://" + AnnotateProperties.getNerd_host() + ":" + AnnotateProperties.getNerd_port() + "/" + REQUEST);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setDoOutput(true);
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Content-Type", "application/json; charset=utf8");

            ObjectMapper mapper = new ObjectMapper();
            ObjectNode node = mapper.createObjectNode();
            node.put("text", input);
            ObjectNode dataTable = mapper.createObjectNode();
            dataTable.put("lang", "fr");
            dataTable.put("lang", "de");
            dataTable.put("lang", "en");
            node.put("language", dataTable);
            byte[] postDataBytes = node.toString().getBytes("UTF-8");

            OutputStream os = conn.getOutputStream();
            os.write(postDataBytes);
            os.flush();
            if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) {
                logger.error("Failed annotating text segment: HTTP error code : "
                        + conn.getResponseCode());
                return null;
            }
            BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));
            String line = null;
            while ((line = br.readLine()) != null) {
                output.append(line);
                output.append(" ");
            }
            os.close();
            conn.disconnect();

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return output.toString().trim();
    }

    /**
     * Checks if Nerd service is responding and available.
     * @return boolean
     */
    public static boolean isNerdReady() throws UnreachableNerdServiceException {
        logger.info("Cheking NERD service...");
        int responseCode = 0;
        HttpURLConnection conn = null;
        try {
            URL url = new URL("http://" + AnnotateProperties.getNerd_host() + ":" + AnnotateProperties.getNerd_port());
            conn = (HttpURLConnection) url.openConnection();
            conn.setDoOutput(true);
            conn.setRequestMethod("GET");
            responseCode = conn.getResponseCode();
        } catch (Exception e) {
            throw new UnreachableNerdServiceException("NERD service is not reachable, check host and port parameters.");
        }
        if (responseCode != 200) {
            logger.error("NERD service is not alive.");
            throw new UnreachableNerdServiceException("NERD service is not alive.");
        }
        conn.disconnect();
{'fraction_non_alphanumeric': 0.11350243402510889, 'fraction_numerical': 0.0020497053548552396, 'mean_word_length': 2.352082438814942, 'pattern_counts': {'":': 4, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '7062500', 'n_tokens_mistral': 2096, 'n_tokens_neox': 2038, 'n_words': 476}