Thesaurus class or API for PHP [edited]

杀马特。学长 韩版系。学妹 提交于 2019-12-05 00:36:04

You could try to leverage PostgreSQL's full text search functionality:

http://www.postgresql.org/docs/9.0/static/textsearch.html

You can configure it with any of the available languages and all sorts of collations to fit your needs. PostgreSQL 9.1 adds some extra collation functionality that you may want to look into if the approach seems reasonable.

The basic steps would be (for each language):

  1. Create the needed table (collated appropriately). For our sake, a single column is enough, e.g.:

    create table dict_en (
      word text check (word = lower(word)) primary key
    );
    
  2. Fetch the needed dictionary/thesaurus files (those from aspell/Open-Office should work).

  3. Configure text search (see link above, namely section 12.6) using the relevant files.

  4. Insert the whole dictionary into the table. (Surely there's a csv file somewhere...)

  5. And finally index the vector, e.g.:

    create index on dict_en using gin (to_tsvector('english', word));
    

You can now run queries that use this index:

-- Find words related to `:word`
select word
from dict_en
where to_tsvector('english', word) @@ plainto_tsquery('english', :word)
and word <> :word;

You might need to create a separate database or schema for each language, and add an additional field (tsvector) if Postgres refuses to index the expression because of the language parameter. (I read the full text docs a long time ago). The details on this would be in section 12.2, and I'm sure you'll know how to adjust the above if this is the case.

Whichever the implementation details, though, I believe the approach should work.

There is a PHP example for a thesaurus API usage here...

http://thesaurus.altervista.org/testphp

Available for Italian, English, French, Deutsch, Spanish and Portuguese.

This seems to be an option, though I'm not sure whether its multilingual: http://developer.dictionary.com/products/synonyms

I also found the following site which does something similar to your end goal, maybe you could try contacting the owner and ask him how he did it: http://www.synonymlab.com/

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!