summaryrefslogtreecommitdiff
path: root/plugins/af_lang_detect/init.php
blob: 3ec0023b669c20b405f3c7275802f32ae6bacff0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<?php
class Af_Lang_Detect extends Plugin {
	private $host;
	private $lang;

	function about() {
		return array(1.1,
			"Detect article language",
			"fox");
	}

	function init($host) {
		$this->host = $host;

		$host->add_hook($host::HOOK_ARTICLE_FILTER, $this);

		require_once __DIR__ . "/languagedetect/Text/LanguageDetect.php";

		$this->lang = new Text_LanguageDetect();
		$this->lang->setNameMode(2);
	}

	function hook_article_filter($article) {

		if ($this->lang) {
			$entry_language = $this->lang->detect($article['title'] . " " . $article['content'], 1);

			if (count($entry_language) > 0) {
				$possible = array_keys($entry_language);
				$entry_language = $possible[0];

				_debug("detected language: $entry_language");

				$article["language"] = $entry_language;
			}
		}

		return $article;
	}

	function api_version() {
		return 2;
	}

}