|
import customtkinter |
|
import pandas as pd |
|
|
|
|
|
def filter_rows_containing_all_keywords(df, keywords): |
|
|
|
final_mask = pd.Series([True] * len(df), index=df.index) |
|
|
|
|
|
for keyword in keywords: |
|
keyword_mask = pd.Series([False] * len(df), index=df.index) |
|
|
|
for column in ['copyright', 'character', 'artist', 'meta', 'general']: |
|
if df[column].dtype == 'object': |
|
keyword_mask |= df[column].str.contains(keyword, na=False) |
|
|
|
final_mask &= keyword_mask |
|
|
|
return df[final_mask] |
|
|
|
def filter_rows_not_containing_all_keywords(df, keywords): |
|
|
|
final_mask = pd.Series([True] * len(df), index=df.index) |
|
|
|
|
|
for keyword in keywords: |
|
keyword_mask = pd.Series([False] * len(df), index=df.index) |
|
|
|
for column in ['copyright', 'character', 'artist', 'meta', 'general']: |
|
if df[column].dtype == 'object': |
|
keyword_mask |= df[column].str.contains(keyword, na=False) |
|
|
|
|
|
final_mask &= ~keyword_mask |
|
|
|
return df[final_mask] |
|
|
|
def process_asterisk_group(df, asterisk_group): |
|
|
|
asterisk_keywords = [keyword.lstrip('*') + ',' for keyword in asterisk_group] |
|
|
|
|
|
df['search_string'] = df[['copyright', 'character', 'artist', 'meta', 'general']].apply(lambda x: ' ' + ', '.join(x.astype(str)) + ',', axis=1) |
|
for keyword in asterisk_keywords: |
|
df = df[df['search_string'].str.contains(keyword, na=False)] |
|
df.drop('search_string', axis=1, inplace=True) |
|
|
|
return df |
|
|
|
def process_perfect_negative_group(df, perfect_negative_group): |
|
|
|
perfect_negative_keywords = [keyword.lstrip('~') + ',' for keyword in perfect_negative_group] |
|
|
|
|
|
df['search_string'] = df[['copyright', 'character', 'artist', 'meta', 'general']].apply(lambda x: ' ' + ', '.join(x.astype(str)) + ',', axis=1) |
|
|
|
|
|
combined_mask = pd.Series([True] * len(df), index=df.index) |
|
for keyword in perfect_negative_keywords: |
|
keyword_mask = df['search_string'].str.contains(keyword, na=False) |
|
combined_mask &= ~keyword_mask |
|
|
|
|
|
df = df[combined_mask] |
|
|
|
|
|
df.drop('search_string', axis=1, inplace=True) |
|
|
|
return df |
|
|
|
def search(df, search_request, exclude_request, E, N, S, G): |
|
if(E == 0): |
|
df = df[~(df['rating'] == 'e')] |
|
if(N == 0): |
|
df = df[~(df['rating'] == 'q')] |
|
if(S == 0): |
|
df = df[~(df['rating'] == 's')] |
|
if(G == 0): |
|
df = df[~(df['rating'] == 'g')] |
|
if(len(df) == 0): |
|
return None |
|
|
|
|
|
|
|
split_requests = [item.strip() for item in search_request.split(',')] |
|
|
|
curly_brace_group = [item for item in split_requests if item.startswith('{') and item.endswith('}')] |
|
asterisk_group = [item for item in split_requests if item.startswith('*')] |
|
normal_group = [item for item in split_requests if item not in curly_brace_group + asterisk_group] |
|
|
|
negative_split_requests = [item.strip() for item in exclude_request.split(',')] |
|
perfect_negative_group = [item for item in negative_split_requests if item.startswith('~')] |
|
negative_group = [item for item in negative_split_requests if item not in perfect_negative_group] |
|
|
|
if '' in split_requests: |
|
split_requests.remove('') |
|
if '' in negative_split_requests: |
|
negative_split_requests.remove('') |
|
|
|
|
|
if split_requests: |
|
|
|
if normal_group: |
|
df = filter_rows_containing_all_keywords(df, normal_group) |
|
if(len(df) == 0): |
|
return None |
|
|
|
|
|
if curly_brace_group: |
|
for keyword in curly_brace_group: |
|
or_search_keyword = [item.strip() for item in keyword[1:-1].split('|')] |
|
results = pd.DataFrame() |
|
for keyword in or_search_keyword: |
|
if keyword.startswith('*'): |
|
keyword = keyword[1:] |
|
for column in ['copyright', 'character', 'artist', 'meta', 'general']: |
|
matched_rows = df[df[column].str.contains(keyword, na=False)] |
|
if not matched_rows.empty: |
|
results = pd.concat([results, matched_rows]) |
|
break |
|
del[[df]] |
|
df = results.copy() |
|
del[[results]] |
|
if(len(df) == 0): |
|
return None |
|
|
|
|
|
if asterisk_group: |
|
df = process_asterisk_group(df,asterisk_group) |
|
if(len(df) == 0): |
|
return None |
|
|
|
|
|
if negative_split_requests: |
|
if negative_group: |
|
df = filter_rows_not_containing_all_keywords(df, negative_group) |
|
if(len(df) == 0): |
|
return None |
|
|
|
if perfect_negative_group: |
|
df = process_perfect_negative_group(df, perfect_negative_group) |
|
if(len(df) == 0): |
|
return None |
|
return df |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|