Skip to content

Commit 4bf8456

Browse files
author
Darshan Sawardekar
committed
Adds Version 1.0.0
1 parent 232669d commit 4bf8456

38 files changed

+5332
-2
lines changed

.editorconfig

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# This file is for unifying the coding style for different editors and IDEs
2+
# editorconfig.org
3+
4+
# WordPress Coding Standards
5+
# https://make.wordpress.org/core/handbook/coding-standards/
6+
7+
root = true
8+
9+
[*]
10+
charset = utf-8
11+
end_of_line = lf
12+
insert_final_newline = true
13+
trim_trailing_whitespace = true
14+
indent_style = tab
15+
indent_size = 4
16+
17+
[{.jshintrc,*.json,*.yml}]
18+
indent_style = space
19+
indent_size = 2
20+
21+
[{*.txt,wp-config-sample.php}]
22+
end_of_line = crlf

README.md

+126-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,126 @@
1-
# klasifai
2-
Classify WordPress Content using Natural Language Processing APIs
1+
## Klasifai
2+
3+
Classify WordPress Content using [IBM Watson Natural Language Processing API](https://www.ibm.com/watson/services/natural-language-understanding/).
4+
5+
## Features
6+
7+
* Classify Post Content using IBM Watson NLU API
8+
* Supports Watson Categories, Keywords, Concepts & Entities
9+
* Bulk Classify Posts
10+
* Automatically classify content on save
11+
12+
## Installation
13+
14+
#### 1. Download or Clone this repo
15+
16+
#### 2. Activate Plugin
17+
18+
#### 3. Configure IBM Watson API Keys under Settings > Klasifai
19+
20+
#### 4. Configure Post Types unde Settings > Klasifai
21+
22+
#### 5. Save Post or run WP CLI command to batch classify posts
23+
24+
## WP CLI
25+
26+
#### 1. Batch Classify Posts
27+
28+
$ wp klasifai post {post_ids} [--post_type=post_type] [--limit=limit] [--link=link]
29+
30+
[--post_type=post_type]
31+
Batch classify posts belonging to this post type. If false
32+
relies on post_ids in args
33+
---
34+
default: false
35+
options:
36+
- any other post type name
37+
- false, if args contains post_ids
38+
---
39+
40+
[--limit=limit]
41+
Limit classification to N posts.
42+
---
43+
default: false
44+
options:
45+
- false, no limit
46+
- N, max number of posts to classify
47+
---
48+
49+
[--link=link]
50+
Whether to link classification results to Taxonomy terms
51+
---
52+
default: true
53+
options:
54+
- bool, any bool value
55+
---
56+
57+
#### 2. Classify Text
58+
59+
wp klasifai text {text} [--category=bool] [--keyword=bool] [--concept=bool] [--entity=bool] [--input=input] [--only-normalize=bool]
60+
61+
Directly classify text using Watson NLU.
62+
63+
Options
64+
65+
[--category=bool]
66+
Enables NLU category feature
67+
---
68+
default: true
69+
options:
70+
- any boolean value
71+
---
72+
73+
[--keyword=bool]
74+
Enables NLU keyword feature
75+
---
76+
default: true
77+
options:
78+
- any boolean value
79+
---
80+
81+
[--concept=bool]
82+
Enables NLU concept feature
83+
---
84+
default: true
85+
options:
86+
- any boolean value
87+
---
88+
89+
[--entity=bool]
90+
Enables NLU entity feature
91+
---
92+
default: true
93+
options:
94+
- any boolean value
95+
---
96+
97+
[--input=input]
98+
Path to input file or URL
99+
---
100+
default: false
101+
options:
102+
- path to local file
103+
- path to remote URL
104+
- false, uses args[0] instead
105+
---
106+
107+
[--only-normalize=<bool>]
108+
Prints the normalized text that will be sent to the NLU API
109+
---
110+
default: false
111+
options:
112+
- any boolean value
113+
---
114+
115+
## Contributing
116+
117+
1. Clone the repo
118+
2. Create Pull Request against the master branch.
119+
3. Fix failing tests if any.
120+
121+
## License
122+
123+
Klasifai is free software; you can redistribute it and/or modify it
124+
under the terms of the GNU General Public License as published by the
125+
Free Software Foundation; either version 2 of the License, or (at your
126+
option) any later version.

autoload.php

+159
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
<?php
2+
3+
namespace Klasifai;
4+
5+
/**
6+
* PSR-4 Autoloader:
7+
* https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-4-autoloader-examples.md#class-example
8+
*/
9+
class Psr4AutoloaderClass
10+
{
11+
/**
12+
* An associative array where the key is a namespace prefix and the value
13+
* is an array of base directories for classes in that namespace.
14+
*
15+
* @var array
16+
*/
17+
protected $prefixes = array();
18+
19+
/**
20+
* Register loader with SPL autoloader stack.
21+
*
22+
* @return void
23+
*/
24+
public function register()
25+
{
26+
spl_autoload_register(array($this, 'loadClass'));
27+
}
28+
29+
/**
30+
* Adds a base directory for a namespace prefix.
31+
*
32+
* @param string $prefix The namespace prefix.
33+
* @param string $base_dir A base directory for class files in the
34+
* namespace.
35+
* @param bool $prepend If true, prepend the base directory to the stack
36+
* instead of appending it; this causes it to be searched first rather
37+
* than last.
38+
* @return void
39+
*/
40+
public function addNamespace($prefix, $base_dir, $prepend = false)
41+
{
42+
// normalize namespace prefix
43+
$prefix = trim($prefix, '\\') . '\\';
44+
45+
// normalize the base directory with a trailing separator
46+
$base_dir = rtrim($base_dir, DIRECTORY_SEPARATOR) . '/';
47+
48+
// initialize the namespace prefix array
49+
if (isset($this->prefixes[$prefix]) === false) {
50+
$this->prefixes[$prefix] = array();
51+
}
52+
53+
// retain the base directory for the namespace prefix
54+
if ($prepend) {
55+
array_unshift($this->prefixes[$prefix], $base_dir);
56+
} else {
57+
array_push($this->prefixes[$prefix], $base_dir);
58+
}
59+
}
60+
61+
/**
62+
* Loads the class file for a given class name.
63+
*
64+
* @param string $class The fully-qualified class name.
65+
* @return mixed The mapped file name on success, or boolean false on
66+
* failure.
67+
*/
68+
public function loadClass($class)
69+
{
70+
// the current namespace prefix
71+
$prefix = $class;
72+
73+
// work backwards through the namespace names of the fully-qualified
74+
// class name to find a mapped file name
75+
while (false !== $pos = strrpos($prefix, '\\')) {
76+
77+
// retain the trailing namespace separator in the prefix
78+
$prefix = substr($class, 0, $pos + 1);
79+
80+
// the rest is the relative class name
81+
$relative_class = substr($class, $pos + 1);
82+
83+
// try to load a mapped file for the prefix and relative class
84+
$mapped_file = $this->loadMappedFile($prefix, $relative_class);
85+
if ($mapped_file) {
86+
return $mapped_file;
87+
}
88+
89+
// remove the trailing namespace separator for the next iteration
90+
// of strrpos()
91+
$prefix = rtrim($prefix, '\\');
92+
}
93+
94+
// never found a mapped file
95+
return false;
96+
}
97+
98+
/**
99+
* Load the mapped file for a namespace prefix and relative class.
100+
*
101+
* @param string $prefix The namespace prefix.
102+
* @param string $relative_class The relative class name.
103+
* @return mixed Boolean false if no mapped file can be loaded, or the
104+
* name of the mapped file that was loaded.
105+
*/
106+
protected function loadMappedFile($prefix, $relative_class)
107+
{
108+
// are there any base directories for this namespace prefix?
109+
if (isset($this->prefixes[$prefix]) === false) {
110+
return false;
111+
}
112+
113+
// look through base directories for this namespace prefix
114+
foreach ($this->prefixes[$prefix] as $base_dir) {
115+
116+
// replace the namespace prefix with the base directory,
117+
// replace namespace separators with directory separators
118+
// in the relative class name, append with .php
119+
$file = $base_dir
120+
. str_replace('\\', '/', $relative_class)
121+
. '.php';
122+
123+
// if the mapped file exists, require it
124+
if ($this->requireFile($file)) {
125+
// yes, we're done
126+
return $file;
127+
}
128+
}
129+
130+
// never found it
131+
return false;
132+
}
133+
134+
/**
135+
* If a file exists, require it from the file system.
136+
*
137+
* @param string $file The file to require.
138+
* @return bool True if the file exists, false if not.
139+
*/
140+
protected function requireFile($file)
141+
{
142+
if (file_exists($file)) {
143+
require $file;
144+
return true;
145+
}
146+
return false;
147+
}
148+
}
149+
150+
// instantiate the loader
151+
$klasifai_loader = new \Klasifai\Psr4AutoloaderClass();
152+
153+
// register the autoloader
154+
$klasifai_loader->register();
155+
156+
// register the base directories for the namespace prefix
157+
$klasifai_loader->addNamespace('Klasifai', __DIR__ . '/includes/Klasifai' );
158+
159+
require_once( __DIR__ . '/includes/Klasifai/Helpers.php' );

0 commit comments

Comments
 (0)