<commit_before>package jp.naist.sd.kenja.factextractor;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;

import org.apache.commons.lang3.tuple.Pair;

import com.google.common.base.Charsets;
import com.google.common.io.Files;

public class TextFormatTreeWriter extends TreeWriter {
	private File outputFile;

	public TextFormatTreeWriter(File baseDir) {
		super(baseDir);
		outputFile = baseDir;
		// TODO Auto-generated constructor stub
	}

	@Override
	public void writeTree(Tree tree) {
		if (tree.isRoot()) {
			try {
				Files.touch(outputFile);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		HashMap<String, Tree> treeMap = new HashMap<String, Tree>();
		for (Tree t : tree.getChildTrees()) {
			treeMap.put(t.getName(), t);
		}

		HashMap<String, Blob> blobMap = new HashMap<String, Blob>();
		for (Blob b : tree.getBlobs()) {
			blobMap.put(b.getName(), b);
		}

		for (Pair<String, String> content : createTreeContents(tree)) {
			if (content.getLeft() == "Blob") {
				writeBlob(blobMap.get(content.getRight()));
			} else if (content.getLeft() == "Tree") {
				writeTree(treeMap.get(content.getRight()));
			}
			StringBuilder builder = new StringBuilder();
			builder.append(content.getLeft());
			builder.append(" ");
			builder.append(content.getRight());
			builder.append("\n");
			try {
				Files.append(builder.toString(), outputFile, Charsets.US_ASCII);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	}

	@Override
	public void writeBlob(Blob blob) {
		int lines = blob.getBody().split("\n").length;
		try {
			Files.append(lines + " lines" + "\n", outputFile, Charsets.US_ASCII);
			Files.append(blob.getBody(), outputFile, Charsets.US_ASCII);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private List<Pair<String, String>> createTreeContents(Tree tree) {
		List<Pair<String, String>> contents = new ArrayList<Pair<String, String>>();
<commit_msg>Add 'Start' and 'End' lines to Tree represents.<commit_after>package jp.naist.sd.kenja.factextractor;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;

import org.apache.commons.lang3.tuple.Pair;

import com.google.common.base.Charsets;
import com.google.common.io.Files;

public class TextFormatTreeWriter extends TreeWriter {
	private File outputFile;

	public TextFormatTreeWriter(File baseDir) {
		super(baseDir);
		outputFile = baseDir;
		// TODO Auto-generated constructor stub
	}

	@Override
	public void writeTree(Tree tree) {
		if (tree.isRoot()) {
			try {
				Files.touch(outputFile);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		String line = "Start" + tree.getName();
		try {
			Files.append(line, outputFile, Charsets.US_ASCII);
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		HashMap<String, Tree> treeMap = new HashMap<String, Tree>();
		for (Tree t : tree.getChildTrees()) {
			treeMap.put(t.getName(), t);
		}

		HashMap<String, Blob> blobMap = new HashMap<String, Blob>();
		for (Blob b : tree.getBlobs()) {
			blobMap.put(b.getName(), b);
		}

		for (Pair<String, String> content : createTreeContents(tree)) {
			if (content.getLeft() == "Blob") {
				writeBlob(blobMap.get(content.getRight()));
			} else if (content.getLeft() == "Tree") {
				writeTree(treeMap.get(content.getRight()));
			}
			StringBuilder builder = new StringBuilder();
			builder.append(content.getLeft());
			builder.append(" ");
			builder.append(content.getRight());
			builder.append("\n");
			try {
				Files.append(builder.toString(), outputFile, Charsets.US_ASCII);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		line = "End" + tree.getName();
		try {
			Files.append(line, outputFile, Charsets.US_ASCII);
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	}

	@Override
	public void writeBlob(Blob blob) {
		int lines = blob.getBody().split("\n").length;
		try {
			Files.append(lines + " lines" + "\n", outputFile, Charsets.US_ASCII);
			Files.append(blob.getBody(), outputFile, Charsets.US_ASCII);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private List<Pair<String, String>> createTreeContents(Tree tree) {
		List<Pair<String, String>> contents = new ArrayList<Pair<String, String>>();

{'fraction_non_alphanumeric': 0.14692936491301614, 'fraction_numerical': 0.0012575979878432194, 'mean_word_length': 3.9297520661157024, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, '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': '4112330', 'n_tokens_mistral': 1723, 'n_tokens_neox': 1536, 'n_words': 340}