<commit_before>package org.maltparser.core.config;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;

import org.maltparser.core.exception.MaltChainedException;
import org.maltparser.core.flow.FlowChartInstance;
import org.maltparser.core.flow.item.ChartItem;
import org.maltparser.core.flow.spec.ChartItemSpecification;
import org.maltparser.core.helper.SystemInfo;
import org.maltparser.core.helper.SystemLogger;
import org.maltparser.core.options.OptionManager;
/**
*
*
* @author <NAME>
*/
public class ConfigDirChartItem extends ChartItem {
	private String idName;
	private String taskName;
	private String optionFileName;
	private URL configDirURL;
	private String configDirName;
	private ConfigurationDir configDir;
	private String outCharSet;
	private String inCharSet;
	
	public ConfigDirChartItem() {}
	
	public void initialize(FlowChartInstance flowChartinstance, ChartItemSpecification chartItemSpecification) throws MaltChainedException {
		super.initialize(flowChartinstance, chartItemSpecification);
		
		for (String key : chartItemSpecification.getChartItemAttributes().keySet()) {
			if (key.equals("id")) {
				idName = chartItemSpecification.getChartItemAttributes().get(key);
			}  else if (key.equals("task")) {
				taskName = chartItemSpecification.getChartItemAttributes().get(key);
			}
		}
		if (idName == null) {
			idName = getChartElement("configdir").getAttributes().get("id").getDefaultValue();
		} else if (taskName == null) {
			taskName = getChartElement("configdir").getAttributes().get("task").getDefaultValue();
		}
		
		if (OptionManager.instance().getOptionValue(getOptionContainerIndex(), "config", "url") != null && OptionManager.instance().getOptionValue(getOptionContainerIndex(),"config", "url").toString().length() > 0) {
			try {			
				configDirURL = new URL(OptionManager.instance().getOptionValue(getOptionContainerIndex(), "config", "url").toString());
			} catch (MalformedURLException e) {
				throw new ConfigurationException("The URL '"+OptionManager.instance().getOptionValue(getOptionContainerIndex(), "config", "url").toString()+"' is malformed. ", e);
			}
		}
		if (OptionManager.instance().getOptionValue(getOptionContainerIndex(), "config", "name").toString().endsWith(".mco")) {
			int index = OptionManager.instance().getOptionValue(getOptionContainerIndex(), "config", "name").toString().lastIndexOf('.');
			configDirName = OptionManager.instance().getOptionValue(getOptionContainerIndex(), "config", "name").toString().substring(0,index);
		} else {
			configDirName = OptionManager.instance().getOptionValue(getOptionContainerIndex(), "config", "name").toString();
		}
		
		try {
			if (OptionManager.instance().getOptionValue(getOptionContainerIndex(), "system", "option_file") != null) {
				optionFileName = OptionManager.instance().getOptionValue(getOptionContainerIndex(), "system", "option_file").toString();
			}
		} catch (ConfigurationException e) {
			throw new ConfigurationException("The option file '"+optionFileName+"' could not be copied. ",e);
		}
		if (OptionManager.instance().getOptionValue(getOptionContainerIndex(), "output", "charset") != null) {
			outCharSet = OptionManager.instance().getOptionValue(getOptionContainerIndex(), "output", "charset").toString();
		} else {
			outCharSet = "UTF-8";
		}
		
		if (OptionManager.instance().getOptionValue(getOptionContainerIndex(), "input", "charset") != null) {
			inCharSet = OptionManager.instance().getOptionValue(getOptionContainerIndex(), "input", "charset").toString();
		} else {
			inCharSet = "UTF-8";
		}
		
		configDir = (ConfigurationDir)flowChartinstance.getFlowChartRegistry(org.maltparser.core.config.ConfigurationDir.class, idName);
		if (configDir == null) {
			if (configDirURL != null) {
				configDir = new ConfigurationDir(configDirURL);
			} else {
				configDir = new ConfigurationDir(configDirName, idName, getOptionContainerIndex());
			}
			
			flowChartinstance.addFlowChartRegistry(org.maltparser.core.config.ConfigurationDir.class, idName, configDir);
		}
		if (taskName.equals("versioning")) {
			configDir.versioning();
		} else if (taskName.equals("loadsavedoptions")) {
			configDir.initCreatedByMaltParserVersionFromInfoFile();
			if (configDir.getCreatedByMaltParserVersion() == null) {
				SystemLogger.logger().warn("Couln't determine which version of MaltParser that created the parser model: " + configDirName+ ".mco\n MaltParser will terminate\n");
				System.exit(1);
			} else if (!configDir.getCreatedByMaltParserVersion().startsWith(SystemInfo.getVersion())) {
				SystemLogger.logger().error("The parser model '"+ configDirName+ ".mco' is created by MaltParser "+configDir.getCreatedByMaltParserVersion()+".\n");
				SystemLogger.logger().error("You have re-train the parser model to be able to parse with current version of MaltParser.");
				System.exit(1);
			}
			OptionManager.instance().loadOptions(getOptionContainerIndex(), configDir.getInputStreamReaderFromConfigFile("savedoptions.sop"));
			configDir.initDataFormat();<commit_msg>Small fix that makes it possible to parse with models created with MaltParser 1.5<commit_after>package org.maltparser.core.config;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;

import org.maltparser.core.exception.MaltChainedException;
import org.maltparser.core.flow.FlowChartInstance;
import org.maltparser.core.flow.item.ChartItem;
import org.maltparser.core.flow.spec.ChartItemSpecification;
import org.maltparser.core.helper.SystemInfo;
import org.maltparser.core.helper.SystemLogger;
import org.maltparser.core.options.OptionManager;
/**
*
*
* @author <NAME>
*/
public class ConfigDirChartItem extends ChartItem {
	private String idName;
	private String taskName;
	private String optionFileName;
	private URL configDirURL;
	private String configDirName;
	private ConfigurationDir configDir;
	private String outCharSet;
	private String inCharSet;
	
	public ConfigDirChartItem() {}
	
	public void initialize(FlowChartInstance flowChartinstance, ChartItemSpecification chartItemSpecification) throws MaltChainedException {
		super.initialize(flowChartinstance, chartItemSpecification);
		
		for (String key : chartItemSpecification.getChartItemAttributes().keySet()) {
			if (key.equals("id")) {
				idName = chartItemSpecification.getChartItemAttributes().get(key);
			}  else if (key.equals("task")) {
				taskName = chartItemSpecification.getChartItemAttributes().get(key);
			}
		}
		if (idName == null) {
			idName = getChartElement("configdir").getAttributes().get("id").getDefaultValue();
		} else if (taskName == null) {
			taskName = getChartElement("configdir").getAttributes().get("task").getDefaultValue();
		}
		
		if (OptionManager.instance().getOptionValue(getOptionContainerIndex(), "config", "url") != null && OptionManager.instance().getOptionValue(getOptionContainerIndex(),"config", "url").toString().length() > 0) {
			try {			
				configDirURL = new URL(OptionManager.instance().getOptionValue(getOptionContainerIndex(), "config", "url").toString());
			} catch (MalformedURLException e) {
				throw new ConfigurationException("The URL '"+OptionManager.instance().getOptionValue(getOptionContainerIndex(), "config", "url").toString()+"' is malformed. ", e);
			}
		}
		if (OptionManager.instance().getOptionValue(getOptionContainerIndex(), "config", "name").toString().endsWith(".mco")) {
			int index = OptionManager.instance().getOptionValue(getOptionContainerIndex(), "config", "name").toString().lastIndexOf('.');
			configDirName = OptionManager.instance().getOptionValue(getOptionContainerIndex(), "config", "name").toString().substring(0,index);
		} else {
			configDirName = OptionManager.instance().getOptionValue(getOptionContainerIndex(), "config", "name").toString();
		}
		
		try {
			if (OptionManager.instance().getOptionValue(getOptionContainerIndex(), "system", "option_file") != null) {
				optionFileName = OptionManager.instance().getOptionValue(getOptionContainerIndex(), "system", "option_file").toString();
			}
		} catch (ConfigurationException e) {
			throw new ConfigurationException("The option file '"+optionFileName+"' could not be copied. ",e);
		}
		if (OptionManager.instance().getOptionValue(getOptionContainerIndex(), "output", "charset") != null) {
			outCharSet = OptionManager.instance().getOptionValue(getOptionContainerIndex(), "output", "charset").toString();
		} else {
			outCharSet = "UTF-8";
		}
		
		if (OptionManager.instance().getOptionValue(getOptionContainerIndex(), "input", "charset") != null) {
			inCharSet = OptionManager.instance().getOptionValue(getOptionContainerIndex(), "input", "charset").toString();
		} else {
			inCharSet = "UTF-8";
		}
		
		configDir = (ConfigurationDir)flowChartinstance.getFlowChartRegistry(org.maltparser.core.config.ConfigurationDir.class, idName);
		if (configDir == null) {
			if (configDirURL != null) {
				configDir = new ConfigurationDir(configDirURL);
			} else {
				configDir = new ConfigurationDir(configDirName, idName, getOptionContainerIndex());
			}
			
			flowChartinstance.addFlowChartRegistry(org.maltparser.core.config.ConfigurationDir.class, idName, configDir);
		}
		if (taskName.equals("versioning")) {
			configDir.versioning();
		} else if (taskName.equals("loadsavedoptions")) {
			configDir.initCreatedByMaltParserVersionFromInfoFile();
			if (configDir.getCreatedByMaltParserVersion() == null) {
				SystemLogger.logger().warn("Couln't determine which version of MaltParser that created the parser model: " + configDirName+ ".mco\n MaltParser will terminate\n");
				System.exit(1);
			} else if (!configDir.getCreatedByMaltParserVersion().substring(0,3).equals(SystemInfo.getVersion().substring(0,3))) {
				SystemLogger.logger().error("The parser model '"+ configDirName+ ".mco' is created by MaltParser "+configDir.getCreatedByMaltParserVersion()+".\n");
				SystemLogger.logger().error("You have re-train the parser model to be able to parse with current version of MaltParser.");
				System.exit(1);
			}
			OptionManager.instance().loadOptions(getOptionContainerIndex(), configDir.getInputStreamReaderFromConfigFile("savedoptions.sop"));
			configDir.initDataFormat();
{'fraction_non_alphanumeric': 0.13872549019607844, 'fraction_numerical': 0.0017647058823529412, 'mean_word_length': 6.010996563573883, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, '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': '4623107', 'n_tokens_mistral': 3158, 'n_tokens_neox': 2863, 'n_words': 538}