summaryrefslogtreecommitdiff
blob: 4d7277309f6f761b300cb587b32777412cfe5220 (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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php
declare( strict_types = 1 );

namespace MediaWiki\Extension\Translate\TranslatorInterface\Aid;

use MediaWiki\Extension\Translate\TranslatorInterface\TranslationHelperException;
use TranslationWebService;
use TranslationWebServiceConfigurationException;

/**
 * Translation aid that provides suggestion from machine translation services.
 * @author Niklas Laxström
 * @license GPL-2.0-or-later
 * @since 2013-01-01 | 2015.02 extends QueryAggregatorAwareTranslationAid
 * @ingroup TranslationAids
 */
class MachineTranslationAid extends QueryAggregatorAwareTranslationAid {
	public function populateQueries(): void {
		$definition = $this->dataProvider->getDefinition();
		$translations = $this->dataProvider->getGoodTranslations();
		$from = $this->group->getSourceLanguage();
		$to = $this->handle->getCode();

		if ( trim( $definition ) === '' ) {
			return;
		}

		foreach ( $this->getWebServices() as $service ) {
			if ( $service->checkTranslationServiceFailure() ) {
				continue;
			}

			try {
				if ( $service->isSupportedLanguagePair( $from, $to ) ) {
					$this->storeQuery( $service, $from, $to, $definition );
					continue;
				}

				// Search for translations which we can use as a source for MT
				// @todo: Support setting priority of languages like Yandex used to have
				foreach ( $translations as $from => $text ) {
					if ( !$service->isSupportedLanguagePair( $from, $to ) ) {
						continue;
					}

					$this->storeQuery( $service, $from, $to, $text );
					break;
				}
			} catch ( TranslationWebServiceConfigurationException $e ) {
				throw new TranslationHelperException( $service->getName() . ': ' . $e->getMessage() );
			}
		}
	}

	public function getData(): array {
		$suggestions = [ '**' => 'suggestion' ];

		foreach ( $this->getQueryData() as $queryData ) {
			$suggestions[] = $this->formatSuggestion( $queryData );
		}

		return array_filter( $suggestions );
	}

	protected function formatSuggestion( array $queryData ): ?array {
		$service = $queryData['service'];
		$response = $queryData['response'];
		$sourceLanguage = $queryData['language'];
		$sourceText = $queryData['text'];

		$result = $service->getResultData( $response );
		if ( $result === null ) {
			return null;
		}

		return [
			'target' => $result,
			'service' => $service->getName(),
			'source_language' => $sourceLanguage,
			'source' => $sourceText,
		];
	}

	/** @return TranslationWebService[] */
	private function getWebServices(): array {
		global $wgTranslateTranslationServices;

		$services = [];
		foreach ( $wgTranslateTranslationServices as $name => $config ) {
			$service = TranslationWebService::factory( $name, $config );
			if ( !$service || $service->getType() !== 'mt' ) {
				continue;
			}

			$services[$name] = $service;
		}

		return $services;
	}
}