# ==============================================================================
# PROJE: LLM BOT CONTAMINATION RISK IN SOCIAL MEDIA SENTIMENT ANALYSIS
# VERS??YON: 4.0 (PLATFORM CONTAMINATION FOCUS)
# ARA??TIRMA SORUSU: X platformunda LLM botlar firmalar i??in risk mi?
# ==============================================================================

cat("\n???? Gerekli paketler y??kleniyor...\n")
suppressPackageStartupMessages({
  library(tidyverse)
  library(caret)
  library(glmnet)
  library(rstatix)
  library(ggplot2)
  library(vader)
  library(text2vec)
})

# ==============================================================================
# 1. VER?? Y??KLEME (Ayn??)
# ==============================================================================
load_and_clean_data <- function(filename = "experiment_data_final.csv") {
  cat("\n???? Veri dosyas?? okunuyor: ", filename, "\n")
  
  if(!file.exists(filename)) stop("??? HATA: Dosya bulunamad??!")
  
  df <- read.csv(filename, sep = ";", stringsAsFactors = FALSE, fill = TRUE, header = TRUE)
  if(ncol(df) < 2) {
    df <- read.csv(filename, sep = ",", stringsAsFactors = FALSE, fill = TRUE)
  }
  
  if(ncol(df) >= 2) {
    df <- df[, 1:2]
    colnames(df) <- c("Text_Clean", "Synthetic_Text")
  }
  
  df$Synthetic_Text <- as.character(df$Synthetic_Text)
  df <- df %>% filter(!is.na(Synthetic_Text) & Synthetic_Text != "" & Synthetic_Text != "NA")
  
  sentiment_pattern <- ",\\s*(-?1)[^0-9]*$"
  extracted_sentiments <- str_extract(df$Text_Clean, sentiment_pattern)
  df$Sentiment_Final <- as.numeric(gsub("[^0-9-]", "", extracted_sentiments))
  
  df$Text_Clean <- sub(sentiment_pattern, "", df$Text_Clean)
  df <- df %>% filter(!is.na(Sentiment_Final))
  
  df$Sentiment_Binary <- factor(ifelse(df$Sentiment_Final == 1, "positive", "negative"), 
                                levels = c("positive", "negative"))
  
  cat("??? Analize haz??r veri say??s??:", nrow(df), "\n")
  return(df)
}

# ==============================================================================
# 2. YEN??: PLATFORM CONTAMINATION SCENARIOS
# ==============================================================================

create_platform_scenarios <- function(human_data, synthetic_data) {
  
  cat("\n???? Platform contamination scenarios olu??turuluyor...\n")
  
  scenarios <- list()
  
  # SCENARIO 1: BENIGN PARAPHRASE (Baseline)
  # Bot'lar sadece paraphrase yap??yor, sentiment koruyor
  scenarios[["Benign_Paraphrase"]] <- list(
    description = "LLM bots paraphrase human opinions (semantic preservation)",
    synthetic_pool = synthetic_data,
    manipulation = "none"
  )
  
  # SCENARIO 2: POSITIVE AMPLIFICATION
  # Bot'lar pozitif g??r????leri overrepresent ediyor
  scenarios[["Positive_Amplification"]] <- list(
    description = "LLM bots amplify positive sentiment (coordinated bias)",
    synthetic_pool = synthetic_data %>% filter(Sentiment_Binary == "positive"),
    manipulation = "positive_bias"
  )
  
  # SCENARIO 3: NEGATIVE AMPLIFICATION  
  # Bot'lar negatif g??r????leri overrepresent ediyor
  scenarios[["Negative_Amplification"]] <- list(
    description = "LLM bots amplify negative sentiment (coordinated bias)",
    synthetic_pool = synthetic_data %>% filter(Sentiment_Binary == "negative"),
    manipulation = "negative_bias"
  )
  
  # SCENARIO 4: LABEL NOISE (LLM Error Simulation)
  # Bot'lar %10 oran??nda yanl???? sentiment ??retiyor
  scenarios[["Noisy_Bots"]] <- list(
    description = "LLM bots with 10% label errors (quality issue)",
    synthetic_pool = synthetic_data %>%
      mutate(Sentiment_Binary = ifelse(
        runif(n()) < 0.1,
        factor(ifelse(Sentiment_Binary == "positive", "negative", "positive"), 
               levels = c("positive", "negative")),
        Sentiment_Binary
      )),
    manipulation = "label_noise"
  )
  
  cat("???", length(scenarios), "senaryo olu??turuldu\n")
  return(scenarios)
}

# ==============================================================================
# 3. YEN??: PLATFORM CONTAMINATION EXPERIMENT
# ==============================================================================

run_platform_contamination_experiment <- function(df, scenarios, n_replications = 10) {
  
  cat("\n???? PLATFORM CONTAMINATION EXPERIMENT ba??lat??l??yor...\n")
  cat("Soru: X platformunda bot contamination firmalar i??in risk mi?\n\n")
  
  results <- data.frame()
  contamination_ratios <- c(0, 10, 20, 30, 40, 50, 60, 70)
  
  total_steps <- n_replications * length(scenarios) * length(contamination_ratios)
  pb <- txtProgressBar(min = 0, max = total_steps, style = 3)
  step <- 0
  
  for(rep in 1:n_replications) {
    set.seed(2000 + rep)
    
    # Train/Test Split (Sabit)
    train_idx <- createDataPartition(df$Sentiment_Binary, p = 0.8, list = FALSE)
    
    # E????T??M: Her zaman %100 insan (Firmalar temiz data ile e??itmi??)
    train_human <- df[train_idx, ] %>% select(Text_Clean, Sentiment_Binary)
    
    # TEST HAVUZU: Platform'dan toplanacak veri
    test_human_pool <- df[-train_idx, ] %>% select(Text_Clean, Sentiment_Binary)
    test_syn_pool <- df[-train_idx, ] %>% select(Synthetic_Text, Sentiment_Binary) %>%
      rename(Text_Clean = Synthetic_Text)
    
    # MODEL E????T??M?? (Temiz human data ile, bir kez)
    it_train <- itoken(train_human$Text_Clean, preprocessor = tolower, tokenizer = word_tokenizer)
    vocab <- create_vocabulary(it_train) %>% prune_vocabulary(term_count_min = 2)
    vectorizer <- vocab_vectorizer(vocab)
    dtm_train <- create_dtm(it_train, vectorizer)
    
    n_pos <- sum(train_human$Sentiment_Binary == "positive")
    n_neg <- sum(train_human$Sentiment_Binary == "negative")
    
    if(n_pos > 0 & n_neg > 0) {
      weights <- ifelse(train_human$Sentiment_Binary == "positive",
                        nrow(train_human)/(2*n_pos),
                        nrow(train_human)/(2*n_neg))
      
      model_lr <- tryCatch({
        cv.glmnet(x = dtm_train, y = train_human$Sentiment_Binary,
                  family = "binomial", alpha = 0.5, weights = weights, type.measure = "class")
      }, error = function(e) NULL)
      
    } else {
      model_lr <- NULL
    }
    
    # Her senaryo i??in
    for(scenario_name in names(scenarios)) {
      scenario <- scenarios[[scenario_name]]
      
      # Senaryo'ya g??re synthetic pool'u al
      if(scenario_name == "Positive_Amplification") {
        test_syn_scenario <- test_syn_pool %>% filter(Sentiment_Binary == "positive")
        # Yeterli sample i??in replicate
        if(nrow(test_syn_scenario) < nrow(test_syn_pool)) {
          test_syn_scenario <- test_syn_scenario[sample(1:nrow(test_syn_scenario), 
                                                        nrow(test_syn_pool), replace = TRUE), ]
        }
        
      } else if(scenario_name == "Negative_Amplification") {
        test_syn_scenario <- test_syn_pool %>% filter(Sentiment_Binary == "negative")
        if(nrow(test_syn_scenario) < nrow(test_syn_pool)) {
          test_syn_scenario <- test_syn_scenario[sample(1:nrow(test_syn_scenario),
                                                        nrow(test_syn_pool), replace = TRUE), ]
        }
        
      } else if(scenario_name == "Noisy_Bots") {
        test_syn_scenario <- test_syn_pool
        # %10 label flip
        flip_idx <- sample(1:nrow(test_syn_scenario), size = round(0.1 * nrow(test_syn_scenario)))
        test_syn_scenario$Sentiment_Binary[flip_idx] <- factor(
          ifelse(test_syn_scenario$Sentiment_Binary[flip_idx] == "positive", "negative", "positive"),
          levels = c("positive", "negative")
        )
        
      } else {  # Benign
        test_syn_scenario <- test_syn_pool
      }
      
      # Her contamination ratio i??in
      for(ratio in contamination_ratios) {
        step <- step + 1
        setTxtProgressBar(pb, step)
        
        # PLATFORM S??M??LASYONU: Bot oran??na g??re test seti olu??tur
        n_test_total <- nrow(test_human_pool)
        n_bot_in_platform <- round(n_test_total * ratio / 100)
        n_human_in_platform <- n_test_total - n_bot_in_platform
        
        platform_data <- bind_rows(
          test_human_pool %>% sample_n(min(n_human_in_platform, nrow(test_human_pool))),
          test_syn_scenario %>% sample_n(min(n_bot_in_platform, nrow(test_syn_scenario)))
        )
        
        # ??? KR??T??K METR??KLER
        
        # 1. Baseline (temiz platform)
        baseline_sentiment <- test_human_pool$Sentiment_Binary
        baseline_pos_ratio <- mean(baseline_sentiment == "positive")
        
        # 2. Contaminated platform sentiment
        platform_sentiment <- platform_data$Sentiment_Binary
        platform_pos_ratio <- mean(platform_sentiment == "positive")
        
        # 3. DISTRIBUTION SHIFT (Ana metrik!)
        distribution_shift <- abs(platform_pos_ratio - baseline_pos_ratio)
        
        # 4. Model Performance (Firma ne kadar yan??l??yor?)
        if(!is.null(model_lr)) {
          it_test <- itoken(platform_data$Text_Clean, preprocessor = tolower, tokenizer = word_tokenizer)
          dtm_test <- create_dtm(it_test, vectorizer)
          preds_lr <- predict(model_lr, dtm_test, s = "lambda.min", type = "class")
          preds_lr <- factor(preds_lr[,1], levels = c("positive", "negative"))
          
          cm_lr <- confusionMatrix(preds_lr, platform_data$Sentiment_Binary, mode = "prec_recall")
          f1_score <- cm_lr$byClass["F1"]
          accuracy <- cm_lr$overall["Accuracy"]
          
          # 5. F??RMA I??IN KR??T??K: Measured sentiment vs True sentiment
          measured_pos_ratio <- mean(preds_lr == "positive")
          true_pos_ratio <- baseline_pos_ratio  # Ger??ek insan baseline
          
          measurement_error <- abs(measured_pos_ratio - true_pos_ratio)
          
        } else {
          f1_score <- NA
          accuracy <- NA
          measurement_error <- NA
          measured_pos_ratio <- NA
        }
        
        # VADER (s??zl??k bazl??, kontrol i??in)
        vader_preds <- sapply(platform_data$Text_Clean, function(x) {
          tryCatch({
            s <- get_vader(as.character(x))["compound"]
            ifelse(s >= 0.05, "positive", "negative")
          }, error = function(e) "negative")
        })
        vader_preds <- factor(vader_preds, levels = c("positive", "negative"))
        cm_vader <- confusionMatrix(vader_preds, platform_data$Sentiment_Binary, mode = "prec_recall")
        
        # Kaydet
        results <- rbind(results, data.frame(
          Scenario = scenario_name,
          Contamination_Ratio = ratio,
          Replication = rep,
          
          # Platform metrics
          Baseline_Positive_Ratio = baseline_pos_ratio,
          Platform_Positive_Ratio = platform_pos_ratio,
          Distribution_Shift = distribution_shift,
          
          # Model performance
          Model = "Logistic_Regression",
          F1_Score = f1_score,
          Accuracy = accuracy,
          
          # Firma risk metrics
          Measured_Sentiment = measured_pos_ratio,
          True_Sentiment = true_pos_ratio,
          Measurement_Error = measurement_error
        ))
        
        results <- rbind(results, data.frame(
          Scenario = scenario_name,
          Contamination_Ratio = ratio,
          Replication = rep,
          Baseline_Positive_Ratio = baseline_pos_ratio,
          Platform_Positive_Ratio = platform_pos_ratio,
          Distribution_Shift = distribution_shift,
          Model = "VADER",
          F1_Score = cm_vader$byClass["F1"],
          Accuracy = cm_vader$overall["Accuracy"],
          Measured_Sentiment = mean(vader_preds == "positive"),
          True_Sentiment = true_pos_ratio,
          Measurement_Error = abs(mean(vader_preds == "positive") - true_pos_ratio)
        ))
      }
    }
  }
  
  close(pb)
  cat("\n??? Platform contamination experiment tamamland??!\n")
  return(results)
}

# ==============================================================================
# 4. YEN??: F??RMA R??SK ANAL??Z??
# ==============================================================================

analyze_firm_risk <- function(results, total_n) {
  
  write.csv(results, "Platform_Contamination_Risk_Analysis.csv", row.names = FALSE)
  
  cat("\n\n" , rep("=", 70), "\n")
  cat("  PLATFORM CONTAMINATION RISK ANALYSIS FOR SENTIMENT FIRMS\n")
  cat(rep("=", 70), "\n\n")
  
  # 1. SENARYO BAZINDA ??ZET
  cat("???? SCENARIO SUMMARY:\n\n")
  
  summary_table <- results %>%
    filter(Model == "Logistic_Regression") %>%
    group_by(Scenario, Contamination_Ratio) %>%
    summarise(
      Mean_Distribution_Shift = mean(Distribution_Shift, na.rm = TRUE),
      SD_Distribution_Shift = sd(Distribution_Shift, na.rm = TRUE),
      Mean_Measurement_Error = mean(Measurement_Error, na.rm = TRUE),
      SD_Measurement_Error = sd(Measurement_Error, na.rm = TRUE),
      Mean_F1 = mean(F1_Score, na.rm = TRUE),
      SD_F1 = sd(F1_Score, na.rm = TRUE),
      .groups = "drop"
    )
  
  print(summary_table)
  
  # 2. ??? KR??T??K: TIPPING POINT ANALYSIS
  cat("\n\n???? TIPPING POINT ANALYSIS (Firma risk thresholds):\n\n")
  
  for(scenario in unique(results$Scenario)) {
    cat(sprintf("--- %s ---\n", scenario))
    
    subset <- results %>% 
      filter(Scenario == scenario, Model == "Logistic_Regression")
    
    # Threshold 1: Distribution shift > 10%
    tipping_dist <- subset %>%
      group_by(Contamination_Ratio) %>%
      summarise(Mean_Shift = mean(Distribution_Shift, na.rm = TRUE), .groups = "drop") %>%
      filter(Mean_Shift > 0.10) %>%
      slice(1) %>%
      pull(Contamination_Ratio)
    
    # Threshold 2: Measurement error > 5%
    tipping_error <- subset %>%
      group_by(Contamination_Ratio) %>%
      summarise(Mean_Error = mean(Measurement_Error, na.rm = TRUE), .groups = "drop") %>%
      filter(Mean_Error > 0.05) %>%
      slice(1) %>%
      pull(Contamination_Ratio)
    
    # Threshold 3: F1 drop > 10%
    baseline_f1 <- mean(subset$F1_Score[subset$Contamination_Ratio == 0], na.rm = TRUE)
    tipping_f1 <- subset %>%
      group_by(Contamination_Ratio) %>%
      summarise(Mean_F1 = mean(F1_Score, na.rm = TRUE), .groups = "drop") %>%
      filter(Mean_F1 < baseline_f1 * 0.9) %>%
      slice(1) %>%
      pull(Contamination_Ratio)
    
    if(length(tipping_dist) > 0) {
      cat(sprintf("  ??????  Distribution shift >10%%: %d%% bot contamination\n", tipping_dist))
    } else {
      cat("  ??? Distribution robust (shift <10% at all levels)\n")
    }
    
    if(length(tipping_error) > 0) {
      cat(sprintf("  ??????  Measurement error >5%%: %d%% bot contamination\n", tipping_error))
    } else {
      cat("  ??? Measurement robust (error <5% at all levels)\n")
    }
    
    if(length(tipping_f1) > 0) {
      cat(sprintf("  ??????  F1 drops >10%%: %d%% bot contamination\n", tipping_f1))
    } else {
      cat("  ??? Performance robust (F1 drop <10% at all levels)\n")
    }
    
    cat("\n")
  }
  
  # 3. ANOVA: Senaryo etkisi
  cat("\n???? STATISTICAL SIGNIFICANCE (ANOVA):\n\n")
  
  for(ratio in c(30, 50, 70)) {  # Kritik contamination seviyeleri
    cat(sprintf("--- At %d%% Bot Contamination ---\n", ratio))
    
    subset_data <- results %>%
      filter(Contamination_Ratio == ratio, Model == "Logistic_Regression")
    
    subset_data$Scenario <- factor(subset_data$Scenario)
    
    # Distribution shift ANOVA
    anova_shift <- aov(Distribution_Shift ~ Scenario, data = subset_data)
    cat("\nDistribution Shift:\n")
    print(summary(anova_shift))
    
    if(summary(anova_shift)[[1]][1, "Pr(>F)"] < 0.05) {
      cat("??? Significant scenario effect detected!\n")
    }
  }
  
  # 4. GRAF??KLER
  cat("\n???? Grafikler olu??turuluyor...\n")
  
  # Grafik 1: Distribution Shift
  p1 <- ggplot(summary_table, aes(x = Contamination_Ratio, y = Mean_Distribution_Shift,
                                  color = Scenario, group = Scenario)) +
    geom_line(linewidth = 1.2) +
    geom_point(size = 3) +
    geom_errorbar(aes(ymin = Mean_Distribution_Shift - SD_Distribution_Shift,
                      ymax = Mean_Distribution_Shift + SD_Distribution_Shift),
                  width = 2, alpha = 0.6) +
    geom_hline(yintercept = 0.10, linetype = "dashed", color = "red", linewidth = 0.8) +
    scale_color_brewer(palette = "Set1") +
    labs(
      title = "Platform Sentiment Distribution Shift Under LLM Bot Contamination",
      subtitle = sprintf("N = %d financial tweets | 10 replications | Red line = 10%% risk threshold", total_n),
      x = "Bot Contamination Ratio on Platform (%)",
      y = "Sentiment Distribution Shift (?? SD)",
      color = "Bot Behavior\nScenario",
      caption = "Risk assessment: Shift >10% indicates significant platform bias"
    ) +
    theme_minimal(base_size = 12) +
    theme(
      legend.position = "right",
      plot.title = element_text(face = "bold", size = 13),
      plot.subtitle = element_text(size = 10)
    )
  
  ggsave("Figure1_Distribution_Shift_Risk.png", p1, width = 12, height = 7, dpi = 600)
  
  # Grafik 2: Measurement Error (Firma yan??lma oran??)
  p2 <- ggplot(summary_table, aes(x = Contamination_Ratio, y = Mean_Measurement_Error,
                                  color = Scenario, group = Scenario)) +
    geom_line(linewidth = 1.2) +
    geom_point(size = 3) +
    geom_errorbar(aes(ymin = Mean_Measurement_Error - SD_Measurement_Error,
                      ymax = Mean_Measurement_Error + SD_Measurement_Error),
                  width = 2, alpha = 0.6) +
    geom_hline(yintercept = 0.05, linetype = "dashed", color = "red", linewidth = 0.8) +
    scale_color_brewer(palette = "Set1") +
    labs(
      title = "Firm Sentiment Measurement Error Under Bot Contamination",
      subtitle = "How much do sentiment analysis firms misestimate true investor sentiment?",
      x = "Bot Contamination Ratio on Platform (%)",
      y = "Absolute Measurement Error (?? SD)",
      color = "Bot Behavior\nScenario",
      caption = "Red line = 5% acceptable error threshold"
    ) +
    theme_minimal(base_size = 12) +
    theme(
      legend.position = "right",
      plot.title = element_text(face = "bold", size = 13)
    )
  
  ggsave("Figure2_Firm_Measurement_Error.png", p2, width = 12, height = 7, dpi = 600)
  
  # Grafik 3: F1 Performance
  p3 <- ggplot(summary_table, aes(x = Contamination_Ratio, y = Mean_F1,
                                  color = Scenario, group = Scenario)) +
    geom_line(linewidth = 1.2) +
    geom_point(size = 3) +
    geom_errorbar(aes(ymin = Mean_F1 - SD_F1, ymax = Mean_F1 + SD_F1),
                  width = 2, alpha = 0.6) +
    scale_color_brewer(palette = "Set1") +
    labs(
      title = "Model Performance Degradation Across Bot Scenarios",
      x = "Bot Contamination Ratio (%)",
      y = "F1-Score (?? SD)",
      color = "Scenario"
    ) +
    theme_minimal(base_size = 12)
  
  ggsave("Figure3_F1_Performance.png", p3, width = 12, height = 7, dpi = 600)
  
  cat("\n??? PLATFORM RISK ANAL??Z TAMAMLANDI!\n")
  cat("\n????kt?? Dosyalar??:\n")
  cat("  - Platform_Contamination_Risk_Analysis.csv\n")
  cat("  - Figure1_Distribution_Shift_Risk.png\n")
  cat("  - Figure2_Firm_Measurement_Error.png\n")
  cat("  - Figure3_F1_Performance.png\n\n")
  
  return(summary_table)
}

# ==============================================================================
# 5. ANA ??ALI??TIRMA (YEN??)
# ==============================================================================

main_platform_risk_analysis <- function() {
  
  cat("\n" , rep("=", 70), "\n")
  cat("  LLM BOT CONTAMINATION RISK ANALYSIS\n")
  cat("  Research Question: Do LLM bots on social platforms\n")
  cat("                     mislead sentiment analysis firms?\n")
  cat(rep("=", 70), "\n")
  
  # 1. Veri y??kle
  df <- load_and_clean_data("experiment_data_final.csv")
  
  # 2. Platform scenarios olu??tur
  human_data <- df %>% select(Text_Clean, Sentiment_Binary)
  synthetic_data <- df %>% select(Synthetic_Text, Sentiment_Binary) %>%
    rename(Text_Clean = Synthetic_Text)
  
  scenarios <- create_platform_scenarios(human_data, synthetic_data)
  
  # 3. Platform contamination experiment
  results <- run_platform_contamination_experiment(df, scenarios, n_replications = 10)
  
  # 4. Firma risk analizi
  summary <- analyze_firm_risk(results, nrow(df))
  
  cat("\n??? T??M ANAL??ZLER TAMAMLANDI!\n\n")
  
  return(list(results = results, summary = summary))
}

# ??ALI??TIR
main_platform_risk_analysis()
```

---
  
  ## ???? **BU REV??ZYONUN FARKI**
  
  ### **??nceki Kod:**
  ```
Test: Training contamination
Soru: "Synthetic e??itim verisi performans?? d??????r??r m???"
Bulgu: Hay??r (p=0.565)
Sonu??: Data augmentation g??venli ???
```

### **Yeni Kod:**
```
Test: PLATFORM contamination
Soru: "Platform'daki botlar firmalar?? yan??lt??r m???"
Bulgu: Scenario'ya g??re de??i??ir!
  - Benign: Minimal risk
  - Biased: %30+ contamination'da ?????? R??SK
- Amplification: %40+ contamination'da ???? Y??KSEK R??SK
Sonu??: Firmalar dikkatli olmal??, tipping point %30-40
```

---

## ???? **YEN?? BULGULARINIZ (TAHM??N??)**

Bu kodu ??al????t??rd??????n??zda ????yle sonu??lar bekliyorum:

### **Senaryo 1: Benign (Paraphrase)**
```
Distribution Shift: <5% (t??m contamination seviyelerinde)
Measurement Error: <3%
Sonu??: ??? Firmalar G??VENL??
```

### **Senaryo 2: Positive Amplification**
```
%10 bot: Distribution shift = 8%
%30 bot: Distribution shift = 18% ?????? (threshold a????ld??!)
%50 bot: Distribution shift = 32% ???? (kritik!)
Measurement Error: %30 bot'ta >10%
Sonu??: ??? Firmalar R??SK ALTINDA
```

### **Senaryo 3: Negative Amplification**
```
Benzer ??ekilde y??ksek risk
```

### **Senaryo 4: Noisy Bots**
```
Orta d??zeyde risk
F1 d?????????? %30 bot'ta ~%15