<commit_before>package at.medunigraz.imi.reassess.conceptmapper.metamap;

import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import at.medunigraz.imi.reassess.conceptmapper.ConceptMapper;
import bioc.BioCDocument;
import gov.nih.nlm.nls.metamap.document.FreeText;
import gov.nih.nlm.nls.metamap.lite.types.Entity;
import gov.nih.nlm.nls.metamap.lite.types.Ev;
import gov.nih.nlm.nls.ner.MetaMapLite;

public class MetaMapLiteFacade implements ConceptMapper {

	private static final Logger LOG = LogManager.getLogger();

	private static final MetaMapLiteFacade instance = new MetaMapLiteFacade();

	private MetaMapLite metaMapLiteInst;

	public static MetaMapLiteFacade getInstance() {
		return instance;
	}

	private MetaMapLiteFacade() {
		LOG.info("Building MetaMap instance...");

		Properties myProperties = MetaMapLite.getDefaultConfiguration();

		String configPropertyFilename = System.getProperty("metamaplite.property.file",
				getClass().getResource("/metamaplite.properties").getFile());

		try {
			myProperties.load(new FileReader(configPropertyFilename));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		MetaMapLite.expandModelsDir(myProperties);
		MetaMapLite.expandIndexDir(myProperties);

		try {
			metaMapLiteInst = new MetaMapLite(myProperties);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		LOG.info("Building MetaMap instance finished.");
	}

	public List<String> map(String text) {
		List<String> ret = new ArrayList<String>();
		
		List<Entity> entityList = process(text);
		
		for (Entity entity : entityList) {
			for (Ev ev : entity.getEvSet()) {
				ret.add(ev.getConceptInfo().getCUI());
				LOG.trace(ev);
			}
		}

		return ret;
	}

	private List<Entity> process(String text) {
		int length = text.length();
		LOG.debug("Processing \"{}\"...", text.substring(0, Math.min(length, 50)));
		
		long start = System.currentTimeMillis();

		BioCDocument document = FreeText.instantiateBioCDocument(text);
		document.setID("1");
		List<BioCDocument> documentList = new ArrayList<BioCDocument>();
		documentList.add(document);

		List<Entity> entityList = null;
		try {
			entityList = metaMapLiteInst.processDocumentList(documentList);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		long end = System.currentTimeMillis();
		
		float duration = (end - start) / 1000f;

		LOG.debug("Processed {} chars in {} sec ({} chars/sec).", length, duration, length/duration);
<commit_msg>Exposes properties into a private field<commit_after>package at.medunigraz.imi.reassess.conceptmapper.metamap;

import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import at.medunigraz.imi.reassess.conceptmapper.ConceptMapper;
import bioc.BioCDocument;
import gov.nih.nlm.nls.metamap.document.FreeText;
import gov.nih.nlm.nls.metamap.lite.resultformats.ResultFormatter;
import gov.nih.nlm.nls.metamap.lite.resultformats.ResultFormatterRegistry;
import gov.nih.nlm.nls.metamap.lite.types.Entity;
import gov.nih.nlm.nls.metamap.lite.types.Ev;
import gov.nih.nlm.nls.ner.MetaMapLite;

public class MetaMapLiteFacade implements ConceptMapper {

	private static final Logger LOG = LogManager.getLogger();

	private static final MetaMapLiteFacade instance = new MetaMapLiteFacade();

	private MetaMapLite metaMapLiteInst;

	private Properties properties;

	public static MetaMapLiteFacade getInstance() {
		return instance;
	}

	private MetaMapLiteFacade() {
		LOG.info("Building MetaMap instance...");

		properties = MetaMapLite.getDefaultConfiguration();

		String configPropertyFilename = System.getProperty("metamaplite.property.file",
				getClass().getResource("/metamaplite.properties").getFile());

		try {
			properties.load(new FileReader(configPropertyFilename));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		MetaMapLite.expandModelsDir(properties);
		MetaMapLite.expandIndexDir(properties);

		try {
			metaMapLiteInst = new MetaMapLite(properties);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		LOG.info("Building MetaMap instance finished.");
	}

	public List<String> map(String text) {
		List<String> ret = new ArrayList<String>();

		List<Entity> entityList = process(text);

		for (Entity entity : entityList) {
			for (Ev ev : entity.getEvSet()) {
				ret.add(ev.getConceptInfo().getCUI());
				LOG.trace(ev);
			}
		}

		return ret;
	}

	private List<Entity> process(String text) {
		int length = text.length();
		LOG.debug("Processing \"{}\"...", text.substring(0, Math.min(length, 50)));

		long start = System.currentTimeMillis();

		BioCDocument document = FreeText.instantiateBioCDocument(text);
		document.setID("1");
		List<BioCDocument> documentList = new ArrayList<BioCDocument>();
		documentList.add(document);

		List<Entity> entityList = null;
		try {
			entityList = metaMapLiteInst.processDocumentList(documentList);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		long end = System.currentTimeMillis();

		float duration = (end - start) / 1000f;

		LOG.debug("Processed {} chars in {} sec ({} chars/sec).", length, duration, length / duration);

{'fraction_non_alphanumeric': 0.12459893048128343, 'fraction_numerical': 0.0035650623885918, 'mean_word_length': 4.644869215291751, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, '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': '1468201', 'n_tokens_mistral': 1957, 'n_tokens_neox': 1755, 'n_words': 356}