File size: 10,574 Bytes
9ddcd69
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d433cc6
 
9ddcd69
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# VerificaUD - Verificador de arquivos CoNLL-U para textos em Português
#
# Este programa recebe um arquivo CoNLL-U que contém um corpus
#   em Português e gera um relatório dos problemas encontrados de
#   acordo com regras de verificação definidas:
#         https://sol.sbc.org.br/index.php/stil/article/view/25485
#
# Parâmetros obrigatórios:
# -o <report.txt> --output <report.txt>   (arquivo onde o relatório será salvo)
# <corpus.conllu>    (arquivo a ser verificado)
#
# Opções:
# -h --help help
# -s --struct só verificação estrutural (struct)
# -t --tagger só verificação estrutural e morfológica (tagger)
# -p --parser verificação estrutural, morfológica e sintática (parser) - default
# 
# Exemplo de utilização:
#
# verificaUD -o report.txt corpus.conllu
#
# Lê o arquivo corpus.conllu e salva o resultado da verificação
#   no arquivo 'report.txt'
#
# last edit: 09/03/2025
# created by Lucelene Lopes - lucelene@gmail.com

import sys, os
sys.path.insert(0, 'src/packages')
from conlluF import conlluFile
from tagger import checkTerm, taggerDef
from parser import checkSent, parserDef
from structure import goodNumbers, wellformedTree, structDef
from reporter import conlluReports

#################################################
### Captura de argumentos da linha de comando
#################################################
def parseOptions(arguments):
    # default options
    output_file, input_file, mode = "report.conllu", "corpus.conllu", "-"
    i = 1
    while i < len(arguments):
        if (arguments[i][0] == "-"):
            # ajuda (help) - mostra ajuda, nada é executado
            if ((arguments[i][1] == "h") and (len(arguments[i])==2)) or \
               (arguments[i] == "-help"):
                print("Opções:\n-h ajuda",
                      "Exemplo de utilização:", \
                      "verificaUD -o report.conllu corpus.conllu", \
                      "Lê o arquivo corpus.conllu e salva o resultado da verificação", \
                      "no arquivo 'report.txt'", \
                      sep="\n")
                return None
            # opção de arquivo de saída (um nome de arquivo)
            elif ((arguments[i][1] == "o") and (len(arguments[i])==2)) or \
                 (arguments[i] == "--output"):
                output_file = arguments[i+1]
                i += 2
            elif ((arguments[i][1] == "s") and (len(arguments[i])==2)) or \
                 (arguments[i] == "--struct"):
                if (mode == "-"):
                    mode = "s"
                i += 1
            elif ((arguments[i][1] == "t") and (len(arguments[i])==2)) or \
                 (arguments[i] == "--tagger"):
                if (mode == "-") or (mode == "s"):
                    mode = "t"
                i += 1
            elif ((arguments[i][1] == "p") and (len(arguments[i])==2)) or \
                 (arguments[i] == "--parser"):
                if (mode == "-") or (mode == "s") or (mode == "t"):
                    mode = "p"
                i += 1
            # opções inválidas - nada é executado
            else:
                print("Opção {} inválida, demais opções ignoradas, por favor execute novamente".format(arguments[i]))
                return None
        # arquivo de entrada - só é incluído se existir
        else:
            if (os.path.isfile(arguments[i])):
                input_file = arguments[i]
                i += 1
            else:
                print("O arquivo {} não foi encontrado, por favor execute novamente".format(arguments[i]))
                return None
    if (mode == "-"):
        mode = "p"
    return [output_file, input_file], mode

########################################
#  Nosso verificador STRUCT
#  input:
#     name     -  the name of the conllu file
#     report   -  the name of the textual output file
########################################
def struct(name, report, samples):
    dump = open(report, "w")
    base = conlluFile(name)
    s, t = base.getSandT()
    print("Base:", name, "- sentenças:", s, "- tokens:", t, "- STRUCT relat:", report)
    totalS, totalT = 0, 0
    for i in range(base.getS()):
        b = base.getSentByIndex(i)
        acc = goodNumbers(b, dump, samples)
        if (acc == 0):
            acc = wellformedTree(b, dump, samples)
        totalT += acc
        if (acc > 0):
            totalS += 1
    dump.close()
    print("Problemas da Estrutura de CoNLL-U (struct):", totalT)
    print("Sentenças sem problems {} ({:2.2f}%)".format(s-totalS, 100*(s-totalS)/s))
    print("Tokens sem problems {} ({:2.2f}%)".format(t-totalT, 100*(t-totalT)/t))
    return s, t, totalT, 0

########################################
#  Nosso verificador TAGGER
#  input:
#     name     -  the name of the conllu file
#     report   -  the name of the textual output file
########################################
def tagger(name, report, samples):
    dump = open(report, "w")
    base = conlluFile(name)
    s, t = base.getSandT()
    print("Base:", name, "- sentenças:", s, "- tokens:", t, "- TAGGER relat:", report)
    totalS, totalT = 0, 0
    for i in range(base.getS()):
        b = base.getSentByIndex(i)
        acc = 0
        for tk in b[4]:
            acc += checkTerm(tk, b[0], dump, samples)
        totalT += acc
        if (acc > 0):
            totalS += 1
    dump.close()
    print("Problemas de Formação Lexical (tagger):", totalT)
    print("Sentenças sem problems {} ({:2.2f}%)".format(s-totalS, 100*(s-totalS)/s))
    print("Tokens sem problems {} ({:2.2f}%)".format(t-totalT, 100*(t-totalT)/t))
    return s, t, totalT, 0

########################################
#  Nosso verificador PARSER
#  input:
#     name     -  the name of the conllu file
#     report   -  the name of the textual output file
########################################
def parser(name, report, samples):
    dump = open(report, "w")
    base = conlluFile(name)
    s, t = base.getSandT()
    print("Base:", name, "- sentenças:", s, "- tokens:", t, "- PARSER relat:", report)
    totalS, totalT, totalW = 0, 0, 0
    for i in range(base.getS()):
        b = base.getSentByIndex(i)
        rules, acc, warn = checkSent(b, dump, samples)
        totalT += acc
        totalW += warn
        if (acc+warn > 0):
            totalS += 1
            ####
            
    dump.close()
    print("Problemas de Dependência Relacional (parser):", totalT)
    print("Avisos de verificação de Dependência Relacional (parser):", totalW)
    print("Sentenças sem problems ou avisos {} ({:2.2f}%)".format(s-totalS, 100*(s-totalS)/s))
    print("Tokens sem problems {} ({:2.2f}%)".format(t-totalT, 100*(t-totalT)/t))
    print("Tokens sem problems ou avisos {} ({:2.2f}%)".format(t-(totalT+totalW), 100*(t-(totalT+totalW))/t))
    return s, t, totalT, totalW

########################################
#  mergeReport
########################################
def mergeReport(name, corpus, outfile, s, t, e_s, w_s, e_t, w_t, e_p, w_p):
    issues = []
    probS = []
    avisoS = []
    infile = open(name+"_struct.txt")
    for line in infile:
        issues.append(line[:-1])
        buf = line.split("\t")
        if (buf[0] not in probS):
            probS.append(buf[0])
    infile.close()
    infile = open(name+"_tagger.txt")
    for line in infile:
        issues.append(line[:-1])
        buf = line.split("\t")
        if (buf[0] not in probS):
            probS.append(buf[0])
    infile.close()
    infile = open(name+"_parser.txt")
    for line in infile:
        issues.append(line[:-1])
        if ("Normalmente" not in line):
            buf = line.split("\t")
            if (buf[0] not in probS):
                probS.append(buf[0])
        else:
            buf = line.split("\t")
            if (buf[0] not in avisoS):
                avisoS.append(buf[0])
    infile.close()
    issues.sort()
    print("Arquivo:", corpus+"\n", file=outfile)
    print("sentencas:                   {:5>} (tokens: {})".format(s,t), file=outfile)
    print("sentencas sem erro:          {:5>}".format(s-len(probS)), file=outfile)
    print("sentencas sem erro ou aviso: {:5>}".format(s-len(probS)-len(avisoS)), file=outfile)
    print("erros estruturais: {:5>} - avisos: {:5>}".format(e_s, w_s), file=outfile)
    print("erros lexicais:    {:5>} - avisos: {:5>}".format(e_t, w_t), file=outfile)
    print("erros sintáticos:  {:5>} - avisos: {:5>}".format(e_p, w_p), file=outfile)
    
    print("\nErros e avisos encontrados:", file=outfile)
    for i in issues:
        buf = i.split("\t")
        print("{:15}\ttoken {:3}\t{}".format(buf[0], buf[1], buf[2]), file=outfile)

########################################
#  Do it all
########################################
def doIt(name, outfile, mode):
    if (name[-7:] != ".conllu"):
        outputfile = name
    else:
        outputfile = name[:-7]
    samples = conlluReports("samples", [structDef,taggerDef,parserDef])
    if (mode in ["s", "t", "p"]):
        print("Executando verificação estrutural...")
        s, t, e_s, w_s = struct(name, outputfile+"_struct.txt", samples)
    else:
        s, t, e_s, w_s = 0,0,0,0
    if (mode in ["t", "p"]):
        print("Executando verificação de tagger...")
        s, t, e_t, w_t = tagger(name, outputfile+"_tagger.txt", samples)
    else:
        s, t, e_t, w_t = 0,0,0,0
    if (mode in ["p"]):
        print("Executando verificação de parser...")
        s, t, e_p, w_p = parser(name, outputfile+"_parser.txt", samples)
    else:
        s, t, e_p, w_p = 0,0,0,0
    samples.closeAll()
    mergeReport(outputfile, name, outfile, s, t, e_s, w_s, e_t, w_t, e_p, w_p)
    return e_s+e_t+e_p, w_s+w_t+w_p


########################################
#  Main function - verificaUD
########################################
def verificaUD(input_file=None, output_file=None, chosen_mode=None):
    if (input_file) and (output_file) and (chosen_mode):
        io_files = [output_file, input_file]
        mode = chosen_mode
    elif (len(sys.argv) == 1):
        io_files = ["report.txt", "corpus.conllu"]
        mode = "p"
    else:
        io_files, mode = parseOptions(sys.argv)
    if (io_files != None):
        print("Starting verification of", io_files[1])
        outfile = open(io_files[0], "w")
        e, w = doIt(io_files[1], outfile, mode)
        outfile.close()
        print("Arquivo {} salvo com o relatório do arquivo {} contendo {} erros e {} avisos".format(io_files[0],io_files[1], e, w))
    else:
        print("Por favor, execute o programa novamente")