Giter VIP home page Giter VIP logo

Comments (12)

gmfmi avatar gmfmi commented on May 6, 2024 4

Hi @ts-thomas, last year you said:

The next main release will get an improvement of handling all language-specific features.

As FlexSearch is currently in 0.7.0 release candidate, I was wondering if there is some new feature about languages processing.

I recently created a project called SearchinGhost, it is an in-browser search plugins for Ghost CMS powered by FlexSearch. I am really happy with FlexSearch (thank your for the work done!) but I would like to bring multi-lang capabilities. For now, with what I read in the issues, I came up with these language default options:

Latin:

FlexSearch.create({
    encode: "simple",
    tokenize: "forward"
});

Arabic

FlexSearch.create({
    encode: false,
    rtl: true,
    split: /\s+/,
    tokenize: "forward"
});

Cyrilic, indian (any word separated by space language)

FlexSearch.create({
    encode: false,
    split: /\s+/,
    tokenize: "forward"
});

Chinese, Japanese or Korean (with dedicated chars w/o spaces)

FlexSearch.create({
    encode: false,
    tokenize: function(str){
        return str.replace(/[\x00-\x7F]/g, "").split("");
    }
});

Do you think there is any possible improvement/optimisation?

EDIT: I finally found this relevent documentation about the v0.7.0 - https://github.com/nextapps-de/flexsearch/blob/0.7.0/doc/0.7.0.md. Hope this version will be out one day :)

from flexsearch.

ts-thomas avatar ts-thomas commented on May 6, 2024 3

Also take into account to use the "rtl" option for right-to-left support:

var index = FlexSearch.create({
    encode: false,
    rtl: true,
    split: /\s+/,
    tokenize: "forward"
});

from flexsearch.

ts-thomas avatar ts-thomas commented on May 6, 2024 2

@mahnunchik The latest version v0.6.1 has the new option split when creating an index. This makes it possible to use built-in tokenizer like "strict" or "forward" (which is required by the contextual index) and also handle word splitting separately.

var index = FlexSearch.create({
    encode: false,
    split: /\s+/,
    tokenize: "reverse"
});
index.add(0, "Фообар");
var results = index.search("Фообар");
var results = index.search("бар");
var results = index.search("Фоо");

from flexsearch.

mahnunchik avatar mahnunchik commented on May 6, 2024 1

This one works too:

const index = new FlexSearch({
  tokenize: function(str){
    return str.split("");
  }
});

from flexsearch.

mahnunchik avatar mahnunchik commented on May 6, 2024 1

@ts-thomas thank you! It helps me in my task.

Maybe there is sense to use more common split by default? To cover all languages which use space as separator of words.

It will be hard to find root of the problem for string like foobar αβγ with default split option /\W+/.

from flexsearch.

ts-thomas avatar ts-thomas commented on May 6, 2024 1

The next main release will get an improvement of handling all language-specific features. This improvement will also cover splitting words.

from flexsearch.

tareefdev avatar tareefdev commented on May 6, 2024 1

The mentioned solutions here helped me to make FlexSearch indexing Arabic text. Thanks guys.

from flexsearch.

ts-thomas avatar ts-thomas commented on May 6, 2024

Hello. Try this settings: https://github.com/nextapps-de/flexsearch#cjk-word-break-chinese-japanese-korean

var index = FlexSearch.create({
    encode: false,
    tokenize: function(str){
        return str.replace(/[\x00-\x7F]/g, "").split("");
    }
});
index.add(1, "Фообар");
var results = index.search("Фообар");

When you just want to index whole words (not partials) then this may be better:

var index = FlexSearch.create({
    encode: false,
    tokenize: function(str){
        return str.split(/\s+/);
    }
});

from flexsearch.

mahnunchik avatar mahnunchik commented on May 6, 2024

Hello @ts-thomas

Yep, it works with the following options.

const index = new FlexSearch({
  tokenize: function(str){
    return str.replace(/[\x00-\x7F]/g, "").split("");
  }
});

But why it doesn't work with the forward tokenizer?

from flexsearch.

ts-thomas avatar ts-thomas commented on May 6, 2024

The forward tokenizer splits words via regex, but this regex will also remove cyrillic chars actually. The tokenizer needs to be enhanced to use „forward“ with non-latin content.

from flexsearch.

mahnunchik avatar mahnunchik commented on May 6, 2024

It would be really helpful 😉

from flexsearch.

EvanPartidas avatar EvanPartidas commented on May 6, 2024

For some reason, the solution to this in typescript seems to be different. I'm just posting how I've gotten it to work in hopes it will help others.

Note: I'm using "flexsearch": "0.7.11" because without that I was facing this issue while using typescript.

I was not able to follow the FlexSearch.create(< options >) nor the new FlexSearch(< options >) paradigms I saw other's using here due to compile errors. The split field doesn't exist on the new Index(< options >)'s options type so that also caused compile errors. I tried to specify the charset as "cyrillic" and "cyrillic:default" but that also wasn't working.

I found that specifying the encode to a function of your choice is all you need to do.

It seems that the only function that actually matters is the encode function. I looked at the source and I played a bit and it seemed that when you specify an encoder, the tokenize, stemmer, and charset it doesn't affect anything.

Here's my working code:

import { Index } from "flexsearch";
import { PorterStemmerRu, WordTokenizer } from "natural";

const tokenizer = new WordTokenizer();

const index_ru = new Index({
    encode: (str) => {
        let ret = tokenizer.tokenize(str);
        for (let i = 0; i < ret.length; i++) {
            ret[i] = PorterStemmerRu.stem(ret[i]);
        }
        return ret;
    },
});

I'm using the natural library to handle the stemming.

I'm new to this library so please correct any horrible mistakes I've made!

from flexsearch.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.