duan32342 2014-04-07 03:31
浏览 22

PHP / MySQL包含搜索中的替代拼写

My PHP search form pulls data from a MySQL database. I am expecting users to sometimes fill the search box with a search term that has a slightly different spelling than my database entry, like "theater" instead of "theater." There are just a few of these that I expect to be very common, so I added an additional row to my database table that contains those alternative spellings, and my PHP search form searches this row of the database as well. It works well, but this will cause a lot of additional work when maintaining the database, so I'm wondering if there's something I can do within my PHP code to search for those predefined alternative spellings (I don't mean to give the user suggested spellings, but I want the search form to return, for example, entries that have "theatre" in it even though the user typed "theater." Is there an easy way to do this (without a search server)?

  • 写回答

1条回答 默认 最新

  • duanfa2014 2014-04-07 03:53
    关注

    Yes you can easily do this work without database search,you need correct spellings so I suggest you do this work from PHP coding instead of databases search...

    You can do this work with PHP Pspell module, PHP Pspell work like android keyboard whenever use type wrong spelling in search box it automatically check that spelling from dictionary and make it correct like if user type "theater" then it automatically correct it with "theatre".

    before starting programming you have to check is Pspell module installed or not

    <?php
    $config_dic= pspell_config_create ('en');
    

    Here is a small function to help you understand how Pspell works:

    <?php
    function orthograph($string)
    {
        // Suggests possible words in case of misspelling
        $config_dic = pspell_config_create('en');
    
        // Ignore words under 3 characters
        pspell_config_ignore($config_dic, 3);
    
        // Configure the dictionary
        pspell_config_mode($config_dic, PSPELL_FAST);
        $dictionary = pspell_new_config($config_dic);
    
        // To find out if a replacement has been suggested
        $replacement_suggest = false;
    
        $string = explode('', trim(str_replace(',', ' ', $string)));
        foreach ($string as $key => $value) {
            if(!pspell_check($dictionary, $value)) {
                $suggestion = pspell_suggest($dictionary, $value);
    
                // Suggestions are case sensitive. Grab the first one.
                if(strtolower($suggestion [0]) != strtolower($value)) {
                    $string [$key] = $suggestion [0];
                    $replacement_suggest = true;
                }
            }
        }
    
        if ($replacement_suggest) {
            // We have a suggestion, so we return to the data.
            return implode('', $string);
        } else {
            return null;
        }
    }
    

    To use this function, it is sufficient to pass to it a string parameter:

    <?php
    $search = $_POST['input'];
    $suggestion_spell = orthograph($search);
    if ($suggestion_spell) {
        echo "Try with this spelling : $suggestion_spell";
    }
    
    $dict = pspell_new ("en");
    if (!pspell_check ($dict, "lappin")) {
        $suggestions = pspell_suggest ($dict, "lappin");
         foreach ($suggestions as $suggestion) {
            echo "Did you mean: $suggestion?<br />";
         }
    }
    // Suggests possible words in case of misspelling
        $config_dic = pspell_config_create('en');
    
        // Ignore words under 3 characters
        pspell_config_ignore($config_dic, 3);
    
        // Configure the dictionary
        pspell_config_mode($config_dic, PSPELL_FAST);
    $dictionary = pspell_new_config($config_dic);
    $config_dic = pspell_config_create ('en');
    pspell_config_personal($config_dic, 'path / perso.pws');
    pspell_config_ignore($config_dic , 2);
    pspell_config_mode($config_dic, PSPELL_FAST);
    $dic = pspell_new_config($config_dic);
    pspell_add_to_personal($dic, "word");
    pspell_save_wordlist($dic);
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算