<commit_before>/*
 * DGSStreamParser.cpp
 *
 *  Created on: 23.12.2013
 *      Author: cls
 */

#include "DGSStreamParser.h"
#include "../auxiliary/StringTools.h"


namespace NetworKit {

DGSStreamParser::DGSStreamParser(std::string path, bool mapped, node baseIndex) : dgsFile(path), mapped(mapped), baseIndex(baseIndex), nextNode(0) {

}

std::vector<GraphEvent> NetworKit::DGSStreamParser::getStream() {
	if (! dgsFile.is_open()) {
		throw std::runtime_error("DGS input file could not be opened.");
	}

	std::vector<GraphEvent> stream; // stream containing the events

	std::string line;
	count lc = 0; // line count

	std::string cookie = "DGS004";
	std::getline(dgsFile, line); // get DGS version
	lc++;
	if (line.compare(0, cookie.size(), cookie)) {
		ERROR("found " << line << " instead of " << cookie << " in first line");
		throw std::runtime_error("expected cookie in first line");
	}

	std::getline(dgsFile, line);
	lc++;
	// TODO: handle second line

	if (mapped) {
		// mapped format

		/**
		 * Maps key string to consecutive, 0-based node id.
		 */
		auto map = [&](std::string key) {
			auto iter = this->key2id.find(key);
			if (iter == key2id.end()) {
				key2id[key] = nextNode;
				nextNode++;
				return key2id[key];
			} else {
				return iter->second;
			}
		};

		while (std::getline(dgsFile, line)) {
			TRACE(line);
			lc++;
			std::vector<std::string> split = Aux::StringTools::split(line);
			std::string tag = split[0];

			// TODO: remove TRACE

			// parse commands
			if (tag.compare("st") == 0) { // clock
				stream.push_back(GraphEvent(GraphEvent::TIME_STEP));
			} else if (tag.compare("an") == 0) { // add node
				node u = map(split[1]);
				auto ev = GraphEvent(GraphEvent::NODE_ADDITION, u);
				TRACE(ev.toString());
				stream.push_back(ev);
			} else if (tag.compare("ae") == 0) { // add edge
				node u = map(split[2]);
				node v = map(split[3]);
				edgeweight w = std::stod(Aux::StringTools::split(split[4], '=')[1]); // weight=<w>
				auto ev = GraphEvent(GraphEvent::EDGE_ADDITION, u, v, w);
				TRACE(ev.toString());
				stream.push_back(ev);
			} else if (tag.compare("ce") == 0) { // update edge. Only the "weight" attribute is supported so far
				std::vector<std::string> uvs = Aux::StringTools::split(split[1], '-');
				node u = map(uvs[0]);
				node v = map(uvs[1]);
				edgeweight w = std::stod(Aux::StringTools::split(split[2], '=')[1]); // weight=<w>
				auto ev = GraphEvent(GraphEvent::EDGE_WEIGHT_UPDATE, u, v, w);
				TRACE(ev.toString());
				stream.push_back(ev);
			} else if (tag.compare("de") == 0) {
				std::vector<std::string> uvs = Aux::StringTools::split(split[1], '-');
				node u = map(uvs[0]);
				node v = map(uvs[1]);
				auto ev = GraphEvent(GraphEvent::EDGE_REMOVAL, u, v);
				TRACE(ev.toString());
				stream.push_back(ev);
			} else if (tag.compare("dn") == 0) {
				node u = map(split[1]);
				auto ev = GraphEvent(GraphEvent::NODE_REMOVAL, u);
				TRACE(ev.toString());
				stream.push_back(ev);
			} else {
				ERROR("malformed line (" << lc << ") : " << line);
				throw std::runtime_error("malformed line in .DGS file");
			}
		}


	} else {<commit_msg>DGSStreamParser: weights optional<commit_after>/*
 * DGSStreamParser.cpp
 *
 *  Created on: 23.12.2013
 *      Author: cls
 */

#include "DGSStreamParser.h"
#include "../auxiliary/StringTools.h"


namespace NetworKit {

DGSStreamParser::DGSStreamParser(std::string path, bool mapped, node baseIndex) : dgsFile(path), mapped(mapped), baseIndex(baseIndex), nextNode(0) {

}

std::vector<GraphEvent> NetworKit::DGSStreamParser::getStream() {
	if (! dgsFile.is_open()) {
		throw std::runtime_error("DGS input file could not be opened.");
	}

	std::vector<GraphEvent> stream; // stream containing the events

	std::string line;
	count lc = 0; // line count

	std::string cookie = "DGS004";
	std::getline(dgsFile, line); // get DGS version
	lc++;
	if (line.compare(0, cookie.size(), cookie)) {
		ERROR("found " << line << " instead of " << cookie << " in first line");
		throw std::runtime_error("expected cookie in first line");
	}

	std::getline(dgsFile, line);
	lc++;
	// TODO: handle second line

	if (mapped) {
		// mapped format

		/**
		 * Maps key string to consecutive, 0-based node id.
		 */
		auto map = [&](std::string key) {
			auto iter = this->key2id.find(key);
			if (iter == key2id.end()) {
				key2id[key] = nextNode;
				nextNode++;
				return key2id[key];
			} else {
				return iter->second;
			}
		};

		while (std::getline(dgsFile, line)) {
			TRACE(line);
			lc++;
			std::vector<std::string> split = Aux::StringTools::split(line);
			TRACE("split line: " << Aux::vectorToString(split));
			std::string tag = split[0];

			// TODO: remove TRACE

			// parse commands
			if (tag.compare("st") == 0) { // clock
				stream.push_back(GraphEvent(GraphEvent::TIME_STEP));
			} else if (tag.compare("an") == 0) { // add node
				node u = map(split[1]);
				auto ev = GraphEvent(GraphEvent::NODE_ADDITION, u);
				TRACE(ev.toString());
				stream.push_back(ev);
			} else if (tag.compare("ae") == 0) { // add edge
				node u = map(split[2]);
				node v = map(split[3]);
				edgeweight w = 1.0;
				if (split.size() >= 5) {
					w = std::stod(Aux::StringTools::split(split[4], '=')[1]); // weight=<w>
				}
				auto ev = GraphEvent(GraphEvent::EDGE_ADDITION, u, v, w);
				TRACE(ev.toString());
				stream.push_back(ev);
			} else if (tag.compare("ce") == 0) { // update edge. Only the "weight" attribute is supported so far
				std::vector<std::string> uvs = Aux::StringTools::split(split[1], '-');
				node u = map(uvs[0]);
				node v = map(uvs[1]);
				edgeweight w = std::stod(Aux::StringTools::split(split[2], '=')[1]); // weight=<w>
				auto ev = GraphEvent(GraphEvent::EDGE_WEIGHT_UPDATE, u, v, w);
				TRACE(ev.toString());
				stream.push_back(ev);
			} else if (tag.compare("de") == 0) {
				std::vector<std::string> uvs = Aux::StringTools::split(split[1], '-');
				node u = map(uvs[0]);
				node v = map(uvs[1]);
				auto ev = GraphEvent(GraphEvent::EDGE_REMOVAL, u, v);
				TRACE(ev.toString());
				stream.push_back(ev);
			} else if (tag.compare("dn") == 0) {
				node u = map(split[1]);
				auto ev = GraphEvent(GraphEvent::NODE_REMOVAL, u);
				TRACE(ev.toString());
				stream.push_back(ev);
			} else {
				ERROR("malformed line (" << lc << ") : " << line);
				throw std::runtime_error("malformed line in .DGS file");
			}
		}


	} else {
{'fraction_non_alphanumeric': 0.18926509595880792, 'fraction_numerical': 0.012950538305507879, 'mean_word_length': 3.5687811831789022, 'pattern_counts': {'":': 0, '<': 47, '<?xml version=': 0, '>': 22, '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': '3082130', 'n_tokens_mistral': 2651, 'n_tokens_neox': 2278, 'n_words': 518}