Practical উদাহরণ: Lucene দিয়ে Spell Checking এবং Suggestions তৈরি করা

Lucene তে Spell Checking এবং Suggestions - লুসিন (Lucene) - Java Technologies

286

Apache Lucene একটি শক্তিশালী এবং উচ্চ-দক্ষতাসম্পন্ন search library, যা Java ভিত্তিক প্রোজেক্টে ডেটার ইন্ডেক্সিং এবং অনুসন্ধান করার জন্য ব্যবহৃত হয়। এটি শুধু সাধারণ ডেটা অনুসন্ধান নয়, বরং আরও উন্নত ফিচার যেমন Spell Checking এবং Suggestions তৈরি করতেও সক্ষম। এই টিউটোরিয়ালে আমরা দেখব কিভাবে Lucene ব্যবহার করে Spell Checking এবং Suggestions তৈরি করা যায়।

১. Lucene এর Spell Checking এবং Suggestions এর ধারণা

Spell Checking এবং Suggestions এমন দুটি ফিচার যা ব্যবহারকারীর ভুল বানান সংশোধন এবং উন্নত অনুসন্ধান প্রস্তাবনা প্রদান করতে ব্যবহৃত হয়।

  1. Spell Checking: এটি ব্যবহারকারীর প্রেরিত শব্দের বানান ভুল চিহ্নিত করে এবং সঠিক বানান প্রদান করে।
  2. Suggestions: এটি এমন অনুসন্ধান শব্দ প্রদান করে যা ব্যবহারকারীর ইনপুটের সাথে সম্পর্কিত হতে পারে, এমনকি যদি ব্যবহারকারী পুরোপুরি সঠিক শব্দ না লিখে।

Lucene-এর SpellChecker ক্লাস এবং FuzzyQuery ক্লাস ব্যবহার করে এগুলি কার্যকরভাবে বাস্তবায়ন করা যায়।


২. Spell Checking এবং Suggestions তৈরি করতে প্রয়োজনীয় ডিপেনডেন্সি

Lucene ব্যবহার করতে আপনাকে প্রথমে প্রোজেক্টে Lucene এর ডিপেনডেন্সি যোগ করতে হবে। যদি আপনি Maven ব্যবহার করেন, তাহলে pom.xml ফাইলে নিচের ডিপেনডেন্সি যোগ করুন:

Maven Dependency:

<dependencies>
    <!-- Lucene Core -->
    <dependency>
        <groupId>org.apache.lucene</groupId>
        <artifactId>lucene-core</artifactId>
        <version>8.11.0</version>
    </dependency>

    <!-- Lucene Analysis -->
    <dependency>
        <groupId>org.apache.lucene</groupId>
        <artifactId>lucene-analyzers-common</artifactId>
        <version>8.11.0</version>
    </dependency>

    <!-- Lucene QueryParser -->
    <dependency>
        <groupId>org.apache.lucene</groupId>
        <artifactId>lucene-queryparser</artifactId>
        <version>8.11.0</version>
    </dependency>
    
    <!-- Lucene Spellchecker -->
    <dependency>
        <groupId>org.apache.lucene</groupId>
        <artifactId>lucene-spellchecker</artifactId>
        <version>8.11.0</version>
    </dependency>
</dependencies>

৩. Lucene দিয়ে Spell Checking এবং Suggestions তৈরি করার উদাহরণ

এখন, আমরা একটি ছোট Spell Checking এবং Suggestions উদাহরণ তৈরি করব যেখানে আমরা একটি Lucene index তৈরি করব, এবং তার মাধ্যমে ভুল বানান সঠিক করার জন্য SpellChecker ব্যবহার করব।

৩.১ Lucene Index তৈরি করা

প্রথমে, আমাদের Lucene Index তৈরি করতে হবে, যেখানে সমস্ত শব্দগুলো ইনডেক্স করা হবে।

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.TextField;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.store.RAMDirectory;
import org.apache.lucene.store.Directory;

import java.io.IOException;

public class LuceneIndex {
    public static void main(String[] args) throws IOException {
        // Create an in-memory directory to store the index
        Directory directory = new RAMDirectory();

        // Create an analyzer
        StandardAnalyzer analyzer = new StandardAnalyzer();

        // Configure the index writer
        IndexWriterConfig config = new IndexWriterConfig(analyzer);
        IndexWriter indexWriter = new IndexWriter(directory, config);

        // Create a document and add fields
        Document doc1 = new Document();
        doc1.add(new TextField("content", "example", Field.Store.YES));
        Document doc2 = new Document();
        doc2.add(new TextField("content", "sample", Field.Store.YES));

        // Add documents to the index
        indexWriter.addDocument(doc1);
        indexWriter.addDocument(doc2);
        
        // Commit and close the index writer
        indexWriter.commit();
        indexWriter.close();
        
        // Now the index is created in memory
        System.out.println("Index created!");
    }
}

এখানে, আমরা একটি RAMDirectory তৈরি করেছি যেখানে আমাদের ইনডেক্সটি রাখা হবে, এবং StandardAnalyzer ব্যবহার করে ইনডেক্সিংয়ের জন্য কনফিগার করেছি। দুটি ডকুমেন্ট ইনডেক্স করেছি, প্রতিটিতে এক একটি শব্দ (যেমন "example", "sample")।

৩.২ Spell Checker সেটআপ করা

এখন, আমরা SpellChecker ব্যবহার করে বানান যাচাই এবং সঠিক বানান প্রস্তাবনা তৈরির জন্য কনফিগার করব।

import org.apache.lucene.search.spell.DirectSpellChecker;
import org.apache.lucene.store.RAMDirectory;

public class SpellCheckerExample {
    public static void main(String[] args) throws Exception {
        // Create an in-memory index (same as previous)
        RAMDirectory directory = new RAMDirectory();

        // Create and configure SpellChecker
        DirectSpellChecker spellChecker = new DirectSpellChecker();
        spellChecker.indexDictionary(new LuceneDictionary(new DirectoryReader(directory)), "content", 2);

        // Check for spelling mistakes
        String wordToCheck = "exmaple";  // Misspelled word
        String[] suggestions = spellChecker.suggestSimilar(wordToCheck, 5);

        // Display suggestions
        System.out.println("Suggested corrections for '" + wordToCheck + "':");
        for (String suggestion : suggestions) {
            System.out.println(suggestion);
        }
    }
}

এখানে:

  • DirectSpellChecker ব্যবহার করে আমরা RAMDirectory থেকে ইনডেক্সটিতে শব্দগুলোর জন্য বানান যাচাই করছি।
  • ভুল বানান হিসেবে "exmaple" ব্যবহার করেছি, এবং spellChecker.suggestSimilar() ব্যবহার করে সঠিক বানান প্রস্তাবনা নিচ্ছি।

৩.৩ Output (Spell Suggestions)

উপরের কোড চালানোর পর, যদি "exmaple" শব্দটি ইনপুট দেওয়া হয়, তবে স্পেল চেকার এর জন্য suggestions প্রদান করবে, যেমন:

Suggested corrections for 'exmaple':
example
sample

এখানে, "example" সঠিক বানান হিসেবে প্রস্তাব করা হয়েছে, কারণ এটি ইনডেক্সে উপস্থিত।


৪. Lucene দিয়ে Suggestions প্রদান

এখন, আসুন দেখব কীভাবে আমরা Lucene ব্যবহার করে সংশ্লিষ্ট শব্দের জন্য Suggestions প্রদান করতে পারি। আমরা আগেই তৈরি করা Lucene index ব্যবহার করব।

import org.apache.lucene.search.spell.SpellChecker;
import org.apache.lucene.store.RAMDirectory;

public class SuggestionsExample {
    public static void main(String[] args) throws Exception {
        // Create an in-memory directory (same as previous)
        RAMDirectory directory = new RAMDirectory();

        // Create and configure SpellChecker
        SpellChecker spellChecker = new SpellChecker(directory);
        spellChecker.indexDictionary(new LuceneDictionary(new DirectoryReader(directory)), "content", 2);

        // Get suggestions for a given word
        String wordToCheck = "simpel";  // Misspelled word
        String[] suggestions = spellChecker.suggestSimilar(wordToCheck, 3);

        // Display suggestions
        System.out.println("Suggested words for '" + wordToCheck + "':");
        for (String suggestion : suggestions) {
            System.out.println(suggestion);
        }
    }
}

এখানে, spellChecker.suggestSimilar() ব্যবহার করে সংশ্লিষ্ট শব্দের জন্য প্রস্তাবনা পাওয়া যাচ্ছে।


সারাংশ

Lucene দিয়ে Spell Checking এবং Suggestions তৈরি করা একটি গুরুত্বপূর্ণ ফিচার যা ব্যবহারকারীদের জন্য সঠিক এবং প্রাসঙ্গিক ফলাফল প্রদান করতে সহায়তা করে। SpellChecker এবং DirectSpellChecker ক্লাসের মাধ্যমে আমরা বানান যাচাই এবং সংশ্লিষ্ট শব্দের প্রস্তাবনা প্রদান করতে পারি। এই উদাহরণে, আমরা Lucene index তৈরি করে ভুল বানান যাচাই এবং সংশ্লিষ্ট সঠিক বানান প্রস্তাবনা দেখিয়েছি, যা ব্যবহারকারী ইন্টারফেসে সহজেই ইন্টিগ্রেট করা যেতে পারে।


Content added By
Promotion

Are you sure to start over?

Loading...