Plural words into singular.. how?

Discussion in 'PHP' started by d1srupt3r, Jun 10, 2008.

  1. #1
    I have a problem with my dictionary website, when people tried to search for a word which is in plural form, my script cannot find it. Is there any way that I could make the words put into singular so that it could be searched in the database?
     
    d1srupt3r, Jun 10, 2008 IP
  2. commandos

    commandos Notable Member

    Messages:
    3,648
    Likes Received:
    329
    Best Answers:
    0
    Trophy Points:
    280
    #2
    u can solve the "S" issues by removing the last letter and replace by %word% ?
     
    commandos, Jun 10, 2008 IP
  3. d1srupt3r

    d1srupt3r Peon

    Messages:
    79
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    but what will happen if the word naturally has "s" in the end like bus? And how about the singular form of mice? Oh man this is hard.. :(
     
    d1srupt3r, Jun 10, 2008 IP
  4. softvision

    softvision Peon

    Messages:
    146
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Also some plural words have "es" at the end. Interesting problem.
     
    softvision, Jun 11, 2008 IP
  5. commandos

    commandos Notable Member

    Messages:
    3,648
    Likes Received:
    329
    Best Answers:
    0
    Trophy Points:
    280
    #5
    Yeah thats why i told u to add the % which if my memory is good will get any word that start with the query he made .

    you cant have a perfect dictionnary , except if you take all cases , all depend on how your db is made .

    but for the "S" cases if the users search for "discuss" and you remove the last "s" and in the query specify that you want to serach for "discus%" discuss should be returned (also any word that contain discusXXXX)
     
    commandos, Jun 11, 2008 IP
  6. d1srupt3r

    d1srupt3r Peon

    Messages:
    79
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #6
    I do get your point commandos and I appreciate that. Take a look on this sample site here. If you searched for "mice" it will return using "mouse" same as with the "viri". My website is also using the same database(wordnet 3.0) so I guess it is on the php script? Here's my code how to search the query in the db:
    
    $word = preg_replace('%\ $%', '', $_REQUEST['w']);
    
    // Find the word
    $result = mysql_query('SELECT * FROM wn_synset WHERE [B]word="'.str_replace(' ', '_', $word).'"[/B] ORDER BY ss_type ASC');
    $num_result = mysql_numrows($result);
    
    Code (markup):
    So what do you think guys?
     
    d1srupt3r, Jun 11, 2008 IP
  7. commandos

    commandos Notable Member

    Messages:
    3,648
    Likes Received:
    329
    Best Answers:
    0
    Trophy Points:
    280
    #7
    Well from their FAQ :

    q.5.3 Can WordNet generate plural forms and other inflected forms?

    No. The morphological component of the WordNet library is unidirectional. Along with a set of irregular forms (e.g. children - child), it uses a sequence of simple rules, stripping common English endings until it finds a word form present in WordNet. Furthermore, it assumes its input is a valid inflected form. So, it will take "childes" to "child", even though "childes" is not a word
     
    commandos, Jun 11, 2008 IP
  8. d1srupt3r

    d1srupt3r Peon

    Messages:
    79
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #8
    How come I haven't read that one? Anyway, if that is the case? how can you correct the words such us "mice" to "mouse" as what in their online sample? Alright this is the hard part, modifying my php code.. Is there tutorial on php wherein you can remove the "s" in the word just what you have said commandos? Thank for the help pal!
     
    d1srupt3r, Jun 11, 2008 IP
  9. commandos

    commandos Notable Member

    Messages:
    3,648
    Likes Received:
    329
    Best Answers:
    0
    Trophy Points:
    280
    #9
    well you dont even need to remove it , they mention this :

    so i suppose it should work
     
    commandos, Jun 11, 2008 IP
  10. d1srupt3r

    d1srupt3r Peon

    Messages:
    79
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Yes I get it now, but I still need help on my php script. To make the queries found on the database is to delimit letters, for example "devastated" to become "devastate" or "divested" into "divest" and so on and so forth until to find a result in the database. I don't know how to write this in php, I am confused. Can you guys help me to start the editing of my code? Here's my php code for the queries:
    // Find the word
    $slashedword = stripslashes($_REQUEST['w']);
    $word = preg_replace('%\ $%', '', $slashedword);
    
    $result = mysql_query('SELECT * FROM wn_synset WHERE word="'.str_replace(' ', '_', $word).'" ORDER BY ss_type ASC');
    
    Code (markup):
    In the $result variable, how should I make the mysql query check each word with delimited "s" "d" "ed" until to find a result?
     
    d1srupt3r, Jun 11, 2008 IP
  11. commandos

    commandos Notable Member

    Messages:
    3,648
    Likes Received:
    329
    Best Answers:
    0
    Trophy Points:
    280
    #11
    well maybe make a loop for (i=0 till word.length) and each time remove the last char and make the query if it return something return it if it dont continue the loop and remove the last letter .
     
    commandos, Jun 12, 2008 IP
  12. ikipei

    ikipei Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #12
    maybe you can use inflector_helper in codeigniter, or akelos PHP inflector.
     
    ikipei, Jun 12, 2008 IP
  13. d1srupt3r

    d1srupt3r Peon

    Messages:
    79
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #13
    Thanks for the advices commandos, I will try that one. Also thanks to ikipei for the advice, how can I use the inflector_helper? Can you explain to me how can I will be able to integrate it? Like do I have something to install? Does the inflector_helper can also correct those terms in past tense? :)
     
    d1srupt3r, Jun 12, 2008 IP
  14. d1srupt3r

    d1srupt3r Peon

    Messages:
    79
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #14
    Okay I am somehow succesful now and here's my solution:
    
    $slashedword = stripslashes($_REQUEST['w']);
    $neword = preg_replace('%\ $%', '', $slashedword);
    
    //reverse some plural forms to singular
    function trimplural($neword){
    
         $plural_end='os';
         $replace_singular='o';
         if(     substr($neword, -2) == $plural_end) {
              $neword = substr($neword, 0,strlen($neword)-2). $replace_singular;
         }
         
    
         $plural_end='ies';
         $replace_singular='y';
         if(     substr($neword, -3) == $plural_end) {
              $neword = substr($neword, 0,strlen($neword)-3). $replace_singular;
         }
         
         
         $plural_end='xes';
         $replace_singular='x';
         if(     substr($neword, -3) == $plural_end) {
              $neword = substr($neword, 0,strlen($neword)-3). $replace_singular;
         }
         
         $plural_end='oes';
         $replace_singular='o';
         if(     substr($neword, -3) == $plural_end) {
              $neword = substr($neword, 0,strlen($neword)-3). $replace_singular;
         }
         
         $plural_end='ies';
         $replace_singular='y';
         if(     substr($neword, -3) == $plural_end) {
              $neword = substr($neword, 0,strlen($neword)-3). $replace_singular;
         }
         
         $plural_end='ves';
         $replace_singular='fe';
         if(     substr($neword, -3) == $plural_end) {
              $neword = substr($neword, 0,strlen($neword)-3). $replace_singular;
         }
    
         $plural_end='s';
         $replace_singular='';
         if(     substr($neword, -1) == $plural_end && !(substr($neword, -2) == 'ss') ) {    
              $neword = substr($neword, 0,strlen($neword)-1). $replace_singular;
         }
       
         return $neword;
    }
    
    $word = trimplural($neword);
    Code (markup):
    everything works fine now but I still have some problem. It still can't find words which plural form ended in "es" like "bosses" and also words that ended in "ves" like "thieves". I don't know how to fix it but it can now find queries such as "beasts" or "lives". Any help guys? It is almost done with some little problems that needs to fix. :)

    PS
    To test it you can use this url:
    http://www.defineitfast.com/browse/terms.html&new=1

    Just change the bolded text into the word for queries. :)

    Should I still use loop here? or if functions? Thanks for the help guys!
     
    d1srupt3r, Jun 12, 2008 IP