-
Notifications
You must be signed in to change notification settings - Fork 2
/
lcc.py
837 lines (684 loc) · 34.5 KB
/
lcc.py
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
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
#!/usr/bin/env python3
import os, sys, re
from os import path
from flask import Flask, current_app
from flask import render_template, g, request, redirect, url_for, send_from_directory, session, flash
import requests
from flask_login import LoginManager, UserMixin, login_required, login_user, logout_user, current_user
from functools import wraps
from common_login import *
from itsdangerous import URLSafeTimedSerializer # for safe session cookies
from werkzeug.utils import secure_filename
import datetime
import mammoth # to parse the docx into html
import lxml.html # to manipulate the html
import nltk
from nltk import tokenize
from nltk.tokenize import word_tokenize
from nltk import pos_tag
from nltk.stem import WordNetLemmatizer
import delphin_call
from feedback import eng_feedback
import lcc_data
import common_sql as csql
from spellchecker import SpellChecker
from collections import defaultdict as dd
def inf_dd():
return dd(inf_dd)
###############################################################################
# Spelling checker (pip install pyspellchecker)
###############################################################################
spell = SpellChecker()
# To make sure some words are not flagged as misspelled
new_words = []
for c in " '":
for w in lcc_data.contractions | lcc_data.wordcase:
new_words = new_words + w.split(c)
spell.word_frequency.load_words(new_words)
###############################################################################
UPLOAD_FOLDER = 'lcc_uploads'
ALLOWED_EXTENSIONS = set(['docx'])
ROOT = path.dirname(path.realpath(__file__))
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
DRYRUN = False # if DRYRUN == True: do everything except error checking
###############################################################################
# This helps achieve better sentence segmentation
###############################################################################
extra_abbreviations = ['dr',
'vs',
'mr',
'n.o',
'mrs',
'pp',
'prof',
'no',
'eds',
'inc',
'i.e',
'e.g',
'ie',
'approx',
'eg',
'n.d',
'pte',
'et',
'etc',
'al',
'et. al',
'fig',
'mfg']
sentence_tokenizer = nltk.data.load('tokenizers/punkt/english.pickle')
sentence_tokenizer._params.abbrev_types.update(extra_abbreviations)
###############################################################################
def remove_nested_parens(input_str):
"""
Returns a copy of string with any parenthesized (..) [..] text removed.
Nested parentheses are handled. It also returns a Boolean asserting if
the parenthesis were well balanced (True) or not (False).
"""
result1 = ''
paren_level = 0
for ch in input_str:
if ch == '(':
paren_level += 1
elif (ch == ')') and paren_level:
paren_level -= 1
elif not paren_level:
result1 += ch
result2 = ''
paren_level2 = 0
for ch in result1:
if ch == '[':
paren_level2 += 1
elif (ch == ']') and paren_level2:
paren_level2 -= 1
elif not paren_level2:
result2 += ch
if (paren_level == 0) and (paren_level2 == 0):
balanced = True
else:
balanced = False
return (balanced, result2)
def remove_parenthetical_punct(input_str, rev=False):
if rev is False:
output_str = ''
paren_level = 0
for ch in input_str:
if ch == '(':
output_str += ch
paren_level += 1
elif (ch == ')') and paren_level:
output_str += ch
paren_level -= 1
elif (ch == '.') and paren_level:
output_str += '|||punct=period|||'
elif (ch == '!') and paren_level:
output_str += '|||punct=exclamationpoint|||'
elif (ch == '?') and paren_level:
output_str += '|||punct=questionmark|||'
else:
output_str += ch
else:
output_str = input_str
output_str = output_str.replace('|||punct=period|||', '.')
output_str = output_str.replace('|||punct=exclamationpoint|||', '!')
output_str = output_str.replace('|||punct=questionmark|||', '?')
return output_str
with app.app_context():
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS
def uploadFile(current_user):
format = "%Y_%b%d_%H:%M:%S"
now = datetime.datetime.now().strftime(format)
try:
file = request.files['file']
lic = request.form['license']
except:
file = None
lic = None
if file and lic and allowed_file(file.filename):
if lic == '1':
lic_txt = 'CC0'
else:
lic_txt = 'PRIVATE'
###################################################################
# While trying to simplify anonymization, we don't keep the file
# name. Instead, we keep a normalized username reference that can
# be anonymized easily.
###################################################################
f_ext = '.' + file.filename.rsplit('.', 1)[1] # .docx
filename = lic_txt + '_' + now + '_' + str(current_user) + f_ext
filename = secure_filename(filename)
file.save(os.path.join(ROOT, app.config['UPLOAD_FOLDER'],
filename))
file_uploaded = True
else:
filename = False
file_uploaded = False
return file_uploaded, filename
def docx2html(docname, db="lcc.db"):
"""
This function reads the uploaded file and converts it to HTML.
An artificial documentRoot is added to make it XML compatible.
Nothing is added to any database at this point.
An HTML string is returned.
"""
if db == "ntucleX.db":
f_path = os.path.join(ROOT,
'ntucle_uploads',
docname)
else:
f_path = os.path.join(ROOT,
app.config['UPLOAD_FOLDER'],
docname)
with open(f_path, 'rb') as docx_file:
result = mammoth.convert_to_html(docx_file)
docx_html = result.value
# replacing non-breaking with a space:
docx_html = docx_html.replace(u'\xa0', ' ')
docx_html = docx_html.replace("\'", "’")
docx_html = docx_html.replace('\t', "<tab/> ")
docx_html = "<documentRoot>" + docx_html + "</documentRoot>"
return(docx_html)
def parse_docx_html(html, filename, db="lcc.db"):
"""
This function gives some structure to the HTML, including <span> tags
for each sentence that will be checked and writes them to the database.
There is also a heavy selection of what sentences should be checked.
E.g. Sentences after a section named "References" will not be checked.
We have two corpus databases: lcc.db (for submissions) and ntucleX.db
which should only have data students have agreed to share. Because of
this, we now need to allow the selection of different databases.
"""
#######################################################################
# Insert DOCUMENT into db.
#######################################################################
docid = csql.fetch_max_doc_id(db) + 1
csql.insert_into_doc(docid, filename, db)
#######################################################################
# In order to allow multiple documents to be uploaded at the same time,
# sids are limited to 100,000 per document, and the sids are given in
# relation to the docid. #FIXME: this must be ensured elsewhere
#######################################################################
sid = docid * 100000
max_sid = sid + 99999
report = inf_dd()
REFERENCES = False
# There are some document processing tools that leave <br />
# which both make document parsing extremly slow (idk why)
# but also inflict sentence tokenization problems
# this replace("<br />", "</p><p>") is a quick fix, not sure
# if it's completely robust, as some element might not be
# paragraphs... [FIXME]
root = lxml.html.fromstring(html.replace("<br />", "</p><p>"))
for element in root:
# print(lxml.html.tostring(element)) # TEST
###################################################################
# Remove the first <tab></tab> element in the element.
# Many documents use <tab> to indent their paragraphs, and this
# is later preventing them from being read as paragraphs since
# we block elements with <tab> elements later (i.e. they are
# usually cost tables, etc.); This removes only the first <tab>
# in each paragraph, which enables a lot more text to be retrieved
# and checked.
###################################################################
for i, subelem in enumerate(element):
if i == 0 and str(subelem.tag) == "tab":
subelem.drop_tag()
# Remove text from within <sub> and <sup> tags
if (str(subelem.tag) == "sub") or (str(subelem.tag) == "sup"):
subelem.text = ""
# print(lxml.html.tostring(element)) # TEST
# The first pass was to fix some weirdness in the XML
# generating a new root from the fixes
root = lxml.html.fromstring(lxml.html.tostring(root))
for element in root:
EXCLUDE = False
element_type = element.tag # p, ol, li, table, etc.
element_text = element.text_content().strip()
# element_text also includes the text of every children
# print("\n\n") #TEST
# print(element_type) #TEST
# print(element.text_content()) #TEST
# for subElement in element.getchildren(): #TEST
# print(subElement.tag) #TEST
###################################################################
# Recuperate bolded paragraphs (likely section titles)
###################################################################
BOLD = False
if (element.text is None) and (len(element.getchildren()) == 1):
if (element[0].tag == "strong"):
# We assume the whole element is bold
BOLD = True
###################################################################
# Check for very specific keywords to exclude element.
# There should not be many of these!
###################################################################
for s in lcc_data.exclude_elems_containing:
if s in element_text:
EXCLUDE = True
###################################################################
# Check for sections (i.e. no punctuation, few words)
# Includes special case for 'References' in numbered sections
# All sentneces after 'References' are ignored.
###################################################################
if (element_text) and\
(element_text[-1] not in lcc_data.punctuation) and\
(len(element_text.split()) < 5):
EXCLUDE = True
# References
for w in lcc_data.refs_section:
if w in element_text.lower() and\
(not element_text[-1].isdigit()): # for TOCs
REFERENCES = True
elif (element_text.lower() in lcc_data.exclude_sents):
EXCLUDE = True
if element_text.lower() in lcc_data.refs_section:
# this is necessary for the cases where punctuation
# is used at the end of the section (e.g. ':')
REFERENCES = True
###################################################################
# Check for tabs (\t), i.e. "quasi-tables"
# - there is an exception for one tab at the beggining of a line
###################################################################
if (element_text):
for subElement in element.getchildren():
if subElement.tag == "tab":
EXCLUDE = True
###################################################################
# Check for special sent starts (e.g. from mandatory cover page)
###################################################################
if (element_text) and\
element_text.lower().startswith(
tuple(lcc_data.exclude_sents_swith)):
EXCLUDE = True
###################################################################
# Check for some titles and names
# - camel-case or ALLCAPS with no punctuation
###################################################################
if (element_text) and\
(element_text[-1] not in lcc_data.punctuation):
if element_text.title() == element_text:
EXCLUDE = True
if element_text.upper() == element_text:
EXCLUDE = True
###################################################################
# Check for figure/table legends (e.g. "Fig" with no punctuation)
###################################################################
if (element_text) and\
(element_text[-1] not in lcc_data.punctuation) and\
element_text.lower().startswith(
('fig.', 'figure', '(fig.', '(figure', 'fig:',
'image', 'img.',
'table', 'tab.')):
EXCLUDE = True
# FIXME, this is currently not catching, e.g., "Fig1:"
###################################################################
# Check if lines are just aesthetic ===== ----- ...... _____
###################################################################
line_separators = '.=-_'
if (element_text):
for c in line_separators:
if element_text.strip().strip(c) == '':
EXCLUDE = True
###################################################################
# Check if lines should be a table:
# X = 80; X - 120$; X : 1000USD
###################################################################
semi_table_separators = '=-:–$'
if (element_text) and\
(len(element_text.split()) < 15) and\
(element_text[-1] not in lcc_data.punctuation):
for c in semi_table_separators:
if c in element_text:
EXCLUDE = True
###################################################################
# Split each paragraph into sentences
###################################################################
###################################################################
# Some DOCX conversion leaves <br> where it should have been
# split into further paragraphs. This causes problems with sentence
# tokenization since "element_text" loses these <br>.
###################################################################
sents = []
if (element_type not in ['table', 'ol', 'ul']) and\
(REFERENCES is False) and\
(EXCLUDE is False):
# Before we tokenize sentences, we need to ensure punctuation
# inside parenthesis will not cause errors
if ('(' in element_text) and (')' in element_text):
clean_text = remove_parenthetical_punct(element_text)
else:
clean_text = element_text
for sent in tokenize.sent_tokenize(clean_text):
sent = remove_parenthetical_punct(sent, rev=True)
sents.append(sent)
if sents:
###############################################################
# We want to clean the elem (from text and sub-elements) as #
# we will reconstruct it from the sentences. #
# This could be improved, to maintain some of the styles, but #
# it currently optimises for practicallity. #
###############################################################
element.text = None #
for subelement in element: #
element.remove(subelement) #
###############################################################
for sent in sents:
sent = sent.strip()
sid += 1
if BOLD:
meta = lxml.html.etree.SubElement(element, "strong")
else:
meta = element
if sent:
sentSpan = lxml.html.etree.SubElement(meta, "span")
sentSpan.set('id', 'sid{}'.format(sid))
sentSpan.set('class', 'sid{} sent'.format(sid))
# sentSpan.set('class',
# 'sid{} sent doccheck'.format(sid)) #TEST
# sentSpan.set('data-toggle', 'tooltip')
# sentSpan.set('data-placement', 'bottom')
# sentSpan.set('data-html', 'true')
# sentSpan.set('title', """<em>test.</em>""")
sentSpan.text = sent + ' ' # Need to leave a space
#TEST Sentence Boundaries
# sentSpan = lxml.html.etree.SubElement(meta, "br") #TEST
# sentSpan = lxml.html.etree.SubElement(meta, "br") #TEST
else:
sentSpan = lxml.html.etree.SubElement(meta, "br")
###########################################################
# Preprocess sentences before writing them to the db.
# We don't want to include certain kinds of sentences, that
# are not worth checking; we also want to remore all
# parenthetic remarks (because they are usually references)
# and just break ERG.
###########################################################
if ('(' in sent) or ('[' in sent):
#######################################################
# FIXME: There is a known problem in cases where the
# IEEE references are used as part of the text, e.g.:
# As can be seen in [3], this is a problem.
#######################################################
(balanced, noparen_sent) = remove_nested_parens(sent)
noparen_sent = noparen_sent.strip()
if (',,' in noparen_sent) and (',,' not in sent):
while (',,' in noparen_sent):
noparen_sent = noparen_sent.replace(',,', ',')
if (', ,' in noparen_sent) and (', ,' not in sent):
while (', ,' in noparen_sent):
noparen_sent = noparen_sent.replace(', ,', ',')
if (',.' in noparen_sent) and (',.' not in sent):
while (',.' in noparen_sent):
noparen_sent = noparen_sent.replace(',.', '.')
if (' .' in noparen_sent) and (' .' not in sent):
while (' .' in noparen_sent):
noparen_sent = noparen_sent.replace(' .', '.')
if (' ,' in noparen_sent) and (' ,' not in sent):
while (' ,' in noparen_sent):
noparen_sent = noparen_sent.replace(' ,', ',')
if (' ' in noparen_sent) and (' ' not in sent):
while (' ' in noparen_sent):
noparen_sent = noparen_sent.replace(' ', ' ')
noparen_sent = noparen_sent.strip()
if noparen_sent == '.':
noparen_sent = ''
if balanced:
sent = noparen_sent
#FIXME ELSE should be registered as an error
############################################################
# FIXME: Quotes "ABC DEF" could be handled in a was similar
# to parenthesis. They could be changed into something
# without spaces.
############################################################
############################################################
# This strips the beginning of sentences that are
# being enumerated. This is often not well handled by ERG
############################################################
enumeration_starters = [
'1.','2.','3.','4.','5.','6.','7.','8.',
'9.','10.','11.','12.','13.','14.','15.',
'16.','17.','18.','19.','20.',
'1)','2)','3)','4)','5)','6)','7)','8)',
'9)','10)','11)','12)','13)','14)','15)',
'16)','17)','18)','19)','20)',
'A.','B.','C.','D.','E.','F.','G.','H.',
'I.','J.','K.','M.','N.','O.','P.','Q.',
'R.','S.','T.','U.','V.','W.','X.','Y.','Z.',
'a)','b)','c)','d)','e)','f)','g)','h)',
'i)','j)','k)','m)','n)', 'o)','p)','q)',
'r)','s)','t)','u)','v)','w)','x)','y)','z)'
]
if (sent[:2] in enumeration_starters):
sent = sent[2:].lstrip()
############################################################
# If, after all the exceptions are implemented, the value
# of sent is not empty, then insert it into the db.
############################################################
if sent and (sid < max_sid):
pid = 0 # we are ignoring paragraph IDs
# print(sid, docid, pid, sent) #TEST
csql.insert_into_sent(sid, docid, pid, sent, db)
# INSERT INTO WORD TABLE
word_list = pos_lemma(sent2words(sent))
# e.g. [('He', 'PRP', 'he'), ('runs', 'VB', 'run')]
for w in word_list:
wid = csql.fetch_max_wid(sid, db) + 1
(surface, pos, lemma) = w
csql.insert_into_word(sid, wid, surface, pos,
lemma, db)
return(docid, root, report)
def sent2words(sent):
"""Given a sentence string, get a list of (word,pos) elements."""
return pos_tag(word_tokenize(sent))
def pos_converter(lemma, pos):
"""
This converts POS tags into wordnet tags, to be used in lemmatization
step. FIXME I'm not sure why we are using the this lemmatizer.
"""
if pos in ['CD', 'NN', 'NNS', 'NNP', 'NNPS', 'WP', 'PRP']:
# include proper nouns and pronouns
# fixme flag for proper nouns
return 'n'
elif pos.startswith('V'):
return('v')
elif pos.startswith('J') or pos in ['WDT', 'WP$', 'PRP$', 'PDT', 'PRP'] or \
(pos=='DT' and not lemma in ['a', 'an', 'the']): ### most determiners
return('a')
elif pos.startswith('RB') or pos == 'WRB':
return('r')
else:
return 'x'
def pos_lemma(tagged_sent):
# Lemmatize = lru_cache(maxsize=5000)(wnl.lemmatize)
wnl = WordNetLemmatizer()
lemmatize = wnl.lemmatize
record_list = []
wid = 0
for word, pos in tagged_sent:
lemma = word
if wid == 0:
lemma = lemma.lower()
wid = 1
wn_pos = pos_converter(lemma, pos)
if wn_pos in "avnr":
lemma = lemmatize(lemma, wn_pos)
record_list.append((word, pos, lemma))
return record_list
def check_docx_html(docid, htmlRoot, report, feedback_set, db="lcc.db"):
"""
This function receives the docID, HTML element, and Report dictionary
and checks each sentences with the ERG and other NLP checks.
It then adds these errors and feedback messages directly on the HTML.
The sentences stored in the DB have been edited (e.g. removed
parenthetic comments, references, etc.) but the sids will match the
HTML span IDs.
"""
#######################################################################
# Fetch sents and words from the database
#######################################################################
sents = csql.fetch_sents_by_docid(docid, db)
# There is a small change a document does not have any text that was
# able to be converted to sentences. This could be caught earlier.
# [FIXME]
if sents.keys():
sid_min = min(sents.keys())
sid_max = max(sents.keys())
words = csql.fetch_words_by_sid(sid_min, sid_max, db)
for sid in sents:
sent_text = sents[sid]
sent_words = words[sid]
if not DRYRUN:
(app_errors, non_app_errors) = full_check_sent(sent_text,
sent_words,
feedback_set)
else:
app_errors = set()
non_app_errors = set()
# print(sents[sid]) #TEST
# print("lcc_errors") #TEST
# print(app_errors) #TEST
# print("non_lcc_errors") #TEST
# print(non_app_errors) #TEST
# print('\n\n') #TEST
###################################################################
# ADD ERRORS TO HTML
###################################################################
sentSpan = htmlRoot.get_element_by_id('sid'+str(sid))
if app_errors:
feedback_msg = ''
feedback_conf = 0
for i, (label, loc) in enumerate(app_errors):
msg = eng_feedback[label][feedback_set][0]
conf = eng_feedback[label][feedback_set][1]
if conf > feedback_conf:
feedback_conf = conf
if i > 0:
feedback_msg += '<br><br>'
feedback_msg += msg.format(loc)
if feedback_conf > 0.51:
sentSpan.set('class', 'sid{} sent seriouserror'.format(sid))
# sentSpan.set('class', 'seriouserror'.format(sid))
else:
sentSpan.set('class', 'sid{} sent milderror'.format(sid))
# sentSpan.set('class', 'milderror'.format(sid))
sentSpan.set('data-toggle', 'tooltip')
sentSpan.set('data-placement', 'bottom')
sentSpan.set('data-trigger', 'click')
sentSpan.set('data-html', 'true')
sentSpan.set('container', 'validation_div')
sentSpan.set('title', feedback_msg)
###################################################################
# WRITE ERRORS TO CORPUS DB
###################################################################
all_errors = app_errors | non_app_errors
for i, (label, loc) in enumerate(all_errors):
csql.insert_into_error(sid, i, label, loc, db)
return(htmlRoot, report)
def NLP_checks_sent(sent, words):
"""
Given a list of sentences, it checks each of them for multiple
problems.
"""
errors = [] # take the form (ErrorLabel, StringSurroundingError)
#######################################################################
# Check Sentence Length
#######################################################################
seriousthreshold = 50
mildthreshold = 40
sentlen = len(list(words.keys()))
if sentlen >= seriousthreshold:
errors.append(("VeryLongSentence", ''))
elif sentlen >= mildthreshold:
errors.append(("LongSentence", ''))
#######################################################################
# Check Contractions
#######################################################################
for c in lcc_data.contractions:
if re.search(r'\b{}\b'.format(c), sent, re.IGNORECASE):
errors.append(("Contraction", c))
#######################################################################
# Check Wordcase
#######################################################################
for exp in lcc_data.wordcase:
# we were getting lots of url matches with word boundary;
# check for space before and beginning of sentence
if re.search(r' {}\b'.format(exp), sent, re.IGNORECASE) and\
(re.search(r'\b{}\b'.format(exp), sent, re.IGNORECASE).group() != exp):
errors.append(("WordCase", exp))
if re.search(r'${}\b'.format(exp), sent, re.IGNORECASE) and\
(re.search(r'\b{}\b'.format(exp), sent, re.IGNORECASE).group() != exp):
errors.append(("WordCase", exp))
# FIXME, Ntu at the beginning of a sentence is not being picked up
#######################################################################
# Check Phrase Choice
#######################################################################
for exp in lcc_data.wordchoice:
if re.search(r'\b{}\b'.format(exp), sent, re.IGNORECASE):
errors.append(("WordChoice", exp))
if re.search(r'${}\b'.format(exp), sent, re.IGNORECASE):
errors.append(("WordChoice", exp))
#######################################################################
# Check Spelling
#######################################################################
for c in lcc_data.punctuation:
sent = sent.replace(c, " ")
for word in sent.split():
# ignore uppercased words or numerical (e.g. 130kg)
if (not word[0].isupper()) and (not word[0].isnumeric()):
misspelled = spell.unknown([word])
if misspelled:
suggestion = spell.correction(word)
errors.append(("Spelling", word+'|'+suggestion))
#######################################################################
# WORD LEVEL CHECKS
#######################################################################
for wid in words.keys():
lemma = words[wid][2].lower()
word_truecase = words[wid][0]
word = words[wid][0].lower()
###################################################################
# Check Repeated Words
###################################################################
if (wid+1 in words) and (words[wid+1][0].lower() == word):
exp = word_truecase + ' ' + word_truecase
errors.append(("RepeatedWord", exp))
###################################################################
# Check Word Style (e.g. Formal, Pronouns, etc.)
###################################################################
for error_label in lcc_data.wordcheck:
if lemma in lcc_data.wordcheck[error_label]:
errors.append((error_label, word_truecase))
return errors
def full_check_sent(sent, words, feedback_set):
"""
Feedback set refers to which set of errors and error messages are
being taken by the app. 'lcc' was the original error set and
feedback messages. But 'callig' is another possible value;
We're currently working on 'lcc2';
"""
nlp_errors = NLP_checks_sent(sent, words)
LongSentenceSkip = False
for e in nlp_errors:
error_label = e[0]
string_location = e[1]
if error_label in ["LongSentence", "VeryLongSentence"]:
LongSentenceSkip = True
if not LongSentenceSkip: # Don't parse long sentences
erg_errors = delphin_call.check_sents([sent])[0][1]
else:
erg_errors= []
app_errors = set()
non_app_errors = set()
for e in nlp_errors + erg_errors:
label = e[0]
if label in eng_feedback.keys():
if feedback_set in eng_feedback[label].keys():
app_errors.add(e)
else:
non_app_errors.add(e)
else:
non_app_errors.add(e)
return (app_errors, non_app_errors)