Skip to content

Commit

Permalink
Modernizing the codebase for use with PHP namespaces and Composer
Browse files Browse the repository at this point in the history
  • Loading branch information
Curtis Farnham committed Jan 24, 2014
1 parent 045eaf0 commit da67ab7
Show file tree
Hide file tree
Showing 18 changed files with 118 additions and 81 deletions.
Empty file modified README 100755 → 100644
Empty file.
12 changes: 12 additions & 0 deletions autoload.php
@@ -0,0 +1,12 @@
<?php

/*
* This file is part of the RNCryptor package.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

require __DIR__.'/lib/PHPInsight/Autoloader.php';

PHPInsight\Autoloader::register();
20 changes: 20 additions & 0 deletions composer.json
@@ -0,0 +1,20 @@
{
"name": "jwhennessey/phpinsight",
"type": "library",
"description": "Sentiment analysis tool for PHP",
"keywords": ["sentiment", "insight", "phpinsight"],
"homepage": "https://github.com/JWHennessey/phpInsight",
"license": "GPLv3 or later",
"authors": [
{
"name": "James Hennessey"
}
],
"require": {
"php": ">=5.3.0",
},
"autoload": {
"psr-0": {"PHPInsight": "lib/"}
}
}

77 changes: 0 additions & 77 deletions example.php

This file was deleted.

30 changes: 30 additions & 0 deletions examples/demo.php
@@ -0,0 +1,30 @@
<?php
if (PHP_SAPI != 'cli') {
echo "<pre>";
}

$strings = array(
1 => 'Weather today is rubbish',
2 => 'This cake looks amazing',
3 => 'His skills are mediocre',
4 => 'He is very talented',
5 => 'She is seemingly very agressive',
6 => 'Marie was enthusiastic about the upcoming trip. Her brother was also passionate about her leaving - he would finally have the house for himself.',
7 => 'To be or not to be?',
);

require_once __DIR__ . '/../autoload.php';
$sentiment = new \PHPInsight\Sentiment();

foreach ($strings as $string) {

// calculations:
$scores = $sentiment->score($string);
$class = $sentiment->categorise($string);

// output:
echo "String: $string\n";
echo "Dominant: $class, scores: ";
print_r($scores);
echo "\n";
}
47 changes: 47 additions & 0 deletions lib/PHPInsight/Autoloader.php
@@ -0,0 +1,47 @@
<?php

namespace PHPInsight;

class Autoloader
{
private $directory;
private $prefix;
private $prefixLength;

/**
* @param string $baseDirectory Base directory where the source files are located.
*/
public function __construct($baseDirectory = __DIR__)
{
$this->directory = $baseDirectory;
$this->prefix = __NAMESPACE__ . '\\';
$this->prefixLength = strlen($this->prefix);
}

/**
* Registers the autoloader class with the PHP SPL autoloader.
*
* @param bool $prepend Prepend the autoloader on the stack instead of appending it.
*/
public static function register($prepend = false)
{
spl_autoload_register(array(new self, 'autoload'), true, $prepend);
}

/**
* Loads a class from a file using its fully qualified name.
*
* @param string $className Fully qualified name of a class.
*/
public function autoload($className)
{
if (0 === strpos($className, $this->prefix)) {
$parts = explode('\\', substr($className, $this->prefixLength));
$filepath = $this->directory.DIRECTORY_SEPARATOR.implode(DIRECTORY_SEPARATOR, $parts).'.php';

if (is_file($filepath)) {
require($filepath);
}
}
}
}
9 changes: 5 additions & 4 deletions sentiment.class.php → lib/PHPInsight/Sentiment.php 100755 → 100644
@@ -1,4 +1,5 @@
<?php
namespace PHPInsight;

/*
phpInsight is a Naive Bayes classifier to calculate sentiment. The program
Expand Down Expand Up @@ -114,7 +115,7 @@ class Sentiment {
*/
public function __construct() {

$this->dataFolder = dirname(__FILE__) . '/data/';
$this->dataFolder = __DIR__ . '/data/';

// Load and cache dictionaries
foreach ($this->classes as $class) {
Expand Down Expand Up @@ -275,7 +276,7 @@ public function setDictionary($class) {
* @param str $string String being broken up
* @return array An array of tokens
*/
public function _getTokens($string) {
private function _getTokens($string) {

// Replace line endings with spaces
$string = str_replace("\r\n", " ", $string);
Expand Down Expand Up @@ -332,7 +333,7 @@ public function getList($type) {
* @param str $string
* @return str
*/
public function _cleanString($string) {
private function _cleanString($string) {

$diac =
/* A */ chr(192) . chr(193) . chr(194) . chr(195) . chr(196) . chr(197) .
Expand All @@ -353,4 +354,4 @@ public function _cleanString($string) {

}

?>
?>
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 4 additions & 0 deletions lib/bootstrap.php
@@ -0,0 +1,4 @@
<?php

require __DIR__ . '/PHPInsight/Autoloader.php';
PHPInsight_Autoloader::register();

0 comments on commit da67ab7

Please sign in to comment.