<commit_before>// $Id: ruleEvaluator.C,v 1.3 2000/05/24 16:20:40 oliver Exp $

#include <BALL/MOLMEC/COMMON/ruleEvaluator.h>
#include <BALL/FORMAT/INIFile.h>
#include <BALL/KERNEL/PTE.h>

using namespace std;

namespace BALL 
{

	RuleEvaluator::RuleEvaluator()
		:	prefix_(),
			rule_map_(),
			valid_(false)
	{
	}

	RuleEvaluator::RuleEvaluator(INIFile& file, const String& prefix)
		:	prefix_(),
			rule_map_(),
			valid_(false)
	{
		valid_ = initialize(file, prefix);
	}

	RuleEvaluator::RuleEvaluator(const RuleEvaluator& evaluator)
		:	prefix_(evaluator.prefix_),
			rule_map_(evaluator.rule_map_),
			valid_(evaluator.valid_)
	{
	}


	RuleEvaluator::~RuleEvaluator()
	{
		destroy();
	}
	
	void RuleEvaluator::destroy()
	{
		clear();
	}
	
	void RuleEvaluator::clear()
	{
		valid_ = false;
		prefix_ = "";
		rule_map_.clear();
	}
	

	const String& RuleEvaluator::getPrefix() const
	{
		return prefix_;
	}

	void RuleEvaluator::setPrefix(const String& prefix)
	{
		prefix_ = prefix;
	}

	bool RuleEvaluator::initialize
		(INIFile& file, const String& prefix)
	{
		// destroy the old rules
		rule_map_.clear();
		valid_ = false;

		// store the new prefix
		prefix_ = prefix;

		// check whether the INI file is valid
		if (!file.isValid())
		{
			// we didn't get a valid prefix file: abort
			return false;
		}

		// check for the sections and create the corresponding
		// Expressions
		for (Position i = 0; i < Element::NUMBER_OF_ELEMENTS; i++)
		{
			extractSection_(file, PTE[i].getSymbol());
		}

		// the last rule is a general rule
		extractSection_(file, "*");
	
		if (rule_map_.size() == 0)
		{
			Log.error() << "RuleEvaluator::initialize: no matching sections found for prefix " << prefix_ << endl;
		}
		
		// we create a map - done.
		valid_ = true;
		return true;
	}

	void RuleEvaluator::extractSection_(INIFile& file, const String& symbol)
	{
		// assemble the section name
		String section_name(prefix_ + ":" + symbol);

		// abort if the INI file does not contain the requested section
		if (!file.hasSection(section_name))
		{
			return;
		}

		// create a new entry for symbol
		if (!rule_map_.has(symbol))
		{
			rule_map_.insert(symbol, list<pair<Expression, String> >());
		}

		// iterate over all lines of the respective section
		Position i = file.getSectionFirstLine(section_name);
		for (; i < file.getSectionLastLine(section_name); i++)
		{
			String line(*file.getLine(i));
			if (line.has('='))
			{
				if (line[0] == '=')
				{
					Log.error() << "RuleEvaluator:: invalid rule in line " << i << ": " << line << endl;
					continue;
				}

				String value = line.before("=");	
				String expression_string;
				if (line.after("=").isValid())
				{
					expression_string = line.after("=");
				}
				expression_string.trim();

				// push the expression into the list
				rule_map_[symbol].push_back(pair<Expression, String>(Expression(expression_string), value));
			}
		}
	}

	String RuleEvaluator::operator () (const Atom& atom) const
	{
		// check whether we got a rule for this element
		String symbol = atom.getElement().getSymbol();
		RuleList::const_iterator it;

		// the return value
		String result = "";
		if (rule_map_.has(symbol))
		{
			// iterate over all rules in the list until the first rule
			// matches
			for (it = rule_map_[symbol].begin(); it != rule_map_[symbol].end(); ++it)
			{
				// check whether the expression matches for this atom
				if (it->first(atom))
				{
					// retrieve the return value
					result = it->second;
					break;
				}
			}
		} 
		
		// if no rule was applicable, check the default rule "*"
		if ((result == "") && (rule_map_.has("*")))
		{
			// iterate over all rules in the list until the first rule
			// matches
			for (it = rule_map_["*"].begin(); it != rule_map_["*"].end(); ++it)
			{
				// check whether the expression matches for this atom
				if (it->first(atom))
				{
					// retrieve the return value
					result = it->second;
					
					// and exit the loop
					break;
				}
			}
		}
				
		// return the value
		return result;
	}

	const RuleEvaluator& RuleEvaluator::operator = (const RuleEvaluator& evaluator)
	{
		set(evaluator);
		return *this;
	}

	void RuleEvaluator::set(const RuleEvaluator& evaluator)
	{
		valid_ = evaluator.valid_;
		prefix_ = evaluator.prefix_;
		rule_map_ = evaluator.rule_map_;
	}

	bool RuleEvaluator::isValid() const
	{
		return valid_;
	}

	void RuleEvaluator::dump(std::ostream& s, Size indent_depth) const
	{
		// BAUSTELLE
	}
}
<commit_msg>fixed: leading and trailing blanks were not removed from the value associated with a rule<commit_after>// $Id: ruleEvaluator.C,v 1.4 2000/05/24 17:58:03 oliver Exp $

#include <BALL/MOLMEC/COMMON/ruleEvaluator.h>
#include <BALL/FORMAT/INIFile.h>
#include <BALL/KERNEL/PTE.h>

using namespace std;

namespace BALL 
{

	RuleEvaluator::RuleEvaluator()
		:	prefix_(),
			rule_map_(),
			valid_(false)
	{
	}

	RuleEvaluator::RuleEvaluator(INIFile& file, const String& prefix)
		:	prefix_(),
			rule_map_(),
			valid_(false)
	{
		valid_ = initialize(file, prefix);
	}

	RuleEvaluator::RuleEvaluator(const RuleEvaluator& evaluator)
		:	prefix_(evaluator.prefix_),
			rule_map_(evaluator.rule_map_),
			valid_(evaluator.valid_)
	{
	}


	RuleEvaluator::~RuleEvaluator()
	{
		destroy();
	}
	
	void RuleEvaluator::destroy()
	{
		clear();
	}
	
	void RuleEvaluator::clear()
	{
		valid_ = false;
		prefix_ = "";
		rule_map_.clear();
	}
	

	const String& RuleEvaluator::getPrefix() const
	{
		return prefix_;
	}

	void RuleEvaluator::setPrefix(const String& prefix)
	{
		prefix_ = prefix;
	}

	bool RuleEvaluator::initialize
		(INIFile& file, const String& prefix)
	{
		// destroy the old rules
		rule_map_.clear();
		valid_ = false;

		// store the new prefix
		prefix_ = prefix;

		// check whether the INI file is valid
		if (!file.isValid())
		{
			// we didn't get a valid prefix file: abort
			return false;
		}

		// check for the sections and create the corresponding
		// Expressions
		for (Position i = 0; i < Element::NUMBER_OF_ELEMENTS; i++)
		{
			extractSection_(file, PTE[i].getSymbol());
		}

		// the last rule is a general rule
		extractSection_(file, "*");
	
		if (rule_map_.size() == 0)
		{
			Log.error() << "RuleEvaluator::initialize: no matching sections found for prefix " << prefix_ << endl;
		}
		
		// we create a map - done.
		valid_ = true;
		return true;
	}

	void RuleEvaluator::extractSection_(INIFile& file, const String& symbol)
	{
		// assemble the section name
		String section_name(prefix_ + ":" + symbol);

		// abort if the INI file does not contain the requested section
		if (!file.hasSection(section_name))
		{
			return;
		}

		// create a new entry for symbol
		if (!rule_map_.has(symbol))
		{
			rule_map_.insert(symbol, list<pair<Expression, String> >());
		}

		// iterate over all lines of the respective section
		Position i = file.getSectionFirstLine(section_name);
		for (; i < file.getSectionLastLine(section_name); i++)
		{
			String line(*file.getLine(i));
			if (line.has('='))
			{
				if (line[0] == '=')
				{
					Log.error() << "RuleEvaluator:: invalid rule in line " << i << ": " << line << endl;
					continue;
				}

				String value = line.before("=");	
				String expression_string;
				if (line.after("=").isValid())
				{
					expression_string = line.after("=");
				}
				expression_string.trim();
				value.trim();

				// push the expression into the list
				rule_map_[symbol].push_back(pair<Expression, String>(Expression(expression_string), value));
			}
		}
	}

	String RuleEvaluator::operator () (const Atom& atom) const
	{
		// check whether we got a rule for this element
		String symbol = atom.getElement().getSymbol();
		RuleList::const_iterator it;

		// the return value
		String result = "";
		if (rule_map_.has(symbol))
		{
			// iterate over all rules in the list until the first rule
			// matches
			for (it = rule_map_[symbol].begin(); it != rule_map_[symbol].end(); ++it)
			{
				// check whether the expression matches for this atom
				if (it->first(atom))
				{
					// retrieve the return value
					result = it->second;
					break;
				}
			}
		} 
		
		// if no rule was applicable, check the default rule "*"
		if ((result == "") && (rule_map_.has("*")))
		{
			// iterate over all rules in the list until the first rule
			// matches
			for (it = rule_map_["*"].begin(); it != rule_map_["*"].end(); ++it)
			{
				// check whether the expression matches for this atom
				if (it->first(atom))
				{
					// retrieve the return value
					result = it->second;
					
					// and exit the loop
					break;
				}
			}
		}
				
		// return the value
		return result;
	}

	const RuleEvaluator& RuleEvaluator::operator = (const RuleEvaluator& evaluator)
	{
		set(evaluator);
		return *this;
	}

	void RuleEvaluator::set(const RuleEvaluator& evaluator)
	{
		valid_ = evaluator.valid_;
		prefix_ = evaluator.prefix_;
		rule_map_ = evaluator.rule_map_;
	}

	bool RuleEvaluator::isValid() const
	{
		return valid_;
	}

	void RuleEvaluator::dump(std::ostream& s, Size indent_depth) const
	{
		// BAUSTELLE
	}
}

{'fraction_non_alphanumeric': 0.13795765328628143, 'fraction_numerical': 0.0041905602117335685, 'mean_word_length': 3.127901684114702, 'pattern_counts': {'":': 4, '<': 51, '<?xml version=': 0, '>': 23, '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': '2631812', 'n_tokens_mistral': 3628, 'n_tokens_neox': 3070, 'n_words': 789}