Someone Needs to Fork BBPress

I really love bbpress, but I have to say how unfortunate it is that the standalone version no longer seems to be supported. I can’t blame the authors though.. I know how it is. You spend all this time writing a bunch of software and it doesn’t get you anywhere except a bunch of people saying things need to be fixed when you already don’t have enough time.

I don’t want to delve into the philosophy behind open source software, but I guess I’m saying if someone is looking to support an open-source project, I’d vote for bbpress standalone. Fantastic forum software. I wish I had the time to maintain it myself, but life is only so long, and there’s only so much time to work on so many things. Who knows. Maybe one day in the future.

Cosine Similarity Calculator

The cosine similarity is a calculation used in data mining.  As far as I’m aware, this is the first and only online cosine similarity calculator.  Sweet.  Enjoy!  :)

Explanation

This Cosine Similarity Calculator will teach you how to calculate the Cosine Similarity (a.k.a. how to calculate the Cosine Measure) of two vectors. Useful for both math homework and data mining.

The Cosine Similarity of two vectors is an arbitrary mathematical measure of how similar two vectors are on a scale of [0, 1]. 1 being that the vectors are either identical, or that their values differ by a constant factor.

The Cosine Similarity of two vectors (d1 and d2) is defined as:
cos( d1, d2 ) = dot(d1,d2) / ||d1|| ||d2||

Where dot(d1,d2) = d1[0]*d2[0] + d1[1]*d2[1] …
And Where ||d1|| = sqrt(d1[0]^2 + d1[1]^2 …)

(Additional Info For Data Miners: The Centroid Similarity Measure is simply the Cosine Measure of your clustering output.  e.g. After clustering some data, if you only have two centroids, to get the Centroid Similarity Measure, you just take the Cosine Measure of the resultant vectors. If you have k centroids though such that k > 2 (and this formula works for k=2 as well), then it is the Summation From i=1 to K(Summation From j=1 to K (Cosine Similarity(Ci,Cj))).

Directions

This is a Cosine Similarity Calculator. There is currently little data validation so make sure your vectors are of equal length, are numeric in type, and with each value separated by a single space. For example ~> “1 2 3″ (without the quote marks)
would be a valid input:

Calculator

Enter Vector 1 (Values Separated by Spaces)
Enter Vector 2 (Values Separated by Spaces)
Your calculations will appear hear after you push the Calculate button!

Using XAJAX with CodeIgniter

If you’re like me, you probably tried just including the php files per the learn xajax in 10 minutes docs and ended up getting some arcane error message in Firebug that reads “[Exception... "'[object Object]‘ when calling method: [nsIDOMEventListener::handleEvent]” nsresult: “0x8057001c (NS_ERROR_XPC_JS_THREW_JS_OBJECT)” location: “<unknown>” data: no] Line 0″

huh?  what am I supposed to do with that?  google turns up nothing… (which is why i’m adding it here)

hmm.. ok over to the CI docs on xajax and there’s an article ( http://codeigniter.com/wiki/XAJAX/ ) .  Perfect!

wait… what is that??  right there on the third line, that little “Note: This doc is obsolete”.  noooo!!

alright, maybe i can still pull this apart.. it kind of looks good, but it’s got a lot of what appears to be unecessary steps (like creating a library loader) and munging the view code into the controller.  i’d like to keep the view code in the view and i don’t need to be messing with library loaders.  let’s try something else:  XAJAX + CodeIgniter in 3… 2… 1…

Install XAJAX
into the funny CodeIgniter directory structure!

  1. Download XAJAX Standard here: http://xajaxproject.org/en/download/ At the time of this writing, the most recent release is 0.5, but it shouldn’t matter much.
  2. Unzip the zip file
  3. Upload the xajax_0.5_standard/xajax_core folder to /www/system/application/xajax_0.5_standard/xajax_core
  4. Upload the xajax_0.5_standard/xajax_js folder to /www/xajax_0.5_standard/xajax_js

I like to leave the version number in the folder names, otherwise, how will I remember a year from now if I need to upgrade?  Or what version to upgrade from?

Notice we had to put the xajax php code with the CI php code, and xajax looks for web stuff based on your url so we have to put it someplace web-accessible which is why we put it in our web root folder.  That’s the first tricky bit.  xajax is nice enough to pop up an error message letting you know if it can’t find the js files it needs (go xajax!  much appreciate that).  I had no need for the other xajax folders as I was just doing simple ajax requests, but i imagine that once you get this working, you can probably deduce where they might belong should a project require them.

Add XAJAX Include Paths
You don’t have to do this, but I did because I think it makes upgrading to a later version of xajax that much easier. I defined the path in a constant by creating a util directory and constants.php file at /www/system/application/util/constants.php with the following code:

<?php
define ("XAJAX_FOLDER", "xajax_0.5_standard");
define ("XAJAX_PHP", APPPATH . XAJAX_FOLDER . '/xajax_core/xajax.inc.php');

I didn’t close my php tag on purpose. You < href="http://codeigniter.com/user_guide/general/styleguide.html#php_closing_tag" target="_blank">shouldn’t close your php tags. Remember that for your next job interview. ;)

When I upgrade xajax later, all I’ll have to do is upload the new version, and change the XAJAX_FOLDER define and not touch any code.

Add XAJAX to the Controller

Pick a controller.. any controller. You can have a special ajax controller, or I would recommend picking the controller for your view (makes sense right? a view requires ajax and its ajax requests are sent to its controller).

Add a constructor to the controller which will initialize xajax. For poipedia I have a controller class View defined in CI at controllers/view.php. The beginning of that controller looks like this:

<?php
 
require_once(APPPATH . 'util/constants.php');
 
require_once(XAJAX_PHP);
 
class View extends Controller {
 
 
	private $xajax = '';
 
    function View() {
        parent::Controller();
        $this->xajax = new xajax();
        $this->xajax->registerFunction(array('addFavorite',&$this,'addFavorite'));
        $this->xajax->processRequest();
    }
 
    function addFavorite()
    {
        // Instantiate the xajaxResponse object
        $objResponse = new xajaxResponse();
        // add a command to the response to assign the innerHTML attribute of
        // the element with id="SomeElementId" to whatever the new content is
        $objResponse->assign("lnkFav","innerHTML", 'xajax response!');
 
        //return the xajaxResponse object
        return $objResponse;
    }
 
 
 
    function poi($id)
    {
        $this->load->model('poi');
        $this->load->model('picture');
        $this->load->model('comment');
 
        $data = array('xajax' => $this->xajax,
        					   'poi' => $this->poi->getPoi($id),
						 'pictures' => $this->picture->getPicturesForPoi($id),
        				 'comments' => $this->comment->getCommentsForPoi($id));
 
        $this->load->view('poiDetail', $data);
    }

Let’s break that down. The important steps are:

We initialize xajax in the constructor of the controller, and assign the xajax object to a class variable which we’ll pass to our view later
The php function we want to call via ajax is located in our controller (in this case “addFavorite”
The $xajax class variable which was initialized by our constructor is passed to our view to be used by the view. (the first array assignment in $data in the poi function–those other things like comments and pictures you probably won’t be using on your site. :) )

Add XAJAX to the View

In my case the view I’m loading in the poi function is poiDetails.php at /system/application/views/poiDetails.php. Here’s the pertinent code from the view:

<?php
require_once(APPPATH . "util/constants.php");
   // the sneaky bit.. tell xajax where to look for it's javascript files (web-accessible of course)
  $xajax->configure('javascript URI', base_url() . XAJAX_FOLDER );
  // this is the name of your controller. my controller was class View in view.php
  // so i have 'view' here.  what is your controller named?
  // this is another very sneaky bit.  we don't let it decide its own URI because
  // that only works if you are coming from the index() function in your
  // controller!!
  $xajax->setRequestURI(site_url('view'));
  // now print what we just configured
  $xajax->printJavascript();
?>
<!-- and finally a link to click on -->
<div id=lnkFav><a href='#' onclick="xajax_addFavorite();return false;">Add to Favorites</a></div>

Don’t forget the return false; after your xajax call and the # sign for the href value or your browser will take you to another page or jump to the top of the current page.

That’s it! There were a couple of sneaky things in there that took me forever to figure out, but hopefully I’ve saved you some measure of the pain I know.

iPhone SDK – How to determine if an NSString is numeric

I was working with the iPhone SDK and needed to determine if an NSString was numeric. After a lot of searching I came up with this function that doesn’t rely on any third party libraries. It uses the numberFromString function from the built-in NSNumberFormatter. Feel free to use it in your applications. :)

/*
    Returns whether or not an NSString represents a numeric value.  
    For more info see:  http://appliedsoftwaredesign.com/blog/iphone-sdk-nsstring-numeric/
*/
-(bool) isNumeric:(NSString*) checkText
{

	NSNumberFormatter* numberFormatter = [[[NSNumberFormatter alloc] init] autorelease];
	
	NSNumber* number = [numberFormatter numberFromString:checkText];
	
	if (number != nil) {
		NSLog(@"%@ is numeric", checkText);
		return true;
	}
	
	NSLog(@"%@ is not numeric", checkText);
        return false;
}

You can invoke it like:

[self isNumeric:@"2"];
[self isNumeric:@"2.0"];
[self isNumeric:@"4"];
[self isNumeric:@"-1"];
[self isNumeric:@"asdf"];

and it will return output like:

2009-10-09 10:27:03.656 App[16351:207] 2 is numeric
2009-10-09 10:27:03.658 App[16351:207] 2.0 is numeric
2009-10-09 10:27:03.659 App[16351:207] 4 is numeric
2009-10-09 10:27:03.659 App[16351:207] -1 is numeric
2009-10-09 10:27:03.660 App[16351:207] asdf is not numeric

Don’t forget to declare the prototype in your .h file:

-(bool) isNumeric:(NSString*) checkText;

Enjoy!

Manually Rounding a Number

How do you manually round a number in computer sciencet? I had this question once too, and here’s how someone showed me how to do it:

1.)  Add 0.5 to the number

2.)  Typecast to an integer.

In code this looks like:

double x = 12345.6789
int y = (int) (x + 0.5);

Hope that helps!