SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    SitePoint Zealot
    Join Date
    Aug 2003
    Location
    MN
    Posts
    178
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    English to Boolean Query

    MySQL supports booleans operators in its fulltext search... The following operators / groupers are accepted

    +
    -
    <>
    ~
    *
    "()

    Does anyone have a function that will turn

    (apple AND pie) OR (shortcake) AND "good recipe"

    into

    ( +apple +pie ) ( shortcake ) +" good recipe "

    ?

  2. #2
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    mysql supports "AND" and "OR" as boolean operators.

  3. #3
    SitePoint Zealot
    Join Date
    Aug 2003
    Location
    MN
    Posts
    178
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by freakysid
    mysql supports "AND" and "OR" as boolean operators.
    Could you provide a reference to that? According to http://dev.mysql.com/doc/mysql/en/Fulltext_Boolean.html "AND" and "OR" are not treated any differantly than literals.

  4. #4
    SitePoint Zealot
    Join Date
    Aug 2003
    Location
    MN
    Posts
    178
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by freakysid
    mysql supports "AND" and "OR" as boolean operators.
    Btw, this was put in the PHP forum because it requires a PHP algorithmn

  5. #5
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hehe - sorry about that - I shouldn't try and mod the forums at 1:30 AM on a Sudnay night.

  6. #6
    SitePoint Wizard
    Join Date
    May 2003
    Location
    Berlin, Germany
    Posts
    1,829
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmm...no i don't have such a function, but why don't you do one yourself?

  7. #7
    SitePoint Zealot
    Join Date
    Aug 2003
    Location
    MN
    Posts
    178
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by DarkAngelBGE
    Hmm...no i don't have such a function, but why don't you do one yourself?
    Why reinvent the wheel?

  8. #8
    SitePoint Addict yjanse's Avatar
    Join Date
    Apr 2004
    Location
    The Netherlands
    Posts
    240
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by tabula_rasa
    Why reinvent the wheel?
    To learn how it's built.
    Yves Janse:
    Android42.net - PHP5 Bookcase project - Developers Blog
    | PHP | MySQL | CSS | XML | XHTML | AJAX | Patterns |

  9. #9
    SitePoint Addict yjanse's Avatar
    Join Date
    Apr 2004
    Location
    The Netherlands
    Posts
    240
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It can be done with normal MySQL, check here:

    http://dev.mysql.com/doc/mysql/en/Lo...Operators.html

    Although I prefer || and && (from a C/C++ background)
    Yves Janse:
    Android42.net - PHP5 Bookcase project - Developers Blog
    | PHP | MySQL | CSS | XML | XHTML | AJAX | Patterns |

  10. #10
    SitePoint Zealot
    Join Date
    Aug 2003
    Location
    MN
    Posts
    178
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by yjanse
    It can be done with normal MySQL, check here:

    http://dev.mysql.com/doc/mysql/en/Lo...Operators.html

    Although I prefer || and && (from a C/C++ background)
    This is for the fulltext (not "like") functionality of mysql. unfortunately, those won't work

  11. #11
    SitePoint Zealot
    Join Date
    Aug 2003
    Location
    MN
    Posts
    178
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by yjanse
    To learn how it's built.
    I can just read source code and learn how it is built. I guess I will just have to program it myself though.

  12. #12
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You need to write a small syntax parser for 'natural language' expression. A small example:

    PHP Code:
    // tokenize
    function tokenize($src){
        global 
    $tok$pos;
        
    $tok=array();
        
    preg_replace('~(".*?")|(AND)|(OR)|(\()|(\))|(\w+)~e','$tok[]="$0"',$src);
        
    $pos=0;
    }

    // return current token
    function ctoken(){
        global 
    $tok$pos;
        if(
    $pos>=count($tok))
            return 
    FALSE;
        return 
    $tok[$pos];
    }

    // advance pointer
    function eat() {
        global 
    $pos;
        
    $pos++;
    }

    // parse expression "x AND y"
    function expr() {
        
    $m[]=term();
        while(
    ctoken() == 'AND'){
            
    eat();
            
    $m[]=term();
        }
        return 
    " +".implode(" +",$m);
    }

    // parse term "x OR y"
    function term() {
        
    $m[]=factor();
        while(
    ctoken() == 'OR'){
            
    eat();
            
    $m[]=factor();
        }
        return 
    implode(" ",$m);
    }

    // parse primitive x or (expr)
    function factor() {
        
    $c=ctoken();
        if(
    $c =='('){
            
    eat();
            
    $e=expr();
            if(
    ctoken()!=')') die(") expected");
            
    eat();
            return 
    "($e)";
        } 
        if(
    $c !== FALSE) {
            
    eat();
            return 
    $c;
        } 
        return 
    "";
    }

    // test
    $src=<<<EE
    (apple AND pie) OR (shortcake) AND "good recipe"
    EE;

    tokenize($src);
    $query=expr();
    echo 
    $query


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •