make it so this unity c# script instead of the default pdf being a pdf it will be…
- User
make it so this unity c# script instead of the default pdf being a pdf it will be wikipedia. using UnityEngine; using TMPro; using UnityEngine.UI; using System.Collections.Generic; using System.IO; // System.IO for file path operations using iTextSharp.text.pdf; using iTextSharp.text.pdf.parser; using SFB; // For Standalone File Browser
public class AIResponse_PDF : MonoBehaviour { [Header("UI References")] public TMP_InputField userInputField; // User input field public TMP_Text aiResponseText; // AI response display public Button submitButton; // Submit button public Button pdfUploadButton; // PDF upload button public TMP_Text statusText; // Status display (e.g., PDF loaded)
[Header("PDF Configuration")] public string pdfFileName = "default.pdf"; // The name of the default PDF file in the StreamingAssets folder private string extractedPDFText = ""; private void Start() { // Add event listeners submitButton.onClick.AddListener(OnSubmit); pdfUploadButton.onClick.AddListener(OnUploadPDF); // Load the default PDF from StreamingAssets folder string defaultPdfPath = System.IO.Path.Combine(Application.streamingAssetsPath, pdfFileName); if (File.Exists(defaultPdfPath)) { ExtractTextFromPDF(defaultPdfPath); } else { statusText.text = "Default PDF file not found in StreamingAssets."; Debug.LogError("Default PDF file not found: " + defaultPdfPath); } } /// <summary> /// Handles the user submitting a query. /// </summary> private void OnSubmit() { string userInput = userInputField.text.Trim().ToLower(); if (string.IsNullOrEmpty(userInput)) { aiResponseText.text = "Please enter a query."; return; } if (string.IsNullOrEmpty(extractedPDFText)) { aiResponseText.text = "PDF content is not available."; return; } string response = ProcessQuery(userInput); aiResponseText.text = response; userInputField.text = string.Empty; } /// <summary> /// Handles the manual PDF upload process. /// </summary> private void OnUploadPDF(){ // Define file extensions for PDF var extensions = new[] { new ExtensionFilter("PDF Files", "pdf"), new ExtensionFilter("All Files", "*" ), };
// Open the file browser dialog var paths = StandaloneFileBrowser.OpenFilePanel("Select PDF", "", extensions, false); // If the user selects a file if (paths.Length > 0 && !string.IsNullOrEmpty(paths[0])) { string filePath = paths[0]; ExtractTextFromPDF(filePath); } // If the user cancels (i.e., no file selected, paths array is empty or null) // Do nothing, no message will be displayed}
/// <summary> /// Extracts text from the selected or default PDF file. /// </summary> private void ExtractTextFromPDF(string filePath) { try { // Ensure the file exists if (!File.Exists(filePath)) { statusText.text = "PDF file not found."; return; } // Open the PDF reader using (PdfReader reader = new PdfReader(filePath)) { StringWriter output = new StringWriter(); // Iterate through each page and extract text for (int i = 1; i <= reader.NumberOfPages; i++) { string text = PdfTextExtractor.GetTextFromPage(reader, i, new LocationTextExtractionStrategy()); output.WriteLine(text); } extractedPDFText = output.ToString(); } // Update status statusText.text = "PDF loaded and text extracted successfully."; aiResponseText.text = "You can now ask questions about the PDF content."; Debug.Log("PDF Text Extracted: " + extractedPDFText.Length + " characters."); } catch (System.Exception ex) { Debug.LogError("Error extracting PDF text: " + ex.Message); statusText.text = "An error occurred while extracting the PDF."; } } /// <summary> /// Processes the user's query by searching the extracted PDF text. /// </summary> private string ProcessQuery(string query) { string[] queryWords = query.Split(' '); string topic = queryWords.Length > 0 ? queryWords[queryWords.Length - 1] : ""; if (string.IsNullOrEmpty(topic)) return "I'm not sure what you're asking. Could you please clarify?"; var sentences = SplitIntoSentences(extractedPDFText); var matchedSentences = sentences.FindAll(s => s.ToLower().Contains(topic)); if (matchedSentences.Count == 0) return $"I couldn't find any information about '{topic}' in the PDF."; int numSentences = Mathf.Min(3, matchedSentences.Count); string response = "Here's what I found:\n\n"; for (int i = 0; i < numSentences; i++) { response += "- " + matchedSentences[i] + "\n"; } return response; } /// <summary> /// Splits a given text into sentences. /// </summary> private List<string> SplitIntoSentences(string text) { char[] delimiters = { '.', '!', '?' }; List<string> sentences = new List<string>(text.Split(delimiters, System.StringSplitOptions.RemoveEmptyEntries)); for (int i = 0; i < sentences.Count; i++) { sentences[i] = sentences[i].Trim(); } return sentences; } - Assistant