calc_all

mail@pastecode.io avatar
unknown
python
a year ago
9.5 kB
2
Indexable
Never
import csv
import re
import string

# Define function to count repeated punctuation in a comment
def count_repeated_punctuation(comment):
    # Find all occurrences of repeated punctuation using a regular expression
    matches = re.findall(r'([{}])\1+'.format(re.escape(string.punctuation)), comment)

    # Count the number of unique matches
    return len(set(matches))

# Open TSV file and read rows
with open('CFE-D.tsv', 'r', newline='') as tsv_file:
    tsv_reader = csv.reader(tsv_file, delimiter='\t')
    next(tsv_reader) # skip header row
    
    # initialize counters 
    interrogation_infavor = 0
    interrogation_against = 0
    excl_infavor = 0
    excl_against = 0
    point_infavor = 0
    point_against = 0
    comma_infavor = 0
    comma_against = 0
    pointVirgule_infavor = 0
    pointVirgule_against = 0
    deuxPoints_infavor = 0
    deuxPoints_against = 0
    parentheses_infavor = 0
    parentheses_against = 0
    guillemets_infavor = 0
    guillemets_against = 0
    ponctRep_infavor = 0
    ponctRep_against = 0
    tiret_infavor = 0
    tiret_against = 0
    total_infavor = 0
    total_against = 0
    ptdSuspension_infavor = 0
    ptdSuspension_against = 0

    for row in tsv_reader:
        # Extract relevant columns
        alignment = row[2]
        comment = row[3]
        label = row[12] if len(row) >= 13 else None

        if alignment == "In favor" or label == "In favor":
            total_infavor += 1
        if alignment == "Against" or label == "Against":
            total_against += 1

        # Count repeated punctuation in comment
        punctuation_count = count_repeated_punctuation(comment)

        # Update counts based on alignment and punctuation count
        if alignment == "In favor" or label == "In favor" and punctuation_count > 0:
            ponctRep_infavor += 1
        elif alignment == "Against" or label == "Against" and punctuation_count > 0:
            ponctRep_against += 1

        # Update counts based on alignment and punctuation count
        if '?' in comment :
            if alignment == "In favor" or label == "In favor":
                interrogation_infavor += 1
            elif alignment == "Against" or label == "Against":
                interrogation_against += 1
        if '!' in comment :
            if alignment == "In favor" or label == "In favor":
                excl_infavor += 1
            elif alignment == "Against" or label == "Against":
                excl_against += 1
        if '.' in comment :
            if alignment == "In favor" or label == "In favor":
                point_infavor += 1
            elif alignment == "Against" or label == "Against":
                point_against += 1
        if ',' in comment :
            if alignment == "In favor" or label == "In favor":
                comma_infavor += 1
            elif alignment == "Against" or label == "Against":
                comma_against += 1
        if ';' in comment :
            if alignment == "In favor" or label == "In favor":
                pointVirgule_infavor += 1
            elif alignment == "Against" or label == "Against":
                pointVirgule_against += 1
        if ':' in comment :
            if alignment == "In favor" or label == "In favor":
                deuxPoints_infavor += 1
            elif alignment == "Against" or label == "Against":
                deuxPoints_against += 1
        if '(' in comment or ')' in comment:
            if alignment == "In favor" or label == "In favor":
                parentheses_infavor += 1
            elif alignment == "Against" or label == "Against":
                parentheses_against += 1
        if '"' in comment or "'" in comment:
            if alignment == "In favor" or label == "In favor":
                guillemets_infavor += 1
            elif alignment == "Against" or label == "Against":
                guillemets_against += 1
        if '-' in comment :
            if alignment == "In favor" or label == "In favor":
                tiret_infavor += 1
            elif alignment == "Against" or label == "Against":
                tiret_against += 1
        if '...' in comment :
            if alignment == "In favor" or label == "In favor":
                ptdSuspension_infavor += 1
            elif alignment == "Against" or label == "Against":
                ptdSuspension_against += 1


    # Print results for each punctuation sign
    print(f'Nombre de commentaires POUR contentant la ponctuation repetée : {ponctRep_infavor}')
    print(f'Nombre de commentaires CONTRE contentant la ponctuation repetée : {ponctRep_against}')    
    print("-"*33)
    print(f'Nombre de commentaires POUR contentant "?" : {interrogation_infavor}')
    print(f'Nombre de commentaires CONTRE contentant "?" : {interrogation_against}')
    print("-"*33)
    print(f'Nombre de commentaires POUR contentant "!" : {excl_infavor}')
    print(f'Nombre de commentaires CONTRE contentant "!" : {excl_against}')
    print("-"*33)
    print(f'Nombre de commentaires POUR contentant "." : {point_infavor}')
    print(f'Nombre de commentaires CONTRE contentant "." : {point_against}')
    print("-"*33)
    print(f'Nombre de commentaires POUR contentant "," : {comma_infavor}')
    print(f'Nombre de commentaires CONTRE contentant "," : {comma_against}')
    print("-"*33)
    print(f'Nombre de commentaires POUR contentant ";" : {pointVirgule_infavor}')
    print(f'Nombre de commentaires CONTRE contentant ";" : {pointVirgule_against}')
    print("-"*33)
    print(f'Nombre de commentaires POUR contentant ":" : {deuxPoints_infavor}')
    print(f'Nombre de commentaires CONTRE contentant ":" : {deuxPoints_against}')
    print("-"*33)
    print(f'Nombre de commentaires POUR contentant "(" ou ")" : {parentheses_infavor}')
    print(f'Nombre de commentaires CONTRE contentant "(" ou ")" : {parentheses_against}')
    print("-"*33)
    print(f'Nombre de commentaires POUR contentant les guillemets : {guillemets_infavor}')
    print(f'Nombre de commentaires CONTRE contentant les guillemets : {guillemets_against}')
    print("-"*33)
    print(f'Nombre de commentaires POUR contentant le tiret : {tiret_infavor}')
    print(f'Nombre de commentaires CONTRE contentant le tiret : {tiret_against}')
    print("-"*33)
    print(f'Nombre de commentaires POUR contentant les points de suspension : {ptdSuspension_infavor}')
    print(f'Nombre de commentaires CONTRE contentant les points de suspension : {ptdSuspension_against}')


    print("-"*33)
    print("-"*33)
    print(f'Fréquence relative des commentaires POUR contentant la ponctuation repetée :   {round(ponctRep_infavor / total_infavor * 100, 2)}')
    print(f'Fréquence relative des commentaires CONTRE contentant la ponctuation repetée :   {round(ponctRep_against / total_against * 100, 2)}')
    print("-"*33)
    print(f'Fréquence relative des commentaires POUR contentant "?" :   {round(interrogation_infavor / total_infavor * 100, 2)}')
    print(f'Fréquence relative des commentaires CONTRE contentant "?" :   {round(interrogation_against / total_against * 100, 2)}')
    print("-"*33)
    print(f'Fréquence relative des commentaires POUR contentant "!" :   {round(excl_infavor / total_infavor * 100, 2)}')
    print(f'Fréquence relative des commentaires CONTRE contentant "!" :   {round(excl_against / total_against * 100, 2)}')
    print("-"*33)
    print(f'Fréquence relative des commentaires POUR contentant "." :   {round(point_infavor / total_infavor * 100, 2)}')
    print(f'Fréquence relative des commentaires CONTRE contentant "." :   {round(point_against / total_against * 100, 2)}')
    print("-"*33)
    print(f'Fréquence relative des commentaires POUR contentant "," :   {round(comma_infavor / total_infavor * 100, 2)}')
    print(f'Fréquence relative des commentaires CONTRE contentant "," :   {round(comma_against / total_against * 100, 2)}')
    print("-"*33)
    print(f'Fréquence relative des commentaires POUR contentant ";" :   {round(pointVirgule_infavor / total_infavor * 100, 2)}')
    print(f'Fréquence relative des commentaires CONTRE contentant ";" :   {round(pointVirgule_against / total_against * 100, 2)}')
    print("-"*33)
    print(f'Fréquence relative des commentaires POUR contentant "(" ou ")" :   {round(parentheses_infavor / total_infavor * 100, 2)}')
    print(f'Fréquence relative des commentaires CONTRE contentant "(" ou ")" :   {round(parentheses_against / total_against * 100, 2)}')
    print("-"*33)
    print(f'Fréquence relative des commentaires POUR contentant les guillemets :   {round(guillemets_infavor / total_infavor * 100, 2)}')
    print(f'Fréquence relative des commentaires CONTRE contentant les guillemets :   {round(guillemets_against / total_against * 100, 2)}')
    print("-"*33)
    print(f'Fréquence relative des commentaires POUR contentant le tiret : {round(tiret_infavor / total_infavor * 100, 2)}')
    print(f'Fréquence relative des commentaires CONTRE contentant le tiret : {round(tiret_against / total_infavor * 100, 2)}')
    print("-"*33)
    print(f'Fréquence relative des commentaires POUR contentant les points de suspension : {round(ptdSuspension_infavor / total_infavor * 100, 2)}')
    print(f'Fréquence relative des commentaires CONTRE contentant les points de suspension : {round(ptdSuspension_against / total_infavor * 100, 2)}')