-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Darshan Sawardekar
committed
Jul 24, 2018
1 parent
232669d
commit 4bf8456
Showing
38 changed files
with
5,332 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# This file is for unifying the coding style for different editors and IDEs | ||
# editorconfig.org | ||
|
||
# WordPress Coding Standards | ||
# https://make.wordpress.org/core/handbook/coding-standards/ | ||
|
||
root = true | ||
|
||
[*] | ||
charset = utf-8 | ||
end_of_line = lf | ||
insert_final_newline = true | ||
trim_trailing_whitespace = true | ||
indent_style = tab | ||
indent_size = 4 | ||
|
||
[{.jshintrc,*.json,*.yml}] | ||
indent_style = space | ||
indent_size = 2 | ||
|
||
[{*.txt,wp-config-sample.php}] | ||
end_of_line = crlf |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,126 @@ | ||
# klasifai | ||
Classify WordPress Content using Natural Language Processing APIs | ||
## Klasifai | ||
|
||
Classify WordPress Content using [IBM Watson Natural Language Processing API](https://www.ibm.com/watson/services/natural-language-understanding/). | ||
|
||
## Features | ||
|
||
* Classify Post Content using IBM Watson NLU API | ||
* Supports Watson Categories, Keywords, Concepts & Entities | ||
* Bulk Classify Posts | ||
* Automatically classify content on save | ||
|
||
## Installation | ||
|
||
#### 1. Download or Clone this repo | ||
|
||
#### 2. Activate Plugin | ||
|
||
#### 3. Configure IBM Watson API Keys under Settings > Klasifai | ||
|
||
#### 4. Configure Post Types unde Settings > Klasifai | ||
|
||
#### 5. Save Post or run WP CLI command to batch classify posts | ||
|
||
## WP CLI | ||
|
||
#### 1. Batch Classify Posts | ||
|
||
$ wp klasifai post {post_ids} [--post_type=post_type] [--limit=limit] [--link=link] | ||
|
||
[--post_type=post_type] | ||
Batch classify posts belonging to this post type. If false | ||
relies on post_ids in args | ||
--- | ||
default: false | ||
options: | ||
- any other post type name | ||
- false, if args contains post_ids | ||
--- | ||
|
||
[--limit=limit] | ||
Limit classification to N posts. | ||
--- | ||
default: false | ||
options: | ||
- false, no limit | ||
- N, max number of posts to classify | ||
--- | ||
|
||
[--link=link] | ||
Whether to link classification results to Taxonomy terms | ||
--- | ||
default: true | ||
options: | ||
- bool, any bool value | ||
--- | ||
|
||
#### 2. Classify Text | ||
|
||
wp klasifai text {text} [--category=bool] [--keyword=bool] [--concept=bool] [--entity=bool] [--input=input] [--only-normalize=bool] | ||
|
||
Directly classify text using Watson NLU. | ||
|
||
Options | ||
|
||
[--category=bool] | ||
Enables NLU category feature | ||
--- | ||
default: true | ||
options: | ||
- any boolean value | ||
--- | ||
|
||
[--keyword=bool] | ||
Enables NLU keyword feature | ||
--- | ||
default: true | ||
options: | ||
- any boolean value | ||
--- | ||
|
||
[--concept=bool] | ||
Enables NLU concept feature | ||
--- | ||
default: true | ||
options: | ||
- any boolean value | ||
--- | ||
|
||
[--entity=bool] | ||
Enables NLU entity feature | ||
--- | ||
default: true | ||
options: | ||
- any boolean value | ||
--- | ||
|
||
[--input=input] | ||
Path to input file or URL | ||
--- | ||
default: false | ||
options: | ||
- path to local file | ||
- path to remote URL | ||
- false, uses args[0] instead | ||
--- | ||
|
||
[--only-normalize=<bool>] | ||
Prints the normalized text that will be sent to the NLU API | ||
--- | ||
default: false | ||
options: | ||
- any boolean value | ||
--- | ||
|
||
## Contributing | ||
|
||
1. Clone the repo | ||
2. Create Pull Request against the master branch. | ||
3. Fix failing tests if any. | ||
|
||
## License | ||
|
||
Klasifai is free software; you can redistribute it and/or modify it | ||
under the terms of the GNU General Public License as published by the | ||
Free Software Foundation; either version 2 of the License, or (at your | ||
option) any later version. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
<?php | ||
|
||
namespace Klasifai; | ||
|
||
/** | ||
* PSR-4 Autoloader: | ||
* https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-4-autoloader-examples.md#class-example | ||
*/ | ||
class Psr4AutoloaderClass | ||
{ | ||
/** | ||
* An associative array where the key is a namespace prefix and the value | ||
* is an array of base directories for classes in that namespace. | ||
* | ||
* @var array | ||
*/ | ||
protected $prefixes = array(); | ||
|
||
/** | ||
* Register loader with SPL autoloader stack. | ||
* | ||
* @return void | ||
*/ | ||
public function register() | ||
{ | ||
spl_autoload_register(array($this, 'loadClass')); | ||
} | ||
|
||
/** | ||
* Adds a base directory for a namespace prefix. | ||
* | ||
* @param string $prefix The namespace prefix. | ||
* @param string $base_dir A base directory for class files in the | ||
* namespace. | ||
* @param bool $prepend If true, prepend the base directory to the stack | ||
* instead of appending it; this causes it to be searched first rather | ||
* than last. | ||
* @return void | ||
*/ | ||
public function addNamespace($prefix, $base_dir, $prepend = false) | ||
{ | ||
// normalize namespace prefix | ||
$prefix = trim($prefix, '\\') . '\\'; | ||
|
||
// normalize the base directory with a trailing separator | ||
$base_dir = rtrim($base_dir, DIRECTORY_SEPARATOR) . '/'; | ||
|
||
// initialize the namespace prefix array | ||
if (isset($this->prefixes[$prefix]) === false) { | ||
$this->prefixes[$prefix] = array(); | ||
} | ||
|
||
// retain the base directory for the namespace prefix | ||
if ($prepend) { | ||
array_unshift($this->prefixes[$prefix], $base_dir); | ||
} else { | ||
array_push($this->prefixes[$prefix], $base_dir); | ||
} | ||
} | ||
|
||
/** | ||
* Loads the class file for a given class name. | ||
* | ||
* @param string $class The fully-qualified class name. | ||
* @return mixed The mapped file name on success, or boolean false on | ||
* failure. | ||
*/ | ||
public function loadClass($class) | ||
{ | ||
// the current namespace prefix | ||
$prefix = $class; | ||
|
||
// work backwards through the namespace names of the fully-qualified | ||
// class name to find a mapped file name | ||
while (false !== $pos = strrpos($prefix, '\\')) { | ||
|
||
// retain the trailing namespace separator in the prefix | ||
$prefix = substr($class, 0, $pos + 1); | ||
|
||
// the rest is the relative class name | ||
$relative_class = substr($class, $pos + 1); | ||
|
||
// try to load a mapped file for the prefix and relative class | ||
$mapped_file = $this->loadMappedFile($prefix, $relative_class); | ||
if ($mapped_file) { | ||
return $mapped_file; | ||
} | ||
|
||
// remove the trailing namespace separator for the next iteration | ||
// of strrpos() | ||
$prefix = rtrim($prefix, '\\'); | ||
} | ||
|
||
// never found a mapped file | ||
return false; | ||
} | ||
|
||
/** | ||
* Load the mapped file for a namespace prefix and relative class. | ||
* | ||
* @param string $prefix The namespace prefix. | ||
* @param string $relative_class The relative class name. | ||
* @return mixed Boolean false if no mapped file can be loaded, or the | ||
* name of the mapped file that was loaded. | ||
*/ | ||
protected function loadMappedFile($prefix, $relative_class) | ||
{ | ||
// are there any base directories for this namespace prefix? | ||
if (isset($this->prefixes[$prefix]) === false) { | ||
return false; | ||
} | ||
|
||
// look through base directories for this namespace prefix | ||
foreach ($this->prefixes[$prefix] as $base_dir) { | ||
|
||
// replace the namespace prefix with the base directory, | ||
// replace namespace separators with directory separators | ||
// in the relative class name, append with .php | ||
$file = $base_dir | ||
. str_replace('\\', '/', $relative_class) | ||
. '.php'; | ||
|
||
// if the mapped file exists, require it | ||
if ($this->requireFile($file)) { | ||
// yes, we're done | ||
return $file; | ||
} | ||
} | ||
|
||
// never found it | ||
return false; | ||
} | ||
|
||
/** | ||
* If a file exists, require it from the file system. | ||
* | ||
* @param string $file The file to require. | ||
* @return bool True if the file exists, false if not. | ||
*/ | ||
protected function requireFile($file) | ||
{ | ||
if (file_exists($file)) { | ||
require $file; | ||
return true; | ||
} | ||
return false; | ||
} | ||
} | ||
|
||
// instantiate the loader | ||
$klasifai_loader = new \Klasifai\Psr4AutoloaderClass(); | ||
|
||
// register the autoloader | ||
$klasifai_loader->register(); | ||
|
||
// register the base directories for the namespace prefix | ||
$klasifai_loader->addNamespace('Klasifai', __DIR__ . '/includes/Klasifai' ); | ||
|
||
require_once( __DIR__ . '/includes/Klasifai/Helpers.php' ); |
Oops, something went wrong.