SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    Jan 2009
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP or JavaScript - interpreting a chat

    Evening all!

    I've hit a brick wall with this problem... can't understand why it's so hard either! I'm hoping that some of you guys will have some ideas...

    I'm trying to write a script in either JavaScript or PHP (doesn't matter which) that will allow you to copy and paste a conversation either from an email or an instant messaging program such as msn or adium. The idea is the script would read the conversation and split it up into it's messages, as well as extract the sender name and the time...

    So for example if I had this conversation:
    gavin holt
    20:50
    this is gavin's 1st message

    rich
    20:50
    this is rich's 1st message

    gavin holt
    20:54
    this is gavin's 2nd message

    54:44
    this is gavin's 3rd message

    54:47
    this is gavin's 4th message

    rich
    20:55
    this is rich's 2nd message

    56:46
    this is rich's 3rd message

    gavin holt
    20:57
    this is gavin's 5th message
    It would be interpreted by the script to this..
    Gavin - 20:50 - this is gavin's 1st message
    Rich - 20:50 - this is rich's 1st message
    Gavin - 20:54 - this is gavin's 2nd message
    Gavin - 20:54 - this is gavin's 3rd message
    Gavin - 20:54 - this is gavin's 4th message
    Rich - 20:55 - this is rich's 2nd message
    Rich - 20:56 - this is rich's 3rd message
    Gavin - 20:57 - this is gavin's 5th message
    Now obviously emails and IM conversations aren't in the same format, so it'd have to be able to cope with a variable input.. so you could specify that the start of each message is in the following format:
    rich
    %TIME%
    ...
    or
    Richard @ %DATETIME%
    ...
    or
    On %DATETIME% Richard wrote:
    ...
    If anyone has any ideas about how to go about this or has seen this done before I'd really appreciate the input.

    Thanks in advance!

  2. #2
    SitePoint Evangelist -T-'s Avatar
    Join Date
    Jun 2002
    Posts
    444
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You will have to create a filter for each format. Then test the content for which filter to use.

    This links pretty close to data migration, which is a pain.
    chrome is a wrapper that combines a browser with spyware

  3. #3
    SitePoint Member
    Join Date
    Jan 2009
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Very true.

    In my eyes there's 2 problems here..

    • The different filters.. I need a way of converting 'Richard @ %DATETIME%' into a regular expression that says 'Richard @ anyword'.
    • The actual process for running the regular expressions against the conversation and extracting the messages

  4. #4
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    I would create 2 elements to the parsing.

    The first would determine which filter to apply, MSN, Yahoo etc.. and then the second would parse the actual data.

    This would be far more extensible.

    PHP Code:
    <?php
    class MessageParserFactory
    {
        private static 
    $aPatterns = array(
            
    'YahooMessageParser'    =>    '~someRegExp~',
            
    'MSNMessageParser'        =>    '~anotherRegExp~'
        
    );
        
        public static function 
    LoadParser($sMessage)
        {
            foreach (
    self::$aPatterns as $sParser => $sRegExp)
            {
                if(
    === preg_match($sRegExp$sMessage))
                {
                    return new 
    $sParser($sMessage);
                }
            }
            return new 
    NullMessageParser();
        }
    }

    $oParser MessageParserFactory::LoadParser('your message to parse');
    ?>
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  5. #5
    SitePoint Member
    Join Date
    Jan 2009
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Another excellent concept - I'll definitely make use of that script.. I didn't think of writing different classes for each format of conversation. It makes things a lot simpler doing it that way!

  6. #6
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Indeed, it would make things a lot cleaner to do it this way. Of course it would need more components, but the benefits greatly out way this.

    PHP Code:
    <?php
    # Load the parser
    $oParser MessageParserFactory::LoadParser($sMessage);

    # Display 3rd message details
    printf('At %s, %s said %s',
        
    $oParser->getMessage(3)->getTime(),
        
    $oParser->getMessage(3)->getName(),
        
    $oParser->getMessage(3)->getMessage()
    );

    # Display 1st message details
    printf('At %s, %s said %s',
        
    $oParser->getMessage(1)->getTime(),
        
    $oParser->getMessage(1)->getName(),
        
    $oParser->getMessage(1)->getMessage()
    );

    #Create a little conversation of sorts
    printf('At %s, %s said %s to %s, who replied %s',
        
    $oParser->getMessage(1)->getTime(),
        
    $oParser->getMessage(1)->getName(),
        
    $oParser->getMessage(1)->getMessage(),
        
    $oParser->getMessage(2)->getName(),
        
    $oParser->getMessage(2)->getMessage()
    );

    #Save 1st message to database
    $sSQL sprintf("INSERT INTO messages (time, name, message)VALUES('%s', '%s', '%s')",
        
    mysql_real_escape_string($oParser->getMessage(1)->getTime()),
        
    mysql_real_escape_string($oParser->getMessage(1)->getName()),
        
    mysql_real_escape_string($oParser->getMessage(1)->getMessage())
    );
    ?>
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.


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
  •