-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathcsv_interface.py
1249 lines (1037 loc) · 52.4 KB
/
csv_interface.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
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from signbank.dictionary.models import *
from tagging.models import Tag, TaggedItem
from signbank.dictionary.forms import *
from signbank.dictionary.consistency_senses import check_consistency_senses
from django.utils.translation import override, gettext_lazy as _, activate
from signbank.settings.server_specific import LANGUAGES, LEFT_DOUBLE_QUOTE_PATTERNS, RIGHT_DOUBLE_QUOTE_PATTERNS
from signbank.dictionary.update_senses_mapping import add_sense_to_revision_history
def add_sentence_to_revision_history(request, gloss, old_value, new_value):
# add update sentence to revision history, indicated by both old and new values
sentence_label = 'Sentence'
revision = GlossRevision(old_value=old_value,
new_value=new_value,
field_name=sentence_label,
gloss=gloss,
user=request.user,
time=datetime.now(tz=get_current_timezone()))
revision.save()
def create_empty_sense(gloss, order, erase=False):
# make a new sense and translations for it
translation_languages = gloss.lemma.dataset.translation_languages.all()
sense_translations = dict()
existing_senses = GlossSense.objects.filter(gloss=gloss, order=order)
if existing_senses.count() > 1:
print('create_empty_sense: multiple senses already exist: ', gloss, str(gloss.id), str(order), existing_senses)
raise MultipleObjectsReturned
if existing_senses:
glosssense = existing_senses.first()
sense_for_gloss = glosssense.sense
for dataset_language in translation_languages:
already_existing_sensetranslations = sense_for_gloss.senseTranslations.filter(language=dataset_language)
if already_existing_sensetranslations.count() > 1:
print('create_empty_sense: multiple sense translations exist for language: ', gloss, str(
gloss.id), str(order), glosssense, dataset_language, sense_for_gloss)
raise MultipleObjectsReturned
if already_existing_sensetranslations:
existing_sensetranslation = already_existing_sensetranslations.first()
if erase:
# force empty
for trans in existing_sensetranslation.translations.all():
existing_sensetranslation.translations.remove(trans)
trans.delete()
sense_translations[dataset_language] = existing_sensetranslation
continue
glosssenselanguage = SenseTranslation(language=dataset_language)
glosssenselanguage.save()
sense_for_gloss.senseTranslations.add(glosssenselanguage)
sense_translations[dataset_language] = glosssenselanguage
continue
return sense_for_gloss, sense_translations
sense_for_gloss = Sense()
sense_for_gloss.save()
glosssense = GlossSense(gloss=gloss, sense=sense_for_gloss, order=order)
glosssense.save()
for dataset_language in translation_languages:
glosssenselanguage = SenseTranslation(language=dataset_language)
glosssenselanguage.save()
sense_for_gloss.senseTranslations.add(glosssenselanguage)
sense_translations[dataset_language] = glosssenselanguage
return sense_for_gloss, sense_translations
def sense_examplesentences_for_language(gloss, language):
# by the time this method is called, the consistency check has already been done on the Senses
glosssenses = GlossSense.objects.filter(gloss=gloss).order_by('order')
if not glosssenses:
return ""
gloss_senses = dict()
for gs in glosssenses:
order = gs.order
sense = gs.sense
if order in gloss_senses.keys():
if settings.DEBUG_CSV:
# if something is messed up with duplicate senses with the same number, just ignore
print('ERROR: sense_examplesentences_for_language duplicate order: ', order)
print(gloss, str(gloss.id), order, sense)
continue
gloss_senses[order] = sense
activate(LANGUAGES[0][0])
sentences_display_list = []
for order in gloss_senses.keys():
sense = gloss_senses[order]
example_sentences = sense.exampleSentences.all()
list_of_sentences = []
for examplesentence in example_sentences:
examplesentence_translations = examplesentence.examplesentencetranslation_set.filter(language=language)
for sentence in examplesentence_translations:
sentence_type_display = examplesentence.sentenceType.name if examplesentence.sentenceType else '-'
sentence_tuple = (str(examplesentence.id), sentence_type_display, str(examplesentence.negative), sentence.text)
list_of_sentences.append(sentence_tuple)
if not list_of_sentences:
continue
sentences_display = []
for (sid, stype, negative, text) in list_of_sentences:
# does not use a comprehension because of possible nested parentheses in text fields
tuple_reordered = '(' + str(order) + ', ' + sid + ', ' + stype + ', ' + negative + ', "' + text + '")'
sentences_display.append(tuple_reordered)
sorted_sentences_display = ' | '.join(sentences_display)
sentences_display_list.append(sorted_sentences_display)
if not sentences_display_list:
return ""
sentences_display = ' | '.join(sentences_display_list)
return sentences_display
def map_values_to_sentence_type(values, include_sentences=True):
map_errors = False
activate(LANGUAGES[0][0])
sentencetype_role_choices = [st.name for st in FieldChoice.objects.filter(field__iexact='SentenceType',
machine_value__gt=1)]
import re
pattern_mapped_sorted_note_names = []
for note_name in sentencetype_role_choices:
escaped_note_name = re.sub(r'([()])', r'\\\1', note_name)
pattern_mapped_sorted_note_names.append(escaped_note_name)
sentence_types = '|'.join(pattern_mapped_sorted_note_names)
if sentence_types:
pattern_sentence_types = '(-|N/A|' + sentence_types + ')'
else:
pattern_sentence_types = '(-|N/A)'
mapped_values = values
if include_sentences:
regex_string = (r'\s?\(([1-9]), %s, (True|False), %s([^\"]+)%s\)\s?'
% (pattern_sentence_types, LEFT_DOUBLE_QUOTE_PATTERNS, RIGHT_DOUBLE_QUOTE_PATTERNS))
else:
regex_string = r'\s?\(([1-9]), %s, (True|False)\)\s?' % pattern_sentence_types
find_all = re.findall(regex_string, mapped_values)
if not find_all:
map_errors = True
return find_all, map_errors
def get_sense_numbers(gloss):
# by the time this method is called, the consistency check has already been done on the Senses
glosssenses = GlossSense.objects.filter(gloss=gloss).order_by('order')
if not glosssenses:
return []
gloss_senses = dict()
for gs in glosssenses:
order = gs.order
sense = gs.sense
if order in gloss_senses.keys():
if settings.DEBUG_CSV:
# if something is messed up with duplicate senses with the same number, just ignore
print('ERROR: get_sense_numbers duplicate order: ', str(gloss.id), str(order))
continue
gloss_senses[order] = sense
sense_numbers = [str(order) for order in gloss_senses.keys()]
return sense_numbers
def get_senses_to_sentences(gloss):
# by the time this method is called, the consistency check has already been done on the Senses
glosssenses = GlossSense.objects.filter(gloss=gloss).order_by('order')
if not glosssenses:
return []
gloss_senses = dict()
gloss_senses_to_sentences_dict = dict()
for gs in glosssenses:
order = gs.order
sense = gs.sense
if order in gloss_senses.keys():
if settings.DEBUG_CSV:
# if something is messed up with duplicate senses with the same number, just ignore
print('ERROR: get_sense_numbers duplicate order: ', str(gloss.id), str(order))
continue
gloss_senses[order] = sense
sense_sentences = sense.exampleSentences.all()
gloss_senses_to_sentences_dict[str(order)] = [str(sentence.id) for sentence in sense_sentences]
return gloss_senses_to_sentences_dict
def trim_columns_in_row(row):
"""If the user copies the initial header from the CSV template,
it is possible that the spreadsheet program added a space after the column text,
before the delimiter, or double quotes around the column text itself.
Example 1: Signbank ID ,Dataset
Example 2: "Signbank ID","Dataset"
This function removes those.
"""
trimmed_row = []
for col in row:
col_without_white_space = col.strip()
col_without_double_quotes = col_without_white_space.strip('"')
trimmed_row.append(col_without_double_quotes)
return trimmed_row
def parse_sentence_row(row_nr, sentence_dict):
errors = []
sentence_fields = '(' + sentence_dict['order'] + ', ' + sentence_dict['sentence_type'] + ', ' + sentence_dict['negative'] + ')'
find_all, map_errors = map_values_to_sentence_type(sentence_fields, include_sentences=False)
if map_errors:
errors += ['Row '+row_nr + ': Error parsing sentence columns Sense Number, Sentence Type, Negative: '+sentence_fields]
gloss_pk = sentence_dict['gloss_pk']
try:
dataset = Dataset.objects.get(acronym=sentence_dict['dataset'])
except ObjectDoesNotExist:
dataset = None
errors += ['Row '+row_nr + ': Dataset '+sentence_dict['dataset']+' does not exist']
try:
gloss = Gloss.objects.get(pk=int(gloss_pk))
except (ObjectDoesNotExist, ValueError, MultipleObjectsReturned):
gloss = None
errors += ['Row '+row_nr + ': Gloss ID '+gloss_pk+' does not exist.']
if gloss and dataset and gloss.lemma and gloss.lemma.dataset != dataset:
errors += ['Row '+row_nr + ': Gloss '+gloss_pk+' is not in dataset '+sentence_dict['dataset']+'.']
if gloss:
gloss_senses = get_sense_numbers(gloss)
if sentence_dict['order'] not in gloss_senses:
errors += ['Row '+row_nr + ': Gloss '+gloss_pk+' does not have a Sense Number '+sentence_dict['order']+'.']
return errors
def update_sentences_parse(sense_numbers, sense_numbers_to_sentences, new_sentences_string):
"""CSV Import Update check the parsing of the senses field"""
if not new_sentences_string:
# do nothing
return True
new_sentences = [k for k in new_sentences_string.split(' | ')]
new_sentence_tuples = []
for sentence_tuple in new_sentences:
find_all, map_errors = map_values_to_sentence_type(sentence_tuple, include_sentences=True)
if map_errors or not find_all:
# examine errors
continue
new_sentence_tuples.append(find_all[0])
if settings.DEBUG_CSV:
print('Parsed sentence tuples: ', new_sentence_tuples)
sentence_ids = []
for order, sentence_id, sentence_type, negative, sentence_text in new_sentence_tuples:
if order not in sense_numbers:
return False
if order not in sense_numbers_to_sentences.keys():
return False
if sentence_id not in sense_numbers_to_sentences[order]:
return False
sentence_ids.append(sentence_id)
if len(sentence_ids) != len(list(set(sentence_ids))):
# updates to same sentence in two different tuples
return False
return True
def sentence_tuple_list_to_string(sentence_tuple_string):
tuple_list_of_strings = []
if not sentence_tuple_string:
return tuple_list_of_strings
sentences = [k for k in sentence_tuple_string.split(' | ')]
for sentence_tuple in sentences:
find_all, map_errors = map_values_to_sentence_type(sentence_tuple, include_sentences=True)
if map_errors or not find_all:
# skip any non-parsing tuples, this was already checked so should not happen
continue
tuple_list_of_strings.append(find_all[0])
return tuple_list_of_strings
def csv_sentence_tuples_list_compare(gloss_id, sentence_string_old, sentence_string_new, errors_found):
# convert input to list of tuples (order, sentence_id, sentence_type, negative, sentence_text)
sentence_tuples_old = sentence_tuple_list_to_string(sentence_string_old)
sentence_tuples_new = sentence_tuple_list_to_string(sentence_string_new)
different_org = []
different_new = []
errors = errors_found
original_sentences_lookup = {sid: (so, styp, sn, stxt)
for (so, sid, styp, sn, stxt) in sentence_tuples_old}
for (order, sentence_id, sentence_type, negative, sentence_text) in sentence_tuples_new:
if (order, sentence_type, negative, sentence_text) != original_sentences_lookup[sentence_id]:
(sord, styp, sneg, stxt) = original_sentences_lookup[sentence_id]
if sord != order:
errors += ['ERROR Gloss ' + gloss_id + ': The Sense Number cannot be modified in CSV Update.']
if styp != sentence_type:
errors += ['ERROR Gloss ' + gloss_id + ': The Sentence Type cannot be modified in CSV Update.']
if sneg != negative:
errors += ['ERROR Gloss ' + gloss_id + ': The Sentence Negative cannot be modified in CSV Update.']
if errors:
continue
tuple_string_new = '(' + order + ', ' + sentence_id + ', ' + sentence_type \
+ ', ' + negative + ', "' + sentence_text + '")'
different_new.append(tuple_string_new)
tuple_string_org = '(' + sord + ', ' + sentence_id + ', ' + styp \
+ ', ' + sneg + ', "' + stxt + '")'
different_org.append(tuple_string_org)
difference_new = ' | '.join(different_new)
difference_org = ' | '.join(different_org)
return difference_org, difference_new, errors
def csv_update_sentences(request, gloss, language, new_sentences_string, update=False):
"""CSV Import Update the senses field"""
# this function assumes the new_senses_string is correctly parsed
# the function update_senses_parse tests this
# the sense numbers in the new_senses_string are unique numbers between 1 and 9
if settings.DEBUG_CSV:
print('call to csv_update_sentences: ', gloss, str(gloss.id), language, new_sentences_string)
if not new_sentences_string:
return
new_sentences = [k for k in new_sentences_string.split(' | ')]
glosssenses = GlossSense.objects.filter(gloss=gloss).order_by('order')
if not glosssenses:
return
gloss_senses = dict()
for gs in glosssenses:
order = gs.order
sense = gs.sense
if order in gloss_senses.keys():
if settings.DEBUG_CSV or settings.DEBUG_SENSES:
print('ERROR: csv_update_sentences: duplicate order: ', order)
print('ERROR: csv_update_sentences: ', gloss, str(gloss.id), order, sense)
gloss_senses[order] = sense
current_sentences_string = sense_examplesentences_for_language(gloss, language)
if settings.DEBUG_CSV:
print('Existing sentences: ', current_sentences_string)
new_sentence_tuples = []
for sentence_tuple in new_sentences:
find_all, map_errors = map_values_to_sentence_type(sentence_tuple, include_sentences=True)
if map_errors or not find_all:
# examine errors
if settings.DEBUG_CSV:
print('ERROR: Parsing error sentence tuple: ', sentence_tuple)
continue
new_sentence_tuples.append(find_all[0])
activate(LANGUAGES[0][0])
sentencetype_roles_to_type = {st.name: st
for st in FieldChoice.objects.filter(field__iexact='SentenceType')}
new_sentences_list = []
for order, sentence_id, sentence_type, negative, sentence_text in new_sentence_tuples:
new_sentence_dict = dict()
new_sentence_dict['order'] = int(order)
new_sentence_dict['sentence_id'] = int(sentence_id)
new_sentence_dict['sentence_type'] = sentencetype_roles_to_type[sentence_type]
new_sentence_dict['negative'] = negative == 'True'
new_sentence_dict['sentence_text'] = sentence_text
new_sentences_list.append(new_sentence_dict)
if settings.DEBUG_CSV:
print('Sentences to update: ', new_sentences_list)
if not update:
if settings.DEBUG_CSV:
print('Sentences to update: update set to False')
return
for sentence_dict in new_sentences_list:
# do not allow to change the sense number since this could cause inconsistencies
# sense = gloss_senses[sentence_dict['order']]
sentence_id = sentence_dict['sentence_id']
try:
examplesentence = ExampleSentence.objects.get(id=sentence_id)
old_example_sentence = str(examplesentence)
except (ObjectDoesNotExist, MultipleObjectsReturned):
continue
# do not change these since this could cause problems with language columns
# examplesentence.negative = sentence_dict['negative']
# examplesentence.sentenceType = sentence_dict['sentence_type']
# examplesentence.save()
try:
sentence_translation = ExampleSentenceTranslation.objects.get(language=language,
examplesentence=examplesentence)
except ObjectDoesNotExist:
sentence_translation = ExampleSentenceTranslation(language=language,
examplesentence=examplesentence)
sentence_translation.text = sentence_dict['sentence_text']
sentence_translation.save()
new_example_sentence = str(examplesentence)
add_sentence_to_revision_history(request, gloss, old_example_sentence, new_example_sentence)
def csv_create_sentence(request, gloss, dataset_languages, sentence_to_create, create=False):
"""CSV Import Update the senses field"""
if settings.DEBUG_CSV:
print('call to csv_create_sentence: ', gloss, str(gloss.id), sentence_to_create)
glosssenses = GlossSense.objects.filter(gloss=gloss).order_by('order')
if not glosssenses:
return ""
gloss_senses = dict()
for gs in glosssenses:
order = gs.order
sense = gs.sense
if order in gloss_senses.keys():
print('ERROR: csv_update_sentences: duplicate order: ', order)
print(gloss, str(gloss.id), order, sense)
gloss_senses[order] = sense
activate(LANGUAGES[0][0])
sentencetype_roles_to_type = {st.name: st
for st in FieldChoice.objects.filter(field__iexact='SentenceType')}
new_sentence_dict = dict()
new_sentence_dict['order'] = int(sentence_to_create['order'])
new_sentence_dict['sentence_type'] = sentencetype_roles_to_type[sentence_to_create['sentence_type']]
new_sentence_dict['negative'] = sentence_to_create['negative'] == 'True'
if not create:
if settings.DEBUG_CSV:
print('New sentences to create: create set to False')
return
sense = gloss_senses[new_sentence_dict['order']]
examplesentence = ExampleSentence(negative=new_sentence_dict['negative'],
sentenceType=new_sentence_dict['sentence_type'])
examplesentence.save()
sense.exampleSentences.add(examplesentence, through_defaults={'order':sense.exampleSentences.count()+1})
for language in dataset_languages:
sentence_text = sentence_to_create['sentence_text_'+language.language_code_2char]
sentence_translation = ExampleSentenceTranslation(language=language,
examplesentence=examplesentence,
text=sentence_text)
sentence_translation.save()
new_example_sentence = str(examplesentence)
add_sentence_to_revision_history(request, gloss, "", new_example_sentence)
def sense_translations_for_language(gloss, language):
# This finds the sense translations for one language
# It is used for export of CSV
# It is used again for import CSV update
# The exact same function is used in order to identify whether a cell has been modified
# The code is flattened out, avoiding usage of 'join' on empty lists
# The 'join' on empty lists causes problems with spaces not matching
# The SenseTranslation get_translations method causes problems with spaces not matching
check_consistency_senses(gloss, delete_empty=True)
glosssenses = GlossSense.objects.filter(gloss=gloss).order_by('order')
if not glosssenses:
return ""
gloss_senses = dict()
for gs in glosssenses:
order = gs.order
sense = gs.sense
if order in gloss_senses.keys():
print('ERROR: duplicate order: ', order)
print(gloss, str(gloss.id), order, sense)
gloss_senses[order] = sense
translations_per_language = []
for order, sense in gloss_senses.items():
sensetranslations = sense.senseTranslations.filter(language=language)
if not sensetranslations.count():
if settings.DEBUG_CSV:
print('No sensetranslation object for ', gloss, ' ( ', str(gloss.id), ') ', language)
continue
elif sensetranslations.count() > 1:
if settings.DEBUG_CSV:
print('Multiple sensetranslation objects for ', gloss, ' ( ', str(gloss.id), ') ', sensetranslations)
sensetranslation = sensetranslations.first()
keywords_list = []
translations = sensetranslation.translations.all().order_by('index')
for translation in translations:
keywords_list.append(translation.translation.text)
if keywords_list:
keywords = ', '.join(keywords_list)
sense_translations = str(order) + '. ' + keywords
translations_per_language.append(sense_translations)
if translations_per_language:
sense_translations = ' | '.join(translations_per_language)
else:
sense_translations = ""
if settings.DEBUG_CSV:
print(gloss, str(gloss.id), language, sense_translations)
return sense_translations
def update_senses_parse(new_senses_string):
"""CSV Import Update check the parsing of the senses field"""
if not new_senses_string:
# do nothing
return True
new_senses = [k for k in new_senses_string.split(' | ')]
order_list = []
for ns in new_senses:
try:
order_string, keywords_string = ns.split('. ')
except ValueError:
# incorrect separator between sense number and keywords
if settings.DEBUG_CSV:
print('first error: ', ns)
return False
try:
order = int(order_string)
except ValueError:
# sense is not a number
if settings.DEBUG_CSV:
print('second error: ', ns, order_string, keywords_string)
return False
if order not in range(1, 9):
# sense out of range
if settings.DEBUG_CSV:
print('third error: ', ns, order, keywords_string)
return False
if order in order_list:
# duplicate sense number found
if settings.DEBUG_CSV:
print('fourth error: ', ns, order, keywords_string)
return False
order_list.append(order)
if not keywords_string:
if settings.DEBUG_CSV:
# no keywords specified
print('fifth error: ', ns)
return False
try:
keywords_list = keywords_string.split(', ')
except ValueError:
if settings.DEBUG_CSV:
print('sixth error: ', ns, order, keywords_string)
return False
if len(keywords_list) != len(list(set(keywords_list))):
# duplicates in same sense
if settings.DEBUG_CSV:
print('seventh error: ', ns, order, keywords_list)
return False
return True
def sense_translations_for_language_mapping(gloss, language):
sense_keywords_mapping = dict()
glosssenses = GlossSense.objects.all().prefetch_related('sense').filter(gloss=gloss).order_by('order')
if not glosssenses:
return sense_keywords_mapping
gloss_senses = dict()
for gs in glosssenses:
gloss_senses[gs.order] = gs.sense
for order, sense in gloss_senses.items():
sensetranslations = sense.senseTranslations.filter(language=language)
if not sensetranslations.count():
if settings.DEBUG_CSV:
print('No sensetranslation object for ', gloss, ' ( ', str(gloss.id), ') ', language)
continue
elif sensetranslations.count() > 1:
if settings.DEBUG_CSV:
print('Multiple sensetranslation objects for ', gloss, ' ( ', str(gloss.id), ') ', sensetranslations)
sensetranslation = sensetranslations.first()
keywords_list = []
translations = sensetranslation.translations.all().order_by('index')
for translation in translations:
keywords_list.append(translation.translation.text)
if keywords_list:
sense_keywords_mapping[order] = keywords_list
if settings.DEBUG_CSV:
print('sense_translations_for_language_mapping: ', gloss, str(gloss.id), language, sense_keywords_mapping)
return sense_keywords_mapping
def csv_create_senses(request, gloss, language, new_senses_string, create=False):
"""CSV Import Update the senses field"""
# this function assumes the new_senses_string is correctly parsed
# the function update_senses_parse tests this
# the sense numbers in the new_senses_string are unique numbers between 1 and 9
# the request argument is needed to save the user sense creation in the Gloss Revision History
if settings.DEBUG_CSV:
print('call to csv_create_senses: ', gloss, str(gloss.id), language, '"', new_senses_string, '"')
if not new_senses_string:
return
current_senses_string = sense_translations_for_language(gloss, language)
if current_senses_string:
# update of senses is not done by this method
return
new_senses = [k.strip() for k in new_senses_string.split(' | ')]
new_senses_dict = dict()
for ns in new_senses:
order_string, keywords_string = ns.split('. ')
keywords_list_split = keywords_string.split(', ')
keywords_list = [kw.strip() for kw in keywords_list_split]
new_senses_dict[int(order_string)] = keywords_list
if settings.DEBUG_CSV:
print('new senses to create: ', new_senses_dict)
gloss_senses = GlossSense.objects.filter(gloss=gloss)
if not gloss_senses:
# there are currently no senses for this gloss, create an empty 1st one
# in case the user has started numbering at something other than 1, get this
new_senses_orders = sorted(ns for ns in new_senses_dict.keys())
if settings.DEBUG_CSV:
print('new sense orders: ', new_senses_orders)
if create:
if settings.DEBUG_CSV:
print('csv_create_senses create: ', gloss, new_senses_string, new_senses_orders)
# there are currently no senses for this gloss
create_empty_sense(gloss, new_senses_orders[0], erase=True)
if not create:
return
revisions = []
for order, keywords in new_senses_dict.items():
sense, sense_translations = create_empty_sense(gloss, order, erase=False)
gloss_sense_translation = sense_translations[language]
for inx, keyword in enumerate(keywords, 1):
(keyword_object, created) = Keyword.objects.get_or_create(text=keyword)
translation = Translation.objects.create(gloss=gloss,
language=language,
orderIndex=order,
translation=keyword_object,
index=inx)
translation.save()
gloss_sense_translation.translations.add(translation)
sense_new_value = str(sense)
revisions.append(('', sense_new_value))
for sense_old_value, sense_new_value in revisions:
add_sense_to_revision_history(request, gloss, sense_old_value, sense_new_value)
def required_csv_columns(dataset_languages, create_or_update='create_gloss'):
lang_attr_name = 'name_' + DEFAULT_KEYWORDS_LANGUAGE['language_code_2char']
annotationidglosstranslation_fields = ["Annotation ID Gloss" + " (" + getattr(language, lang_attr_name) + ")"
for language in dataset_languages]
lemmaidglosstranslation_fields = ["Lemma ID Gloss" + " (" + getattr(language, lang_attr_name) + ")"
for language in dataset_languages]
keyword_fields = ["Senses" + " (" + getattr(language, lang_attr_name) + ")"
for language in dataset_languages]
sentence_fields = ["Example Sentences" + " (" + getattr(language, lang_attr_name) + ")"
for language in dataset_languages]
activate(LANGUAGES[0][0])
fieldnames = FIELDS['main'] + FIELDS['phonology'] + FIELDS['semantics'] + ['inWeb', 'isNew']
fields = [Gloss.get_field(fname) for fname in fieldnames if fname in Gloss.get_field_names()]
gloss_fields = [f.verbose_name.encode('ascii', 'ignore').decode() for f in fields]
extra_columns = ['SignLanguages', 'Dialects', 'Sequential Morphology', 'Simultaneous Morphology',
'Blend Morphology', 'Relations to other signs', 'Relations to foreign signs', 'Tags', 'Notes']
required_columns = []
language_fields = []
optional_columns = []
if create_or_update == 'create_gloss':
required_columns = ['Dataset'] + lemmaidglosstranslation_fields + annotationidglosstranslation_fields
elif create_or_update == 'update_lemma':
required_columns = ['Lemma ID', 'Dataset'] + lemmaidglosstranslation_fields
# for convenience, allow but ignore annotation fields
optional_columns = ['Signbank ID'] + annotationidglosstranslation_fields
elif create_or_update == 'update_gloss':
required_columns = ['Signbank ID', 'Dataset']
# for display convenience in template, separate the language fields
language_fields = (lemmaidglosstranslation_fields + annotationidglosstranslation_fields
+ keyword_fields + sentence_fields)
optional_columns = gloss_fields + extra_columns
elif create_or_update == 'create_sentences':
required_columns = ['Signbank ID', 'Dataset', 'Sense Number', 'Sentence Type', 'Negative']
for lang in dataset_languages:
language_name = getattr(lang, settings.DEFAULT_LANGUAGE_HEADER_COLUMN['English'])
column_name = "Example Sentences (%s)" % language_name
required_columns.append(column_name)
else:
print('required_csv_columns: Required columns not defined for create_or_update: ', create_or_update)
return required_columns, language_fields, optional_columns
def csv_header_row_glosslist(dataset_languages):
fieldnames = FIELDS['main'] + FIELDS['phonology'] + FIELDS['semantics'] + FIELDS['frequency'] + ['inWeb', 'isNew']
fields = [Gloss.get_field(fname) for fname in fieldnames if fname in Gloss.get_field_names()]
lang_attr_name = 'name_' + DEFAULT_KEYWORDS_LANGUAGE['language_code_2char']
annotationidglosstranslation_fields = ["Annotation ID Gloss" + " (" + getattr(language, lang_attr_name) + ")"
for language in dataset_languages]
lemmaidglosstranslation_fields = ["Lemma ID Gloss" + " (" + getattr(language, lang_attr_name) + ")"
for language in dataset_languages]
keyword_fields = ["Senses" + " (" + getattr(language, lang_attr_name) + ")"
for language in dataset_languages]
sentence_fields = ["Example Sentences" + " (" + getattr(language, lang_attr_name) + ")"
for language in dataset_languages]
# CSV should be the first language in the settings
activate(LANGUAGES[0][0])
header = ['Signbank ID', 'Dataset'] + lemmaidglosstranslation_fields + annotationidglosstranslation_fields \
+ keyword_fields + sentence_fields + [f.verbose_name.encode('ascii', 'ignore').decode() for f in fields]
for extra_column in ['SignLanguages', 'Dialects', 'Sequential Morphology', 'Simultaneous Morphology',
'Blend Morphology',
'Relations to other signs', 'Relations to foreign signs', 'Tags', 'Notes']:
header.append(extra_column)
return header
def csv_gloss_to_row(gloss, dataset_languages, fields):
row = [str(gloss.pk), gloss.lemma.dataset.acronym]
for language in dataset_languages:
lemmaidglosstranslations = gloss.lemma.lemmaidglosstranslation_set.filter(language=language)
if lemmaidglosstranslations.count() > 0:
# get rid of any invisible characters at the end such as \t
lemmatranslation = lemmaidglosstranslations.first().text.strip()
row.append(lemmatranslation)
else:
row.append("")
for language in dataset_languages:
annotationidglosstranslations = gloss.annotationidglosstranslation_set.filter(language=language)
if annotationidglosstranslations.count() > 0:
# get rid of any invisible characters at the end such as \t
annotation = annotationidglosstranslations.first().text.strip()
row.append(annotation)
else:
row.append("")
# Put senses (keywords) per language in a cell
for language in dataset_languages:
gloss_senses_of_language = sense_translations_for_language(gloss, language)
row.append(gloss_senses_of_language)
# Put example sentences per language in a cell
for language in dataset_languages:
gloss_example_sentences_of_language = sense_examplesentences_for_language(gloss, language)
row.append(gloss_example_sentences_of_language)
for f in fields:
# Try the value of the choicelist
if hasattr(f, 'field_choice_category'):
if hasattr(gloss, 'get_' + f.name + '_display'):
value = getattr(gloss, 'get_' + f.name + '_display')()
else:
field_value = getattr(gloss, f.name)
value = field_value.name if field_value else '-'
elif isinstance(f, models.ForeignKey) and f.related_model == Handshape:
handshape_field_value = getattr(gloss, f.name)
value = handshape_field_value.name if handshape_field_value else '-'
elif f.related_model == SemanticField:
value = ", ".join([str(sf.name) for sf in gloss.semField.all()])
elif f.related_model == DerivationHistory:
value = ", ".join([str(sf.name) for sf in gloss.derivHist.all()])
else:
value = getattr(gloss, f.name)
# some legacy glosses have empty text fields of other formats
if (f.__class__.__name__ == 'CharField' or f.__class__.__name__ == 'TextField') \
and value in ['-', '------', ' ']:
value = ''
if value is None:
if f.name in settings.HANDEDNESS_ARTICULATION_FIELDS:
value = 'Neutral'
elif f.name in settings.HANDSHAPE_ETYMOLOGY_FIELDS:
value = 'False'
else:
if hasattr(f, 'field_choice_category'):
value = '-'
elif f.__class__.__name__ == 'CharField' or f.__class__.__name__ == 'TextField':
value = ''
elif f.__class__.__name__ == 'IntegerField':
value = 0
else:
# what to do here? leave it as None or use empty string (for export to csv)
value = ''
if not isinstance(value, str):
# this is needed for csv
value = str(value)
row.append(value)
# get languages
signlanguages = gloss.get_signlanguage_display()
row.append(signlanguages)
# get dialects
dialects = gloss.get_dialect_display()
row.append(dialects)
# get morphology
# Sequential Morphology
morphemes = gloss.get_hasComponentOfType_display()
row.append(morphemes)
# Simultaneous Morphology
simultaneous_morphemes = gloss.get_morpheme_display()
row.append(simultaneous_morphemes)
# Blend Morphology
blend_morphemes = gloss.get_blendmorphology_display()
row.append(blend_morphemes)
# get relations to other signs
relations_categories = gloss.get_relation_display()
row.append(relations_categories)
# get relations to foreign signs
relations_categories = gloss.get_relationToForeignSign_display()
row.append(relations_categories)
# export tags
tags_of_gloss = TaggedItem.objects.filter(object_id=gloss.id)
tag_names_of_gloss = []
for t_obj in tags_of_gloss:
tag_id = t_obj.tag_id
tag_name = Tag.objects.get(id=tag_id)
tag_names_of_gloss += [str(tag_name).replace('_', ' ')]
tag_names = ", ".join(tag_names_of_gloss)
row.append(tag_names)
# export notes
notes_of_gloss = gloss.definition_set.all()
notes_list = []
for note in notes_of_gloss:
notes_list += [note.note_tuple()]
sorted_notes_list = sorted(notes_list, key=lambda x: (x[0], x[1], x[2], x[3]))
notes_list = []
for (role, published, count, text) in sorted_notes_list:
# does not use a comprehension because of nested parentheses in role and text fields
tuple_reordered = role + ': (' + published + ',' + count + ',' + text + ')'
notes_list.append(tuple_reordered)
notes_display = ", ".join(notes_list)
row.append(notes_display)
# Make it safe for weird chars
safe_row = []
for column in row:
try:
safe_row.append(column.encode('utf-8').decode())
except AttributeError:
safe_row.append("")
return safe_row
def csv_header_row_morphemelist(dataset_languages, fields):
lang_attr_name = 'name_' + DEFAULT_KEYWORDS_LANGUAGE['language_code_2char']
annotationidglosstranslation_fields = ["Annotation ID Gloss" + " (" + getattr(language, lang_attr_name) + ")"
for language in dataset_languages]
# TO DO: make multilingual columns
keyword_fields = ["Keywords" + " (" + getattr(language, lang_attr_name) + ")"
for language in dataset_languages]
with override(LANGUAGES[0][0]):
header = ['Signbank ID'] + annotationidglosstranslation_fields + keyword_fields + [f.verbose_name.title().encode('ascii', 'ignore').decode() for f in fields]
for extra_column in ['Appears in signs']:
header.append(extra_column)
return header
def csv_morpheme_to_row(gloss, dataset_languages, fields):
row = [str(gloss.pk)]
for language in dataset_languages:
annotationidglosstranslations = gloss.annotationidglosstranslation_set.filter(language=language)
if annotationidglosstranslations.count() > 0:
row.append(annotationidglosstranslations.first().text)
else:
row.append("")
# get keywords
for language in dataset_languages:
keywords = [t.translation.text for t in gloss.translation_set.filter(language=language).order_by('index')]
row.append(", ".join(keywords))
for f in fields:
# Try the value of the choicelist
if hasattr(f, 'field_choice_category'):
if hasattr(gloss, 'get_' + f.name + '_display'):
value = getattr(gloss, 'get_' + f.name + '_display')()
else:
field_value = getattr(gloss, f.name)
value = field_value.name if field_value else '-'
elif isinstance(f, models.ForeignKey) and f.related_model == Handshape:
handshape_field_value = getattr(gloss, f.name)
value = handshape_field_value.name if handshape_field_value else '-'
elif f.related_model == SemanticField:
value = ", ".join([str(sf.name) for sf in gloss.semField.all()])
elif f.related_model == DerivationHistory:
value = ", ".join([str(sf.name) for sf in gloss.derivHist.all()])
else:
value = getattr(gloss, f.name)
value = str(value)
row.append(value)
# Got all the glosses this morpheme appears in
appearsin = gloss.get_appearsin_display()
row.append(appearsin)
# Make it safe for weird chars
safe_row = []
for column in row:
try:
safe_row.append(column.encode('utf-8').decode())
except AttributeError:
safe_row.append("")
return safe_row
def csv_header_row_handshapelist(fields):
activate(LANGUAGES[0][0])
header = ['Handshape ID'] + [f.verbose_name.encode('ascii', 'ignore').decode().capitalize()
for f in fields]
return header
def csv_handshape_to_row(handshape, fields):
row = [str(handshape.pk)]
for f in fields:
# Try the value of the choicelist
if hasattr(f, 'field_choice_category'):
if hasattr(handshape, 'get_' + f.name + '_display'):
value = getattr(handshape, 'get_' + f.name + '_display')()
else:
value = getattr(handshape, f.name)
if value is not None:
value = value.name
else:
value = getattr(handshape, f.name)
if not isinstance(value, str):
value = str(value)
if value is None:
if f.__class__.__name__ == 'CharField' or f.__class__.__name__ == 'TextField':
value = ''
elif f.__class__.__name__ == 'IntegerField':
value = 0
else:
value = ''
row.append(value)
# Make it safe for weird chars
safe_row = []
for column in row:
try:
safe_row.append(column.encode('utf-8').decode())
except AttributeError:
safe_row.append("")
return safe_row
def csv_header_row_lemmalist(dataset_languages):
lang_attr_name = 'name_' + DEFAULT_KEYWORDS_LANGUAGE['language_code_2char']
lemmaidglosstranslation_fields = ["Lemma ID Gloss" + " (" + getattr(language, lang_attr_name) + ")"
for language in dataset_languages]
with override(LANGUAGES[0][0]):
header = ['Lemma ID', 'Dataset'] + lemmaidglosstranslation_fields + ['Number of glosses']
return header