from lime.lime_text import LimeTextExplainer from nltk.tokenize import sent_tokenize from predictors import predict_proba_quillbot def explainer(text): class_names = ['negative', 'positive'] explainer = LimeTextExplainer(class_names=class_names, split_expression=sent_tokenize) exp = explainer.explain_instance(text, predict_proba_quillbot, num_features=20, num_samples=300) sentences = [sent for sent in sent_tokenize(text)] weights_mapping = exp.as_map()[1] sentences_weights = {sentence: 0 for sentence in sentences} for idx, weight in weights_mapping: if 0 <= idx < len(sentences): sentences_weights[sentences[idx]] = weight print(sentences_weights) return sentences_weights def analyze_and_highlight(text): highlighted_text = "" sentences_weights = explainer(text) min_weight = min(sentences_weights.values()) max_weight = max(sentences_weights.values()) for sentence, weight in sentences_weights.items(): normalized_weight = (weight - min_weight) / (max_weight - min_weight) if weight >= 0: color = f'rgba(255, {255 * (1 - normalized_weight)}, {255 * (1 - normalized_weight)}, 1)' else: color = f'rgba({255 * normalized_weight}, 255, {255 * normalized_weight}, 1)' sentence = sentence.strip() if not sentence: continue highlighted_sentence = f'{sentence} ' highlighted_text += highlighted_sentence return highlighted_text