<commit_before>/*
	Copyright (C) 2013, The Aikuma Project
	AUTHORS: <NAME> and <NAME>
*/
package org.lp20.aikuma.model;

import android.util.Pair;
import java.io.File;
import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.Iterator;
import java.util.UUID;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.lp20.aikuma.util.FileIO;

/**
 * A class to represent the alignment between segments in an original recording
 * and a respeaking.
 *
 * @author	<NAME>	<<EMAIL>>
 * @author	<NAME>	<<EMAIL>>
 */
public class Segments {

	/**
	 * Creates an object that represents a mapping of recording segments between
	 * the original and the respeaking.
	 *
	 * @param	respeaking	the respeaking in question.
	 */
	public Segments(Recording respeaking) {
		this();
		this.respeaking = respeaking;
		try {
			readSegments(new File(Recording.getRecordingsPath(),
					respeaking.getGroupId() + "/" +
					respeaking.getId() + ".map"));
		} catch (Exception e) {
			//Issue with reading mapping. Maybe throw an exception?
		}
	}

	/**
	 * Constructor to create an empty Segments object.
	 */
	public Segments() {
		segmentMap = new LinkedHashMap<Segment, Segment>();
	}

	/**
	 * Gets the respeaking segment associated with the supplied original
	 * segment.
	 *
	 * @param	originalSegment	A Segment object representing a segment of the original audio.
	 * @return	A respeaking Segment object corresponding to the
	 * originalSegment
	 */
	public Segment getRespeakingSegment(Segment originalSegment) {
		return segmentMap.get(originalSegment);
	}

	/**
	 * Adds a pair of segments to the Segments; analogous to Map.add().
	 *
	 * @param	originalSegment	A segment of an original recording.
	 * @param	respeakingSegment	A respeaking segment corresponding to
	 * originalSegment.
	 */
	public void put(Segment originalSegment,
					Segment respeakingSegment) {
		segmentMap.put(originalSegment, respeakingSegment);
	}

	/**
	 * Returns an iterator over the segments of the original recording.
	 *
	 * @return	An iterator over the segments of the original recording.
	 */
	public Iterator<Segment> getOriginalSegmentIterator() {
		return segmentMap.keySet().iterator();
	}

	/**
	 * Reads the segments from a file.
	 *
	 * @param	path	The path to the file.
	 * @throws	Exception	If the segments cannot be read.
	 */
	private void readSegments(File path) throws Exception {
		String mapString = FileIO.read(path);
		String[] lines = mapString.split("\n");
		segmentMap = 
				new LinkedHashMap<Segment, Segment>();
		for (String line : lines) {
			String[] segmentMatch = line.split(":");
			if (segmentMatch.length != 2) {
				throw new Exception(
						"There must be just one colon on in a segment mapping line");
			}
			String[] originalSegment = segmentMatch[0].split(",");
			String[] respeakingSegment = segmentMatch[1].split(",");
			segmentMap.put(new Segment(Long.parseLong(originalSegment[0]),
								Long.parseLong(originalSegment[1])),
					new Segment(Long.parseLong(respeakingSegment[0])
						, Long.parseLong(respeakingSegment[1])));<commit_msg>Removed pokemon exception handling in Segments.<commit_after>/*
	Copyright (C) 2013, The Aikuma Project
	AUTHORS: <NAME> and <NAME>
*/
package org.lp20.aikuma.model;

import android.util.Pair;
import android.widget.Toast;
import java.io.File;
import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.Iterator;
import java.util.UUID;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.lp20.aikuma.util.FileIO;

/**
 * A class to represent the alignment between segments in an original recording
 * and a respeaking.
 *
 * @author	<NAME>	<<EMAIL>>
 * @author	<NAME>	<<EMAIL>>
 */
public class Segments {

	/**
	 * Creates an object that represents a mapping of recording segments between
	 * the original and the respeaking.
	 *
	 * @param	respeaking	the respeaking in question.
	 */
	public Segments(Recording respeaking) {
		this();
		this.respeaking = respeaking;
		try {
			readSegments(new File(Recording.getRecordingsPath(),
					respeaking.getGroupId() + "/" +
					respeaking.getId() + ".map"));
		} catch (IOException e) {
			//Issue with reading mapping.
			throw new RuntimeException(e);
		}
	}

	/**
	 * Constructor to create an empty Segments object.
	 */
	public Segments() {
		segmentMap = new LinkedHashMap<Segment, Segment>();
	}

	/**
	 * Gets the respeaking segment associated with the supplied original
	 * segment.
	 *
	 * @param	originalSegment	A Segment object representing a segment of the original audio.
	 * @return	A respeaking Segment object corresponding to the
	 * originalSegment
	 */
	public Segment getRespeakingSegment(Segment originalSegment) {
		return segmentMap.get(originalSegment);
	}

	/**
	 * Adds a pair of segments to the Segments; analogous to Map.add().
	 *
	 * @param	originalSegment	A segment of an original recording.
	 * @param	respeakingSegment	A respeaking segment corresponding to
	 * originalSegment.
	 */
	public void put(Segment originalSegment,
					Segment respeakingSegment) {
		segmentMap.put(originalSegment, respeakingSegment);
	}

	/**
	 * Returns an iterator over the segments of the original recording.
	 *
	 * @return	An iterator over the segments of the original recording.
	 */
	public Iterator<Segment> getOriginalSegmentIterator() {
		return segmentMap.keySet().iterator();
	}

	/**
	 * Reads the segments from a file.
	 *
	 * @param	path	The path to the file.
	 * @param	IOException	In case there is an issue reading from the segments
	 * file
	 */
	private void readSegments(File path) throws IOException {
		String mapString = FileIO.read(path);
		String[] lines = mapString.split("\n");
		segmentMap = 
				new LinkedHashMap<Segment, Segment>();
		for (String line : lines) {
			String[] segmentMatch = line.split(":");
			if (segmentMatch.length != 2) {
				throw new RuntimeException(
						"There must be just one colon on in a segment mapping line");
			}
			String[] originalSegment = segmentMatch[0].split(",");
			String[] respeakingSegment = segmentMatch[1].split(",");
			segmentMap.put(new Segment(Long.parseLong(originalSegment[0]),
								Long.parseLong(originalSegment[1])),
					new Segment(Long.parseLong(respeakingSegment[0])
						, Long.parseLong(respeakingSegment[1])));
{'fraction_non_alphanumeric': 0.1104743712193569, 'fraction_numerical': 0.005093919134033747, 'mean_word_length': 3.9239811912225706, 'pattern_counts': {'":': 2, '<': 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': '3391984', 'n_tokens_mistral': 2056, 'n_tokens_neox': 1842, 'n_words': 575}