File size: 8,853 Bytes
054f09c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import pandas as pd
import numpy as np
import re

def parse_and_execute_commands(processed, user_input, fix, after, popped_row):
    commands = [cmd.strip() for cmd in user_input.split(',')]
    for command in commands:
        condition, cmd = parse_conditional_command(command)
        if check_condition(processed, condition, popped_row):
            processed = execute_command(processed, cmd, fix, after)
    return processed

def parse_conditional_command(command):
    match = re.match(r"\((.*?)\)\:(.*)", command)
    if match:
        return match.groups()
    return '', command

def check_condition(processed, condition, popped_row):
    if not condition:
        return True
    sub_conditions = re.split(r'\)\s*&\s*\(', condition)
    sub_conditions = [re.sub(r'^\(|\)$', '', cond) for cond in sub_conditions]

    results = []
    for sub_cond in sub_conditions:
        if '&' in sub_cond:
            results.append(all(check_condition(processed, cond, popped_row) for cond in sub_cond.split('&')))
        elif '|' in sub_cond:
            results.append(any(check_condition(processed, cond, popped_row) for cond in sub_cond.split('|')))
        else:
            if sub_cond in ['e', 'q', 's', 'g']:
                results.append(sub_cond == popped_row['rating'])
            elif sub_cond in ['~e', '~q', '~s', '~g']:
                results.append(sub_cond != popped_row['rating'])
            # PM
            elif sub_cond.startswith('*'):
                results.append(sub_cond[1:] in processed)
            # NOT IN
            elif sub_cond.startswith('~!'):
                results.append(sub_cond[2:] not in processed)
            elif sub_cond.startswith('~'):
                results.append(any(sub_cond[1:] not in element for element in processed))
            # CONTAIN
            else:
                results.append(any(sub_cond in element for element in processed))
    return all(results)

def execute_command(processed, command, fix, after):
    if '+=' in command:
        keyword, addition = command.split('+=', 1)
        addition = addition.replace('^', ', ')
        #addiction split ํ•ด์„œ ํ”„๋กฌํ”„ํŠธ ์กด์žฌํ•˜๋Š”์ง€ ์ฒดํฌ ํ•„์š”
        return insert_text_after_keyword(processed, keyword, addition, fix, after)
    elif '=' in command:
        keyword, replacement = command.split('=', 1)
        if keyword in processed:
            replacement = replacement.replace('^', ', ')
            #replacement split ํ•ด์„œ ํ”„๋กฌํ”„ํŠธ ์กด์žฌํ•˜๋Š”์ง€ ์ฒดํฌ ํ•„์š”
            index = processed.index(keyword)
            processed[index] = replacement
    return processed

def insert_text_after_keyword(processed, user_keyword, user_additional_keyword, fix, after):
    if user_keyword == "prompt":
        processed.append(user_additional_keyword)
    elif user_keyword == "prefix":
        fix.append(user_additional_keyword)
    elif user_keyword == "postfix":
        after.append(user_additional_keyword)
    elif user_keyword in processed:
        index = processed.index(user_keyword) + 1
        processed.insert(index, user_additional_keyword)
    return processed

def find_keyword_index(general):
    # boys์™€ girls ๋ฆฌ์ŠคํŠธ์˜ ์›์†Œ๊ฐ€ ์žˆ๋Š”์ง€ ํ™•์ธ ๋ฐ ์ธ๋ฑ์Šค ์ €์žฅ
    boys = ["1boy", "2boys", "3boys", "4boys", "5boys", "6+boys"]
    girls = ["1girl", "2girls", "3girls", "4girls", "5girls", "6+girls"]
    #others = ["1other", "2others", "3others", "4others", "5others", "6+others"]
    boys_indices = [i for i, item in enumerate(general[:6]) if item in boys]
    girls_indices = [i for i, item in enumerate(general[:6]) if item in girls]

    # case 1๊ณผ case 2: girls ๋ฆฌ์ŠคํŠธ์˜ ์›์†Œ ์ฐพ๊ธฐ
    if girls_indices:
        return girls_indices[0]+1
    
    # case 3: boys ๋ฆฌ์ŠคํŠธ์˜ ์›์†Œ ์ฐพ๊ธฐ
    if boys_indices:
        return boys_indices[0]+1

    # case 4: ํ•ด๋‹น ์‚ฌํ•ญ ์—†์Œ
    # 2์›” 12์ผ์— ํ•ด๋‹น ๋ถ€๋ถ„ return 2 -> return 0 ์œผ๋กœ ์ˆ˜์ •ํ•˜์˜€์œผ๋ฉฐ ์ด ๋ถ€๋ถ„ ํŠธ๋ž˜ํ‚น ํ•„์š”
    return 0

def RFP(popped_row, fix_prompt, after_prompt, auto_hide_prompt, rm_a, rm_s, rm_c, nsfw, data, magic_word):
    boys = ["1boy", "2boys", "3boys", "4boys", "5boys", "6+boys"]
    girls = ["1girl", "2girls", "3girls", "4girls", "5girls", "6+girls"]
    general = [item.strip() for item in popped_row['general'].split(',')]
    special_word_check = fix_prompt+', '+after_prompt
    special_word_check = [item.strip() for item in special_word_check.split(',')]
    special_word_check = [item for item in special_word_check if item.startswith('*')]
    exc = []
    for keyword in general:
        if keyword == '!' or keyword == '!?' or keyword == '!!':
            exc.append(keyword)
    if exc:
        for keyword in exc:
            general.remove(keyword)
        general[3:3] = exc

    if nsfw == 1:
        nsfw_word = []
        for keyword in general:
            if keyword in data.qe_word or keyword in data.bag_of_tags or "horns" in keyword or "(" in keyword or keyword in boys or keyword in girls:
                nsfw_word.append(keyword)
        general = nsfw_word
    if rm_c == 1:
        temp_general = []
        for keyword in general:
            if keyword in data.bag_of_tags:
                temp_general.append(keyword)
        for keyword in temp_general:
            general.remove(keyword)
    fix = [item.strip() for item in fix_prompt.split(',')]
    at_first = []
    for fp in fix:
        if fp.startswith('*'):
            at_first.append(fp[1:])
    if at_first:
        for af in at_first:
            if '*'+af in fix:
                fix.remove('*'+af)
    if rm_a == 0:
        if popped_row['artist']:
            artists = [item.strip() for item in popped_row['artist'].split(',')]
            artist = ["artist:" + _artist for _artist in artists]
            fix = fix + artist
    if rm_s == 0:
        if popped_row['copyright']:
            series = [item.strip() for item in popped_row['copyright'].split(',')]
        else:
            series = []
        fix = fix + series
    after = [item.strip() for item in after_prompt[:-1].split(',')]
    auto_hide = [item.strip() for item in auto_hide_prompt[:-1].split(',')] + ["| |", ":|", "\||/", "<|> <|>", "|| ||", ";|"]
    fix_index = find_keyword_index(general)
    processed = general.copy()
    temp_hide_prompt = []
    for keyword in processed:
        if keyword in auto_hide:
            temp_hide_prompt.append(keyword)
    for keyword in temp_hide_prompt:
        processed.remove(keyword)
    #์—ฌ๊ธฐ์—์„œ ๋ฌธ์ œ์˜ magic word ์ฒ˜๋ฆฌ
    if "cond" in magic_word and magic_word["cond"]:
        user_input = magic_word["cond"]
        processed = parse_and_execute_commands(processed, user_input, fix, after, popped_row)
    
    processed[fix_index:fix_index] = fix
    processed += after

    if rm_c == 0 and '*(remove character name)' not in special_word_check and '*(set character name after prefix)' not in special_word_check and '*(set character name before prefix)' not in special_word_check:
        if popped_row['character']:
            character = [item.strip() for item in popped_row['character'].split(',')]
            processed[fix_index:fix_index] = character
            fix_index+=len(character)
    elif rm_c == 0 and '*(set character name after prefix)' in special_word_check:
        if popped_row['character']:
            character = [item.strip() for item in popped_row['character'].split(',')]
            processed[fix_index+len(fix):fix_index+len(fix)] = character
            #fix_index+=len(character)
    elif rm_c == 0 and '*(set character name before prefix)' in special_word_check:
        if popped_row['character']:
            character = [item.strip() for item in popped_row['character'].split(',')]
            processed[fix_index+1:fix_index+1] = character
            fix_index+=len(character)
    if magic_word["random_artist"] == True:
        processed.insert(fix_index, magic_word["random_artist_name"])
        
    boy_in_processed = girl_in_processed = None
    for boy in boys:
        if boy in processed:
            boy_in_processed = boy
            break

    for girl in girls:
        if girl in processed:
            girl_in_processed = girl
            break
        
    if boy_in_processed and girl_in_processed:
        boy_index = processed.index(boy_in_processed)
        girl_index = processed.index(girl_in_processed)
        if boy_index > girl_index:
            processed.pop(boy_index)
            processed.insert(girl_index, boy_in_processed)

    s_word = ['*(remove character name)', '*(set character name after prefix)', '*(set character name before prefix)']
    for keyword in s_word:
        if keyword in processed:
            processed.remove(keyword)
            
    if at_first:
        processed = at_first + processed
    return ', '.join(processed), popped_row['rating']