Pages

Search This Blog

Thursday 1 October 2015

Installing Vowpal Wabbit Machine Learning Library on Ubuntu


Installing the prerequisites for Vowpal Wabbit 



sudo apt-get -y install build-essential
sudo apt-get -y install automake
sudo apt-get -y install autoconf
sudo apt-get -y install libxmu-dev
sudo apt-get -y install gcc
sudo apt-get -y install libpthread-stubs0-dev
sudo apt-get -y install libtool
sudo apt-get -y install libboost-program-options-dev
sudo apt-get -y install zlib1g-dev
sudo apt-get -y install libc6
sudo apt-get -y install libgcc1
sudo apt-get -y install libstdc++6


Configure Environment


export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib

Install vowpal_wabbit


Note: If git is not installed then first install it using command : sudo apt-get install git

git clone https://github.com/JohnLangford/vowpal_wabbit.git
cd vowpal_wabbit
./autogen.sh
./configure
make
sudo make install

Tuesday 16 June 2015

Implementation Of Decorator Design Pattern in PHP (Practical Example)

<?php

/*
 *
 * @Author: Zuhair Mirza
 * @Implementation Of Decorator Design Pattern in PHP (Practical Example)
 * @Date : 16-June-2015
 * @Defination  :  The decorator pattern (also known as Wrapper, an alternative naming
 * shared with the Adapter pattern) is a design pattern that allows behavior to be added
 * to an individual object, either statically or dynamically, without affecting the behavior
 * of other objects from the same class.
 *
 * Every application we have needs some sort of alteration and/or improvements at uniform intervals.
 * So in such a case we can implement the decorator design pattern and it will ultimately improve
 * code quality and make our code more extendable.
 */


interface eMailBody {
    public function loadBody();
}

class eMail implements eMailBody {
    public function loadBody() {
        echo "This is Main Email body.<br />";
    }
}

abstract class emailBodyDecorator implements eMailBody {
   
    protected $emailBody;
   
    public function __construct(eMailBody $emailBody) {
        $this->emailBody = $emailBody;
    }
   
    abstract public function loadBody();
   
}

class christmasEmailBody extends emailBodyDecorator {
   
    public function loadBody() {
       
        echo 'This is Extra Content for Christmas<br />';
        $this->emailBody->loadBody();
       
    }
   
}

class newYearEmailBody extends emailBodyDecorator {

    public function loadBody() {
       
        echo 'This is Extra Content for New Year.<br />';
        $this->emailBody->loadBody();
       
    }

}

/*
 *  Running Code
 */

$email = new eMail();
$email->loadBody();

// Output
//This is Main Email body.


/*
 *  Email with Xmas Greetings
 */

$email = new eMail();
$email = new christmasEmailBody($email);
$email->loadBody();

// Output
//This is Extra Content for Christmas
//This is Main Email body.

/*
 *  Email with New Year Greetings
 */

$email = new eMail();
$email = new newYearEmailBody($email);
$email->loadBody();


// Output
//This is Extra Content for New Year.
//This is Main Email body.

/*
 *  Email with Xmas and New Year Greetings
 */

$email = new eMail();
$email = new christmasEmailBody($email);
$email = new newYearEmailBody($email);
$email->loadBody();

// Output
//This is Extra Content for New Year.
//This is Extra Content for Christmas
//This is Main Email body.

Monday 15 June 2015

Implementation Of Oberver Design Pattern in PHP (Practical Example)

<?php

/*
 *
 * @Author: Zuhair Mirza
 * @Implementation Of Oberver Design Pattern in PHP (Practical Example)
 * @Date : 15-June-2015
 * @Defination  :  An object is made observable by adding a method that allows
 * another object, the observer to get registered. If the observable object gets changed,
 * it sends a message to the objects which are registered as observers:
 *
 */

interface Observer {

    function onChanged($sender, $args);
}

interface Observable {

    function addObserver($observer);
}

class CustomerList implements Observable {

    private $_observers = array();

    public function addCustomer($name) {
        foreach ($this->_observers as $obs)
            $obs->onChanged($this, $name);
    }

    public function addObserver($observer) {
        $this->_observers [] = $observer;
    }

    public function getObserver() {

        return $this->_observers;
    }

}

class CustomerListLogger implements Observer {

    public function onChanged($sender, $args) {
        echo( "'$args' Customer has been added to the list \n" );
    }

}

$ul = new CustomerList();

$ul->addObserver(new CustomerListLogger());
$ul->addCustomer("Zuhair");   // Output : 'Zuhair' Customer has been added to the list
$ul->addCustomer("Mirza");    // Output : 'Mirza'  Customer has been added to the list

var_dump($ul->getObserver()); // Output :  array(1) { [0]=> object(CustomerListLogger)#2 (0) { } }

Implementation Of Registry Design Pattern in PHP (Practical Example)

<?php

/*
 *
 * @Author: Zuhair Mirza
 * @Implementation Of Registry Design Pattern in PHP (Practical Example)
 * @Date : 15-June-2015
 * @Defination  :  This pattern is a bit unusual from the overall list, because it is not a Creational pattern. Well, register – it is hash, and you access to data through the static methods.
 *
 */

/**
* Registry class
*/

class Registry {

    protected static $data = array();

    public static function set($key, $value) {
        self::$data[$key] = $value;
    }

    public static function get($key) {
        return isset(self::$data[$key]) ? self::$data[$key] : null;
    }

    final public static function removeObject($key) {
        if (array_key_exists($key, self::$data)) {
            unset(self::$data[$key]);
        }
    }
}

Registry::set('name', 'Registry Name');

print_r(Registry::get('name'));

// Output: Registry Name
?>

Friday 12 June 2015

Implementation Of Strategy Design Pattern in PHP (Practical Example)

<?php

/*
 *
 * @Author: Zuhair Mirza
 * @Implementation Of Strategy Design Pattern in PHP (Practical Example)
 * @Date : 12-June-2015
 * @Defination  :  In computer programming, the strategy pattern (also known as the policy pattern) is a
 * software design pattern that enables an algorithm's behavior to be selected at runtime. The strategy pattern
 * defines a family of algorithms,
 * encapsulates each algorithm, and
 * makes the algorithms interchangeable within that family.
 *
 * In the Strategy Pattern a context will choose the appropriate concrete extension of a class interface.
 *
 * In this example, the StrategyContext class will set a strategy of StrategyCaps,
 * StrategyExclaim, or StrategyStars depending on a paramter StrategyContext receives
 * at instantiation. When the showName() method is called in StrategyContext it will call
 * the showName() method in the Strategy that it set.
 *
 */

class StrategyContext {
   
    private $strategy = NULL;
    // list is not instantiated at construct time
    public function __construct($strategy_ind_id) {
        switch ($strategy_ind_id) {
            case "C":
                $this->strategy = new StrategyCaps();
            break;
            case "E":
                $this->strategy = new StrategyExclaim();
            break;
            case "S":
                $this->strategy = new StrategyStars();
            break;
        }
    }
   
    public function showBookTitle($book) {
      return $this->strategy->showTitle($book);
    }
}

interface StrategyInterface {
    public function showTitle($book_in);
}

class StrategyCaps implements StrategyInterface {
    public function showTitle($book_in) {
        $title = $book_in->getTitle();
        $this->titleCount++;
        return strtoupper ($title);
    }
}

class StrategyExclaim implements StrategyInterface {
    public function showTitle($book_in) {
        $title = $book_in->getTitle();
        $this->titleCount++;
        return Str_replace(' ','!',$title);
    }
}

class StrategyStars implements StrategyInterface {
    public function showTitle($book_in) {
        $title = $book_in->getTitle();
        $this->titleCount++;
        return Str_replace(' ','*',$title);
    }
}

class Book {
   
    private $author;
    private $title;
   
    function __construct($title_in, $author_in) {
        $this->author = $author_in;
        $this->title  = $title_in;
    }
   
    function getAuthor() {
        return $this->author;
    }
   
    function getTitle() {
        return $this->title;
    }
   
    function getAuthorAndTitle() {
        return $this->getTitle() . ' by ' . $this->getAuthor();
    }
}

  writeln('BEGIN TESTING STRATEGY PATTERN');
  writeln('');

  $book = new Book('Strategy Design Patern for PHP','Zuhair Mirza');

  $strategyContextC = new StrategyContext('C');
  $strategyContextE = new StrategyContext('E');
  $strategyContextS = new StrategyContext('S');

  writeln('Sample 1 ---> show name context C');
  writeln($strategyContextC->showBookTitle($book));
  writeln('');

  writeln('Sample 2 ---> show name context E');
  writeln($strategyContextE->showBookTitle($book));
  writeln('');

  writeln('Sample 3 ---> show name context S');
  writeln($strategyContextS->showBookTitle($book));
  writeln('');

  writeln('END TESTING STRATEGY PATTERN');

  function writeln($line_in) {
    echo $line_in."<br/>";
  }

Implementation Of Factory Design Pattern in PHP (Practical Example)



<?php

/*
 *
 * @Author: Zuhair Mirza
 * @Implementation Of Factory Design Pattern in PHP (Practical Example)
 * @Date : 12-June-2015
 * @Defination  :  * One of the most commonly used design patterns is the factory pattern. In this pattern,
 * a class simply creates the object you want to use.
 *
 */

class Automobile
{
    private $vehicleMake;
    private $vehicleModel;

    public function __construct($make, $model)
    {
        $this->vehicleMake = $make;
        $this->vehicleModel = $model;
    }

    public function getMakeAndModel()
    {
        return $this->vehicleMake . ' ' . $this->vehicleModel;
    }
}

class AutomobileFactory
{
    public static function create($make, $model)
    {
        return new Automobile($make, $model);
    }
}

// Automobile Factory create the Automobile object

$veyron = AutomobileFactory::create('Mercedes ', 'Sedans');

print_r($veyron->getMakeAndModel()); // Output :  "Mercedes Sedans"

Implementation Of Singleton Design Pattern in PHP (Practical Example)




<?php

/*
 *
 * @Author: Zuhair Mirza
 * @Implementation Of Singleton Design Pattern in PHP (Practical Example)
 * @Date : 12-June-2015
 * @Defination  : In software engineering, the singleton pattern is a design pattern that
 * restricts the instantiation of a class to one object. This is useful when exactly one
 * object is needed to coordinate actions across the system.
 */

/*
 *   Singleton classes
 */

class BookSingleton {

    private $author = 'Zuhair Mirza Informative';
    private $title = 'Design Patterns';
    private static $book = NULL;
    private static $isLoanedOut = FALSE;

    private function __construct() {
       
    }

    static function borrowBook() {
        if (FALSE == self::$isLoanedOut) {
            if (NULL == self::$book) {
                self::$book = new BookSingleton();
            }
            self::$isLoanedOut = TRUE;
            return self::$book;
        } else {
            return NULL;
        }
    }

    function returnBook(BookSingleton $bookReturned) {
        self::$isLoanedOut = FALSE;
    }

    function getAuthor() {
        return $this->author;
    }

    function getTitle() {
        return $this->title;
    }

    function getAuthorAndTitle() {
        return $this->getTitle() . ' by ' . $this->getAuthor();
    }

}

class BookBorrower {

    private $borrowedBook;
    private $haveBook = FALSE;

    function __construct() {
       
    }

    function getAuthorAndTitle() {
        if (TRUE == $this->haveBook) {
            return $this->borrowedBook->getAuthorAndTitle();
        } else {
            return "I don't have the book";
        }
    }

    function borrowBook() {
        $this->borrowedBook = BookSingleton::borrowBook();
        if ($this->borrowedBook == NULL) {
            $this->haveBook = FALSE;
        } else {
            $this->haveBook = TRUE;
        }
    }

    function returnBook() {
        $this->borrowedBook->returnBook($this->borrowedBook);
    }

}

/*
 *   Initialization
 */

writeln('BEGIN TESTING SINGLETON PATTERN');
writeln('');

$bookBorrower1 = new BookBorrower();
$bookBorrower2 = new BookBorrower();

$bookBorrower1->borrowBook();
writeln('BookBorrower1 asked to borrow the book');
writeln('BookBorrower1 Author and Title: ');
writeln($bookBorrower1->getAuthorAndTitle());
writeln('');

$bookBorrower2->borrowBook();
writeln('BookBorrower2 asked to borrow the book');
writeln('BookBorrower2 Author and Title: ');
writeln($bookBorrower2->getAuthorAndTitle());
writeln('');

$bookBorrower1->returnBook();
writeln('BookBorrower1 returned the book');
writeln('');

$bookBorrower2->borrowBook();
writeln('BookBorrower2 Author and Title: ');
writeln($bookBorrower1->getAuthorAndTitle());
writeln('');

writeln('END TESTING SINGLETON PATTERN');

function writeln($line_in) {
    echo $line_in . '<br/>';
}

?>