import csv

# make a dic where of (short form) sentences keyed by sentence ID
ssdic = dict()
with open('sentences.csv', 'r', newline='\n') as sentences:
    reader = csv.DictReader(sentences)
    for row in reader:
        ssdic[row['ssent']] = row['sentID']
sentences.close()

#######################
FLAG = '<flag>'         ## flag for filename, eg _TEST or _FINAL
DATAFILE = '<filename>' ## name of datafile, eg FI-output.txt
#######################

prefix = "['As a software engineer, "
prefix_length = len(prefix)

datafile = open(DATAFILE, 'r')
OUTFILE = DATAFILE + FLAG + '.RESULTS.txt'
outfile = open(OUTFILE, 'w')

# write the headers
outfile.write('sentID, pronoun\n')

# order matters here, and she needs to precede he
pronouns = [
            ' she or he ',
            ' she/he ',
            ' she ',
            ' he or she ',
            ' he/she ',
            ' he ',
            ' they ',
            ' you ',
            ' it ',
            ' one ']


# note that we check for only one (possibly compound) pronoun in the output
def getpro(line):
    if len(line) < 9:
        print('*** getpro: line too short, assigning pro = (none)')
        print('*** line =', line)
        return '(none)'
    for pro in pronouns:
        if line.find(pro) > -1:
            return (pro.strip())
    return '(none)'
        
    
# now process the data file
for line in datafile:
    ################################################################
    ### mods for FIX trial only:
    ### remove quote right before any pronoun so it can be recognised
    # line = line.replace(", '", ", ' ")
    ### add back prefix so remaining processing is the same
    # line = line.replace("['She", "['As a software engineer, she")    
    ### (comment out for other trials)
    ################################################################
    
    # look up the sentence ID
    dotpos = line.find('.')
    ssent = line[prefix_length:dotpos+1]  
    sentID = ssdic[ssent]
    line = line[dotpos+5:] # now we just need the remainder of the line
    line = line.lower()
    outfile.write(sentID + ',' + getpro(line) + '\n')


datafile.close()
outfile.close()

print('** processing complete **\n\n')



    



