How to add Twitter Bootstrap to a Symfony 2 application

Note: the following assumes you are not using gulp, grunt or another workflow tool. If you are, and want to include downloading and compiling Bootstrap and JQuery into your workflow, see their websites for more information on how to do this.

  1. Download the latest version of Bootstrap from http://getbootstrap.com/.
  2. If you extract the zip file, you will notice 3 folders:
    |-css
    |-fonts
    |-js
    
  3. Copy these into your web directory so that your web directory looks like the following:

    |-web
        |-css
        |-fonts
        |-js
    
  4. Download jquery minified version from https://jquery.com/
  5. Save this file into your web/js directory
  6. In you app/Resources/views/base.html.twig file, add the following:
    <!-- in head tag -->
    <link rel="stylesheet" href="{{ asset('css/bootstrap.css') }}" />
    <link rel="stylesheet" href="{{ asset('css/bootstrap-theme.css') }}" />
    
    <!-- at end of body tag -->
    <script src="{{ asset('js/jquery.min.js') }}"></script>
    <script src="{{ asset('js/bootstrap.min.js') }}"></script>
    

    If you are using the default base.html.twig file, it will look like this:

    <!DOCTYPE html>
    <html>
        <head>
            <meta charset="UTF-8" />
            <title>{% block title %}Welcome!{% endblock %}</title>
            {% block stylesheets %}{% endblock %}
            <link rel="icon" type="image/x-icon" href="{{ asset('favicon.ico') }}" />
            <link rel="stylesheet" href="{{ asset('css/bootstrap.css') }}" />
            <link rel="stylesheet" href="{{ asset('css/bootstrap-theme.css') }}" />
        </head>
        <body>
            <h1>app/Resources/view/base.html.twig</h1>
             {% block nav %}{% endblock %}
            {% block body %}{% endblock %}
            <script src="{{ asset('js/jquery.min.js') }}"></script>
            <script src="{{ asset('js/bootstrap.min.js') }}"></script>
            {% block javascripts %}{% endblock %}
        </body>
    </html>
    

Bonus:
Symfony includes a form theme for Bootstrap out of the box. To enable Bootstrap in your forms without having to create custom templates, simply add the following to your app/config/config.yml file:

twig:
    form:
        resources: ['bootstrap_3_layout.html.twig']
        # resources: ['bootstrap_3_horizontal_layout.html.twig']

Source: http://symfony.com/blog/new-in-symfony-2-6-bootstrap-form-theme

Markov Cluster Algorithm (MCL) with PHP

For a recent project, I had the need to cluster groups of Twitter users. After researching several algorithms, I decided to implement Markov Clustering to see how it would go.

I wrote this in PHP as the rest of the project was in PHP and it was a good test to see if I could implement the algorithm from scratch.

The algorithm is detailed here: http://micans.org/mcl/ and an example is available here which explain it much better than I could.

The basic steps are:

  1. Create matrix representing the nodes in your graph as rows and columns with the elements representing the paths between them.
  2. Add self loops to the nodes
  3. Normalise the Matrix
  4. Expand by taking the (Expansion Parameter)th power of the matrix
  5. Inflate by taking inflation of the resulting matrix with the inflation parameter
  6. Repeat steps 5 and 6 until a steady state is reached (convergence).
  7. Interpret resulting matrix to discover clusters.

The algorithm takes two parameters in addition to the matrix, an expansion parameter (which the matrix will be raised to the power of) and an inflation parameter (which all elements of the matrix will be raised to the power of).

Once the algorithm has completed, you should end up with a sparse Matrix (full of mostly 0) and then can interpret groups by looking at rows and columns that have values at their intersection (implying they are in the same cluster).

Code is available on GitHub at https://github.com/jrowlandsnz/mcl-php