import itertools
import os
import warnings
from core.utils import *
from core.spacy_utils.load_nlp_model import init_nlp, SPLIT_BY_COMMA_FILE, SPLIT_BY_MARK_FILE

warnings.filterwarnings("ignore", category=FutureWarning)

def analyze_comma(start, doc, token):
    left_phrase = doc[max(start, token.i - 9):token.i]
    right_phrase = doc[token.i + 1:min(len(doc), token.i + 10)]
    
    # 基于字符数判定（去除标点），左右各 ≥6 字符则切分
    left_text  = ''.join([t.text for t in left_phrase if not t.is_punct]).strip()
    right_text = ''.join([t.text for t in right_phrase if not t.is_punct]).strip()

    suitable_for_splitting = len(left_text) >= 6 and len(right_text) >= 6

    return suitable_for_splitting

def split_by_comma(text, nlp):
    doc = nlp(text)
    sentences = []
    start = 0
    
    for i, token in enumerate(doc):
        if token.text == "," or token.text == "，":
            suitable_for_splitting = analyze_comma(start, doc, token)
            
            if suitable_for_splitting:
                sentences.append(doc[start:token.i].text.strip())
                rprint(f"[yellow]✂️  Split at comma: {doc[start:token.i][-4:]},| {doc[token.i + 1:][:4]}[/yellow]")
                start = token.i + 1
    
    sentences.append(doc[start:].text.strip())
    return sentences

def split_by_comma_main(nlp):

    with open(SPLIT_BY_MARK_FILE, "r", encoding="utf-8") as input_file:
        sentences = input_file.readlines()

    all_split_sentences = []
    for sentence in sentences:
        split_sentences = split_by_comma(sentence.strip(), nlp)
        all_split_sentences.extend(split_sentences)

    with open(SPLIT_BY_COMMA_FILE, "w", encoding="utf-8") as output_file:
        for sentence in all_split_sentences:
            output_file.write(sentence + "\n")
    
    # delete the original file
    os.remove(SPLIT_BY_MARK_FILE)
    
    rprint(f"[green]💾 Sentences split by commas saved to →  `{SPLIT_BY_COMMA_FILE}`[/green]")

def force_split_by_comma_main():
    """
    强制在每个中文逗号处切分句子，不使用复杂的NLP判断。
    """
    try:
        with open(SPLIT_BY_MARK_FILE, "r", encoding="utf-8") as input_file:
            content = input_file.read()
        
        # 将所有中文逗号替换为换行符，实现强制切分
        content_split = content.replace('，', '\n')
        
        with open(SPLIT_BY_COMMA_FILE, "w", encoding="utf-8") as output_file:
            output_file.write(content_split)
            
        os.remove(SPLIT_BY_MARK_FILE)
        
        rprint(f"[green]💾 Sentences forcefully split by Chinese commas saved to →  `{SPLIT_BY_COMMA_FILE}`[/green]")
    except Exception as e:
        rprint(f"[red]Error in force_split_by_comma_main: {e}[/red]")


if __name__ == "__main__":
    nlp = init_nlp()
    split_by_comma_main(nlp)
    # nlp = init_nlp()
    # test = "So in the same frame, right there, almost in the exact same spot on the ice, Brown has committed himself, whereas McDavid has not."
    # print(split_by_comma(test, nlp))