#!/usr/bin/env python3
# encoding: utf-8
from Lib import stimuli

class Participant:
	pass

class Row:
	def __init__(self, participant, sentence_pos, word_pos, word_name, reason):
		self.participant = participant
		self.sentence_pos = sentence_pos
		self.word_pos = word_pos
		self.word_name = word_name
		self.reason = reason
	
def get_participant(sid):
	participant = Participant()
	participant.sid = sid
	participant.sentences = stimuli.readfile('Output/Stimuli/' + sid + '.txt', 80)
	return participant

def select_by_word(participant, match_word, reason):
	result = list()
	for i in range(0, 80):
		sentence = participant.sentences[i]
		
		for j in range(0, len(sentence.words)):
			word = sentence.words[j]
			
			if word.word == match_word:
				result.append(Row(participant, i+1, j+1, word.word, reason))
	
	return result

def select_by_sentence(participant, sentence_is, reason):
	result = list()
	for i in sentence_is:
		sentence = participant.sentences[i]
		
		for j in range(0, len(sentence.words)):
			word = sentence.words[j]
			
			result.append(Row(participant, i+1, j+1, word.word, reason))
	
	return result

def select_specific(participant, sentence_is, match_word, reason):
	result = list()
	for i in sentence_is:
		sentence = participant.sentences[i]

		for j in range(0, len(sentence.words)):
			word = sentence.words[j]
	
			if word.word == match_word:
				result.append(Row(participant, i+1, j+1, word.word, reason))		
				
	return result
	
excluded = list()

S002 = get_participant('S002')
S004 = get_participant('S004')
S006 = get_participant('S006')
S007 = get_participant('S007')
S011 = get_participant('S011')

excluded.extend(select_by_word(S002, 'Hoogkarspel', 'invalid form'))
excluded.extend(select_by_sentence(S004, [30], 'sentence terminated'))
excluded.extend(select_specific(S004, [60], 'Rijswijk', 'invalid form'))
excluded.extend(select_by_word(S007, 'Maassluis', 'invalid form'))
excluded.extend(select_specific(S007, [27, 31, 32], 'Roermond', 'invalid form'))
excluded.extend(select_by_sentence(S011, [15, 23], 'hesitation'))
excluded.extend(select_specific(S011, [40], 'Venlo', 'invalid form'))

try:
	with open('Output/excluded_missings.csv') as file:
		next(file)
		participant = None
		for line in file:
			cells = line.split(',')
			
			sid = cells[1].replace('"', '')
			
			if participant is None or participant.sid != sid:
				participant = get_participant(sid)
				
			sentence_i = int(cells[2]) - 1
			match_word = cells[3].replace('"', '')
			print(select_specific(participant, [sentence_i], match_word, '')[0].word_pos)
	
	quit()
except IOError:
	pass # nevermind, does not exist yet

print('participant;sentence_pos;word_pos;word_name;reason')

for e in excluded:
    print(";".join([e.participant.sid, 
                    str(e.sentence_pos),
                    str(e.word_pos),
                    e.word_name,
                    e.reason]))

