-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgrb.py
More file actions
692 lines (567 loc) · 26.2 KB
/
grb.py
File metadata and controls
692 lines (567 loc) · 26.2 KB
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
import re
import os
import shlex
import json
import shutil
import nltk
import argparse
from langdetect import detect
from pathlib import Path
from datetime import datetime
from datetime import date
from pybtex.database import parse_file, BibliographyDataError, Entry
from pybtex.database.input.bibtex import UndefinedMacro
from pybtex.scanner import TokenRequired
from pybtex.richtext import Text, Tag
from nltk.corpus import stopwords
now = datetime.now()
label_date = str(date.today()) + now.strftime("_%H-%M-%S")
#Stopwords for capitalization
#nltk.download('stopwords')
LIST_STOPWORDS_ENGLISH = stopwords.words('english')
LIST_STOPWORDS_ENGLISH.append('st')
LIST_STOPWORDS_ENGLISH.append('nd')
LIST_STOPWORDS_ENGLISH.append('rd')
LIST_STOPWORDS_PORTUGUESE = stopwords.words('portuguese')
LIST_STOPWORDS_SPANISH = stopwords.words('spanish')
LIST_STOPWORDS_GERMAN = stopwords.words('german')
LIST_STOPWORDS_GERMAN.append('and')
# Exception List
EXCEPTION_LIST = ['ArXiv', 'arXiv', 'arxiv', '-', '\&', '&']
#Create directories
path_current = os.getcwd() # Current directory
path_bib_original = os.path.join(path_current, "OriginalBIB")
path_reports = os.path.join(path_current, "GenerateReports")
path_bib = os.path.join(path_current, "GenerateBIB")
## Load configuration
def main():
LANGUAGES = {
"en": "english",
"pt": "portuguese",
}
TYPES = {
"num": "num-alpha",
"alpha": "num-alpha",
"apa": "apa",
}
#arguments entered via the command line
argparser = argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpFormatter
)
argparser.add_argument(dest="filename")
argparser.add_argument(
"-L",
"--language",
choices=LANGUAGES.keys(),
help="Select a language { 'en' or 'pt' }",
default="english",
)
argparser.add_argument(
"-T",
"--type",
choices=TYPES.keys(),
help="Select a type { 'num', 'alpha' or 'apa' }",
default="num",
)
#for test
'''
LANGUAGE = 'en'
print('LANGUAGE: ', LANGUAGE)
LANGUAGE = LANGUAGES[LANGUAGE]
TYPE_REFERENCES = 'num'
print('TYPE_REFERENCES: ', TYPE_REFERENCES)
TYPE_REFERENCES = TYPES[TYPE_REFERENCES]
FILE_NAME = 'referencesTest.bib'
print('FILE_NAME: ', FILE_NAME)
'''
#production
#'''
args = argparser.parse_args()
LANGUAGE = args.language
print('LANGUAGE: ', LANGUAGE)
LANGUAGE = LANGUAGES[LANGUAGE]
TYPE_REFERENCES = args.type
print('TYPE_REFERENCES: ', TYPE_REFERENCES)
TYPE_REFERENCES = TYPES[TYPE_REFERENCES]
FILE_NAME = args.filename
print('FILE_NAME: ', FILE_NAME)
#'''
NAME_FILE_OUTPUT_REPORT_MD = FILE_NAME[:-4] +"_Report_" + label_date + ".md"
NAME_FILE_OUTPUT_REPORT_HTML = FILE_NAME[:-4] +"_Report_" + label_date + ".html"
NAME_FILE_OUTPUT_BIB = FILE_NAME[:-4] + "_" + label_date + ".bib"
file = open("results_temp.txt","w+",encoding="utf-8")
lineHeader1 = "# References Report: " + str(label_date) + '\r'
lineHeader2 = "## - Configurations: _Bib File:_ **"+ FILE_NAME +"** / _Language:_ **"+ LANGUAGE +"** / _Type References:_ **"+ TYPE_REFERENCES +"**\r\n"
file.write(lineHeader1)
file.write(lineHeader2)
# ===========================================================
# If repeated bibliograhpy entry or others erros
msg_erros = ''
stop = True
read_error_bib = False
error_command_line = False
file_found = False
#pass_language = False
count_repeated_entry = 0
count_duplicate_field = 0
rep_tag_ant = ''
dup_field_ant = ''
while stop == True:
try:
'''
assert LANGUAGE in ['english', 'portuguese']
pass_language = True
assert TYPE_REFERENCES in ['apa', 'num-alpha']
'''
if os.path.exists("refer_find_errors_generate_temp.bib") == False:
with os.scandir(path_bib_original) as entries:
for entry in entries:
if entry.name == FILE_NAME:
shutil.copyfile(os.path.join(path_bib_original, FILE_NAME), 'refer_find_errors_generate_temp.bib')
file_found = True
break
if file_found == True:
bib_data = parse_file('refer_find_errors_generate_temp.bib')
else:
error_command_line = True
msg_erros += '- ## The name of the .bib file set as a parameter on the command line was not found in the bib file folder. '
msg_erros += 'Check if it is actually in the folder (OriginalBIB) or if the file name was spelled correctly in the parameter.\r'
stop = False
except FileNotFoundError as identifier:
error_command_line = True
msg_erros += '- ## The name of the .bib file set as a parameter on the command line was not found in the bib file folder. '
msg_erros += 'Check if it is actually in the folder (OriginalBIB) or if the file name was spelled correctly in the parameter.\r'
stop = False
'''
except AssertionError as identifier:
error_command_line = True
if pass_language == True:
msg_erros += '- ## The value defined in the parameter of your reference type is not valid. '
msg_erros += 'Valid values: [`num`, `alpha` or `apa`].\r'
else:
msg_erros += '- ## The value defined in your reference language parameter is not valid. '
msg_erros += 'Valid values: [`en` or `pt`].\r'
stop = False
'''
except BibliographyDataError as identifier:
read_error_bib = True
msg_erros += '- ## ' + str(identifier) + '\r'
if 'repeated bibliograhpy entry' in str(identifier):
tag_rep = str(identifier).split(':', 1)
if rep_tag_ant == tag_rep[1].strip():
count_repeated_entry+=1
else:
count_repeated_entry=1
rep_tag_ant = tag_rep[1].strip()
#print(tag_rep_ant,'\r')
#input file
fin = open("refer_find_errors_generate_temp.bib", "rt",encoding="utf-8")
old = str(tag_rep[1]).strip()+str(",")
new = str(tag_rep[1]).strip()+str(count_repeated_entry)+str(",")
contents = fin.read().replace(old, new, count_repeated_entry)
fin.close()
fin = open("refer_find_errors_generate_temp.bib", "w+",encoding="utf-8")
fin.write(contents)
fin.close()
elif 'has a duplicate' in str(identifier):
arr_msg = str(identifier).split(' ')
tag = arr_msg[3]
dup_field = arr_msg[7]
if dup_field_ant == dup_field.strip():
count_duplicate_field+=1
else:
count_duplicate_field=1
dup_field_ant = dup_field.strip()
old = dup_field
new = dup_field+str(count_duplicate_field)
#input file
fin = open("refer_find_errors_generate_temp.bib", "rt",encoding="utf-8")
contents = fin.read()
contents_partition = contents.partition(tag)
res_replace = contents_partition[2].replace(old, new, count_duplicate_field)
contents = contents_partition[0]+contents_partition[1]+res_replace
contents
fin.close()
fin = open("refer_find_errors_generate_temp.bib", "w+",encoding="utf-8")
fin.write(contents)
fin.close()
except UndefinedMacro as identifier:
read_error_bib = True
erro = identifier.args[0]
msg_erros += '- ## ' + str(identifier) + '\r'
#input file
fin = open("refer_find_errors_generate_temp.bib", "rt",encoding="utf-8")
contents = fin.read().replace(erro, 'JAN')
fin.close()
fin = open("refer_find_errors_generate_temp.bib", "w+",encoding="utf-8")
fin.write(contents)
fin.close()
except TokenRequired as identifier:
read_error_bib = True
erro = identifier.args[0]
value_new = ''
msg_erros += '## - ' + str(identifier) + '\r'
#input file
fin = open("refer_find_errors_generate_temp.bib", "rt",encoding="utf-8")
contents = fin.read().replace(erro, value_new)
fin.close()
fin = open("refer_find_errors_generate_temp.bib", "w+",encoding="utf-8")
fin.write(contents)
fin.close()
stop = False
if os.path.exists("refer_find_errors_generate_temp.bib"):
os.remove("refer_find_errors_generate_temp.bib")
# ===========================================================
if read_error_bib == True:
line = '# Error reading your .bib file!\r\n'
line += '### Errors may be related:\r'
line += '- the label of repeated references. Repeated entries will be listed below. '
line += 'Check the labels (tags) used before continuing to run the report.\r'
line += '- information for month={WRONG}. This field must always be filled in English (even if its volume is in Portuguese), '
line += 'nor can it be empty. '
line += '(_Accepted formats: [Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec]_)\r\n'
line += '## Errors found:\r'
line_footer = '#### *Warning:* Correct repeated entries or month information in your original .bib file. '
line_footer += '#### Afterwards, run this script again to generate the error report. \r'
file.write(line)
file.write(msg_erros+'\r\n')
file.write(line_footer)
elif error_command_line == True:
line = '# Error trying to run your command line.!\r\n'
line += '## Errors found:\r'
line_footer = '#### Afterwards, run this script again to generate the error report. \r'
file.write(line)
file.write(msg_erros+'\r\n')
file.write(line_footer)
else:
MONTHS_ENG_VALID = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
MONTHS_PORT_VALID = ['Jan', 'Fev', 'Mar', 'Abr', 'Mai', 'Jun', 'Jul', 'Ago', 'Set', 'Out', 'Nov', 'Dez']
assert LANGUAGE in ['english', 'portuguese']
if LANGUAGE == 'english' and TYPE_REFERENCES == 'apa':
MONTHS = ['Jan,', 'Feb,', 'Mar,', 'Apr,', 'May,', 'Jun,', 'Jul,', 'Aug,', 'Sep,', 'Oct,', 'Nov,', 'Dec,']
elif LANGUAGE == 'english' and TYPE_REFERENCES == 'num-alpha':
MONTHS = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
elif LANGUAGE == 'portuguese' and TYPE_REFERENCES == 'apa':
MONTHS = ['Jan,', 'Fev,', 'Mar,', 'Abr,', 'Mai,', 'Jun,', 'Jul,', 'Ago,', 'Set,', 'Out,', 'Nov,', 'Dez,']
elif LANGUAGE == 'portuguese' and TYPE_REFERENCES == 'num-alpha':
MONTHS = ['Jan', 'Fev', 'Mar', 'Abr', 'Mai', 'Jun', 'Jul', 'Ago', 'Set', 'Out', 'Nov', 'Dez']
assert TYPE_REFERENCES in ['apa', 'num', 'alpha', 'num-alpha']
if TYPE_REFERENCES == 'num-alpha':
REQ = {
'book': {'author', 'title', 'publisher', 'year', 'numpages'},
'article': {'title', 'author', 'journal', 'volume', 'year', 'month', 'pages'},
'inproceedings': {'title', 'author', 'booktitle', 'pages', 'year'},
'conference': {'title', 'author', 'booktitle', 'pages', 'year'},
'proceedings': {'title', 'author', 'booktitle', 'pages', 'year'},
'mastersthesis': {'title', 'author', 'numpages', 'school', 'year'},
'phdthesis': {'title', 'author', 'numpages', 'school', 'year'},
'techreport': {'title', 'author', 'numpages', 'institution', 'year'},
'misc': {'title', 'author', 'url', 'urlaccessdate'},
'booklet': {'title', 'author', 'howpublished', 'address', 'year', 'numpages'},
'inbook': {'title', 'author', 'year', 'pages', 'publisher', 'chapter'},
'incollection': {'title', 'author', 'year', 'booktitle', 'publisher'}
}
elif TYPE_REFERENCES == 'apa':
REQ = {
'book': {'title', 'author', 'publisher', 'year'},
'article': {'title', 'author', 'year', 'journal', 'pages', 'volume'},
'inproceedings': {'title', 'author', 'booktitle', 'pages', 'organization', 'year'},
'conference': {'title', 'author', 'booktitle', 'pages', 'organization', 'year'},
'proceedings': {'title', 'author', 'booktitle', 'pages', 'year'},
'mastersthesis': {'title', 'author', 'year', 'school', 'address'},
'phdthesis': {'title', 'author', 'year', 'school', 'address'},
'techreport': {'title', 'author', 'institution', 'year', 'type'},
'misc': {'title', 'author', 'year', 'note', 'howpublished'},
'booklet': {'title', 'author', 'howpublished', 'year'},
'inbook': {'title', 'author', 'year', 'pages', 'publisher', 'chapter'},
'incollection': {'title', 'author', 'year', 'booktitle', 'publisher', 'volume', 'pages', 'edition'}
}
bib_data = parse_file(os.path.join(path_bib_original, FILE_NAME))
##
line1 = '| Error | Type |Tag | Title | Warning |' + '\r'
line2 = '|-:|:---:|:---:|:---:|:------:|' + '\r'
file.write(line1)
file.write(line2)
congratulations = False
count=0
tag_inv_global = False
for entry in bib_data.entries:
bib = bib_data.entries[entry]
#print('entry: ============ ', entry)
msg, tag_inv = check(bib, REQ, MONTHS, TYPE_REFERENCES)
if tag_inv == False:
msg_gen, bib = regenerate_bib(bib, REQ)
msg+=msg_gen
bib_data.entries[entry] = bib
else:
tag_inv_global = True
if len(msg) > 0:
count+=1
line = '| ' + str(count) +' |@'+ bib.type + '| {' + str(entry) + '} |' + str(bib.fields['title']) + ' | ' + msg + ' | ' + '\r'
file.write(line)
if count != 0:
lineFooter1 = "# _Total references:_ **" + str(len(bib_data.entries)) + "** / _References with errors:_ **" + str(count) +"**\r\n"
lineFooter2 = '## _Check the errors shown below_ \r'
lineFooter3 = '- After correcting them, run the script again to ensure no new inconsistencies (or not).\r\n'
file.write(lineFooter1)
file.write(lineFooter2)
file.write(lineFooter3)
if tag_inv_global == True:
line5 = '## **New .bib file was not generated!** _Invalid tags have been identified in your .bib_.\r'
line6 = '- In order to generate a .bib with the missing fields, first you need to correct these tags in your '
line7 = 'original .bib and only after running the script again.\r'
line8 = '- In the column _Warning_ you will enter which of the references is with: _**Type not implemented**_\r'
line9 = '- Valid tags: _@book, @article, @inproceedings, @proceedings, @mastersthesis, @phdthesis, '
line10 = '@techreport, @misc, @booklet, @inbook, @incollection_.'
file.write(line5)
file.write(line6)
file.write(line7)
file.write(line8)
file.write(line9)
file.write(line10)
else:
congratulations = True
lineCongrat1 = '# Congratulations! \r\n'
lineCongrat2 = '## - No errors were identified in the fields. However, it is still necessary to check the standardization of your references, which this script does not guarantee.'
file.write(lineCongrat1)
file.write(lineCongrat2)
if tag_inv_global == False and congratulations == False:
file_bib = open(os.path.join(path_bib, NAME_FILE_OUTPUT_BIB),"w+", encoding="utf-8")
file_bib.write(bib_data.to_string('bibtex'))
file_bib.close()
if LANGUAGE == 'portuguese':
fin = open(os.path.join(path_bib, NAME_FILE_OUTPUT_BIB), "rt+",encoding="utf-8")
contents = ''
for index, month in enumerate(MONTHS_ENG_VALID):
month_pt = MONTHS_PORT_VALID[index].upper()
contents += fin.read().replace(month, month_pt)
fin.close()
fin = open(os.path.join(path_bib, NAME_FILE_OUTPUT_BIB), "w+",encoding="utf-8")
fin.write(contents)
fin.close()
file.close()
shutil.copyfile("results_temp.txt", NAME_FILE_OUTPUT_REPORT_MD)
command = "grip "+NAME_FILE_OUTPUT_REPORT_MD+" --export "+NAME_FILE_OUTPUT_REPORT_HTML
os.system(command)
del_html = False
if os.path.exists(NAME_FILE_OUTPUT_REPORT_HTML):
file_html = open(NAME_FILE_OUTPUT_REPORT_HTML, "rt",encoding="utf-8")
contents = file_html.read()
if '500 Internal Server Error' in contents:
del_html = True
file_html.flush()
file_html.close()
else:
del_html = True
if del_html == True:
shutil.copyfile(NAME_FILE_OUTPUT_REPORT_MD, os.path.join(path_reports, NAME_FILE_OUTPUT_REPORT_MD))
else:
shutil.copyfile(NAME_FILE_OUTPUT_REPORT_HTML, os.path.join(path_reports, NAME_FILE_OUTPUT_REPORT_HTML))
if os.path.exists("results_temp.txt"):
os.remove("results_temp.txt")
if os.path.exists(NAME_FILE_OUTPUT_REPORT_MD):
os.remove(NAME_FILE_OUTPUT_REPORT_MD)
if os.path.exists(NAME_FILE_OUTPUT_REPORT_HTML):
os.remove(NAME_FILE_OUTPUT_REPORT_HTML)
def regenerate_bib(bib, REQ):
list_fields_keys = [field_key.lower() for field_key in bib.fields.keys() if bib.fields[field_key] != '']
list_fields_persons = [field_person.lower() for field_person in bib.persons.keys() if bib.persons[field_person] != '']
fields = set(list_fields_keys + list_fields_persons)
missing = REQ[bib.type].difference(fields)
msg = ''
if bib.type == 'book' or bib.type == 'inbook':
exist = 'publisher' in missing
if exist == False:
phrase = bib.fields['publisher']
msg_year, phrase = find_year(phrase, 'publisher')
msg += msg_year
publisher_capitalize, phrase_cap = check_parentheses_and_capitalize(phrase)
#capitalize
if len(phrase_cap) > 0:
bib.fields['publisher'] = phrase_cap
elif '&' in phrase:
phrase = treatAmpersand(phrase)
bib.fields['publisher'] = phrase
if publisher_capitalize == True:
msg += 'Field { publisher } is not capitalized; '
if bib.type == 'article':
exist = 'journal' in missing
if exist == False:
phrase = bib.fields['journal']
msg_year, phrase = find_year(phrase, 'journal')
msg += msg_year
journal_capitalize, phrase_cap = check_parentheses_and_capitalize(phrase)
#capitalize
if len(phrase_cap) > 0:
bib.fields['journal'] = phrase_cap
elif '&' in phrase:
phrase = treatAmpersand(phrase)
bib.fields['journal'] = phrase
if journal_capitalize == True:
msg += 'Field { journal } is not capitalized; '
if bib.type == 'inproceedings' or bib.type == 'proceedings' or bib.type == 'incollection' or bib.type == 'conference':
exist = 'booktitle' in missing
if exist == False:
phrase = bib.fields['booktitle']
msg_year, phrase = find_year(phrase, 'booktitle')
msg += msg_year
booktitle_capitalize, phrase_cap = check_parentheses_and_capitalize(phrase)
#capitalize
if len(phrase_cap) > 0:
bib.fields['booktitle'] = phrase_cap
elif '&' in phrase:
phrase = treatAmpersand(phrase)
bib.fields['booktitle'] = phrase
if booktitle_capitalize == True:
msg += 'Field { booktitle } is not capitalized; '
if bib.type == 'mastherthesis' or bib.type == 'phdthesis':
exist = 'school' in missing
if exist == False:
phrase = bib.fields['school']
msg_year, phrase = find_year(phrase, 'school')
msg += msg_year
school_capitalize, phrase_cap = check_parentheses_and_capitalize(phrase)
#capitalize
if len(phrase_cap) > 0:
bib.fields['school'] = phrase_cap
elif '&' in phrase:
phrase = treatAmpersand(phrase)
bib.fields['school'] = phrase
if school_capitalize == True:
msg += 'Field { school } is not capitalized; '
if bib.type == 'techreport':
exist = 'institution' in missing
if exist == False:
phrase = bib.fields['institution']
msg_year, phrase = find_year(phrase, 'institution')
msg += msg_year
institution_capitalize, phrase_cap = check_parentheses_and_capitalize(phrase)
#capitalize
if len(phrase_cap) > 0:
bib.fields['institution'] = phrase_cap
elif '&' in phrase:
phrase = treatAmpersand(phrase)
bib.fields['institution'] = phrase
if institution_capitalize == True:
msg += 'Field { institution } is not capitalized; '
if bib.type in REQ.keys():
for value in missing:
bib.fields[value] = 'MISSING'
return msg, bib
##
def check(bib, req, months, type_references):
list_fields_keys = [field_key.lower() for field_key in bib.fields.keys()]
list_fields_persons = [field_person.lower() for field_person in bib.persons.keys()]
fields = set(list_fields_keys + list_fields_persons)
msg = ''
tag_inv = False
if bib.type not in req.keys():
msg = 'Type not implemented: [@' + bib.type + '] remove or replace; '
tag_inv = True
else:
if type_references == 'apa':
if bib.type == 'article' and 'year' in fields:
year_month_check = check_article_year_month(bib.fields['year'], months, type_references)
if not year_month_check:
msg += 'Failed Month and Year: year={Mon, Year} check; '
if type_references == 'num-alpha':
if bib.type == 'article' and 'month' in fields:
month_check = check_article_year_month(bib.fields['month'], months, type_references)
if not month_check:
msg += 'Failed Month month={ Mon } check; '
missing = req[bib.type].difference(fields)
if len(missing) > 0:
msg += 'Missing: ' + str(missing) + "; "
return msg, tag_inv
def find_year(phrase, tag):
msg = ''
result = re.findall(re.compile('.*([1-3][0-9]{3})'), phrase)
if len(result) > 0:
msg = 'The {'+tag+'} field should not contain the year information: '+ str(result) +' remove; '
phrase = phrase.replace(str(result[0]), '')
return msg, phrase
def check_parentheses_and_capitalize(phrase):
language = detect(phrase)
#remove parenthesis
phrase = re.sub(r"\((.*?)\)", ' ', phrase)
words = phrase.split()
words_aux = []
uncapitalized = False
hif = False
word2 = ''
STOP_LIST = []
if language == 'en':
STOP_LIST = LIST_STOPWORDS_ENGLISH
elif language == 'pt':
STOP_LIST = LIST_STOPWORDS_PORTUGUESE
elif language == 'es':
STOP_LIST = LIST_STOPWORDS_SPANISH
elif language == 'de':
STOP_LIST = LIST_STOPWORDS_GERMAN
else:
STOP_LIST = LIST_STOPWORDS_ENGLISH
for word in words:
if word not in STOP_LIST:
if "-" in word and word != '-':
hif = True
words_hifen = word.split('-')
word = words_hifen[0]
word2 = words_hifen[1]
if word == 'e': # e-Business, e-Science, ...
words_aux.append(word+"-"+word2)
elif word in EXCEPTION_LIST:
if "&" in word:
words_aux.append("&")
else:
words_aux.append(word)
elif len(word) > 0:
if word.isdigit() == True:
words_aux.append(word)
else:
if word.isupper() == False:
if word.isupper() == False:
#if word != word.capitalize():
uncapitalized = True
if hif == True:
words_aux.append(word.capitalize()+"-"+word2)
else:
words_aux.append(word.capitalize())
else:
if hif == True:
words_aux.append(word+"-"+word2)
else:
words_aux.append(word)
else:
words_aux.append("{"+word+"}")
else:
words_aux.append(word)
if uncapitalized == True:
phrase_cap = ' '.join([word for word in words_aux])
else:
phrase_cap = ''
return uncapitalized, phrase_cap
def treatAmpersand(phrase):
phrase = re.sub(r"\((.*?)\)", ' ', phrase)
words = phrase.split()
words_aux = []
for word in words:
if "&" in word:
words_aux.append("&")
else:
words_aux.append(word)
new_phrase = ' '.join([word for word in words_aux])
return new_phrase
def check_article_year_month(fields, months, type_references):
fields = fields.split()
if type_references == "apa":
try:
month, year = fields[0][-4:], fields[1]
months.index(month)
int(year)
except:
return False
#elif type_references == "num-alpha":
# month, year = fields[0][-3:], fields[1]
return True
if __name__ == "__main__":
main()