Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Lucene Stemmer En Ru Laravel Package

nqxcode/lucene-stemmer-en-ru

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation Add the package via Composer:

    composer require nqxcode/lucene-stemmer-en-ru
    
  2. Basic Usage Require the autoloader and instantiate the stemmer:

    require 'vendor/autoload.php';
    use Nqxcode\LuceneStemmer\Stemmer;
    
    $stemmer = new Stemmer();
    
  3. First Use Case Stemming English or Russian words:

    $englishWord = $stemmer->stem('running'); // Output: 'run'
    $russianWord = $stemmer->stem('бегущий');  // Output: 'бег'
    
  4. Integration with Laravel Register the service provider in config/app.php:

    'providers' => [
        // ...
        Nqxcode\LuceneStemmer\LuceneStemmerServiceProvider::class,
    ],
    

    Bind the stemmer in config/services.php (if needed) or use the facade:

    use Nqxcode\LuceneStemmer\Facades\Stemmer;
    
    $stemmedWord = Stemmer::stem('example');
    

Implementation Patterns

Workflows

  1. Search Optimization Use the stemmer in Laravel Scout or custom search logic:

    $searchTerm = Stemmer::stem($request->input('q'));
    $results = Model::search($searchTerm)->get();
    
  2. Text Processing Pipeline Chain stemming with other text transformations:

    $processedText = strtolower($text);
    $processedText = Stemmer::stem($processedText);
    
  3. Database Indexing Store stemmed versions of text in a separate column for faster searches:

    $stemmed = Stemmer::stem($post->content);
    $post->stemmed_content = $stemmed;
    $post->save();
    

Integration Tips

  • Laravel Scout Customizer Extend ScoutEngine to use stemming in search queries:

    public function scopeSearchUsingStemmer($query, $search)
    {
        $stemmedSearch = Stemmer::stem($search);
        return $query->where('stemmed_column', 'like', "%{$stemmedSearch}%");
    }
    
  • Form Request Validation Validate and stem input before processing:

    public function rules()
    {
        return [
            'query' => 'required|string|max:255',
        ];
    }
    
    public function passedValidation()
    {
        $this->merge([
            'stemmed_query' => Stemmer::stem($this->query),
        ]);
    }
    
  • Middleware for Global Stemming Apply stemming to all incoming search queries via middleware:

    public function handle($request, Closure $next)
    {
        if ($request->is('search*')) {
            $request->merge(['q' => Stemmer::stem($request->q)]);
        }
        return $next($request);
    }
    

Gotchas and Tips

Pitfalls

  1. Deprecated Dependencies The package relies on ZendSearch/Lucene, which is outdated. Ensure compatibility with your Laravel version (pre-Laravel 5.5 recommended due to PHP 5.6+ requirements).

  2. Performance Overhead Stemming adds processing time. Cache results for frequent queries:

    $stemmed = Cache::remember("stem_{$word}", now()->addHours(1), function() use ($word) {
        return Stemmer::stem($word);
    });
    
  3. Language Detection The stemmer assumes input language. Explicitly handle mixed-language text:

    if (Text::contains($text, ['а', 'б', 'в'])) {
        // Russian logic
    } else {
        // English logic
    }
    
  4. Edge Cases

    • Empty Input: Handle stem('') to avoid errors.
    • Non-Alphabetic Characters: Strip punctuation before stemming:
      $cleaned = preg_replace('/[^a-zа-я]/u', '', $text);
      $stemmed = Stemmer::stem($cleaned);
      

Debugging

  • Verify Output: Compare stemmed results against known values (e.g., runningrun, бегущийбег).
  • Log Stemming: Temporarily log stemmed terms to debug:
    \Log::debug('Stemmed: ' . Stemmer::stem($input));
    

Extension Points

  1. Custom Stemming Rules Extend the stemmer by subclassing and overriding methods:

    class CustomStemmer extends Stemmer {
        protected function customRule($word) {
            // Add logic here
        }
    }
    
  2. Integration with Laravel Search Create a custom search trait:

    trait UsesStemmedSearch {
        public function scopeStemmedSearch($query, $term) {
            return $query->where('stemmed_column', Stemmer::stem($term));
        }
    }
    
  3. Testing Mock the stemmer in tests to avoid dependency on external libraries:

    $this->app->instance(Stemmer::class, Mockery::mock(Stemmer::class));
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
besmartand-pro/php-quality-config
sentix/ai-chatbot
terminal42/code-quality-tools
codifyo/ts-generator-bundle
testo/fiber
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity