I’m trying to run an elemental full-text search algorithm coded in C# using Visual Studio


I coded a full-text search algorithm in C#, which I want to run on Visual Studio Code 2022, by pasting it on a file. The code is the following:

using System;
using System.Collections.Generic;
using System.Linq;

public class FullTextSearch
{
    private readonly Dictionary<int, string> _documents = new Dictionary<int, string>();

    // Add a document to the index
    public void AddDocument(int id, string content)
    {
        _documentshttps://stackoverflow.com/q/79722801 = content ?? string.Empty;
    }

    // Basic full-text search
    public List<int> Search(string query)
    {
        if (string.IsNullOrWhiteSpace(query))
            return new List<int>();

        string[] queryTerms = query
            .ToLower()
            .Split(new[] { ' ', '\t', '\r', '\n', ',', '.', '!', '?' },
                   StringSplitOptions.RemoveEmptyEntries);

        var results = new List<int>();

        foreach (var doc in _documents)
        {
            string content = doc.Value.ToLower();
            bool allTermsFound = queryTerms.All(term => content.Contains(term));

            if (allTermsFound)
                results.Add(doc.Key);
        }

        return results;
    }
}

public class Program
{
    public static void Main()
    {
        var searchEngine = new FullTextSearch();

        // Adding documents
        searchEngine.AddDocument(1, "The quick brown fox jumps over the lazy dog.");
        searchEngine.AddDocument(2, "C# is a modern programming language developed by Microsoft.");
        searchEngine.AddDocument(3, "Foxes are wild animals.");

        // Search queries
        var result1 = searchEngine.Search("fox");
        var result2 = searchEngine.Search("modern language");
        var result3 = searchEngine.Search("lazy dog");

        Console.WriteLine("Results for 'fox': " + string.Join(", ", result1));
        Console.WriteLine("Results for 'modern language': " + string.Join(", ", result2));
        Console.WriteLine("Results for 'lazy dog': " + string.Join(", ", result3));
    }
}

What I’m trying to do in VS 2022 is to create a new file, and then I select the “C# Class” type of file. Then I paste the code in the newly opened file, but when I do this, VS2022 does not give me the option to run it.

Leave a Reply

Your email address will not be published. Required fields are marked *