-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtransform_text.py
1545 lines (1134 loc) · 48.5 KB
/
transform_text.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
import random
import re
import shutil
import tempfile
import warnings
from copy import copy
from pathlib import Path
from typing import *
from lxml import etree, objectify
from pymystem3 import Mystem
warnings.filterwarnings("ignore")
PLAY_NAME = "test"
MAIN_DIRECTORY = f"{PLAY_NAME}/game"
PATH_TO_INPUT_FILE: str = "texts/gogol-teatralnyi-razezd.xml"
PATH_TO_OUTPUT_FILE: str = f"{MAIN_DIRECTORY}/script.rpy"
PATH_TO_IMAGES_DIRECTORY: str = f"{MAIN_DIRECTORY}/images"
PATH_TO_PICTURES_DIRECTORY: str = "pictures"
PATH_TO_CHARACTERS_DIRECTORY: str = f"{PATH_TO_IMAGES_DIRECTORY}/characters"
PATH_TO_CHARACTERS_PICTURES: str = f"{PATH_TO_PICTURES_DIRECTORY}/characters"
PATH_TO_BACKGROUNDS_DIRECTORY: str = f"{PATH_TO_IMAGES_DIRECTORY}/bg"
PATH_TO_BACKGROUNDS_PICTURES: str = f"{PATH_TO_PICTURES_DIRECTORY}/background"
PATH_TO_GUI_DIRECTORY: str = f"{MAIN_DIRECTORY}/gui"
PATH_TO_GUI_FILES: str = f"gui"
PATH_TO_AUDIO_DIRECTORY: str = f"{MAIN_DIRECTORY}/audio"
PATH_TO_MUSIC_DIRECTORY: str = f"{PATH_TO_AUDIO_DIRECTORY}/music"
PATH_TO_SOUNDS_DIRECTORY: str = f"{PATH_TO_AUDIO_DIRECTORY}/sounds"
PATH_TO_MUSIC_AUDIO: str = f"audio/music"
PATH_TO_SOUNDS_AUDIO: str = f"audio/sounds"
XML_SCHEME: str = "{http://www.w3.org/XML/1998/namespace}"
# CHARACTERS dicts
CHARACTER_NAME_TO_CODE: OrderedDict[str, str] = OrderedDict()
CHARACTER_CODE_TO_NAME: OrderedDict[str, str] = OrderedDict()
CHARACTER_CODE_TO_SEX: Dict[str, str] = {}
SEX_TO_CHARACTER_CODE: Dict[str, List[str]] = {}
CHARACTER_CODE_TO_ROLE_DESC: OrderedDict[str, str] = OrderedDict()
# MENU labels
CHARACTERS: str = "Characters"
CHARACTERS_NAME: str = '' # name of the `CHARACTERS` label
NUM_OF_CHARACTERS: int = 0 # number of castLists
ACT: str = "Act"
SCENE: str = "Scene"
SUB_SCENE: str = "Subscene"
FURTHER: str = "Further"
# ACTS lists
ACT_CODES: List[str] = []
ACT_NAMES: List[str] = []
# SCENES lists
SCENE_CODES: List[str] = []
SCENE_NAMES: List[str] = []
# SUB_SCENES lists
SUB_SCENE_CODES: List[str] = []
SUB_SCENE_NAMES: List[str] = []
# COLORS
KELLY_COLORS_HEX: List[str] = [
"#FFB300", # Vivid Yellow
"#803E75", # Strong Purple
"#FF6800", # Vivid Orange
"#A6BDD7", # Very Light Blue
"#C10020", # Vivid Red
"#CEA262", # Grayish Yellow
"#817066", # Medium Gray
# The following don't work well for people with defective color vision
"#007D34", # Vivid Green
"#F6768E", # Strong Purplish Pink
"#00538A", # Strong Blue
"#FF7A5C", # Strong Yellowish Pink
"#53377A", # Strong Violet
"#FF8E00", # Vivid Orange Yellow
"#B32851", # Strong Purplish Red
"#F4C800", # Vivid Greenish Yellow
"#7F180D", # Strong Reddish Brown
"#93AA00", # Vivid Yellowish Green
"#593315", # Deep Yellowish Brown
"#F13A13", # Vivid Reddish Orange
"#232C16", # Dark Olive Green
]
KELLY_COLORS_HEX_INDEX: int = 0
# max number of options on a screen
MAX_NUM_OF_VALUES_IN_LIST: int = 6
BACKGROUNDS_TAKEN: Set[int] = set()
MUSIC_TAKEN: Set[int] = set()
PREV_CHARACTER: Optional[str] = None
BLACK_COLOR = "#000"
IDENTIFIER = None
CHARACTERS_TEXT: str = ""
FRONT_TEXTS: List[str] = []
ACTS_TEXT: str = ""
STAND_OFF: str = ""
TAB: str = " "
mystem = Mystem()
# random
random.seed(123)
def get_random_colour():
global KELLY_COLORS_HEX_INDEX
if KELLY_COLORS_HEX_INDEX < len(KELLY_COLORS_HEX):
result = KELLY_COLORS_HEX[KELLY_COLORS_HEX_INDEX]
KELLY_COLORS_HEX_INDEX += 1
return result
return "#" + ''.join([random.choice('0123456789ABCDEF') for _ in range(6)])
def create_character_variables_with_given_sex(character_elems, sex: str) -> str:
s: str = ""
characters = []
for character_elem in character_elems:
sex_attrib: str = character_elem.attrib['sex']
pers_code: str = character_elem.get(f"{XML_SCHEME}id").replace('-', '_').lower()
main_name: Optional[str] = None
for elem in character_elem:
if elem.tag in ["persName", "name"]:
name = get_text(elem)
CHARACTER_NAME_TO_CODE[name] = pers_code
for property in ["фам", "имя", "отч"]:
for key in get_lexemes(name, properties=[property], return_text=True):
key = key.lower()
if key not in CHARACTER_NAME_TO_CODE:
CHARACTER_NAME_TO_CODE[key] = pers_code
# if not name.endswith("а"):
# # adding "а" in case of an error in morphological analysis
# CHARACTER_NAME_TO_CODE[name + "а"] = pers_code
if main_name is None:
main_name = name
CHARACTER_CODE_TO_NAME[pers_code] = main_name
if sex_attrib == sex:
CHARACTER_CODE_TO_SEX[pers_code] = sex_attrib
if sex_attrib not in SEX_TO_CHARACTER_CODE:
SEX_TO_CHARACTER_CODE[sex_attrib] = []
SEX_TO_CHARACTER_CODE[sex_attrib].append(pers_code)
colour: str = get_random_colour()
s += f'define {pers_code} = Character("{pers_code}_var", color="{colour}", dynamic=True)\n'
characters.append(pers_code)
num_of_pictures = len(list(Path(f'{PATH_TO_CHARACTERS_PICTURES}/{sex.lower()}').glob('*')))
assert len(characters) <= num_of_pictures, \
f'actual: {len(characters)}, current: {num_of_pictures}'
numbers = random.sample(range(1, num_of_pictures), len(characters))
Path(PATH_TO_CHARACTERS_DIRECTORY).mkdir(exist_ok=True)
for i, sex_character in enumerate(characters):
shutil.copy(
Path(f'{PATH_TO_CHARACTERS_PICTURES}/{sex.lower()}/{numbers[i]}.png'),
Path(f'{PATH_TO_CHARACTERS_DIRECTORY}/{sex_character}.png')
)
return s
def create_character_variables(character_elems) -> str:
s: str = ""
for sex in ["MALE", "FEMALE", "UNKNOWN"]:
s += create_character_variables_with_given_sex(character_elems, sex)
return s
def get_root_menu(indent: str = ""):
s = ""
s += f"{indent}menu:\n"
indent += TAB
if CHARACTERS_NAME:
s += f'{indent}"{{color={BLACK_COLOR}}}{CHARACTERS_NAME}{{/color}}":\n'
s += f' {indent}jump {CHARACTERS}_1\n'
num_of_pages: int = (len(ACT_CODES) - 1) // MAX_NUM_OF_VALUES_IN_LIST + 1
for i in range(num_of_pages):
for act_num, act_code in \
enumerate(ACT_CODES[MAX_NUM_OF_VALUES_IN_LIST * i:MAX_NUM_OF_VALUES_IN_LIST * (i + 1)]):
s += f'{indent}"{{color={BLACK_COLOR}}}{ACT_NAMES[MAX_NUM_OF_VALUES_IN_LIST * i + act_num]}{{/color}}":\n'
s += f' {indent}jump {act_code}\n'
if i < num_of_pages - 1:
further_code = f'{FURTHER}_{ACT}_{i + 1}'
s += f'{indent}"{{color={BLACK_COLOR}}}>{{/color}}":\n'
s += f' {indent}jump {further_code}\n\n'
indent = indent[8:]
s += f'{indent}label {further_code}:\n'
indent += TAB
s += f"{indent}menu:\n"
indent += TAB
return s
def play_music(tag, indent: str = "", attrib_name: str = "music", get_random=False) -> str:
s: str = ''
# music
if tag is not None and attrib_name in tag.attrib:
names = [f"audio/music/{name}.mp3" for name in tag.attrib[attrib_name].split()]
s += f'{indent}play music {names} fadeout 1.0 fadein 1.0\n\n'
elif get_random:
Path(PATH_TO_MUSIC_DIRECTORY).mkdir(exist_ok=True)
num_of_songs: int = len(list(Path(PATH_TO_MUSIC_AUDIO).glob('*')))
num: int = random.randint(1, num_of_songs)
while num in MUSIC_TAKEN:
num = random.randint(1, num_of_songs)
MUSIC_TAKEN.add(num)
shutil.copy(
Path(f'{PATH_TO_MUSIC_AUDIO}/{num}.mp3'),
Path(f'{PATH_TO_MUSIC_DIRECTORY}/{num}.mp3')
)
s += f'{indent}play music "audio/music/{num}.mp3" fadeout 1.0 fadein 1.0\n\n'
return s
def get_text_parts(elem, text: str) -> List[str]:
parts = re.split(r"(?<=[.;!?])\s+", text)
text_parts: List[str] = []
curr_parts: List[str] = []
curr_length: int = 0
for part in parts:
inner_parts = [part]
if len(part) >= 260:
inner_parts = re.split(r'(?<=[,»"])\s+', part)
for inner_part in inner_parts:
if curr_length + len(inner_part) >= 260:
text_parts.append(' '.join(curr_parts))
curr_parts = []
curr_length = 0
curr_parts.append(inner_part)
curr_length += len(inner_part)
if curr_parts:
text_parts.append(' '.join(curr_parts))
return text_parts
def text_splitter(func):
def inner_func(*args, **kwargs):
item = args[0]
s: str = ''
text = re.sub(r"\s+", " ", item.text)
text_parts: List[str] = get_text_parts(item, text)
for text_part in text_parts:
inner_elem = copy(item)
inner_elem.text = text_part
s += func(inner_elem, *args[1:], **kwargs)
item.text = text
# for parse_cast_item method
global IDENTIFIER
IDENTIFIER = None
return s
return inner_func
@text_splitter
def parse_cast_item(elem, indent: str = "", notes: List = None) -> str:
if notes is None:
notes = []
was_role: bool = False
for _ in elem:
was_role = True
if was_role:
elem = elem[0]
s: str = ''
text: str = get_text(elem)
text = add_notes_to_text(text, notes=notes)
enrich_elem(elem)
characters = elem.attrib.get("characters").split()
identifier: str = characters[0] if characters else None
if "id" in elem.attrib:
identifier = elem.attrib["id"].lower()
global IDENTIFIER
if identifier is None:
identifier = IDENTIFIER
IDENTIFIER = identifier
role_desc: str = text
if ',' in text:
role_desc = ' '.join(text.split(",")[1:]).strip()
for elem in [' of ', ' de ', " d'"]:
if elem in role_desc:
elem_index = role_desc.index(elem)
role_desc = role_desc[:elem_index]
CHARACTER_CODE_TO_ROLE_DESC[identifier] = role_desc
if identifier is not None:
s += f'{indent}show {identifier} at truecenter\n' # with dissolve
s += f'{indent}"{text}"\n'
if identifier is not None:
s += f'{indent}hide {identifier}\n' # with dissolve
s += "\n"
return s
def before_characters(elem, characters: List[str], indent: str = "") -> str:
s: str = ''
if "characters" in elem.attrib:
# add character for one speech or one stage
s += hide_characters(characters, indent)
new_characters = elem.attrib["characters"].split()
prev_characters = characters[:]
characters.clear()
characters.extend(value for value in prev_characters if value not in new_characters)
s += show_stage_characters(characters + new_characters, indent)
return s
def after_characters(elem, characters: List[str], indent: str = "") -> str:
s: str = ''
if "characters" in elem.attrib:
# add character for one speech or one stage
s += hide_characters(elem.attrib["characters"].split(), indent)
s += show_stage_characters(characters, indent)
return s
def enrich_stage(stage, main_character: str):
stage_types = stage.attrib["type"].split("/") if "type" in stage.attrib else []
stage_text = stage.text.lower().strip()
lexemes_dict = get_lexemes(stage_text)
lexemes = [key for key in lexemes_dict.keys()]
sounds = []
prefix = 'male' if CHARACTER_CODE_TO_SEX.get(main_character) == 'MALE' else 'female'
if any(stage_type in stage_types for stage_type in ['slap']) or \
any(word in lexemes for word in ['soufflet']):
sounds.append('slap')
# if any(stage_type in stage_types for stage_type in ['exit']) or \
# any(word in lexemes
# for word in ["aller", "sortie", "sortir", "отправляться", "разбредаться", "возвращаться",
# "скрываться", "идти", "расхаживать", "водить"]) or \
# any(any(lexeme.endswith(word) for lexeme in lexemes) for word in ["ходить", "бегать"]) or \
# any(lexemes[i:i + 2] in [["к", "дверь"], ["от", "дверь"]] for i in range(len(lexemes) - 1)):
# sounds.append('footsteps')
if any(stage_type in stage_types for stage_type in ['furniture']) or \
any(word in lexemes for word in ["chaise"]):
sounds.append('chair')
if any(stage_type in stage_types for stage_type in ['run']) or \
any(word in lexemes for word in ["courir", "бежать", "бегать"]):
sounds.append('running')
if any(stage_type in stage_types for stage_type in ['cry']) or \
any(word in lexemes for word in ["larme", "слеза", "плач"]) or \
any(any(lexeme.endswith(word) for lexeme in lexemes) for word in ["плакать", "рыдать"]):
sounds.append(f'{prefix}_cry')
if any(word in lexemes for word in ["вздох", "вздыхать"]):
sounds.append(f'{prefix}_sigh')
if any(word in lexemes for word in ["уезжать"]):
sounds.append('horse_run')
if any(word in lexemes for word in ["хохотать", "хохот", "смех", "смеяться"]):
sounds.append(f'{prefix}_laughter')
if any(stage_type in stage_types for stage_type in ['cough']) or \
any(word in lexemes for word in ["tousser", "покашливать", "кашлять"]):
sounds.append(f'{prefix}_cough')
if any(word in lexemes for word in ['фортопиять', 'фортопияно', 'фортепияно', 'пианино']):
sounds.append('piano')
if any(word in lexemes for word in ['флейта']):
sounds.append('flute')
if any(word in lexemes for word in ['целовать', 'целоваться']):
sounds.append('kiss')
if any(word in lexemes for word in ['лобызание']):
sounds.append('kisses')
if any(word in lexemes for word in ['стучаться', "стучать", "стук"]):
sounds.append('knocking')
if any(lexemes[i:i + 2] in [["часы", "бить"], ["бить", "часы"], ["часовой", "музыка"], ["часы", "ударять"],
["часы", "ударить"]]
for i in range(len(lexemes) - 1)):
sounds.append('clock')
if "зажимать" in lexemes and "рот" in lexemes and lexemes.index("зажимать") < lexemes.index("рот"):
sounds.append(f'{prefix}_gag')
if any(word in lexemes for word in ['цыпочки']):
sounds.append('tiptoes')
if any(lexemes[i:i + 2] in [["тушить", "свеча"]] for i in range(len(lexemes) - 1)):
sounds.append(f'{prefix}_blow_candle')
if any(word in lexemes for word in ['сталкиваться', "выталкивать", "хлопнуть", "бросить", "падать"]):
sounds.append('punch')
for verb in ["затворять", "закрывать", "прикрывать", "отпирать"]:
if verb in lexemes and "дверь" in lexemes and lexemes.index(verb) < lexemes.index("дверь"):
sounds.append('door_creak')
break
if any(word in lexemes for word in ['звонок', 'звонить']):
sounds.append('telephone')
if any(word in lexemes for word in ['свистеть', 'насвистывать', 'свистнуть', 'свист']):
sounds.append('whistle')
for sound in sounds:
Path(PATH_TO_SOUNDS_DIRECTORY).mkdir(exist_ok=True)
shutil.copy(
Path(f'{PATH_TO_SOUNDS_AUDIO}/{sound}.mp3'),
Path(f'{PATH_TO_SOUNDS_DIRECTORY}/{sound}.mp3')
)
if sounds:
stage.set("sound", " ".join(sounds))
def play_sound(elem, indent: str = "") -> str:
s: str = ''
if "sound" in elem.attrib:
for i, sound in enumerate(elem.attrib["sound"].split(), start=1):
s += f'{indent}play sound{i} {sound}\n'
s += "\n"
return s
def stop_sound(elem, indent: str = "") -> str:
s: str = ''
if "sound" in elem.attrib:
for i, sound in enumerate(elem.attrib["sound"].split(), start=1):
s += f'{indent}stop sound{i}\n'
s += '\n'
return s
@text_splitter
def add_stage(elem, characters: List[str], indent: str, notes: List) -> str:
enrich_elem(elem, characters[0] if characters else None)
s: str = ""
s += show_background(elem, indent)
s += play_sound(elem, indent)
s += before_characters(elem, characters, indent)
s += add_line(elem, characters, indent, notes)
s += after_characters(elem, characters, indent)
s += stop_sound(elem, indent)
return s
def add_notes_to_text(text: str, notes: List) -> str:
if notes is None:
notes = []
for note in notes:
key_text = ''
value_text = ''
for elem in note:
if elem.tag == "key":
key_text = get_text(elem)
elif elem.tag == "value":
value_text = get_text(elem)
value_text = value_text.replace('{i}', '').replace('{/i}', '')
text = text.replace(
key_text,
f'{{a=myshow|tooltip|text={value_text}}}{key_text}{{/a}}'
)
return text
@text_splitter
def add_line(line, characters: List[str], indent: str = "", notes: List = None) -> str:
if notes is None:
notes = []
s: str = ""
text: str = line.text.replace('"', '\\"').replace("[", "[[").strip()
if not text.strip():
return s
s += play_sound(line, indent)
s += before_characters(line, characters, indent)
additional_spaces = ''
if "part" in line.attrib:
if line.attrib["part"] == 'M':
additional_spaces = '{space=200}'
elif line.attrib["part"] == 'F':
additional_spaces = '{space=400}'
if "rend" in line.attrib:
if line.attrib["rend"] == 'indent':
additional_spaces = '{space=400}'
text = add_notes_to_text(text, notes)
if is_stage(line):
text = f'<{{i}}{text}{{/i}}>'
elif line.tag == 'head':
text = f'{{b}}{text}{{/b}}'
s += f'{indent}{characters[0] + " " if characters else ""}"{additional_spaces}{text}"\n\n'
s += after_characters(line, characters, indent)
s += stop_sound(line, indent)
return s
def show_stage_characters(characters: List[str], indent: str = "") -> str:
s: str = ''
if len(characters) >= 3:
new_characters = characters[-3:]
characters.clear()
characters.extend(new_characters)
s += f'{indent}show {characters[0]} at left\n'
s += f'{indent}show {characters[1]} at truecenter\n'
s += f'{indent}show {characters[2]} at right\n\n'
elif len(characters) == 2:
s += f'{indent}show {characters[0]} at left\n'
s += f'{indent}show {characters[1]} at right\n\n'
elif len(characters) == 1:
s += f'{indent}show {characters[0]} at truecenter\n\n'
return s
def hide_characters(characters: List[str], indent: str = "") -> str:
s: str = ''
for character in characters:
s += f'{indent}hide {character}\n'
if s:
s += '\n'
return s
def get_sp(sp_elem, characters: List[str], indent: str = ""):
s: str = ""
for speaker in sp_elem.get("who").split():
characters.append(speaker[1:].replace('-', '_').lower())
s += show_stage_characters(characters, indent)
if sp_elem.find('./speaker') is None:
s += f'{indent}$ {characters[0]}_var = "{{noalt}}{CHARACTER_CODE_TO_NAME[characters[0]]}"\n\n'
notes = []
for outer_elem in sp_elem:
s += parse_any(outer_elem, characters, indent, notes)
s += hide_characters(characters, indent)
return s
def get_text(elem) -> str:
return re.sub(r"\s+", " ", "".join(elem.itertext())) \
.replace("[", "[[") \
.replace('"', '\\"').strip()
def show_background(tag, indent: str = "", get_random: bool = False) -> str:
s: str = ''
# background
if tag is not None and "background" in tag.attrib:
s += f'{indent}scene {tag.attrib["background"]} with fade\n\n'
elif get_random:
Path(PATH_TO_BACKGROUNDS_DIRECTORY).mkdir(exist_ok=True)
num_of_background_pictures: int = len(list(Path(PATH_TO_BACKGROUNDS_PICTURES).glob('*')))
num: int = random.randint(1, num_of_background_pictures)
while num in BACKGROUNDS_TAKEN:
num = random.randint(1, num_of_background_pictures)
BACKGROUNDS_TAKEN.add(num)
shutil.copy(
Path(f'{PATH_TO_BACKGROUNDS_PICTURES}/{num}.jpeg'),
Path(f'{PATH_TO_BACKGROUNDS_DIRECTORY}/{num}.jpeg')
)
s += f'{indent}scene {num} with fade\n\n'
return s
def get_lexemes(text: str, parts_of_speech: List[str] = None, add_text: bool = False, properties: List[str] = None,
return_text: bool = False) -> OrderedDict[str, str]:
if properties is None:
properties = []
text_analysis = mystem.analyze(text)
lexemes = OrderedDict()
for text_analysis_elem in text_analysis:
if "analysis" in text_analysis_elem and text_analysis_elem["analysis"]:
analysis = text_analysis_elem["analysis"][0]
source_text = text_analysis_elem["text"]
text_elem = source_text.lower()
if " " in text_elem:
continue
grammar = re.split("[^a-zA-Zа-яА-ЯёЁ]", analysis['gr'])
was_break: bool = False
for property_ in properties:
if ',' in property_:
if property_ not in analysis['gr']:
was_break = True
break
elif property_.lower() not in grammar:
was_break = True
break
if was_break:
continue
if parts_of_speech is not None and grammar[0] not in parts_of_speech:
continue
if add_text:
lexemes[text_elem] = source_text
lexeme: str = source_text if return_text else analysis['lex']
lexemes[lexeme] = source_text
# if lexeme.endswith("а"):
# lexemes[lexeme[:-1]] = source_text
return lexemes
def character_name_to_lexemes(text: str) -> List[str]:
return [elem for elem in re.split("[^a-zA-zа-яА-ЯёЁ0-9]+", text) if elem] #
def search_for_characters(elem, items, characters, main_character, lexemes, text, used_lexemes_indices,
character_code_to_index, index_to_character_code, index_to_character_name):
split_text_lexemes = character_name_to_lexemes(text)
for text_lexemes in [split_text_lexemes, lexemes]:
for character_name, character_code in items:
if character_code == main_character:
continue
character_name_lexemes = character_name_to_lexemes(character_name)
for i in range(0, len(text_lexemes) - len(character_name_lexemes) + 1):
if character_name_lexemes and \
[value.lower() for value in text_lexemes[i: i + len(character_name_lexemes)]] == \
[value.lower() for value in character_name_lexemes]:
# # name should start with a capital letter unless elem.tag is "castItem"
# if elem.tag != "castItem" and text_lexemes[i][0].isalpha() and \
# text_lexemes[i][0] == text_lexemes[i][0].lower():
# continue
index: int = i
if index in used_lexemes_indices and \
len(character_name_to_lexemes(index_to_character_name[index])) >= len(character_name_lexemes):
continue
new_indices = {i for i in range(index, index + len(character_name_lexemes))}
intersection = used_lexemes_indices & new_indices
if intersection:
for elem_index in intersection & index_to_character_code.keys():
key_to_remove = index_to_character_code[elem_index]
characters.remove(key_to_remove)
used_lexemes_indices.remove(character_code_to_index[key_to_remove])
del character_code_to_index[key_to_remove]
del index_to_character_code[elem_index]
del index_to_character_name[elem_index]
if character_code in characters:
continue
used_lexemes_indices.add(index)
character_code_to_index[character_code] = index
index_to_character_code[index] = character_code
index_to_character_name[index] = character_name
characters.append(character_code)
break
def enrich_elem(elem, main_character: str = None):
text: str = elem.text.strip()
lexemes_dict = get_lexemes(text) # , parts_of_speech=["S", "A", "ANUM"], add_text=True
lexemes = [key for key in lexemes_dict.keys()]
character_code_to_index = {}
index_to_character_code = {}
index_to_character_name = {}
characters = []
used_lexemes_indices = set()
search_for_characters(elem, CHARACTER_NAME_TO_CODE.items(), characters, main_character, lexemes, text,
used_lexemes_indices, character_code_to_index, index_to_character_code,
index_to_character_name)
# for character_code, role_desc in CHARACTER_CODE_TO_ROLE_DESC.items():
# if character_code in characters or character_code == main_character or character_code is None:
# continue
#
# role_desc_lexemes_dict = get_lexemes(role_desc, parts_of_speech=["S"], properties=["им,ед"])
# for lower_role_desc in role_desc_lexemes_dict:
# if lower_role_desc in lexemes:
# index: int = text.index(lexemes_dict[lower_role_desc])
# if index in used_lexemes_indices:
# continue
# used_lexemes_indices.add(index)
# character_code_to_index[character_code] = index
# characters.append(character_code)
# break
# global PREV_CHARACTER
# if PREV_CHARACTER is not None and PREV_CHARACTER not in characters and PREV_CHARACTER != main_character:
# pronoun_lexemes_dict = get_lexemes(
# text, parts_of_speech=["SPRO", "APRO"], # if you will add "APRO" then there would be a lot of noise
# properties=["муж" if CHARACTER_CODE_TO_SEX.get(PREV_CHARACTER) == 'MALE' else "жен"],
# return_text=True
# )
#
# if pronoun_lexemes_dict:
# character_code_to_index[PREV_CHARACTER] = text.index(next(iter(pronoun_lexemes_dict)))
# characters.append(PREV_CHARACTER)
# if not characters and main_character is None and 'UNKNOWN' in SEX_TO_CHARACTER_CODE:
# # weren't able to find male/female characters, so we are searching for unknown characters
# search_for_characters(
# elem, {CHARACTER_CODE_TO_NAME[code]: code for code in SEX_TO_CHARACTER_CODE['UNKNOWN']}.items(),
# characters, main_character, lexemes, text, lexemes_dict, used_lexemes_indices, character_code_to_index
# )
characters.sort(key=lambda e: character_code_to_index[e])
elem.set("characters", " ".join(characters))
if characters:
global PREV_CHARACTER
PREV_CHARACTER = characters[0]
enrich_stage(elem, characters[0] if characters else main_character)
def get_menu(codes: List[str], names: List[str], indent: str) -> str:
s: str = ""
if not codes:
return s
s += f"{indent}menu:\n"
indent += TAB
num_of_pages: int = (len(codes) - 1) // MAX_NUM_OF_VALUES_IN_LIST + 1
for i in range(num_of_pages):
scene_code: int = 0
for scene_num, scene_code in \
enumerate(codes[MAX_NUM_OF_VALUES_IN_LIST * i:MAX_NUM_OF_VALUES_IN_LIST * (i + 1)]):
s += f'{indent}"{{color={BLACK_COLOR}}}{names[MAX_NUM_OF_VALUES_IN_LIST * i + scene_num]}{{/color}}":\n'
s += f' {indent}jump {scene_code}\n'
if i < num_of_pages - 1:
further_code = f'{FURTHER}_{scene_code}_{i + 1}'
s += f'{indent}"{{color={BLACK_COLOR}}}>{{/color}}":\n'
s += f' {indent}jump {further_code}\n\n'
indent = indent[8:]
s += f'{indent}label {further_code}:\n'
indent += TAB
s += f"{indent}menu:\n"
indent += TAB
return s
def parse_scene_div(scene_div, act_num: str, indent: str, scene_codes: List[str], scene_names: List[str]) -> str:
s: str = ""
global PREV_CHARACTER
PREV_CHARACTER = None
scene_num = len(scene_codes) + 1
scene_code: str = f'{ACT}_{act_num}_{SCENE}_{scene_num}'
s += f'{indent}label {scene_code}:\n'
indent = indent + TAB
# music
s += play_music(scene_div, indent)
# background
s += show_background(scene_div, indent)
scene_name = f'{scene_div.attrib.get("type")}_{scene_num}'
if not scene_div.findall("head"):
s += f'{indent}"{{b}}{scene_name}{{/b}}"\n\n'
scene_names.append(scene_name)
characters = []
notes: List = []
for elem in scene_div:
if characters and characters[0] != PREV_CHARACTER:
PREV_CHARACTER = characters[0]
if elem.tag == "head":
scene_text: str = get_text(elem)
scene_parts: List[str] = re.split(r"(?<=[.])", scene_text)
scene_name: str = scene_parts[0]
scene_names.append(scene_name)
elem.text = scene_name
s += add_line(elem, characters, indent, notes)
if len(scene_parts) > 1 and not (len(scene_parts) == 2 and not scene_parts[1]):
scene_stage = etree.Element("stage")
scene_stage.text = "".join(scene_parts[1:]).strip()
s += add_stage(scene_stage, [], indent, notes)
elif elem.tag == "castList":
s += parse_cast_list(elem, indent, from_tag="scene")
else:
s += parse_any(elem, characters, indent, notes)
PREV_CHARACTER = None
scene_codes.append(scene_code)
return s
def parse_act_div(act_div, indent: str, scene_codes: List[str], scene_names: List[str],
act_codes: List[str], act_names: List[str], act: str) -> str:
s: str = ""
global PREV_CHARACTER
PREV_CHARACTER = None
act_num: str = f'{len(ACT_CODES) + 1}' \
if act == ACT \
else f'{len(ACT_CODES) + 1}_{len(act_codes) + 1}_{len(scene_codes) + 1}'
act_code: str = f'{act}_{act_num}'
act_num: int = len([elem for elem in act_codes if elem.startswith(act)])
act_codes.append(act_code)
s += f'{indent}label {act_code}:\n'
indent = indent + TAB
# music
s += play_music(act_div, indent, get_random=True)
# background
s += show_background(act_div, indent, get_random=True)
act_name = f'{act_div.attrib.get("type")}_{act_num + 1}' if act_div.attrib.get("type") else act_div.tag
if not act_div.findall("head"):
s += f'{indent}"{{b}}{act_name}{{/b}}"\n\n'
act_names.append(act_name)
s_content: str = ""
characters = []
scenes: str = ''
notes = []
was_sp_or_div: bool = False
for elem in act_div:
if elem.tag in ["sp", "div"]:
was_sp_or_div = True
tmp_s = scenes if was_sp_or_div else s_content
if elem.tag == "head":
act_text: str = get_text(elem)
act_parts: List[str] = re.split(r"(?<=[.])", act_text)
act_name: str = act_parts[0]
if len(act_codes) != len(act_names):
act_names.append(act_name)
elem.text = act_name
tmp_s += add_line(elem, characters, indent, notes)
if len(act_parts) > 1 and not (len(act_parts) == 2 and not act_parts[1]):
act_stage = etree.Element("stage")
act_stage.text = "".join(act_parts[1:]).strip()
tmp_s += add_stage(act_stage, [], indent, notes)
elif elem.tag == "div" and elem.attrib["type"] == "scene":
tmp_s += parse_scene_div(elem, act_num, indent,
SUB_SCENE_CODES if act == SCENE else SCENE_CODES,
SUB_SCENE_NAMES if act == SCENE else SCENE_NAMES)
elif elem.tag == "div":
tmp_s += parse_act_div(elem, indent, SUB_SCENE_CODES, SUB_SCENE_NAMES, SCENE_CODES, SCENE_NAMES, SCENE)
elif elem.tag in ["trailer", "dateline"]:
tmp_s += add_stage(elem, characters, indent, notes)
elif act_div.tag == "set" and elem.tag in ["p", "l"]: # hardcode
elem.tag = "stage"
tmp_s += add_stage(elem, characters, indent, notes)
elif elem.tag == "castList":
scenes += parse_cast_list(elem, indent, from_tag="act")
else:
tmp_s += parse_any(elem, characters, indent, notes)
if was_sp_or_div:
scenes = tmp_s
else:
s_content = tmp_s
s_content += get_menu(scene_codes, scene_names, indent)
s_content += f'\n{scenes}'
scene_codes.clear()
scene_names.clear()
if s_content.strip():
s += s_content
else: