[MediaWiki-commits] [Gerrit] mediawiki...cxserver[master]: MWReference: add reference adaptation

2017-08-28 Thread jenkins-bot (Code Review)
jenkins-bot has submitted this change and it was merged. ( 
https://gerrit.wikimedia.org/r/372833 )

Change subject: MWReference: add reference adaptation
..


MWReference: add reference adaptation

* Some misc doc tweaks noticed while inspecting the code
* Introduced TestClient for MT. Speeds up the tests and
  removed dependency on an actual MT client. It also always
  provides consistent results.

Change-Id: I1fb5b29c6d65ad6da83e4b0140138e43a3e84fd2
---
M lib/Adapter.js
A lib/mt/TestClient.js
M lib/mt/index.js
A lib/translationunits/MWReference.js
M lib/translationunits/index.js
M test/adaptation/AdaptationTests.json
M test/translationunits/MWLink.test.json
A test/translationunits/MWReference.test.js
A test/translationunits/MWReference.test.json
9 files changed, 194 insertions(+), 7 deletions(-)

Approvals:
  jenkins-bot: Verified
  Santhosh: Looks good to me, approved



diff --git a/lib/Adapter.js b/lib/Adapter.js
index d7aa71d..167a415 100644
--- a/lib/Adapter.js
+++ b/lib/Adapter.js
@@ -23,7 +23,7 @@
/**
 * Get the adapter for the given tag(translation unit).
 * @param {Object} element
-* @return {TranslationUnit}
+* @return {TranslationUnit|null}
 */
getAdapter( element ) {
let name, match = false,
diff --git a/lib/mt/TestClient.js b/lib/mt/TestClient.js
new file mode 100644
index 000..1ba0cca
--- /dev/null
+++ b/lib/mt/TestClient.js
@@ -0,0 +1,19 @@
+'use strict';
+
+const MTClient = require( './MTClient.js' );
+
+class TestClient extends MTClient {
+   /**
+* Translate plain text for tests.
+*
+* @param {string} sourceLang Source language code
+* @param {string} targetLang Target language code
+* @param {string} sourceText Source language text
+* @return {Promise} Promise of the translated text
+*/
+   translateText( sourceLang, targetLang, sourceText ) {
+   return Promise.resolve( 
`[${sourceLang}→${targetLang}]${sourceText}` );
+   }
+}
+
+module.exports = TestClient;
diff --git a/lib/mt/index.js b/lib/mt/index.js
index 505fa97..d05029a 100644
--- a/lib/mt/index.js
+++ b/lib/mt/index.js
@@ -1,8 +1,9 @@
 'use strict';
 
 module.exports = {
-   Apertium: require( './Apertium.js' ),
-   Yandex: require( './Yandex.js' ),
-   Youdao: require( './Youdao.js' ),
-   Matxin: require( './Matxin.js' )
+   Apertium: require( './Apertium' ),
+   Matxin: require( './Matxin' ),
+   TestClient: require( './TestClient' ),
+   Yandex: require( './Yandex' ),
+   Youdao: require( './Youdao' )
 };
diff --git a/lib/translationunits/MWReference.js 
b/lib/translationunits/MWReference.js
new file mode 100644
index 000..ff0294c
--- /dev/null
+++ b/lib/translationunits/MWReference.js
@@ -0,0 +1,65 @@
+'use strict';
+
+const cxutil = require( '../util.js' ),
+   TranslationUnit = require( './TranslationUnit.js' );
+
+/**
+ * This class handles the `` wikitext tag.
+ *
+ * This tag rendered as span with typeof="mw:Extension/ref" and 
rel="dc:references" by parsoid.
+ * By default parsoid also adds data-mw.body.id that refers to the rendered 
element produced by
+ * the `` tag. But here we expect that data-mw.body.html is also 
provided so that
+ * it can be adapted without any dependencies to other sections of the article.
+ */
+class MWReference extends TranslationUnit {}
+
+MWReference.prototype.adapt = cxutil.async( function* () {
+   let refData, refBody, wrappedRefBody, translatedRefBody, 
unwrappedTranslatedRefBody;
+
+   // TODO: This format is not decided yet. We do need to inform client 
about failed
+   // adaptations somehow.
+   // This will be reset later if adaptation is succesful
+   this.node.attributes[ 'data-cx' ] = JSON.stringify( {
+   adapted: false
+   } );
+
+   try {
+   refData = JSON.parse( this.node.attributes[ 'data-mw' ] );
+   } catch ( e ) {
+   this.log( 'error', 'Not-adapting a reference node with non-JSON 
data-mw: ' + this.node.attributes.id );
+   return this.node;
+   }
+
+   if ( !refData ) {
+   this.log( 'error', 'Not-adapting a reference node without 
data-mw: ' + this.node.attributes.id );
+   return this.node;
+   }
+
+   refBody = refData.body && refData.body.html;
+   if ( !refBody ) {
+   this.log( 'debug', 'Not-adapting a reference node without 
data-mw.body.html: ' + this.node.attributes.id );
+   return this.node;
+   }
+
+   // MTClient only accepts content wrapped in block element tags in HTML 
mode.
+   wrappedRefBody = '' + refBody + '';
+   // TODO: Parse recursively instead of just MTing to handle templates
+   // Translate reference contents
+   translatedRefBody = yield this.context.conf.mtClient.translate(
+   

[MediaWiki-commits] [Gerrit] mediawiki...cxserver[master]: MWReference: add reference adaptation

2017-08-21 Thread Nikerabbit (Code Review)
Nikerabbit has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/372833 )

Change subject: MWReference: add reference adaptation
..

MWReference: add reference adaptation

Depends-On: Ifd79a2f754af442483ef5b6c661be82d2fea648e
Change-Id: I1fb5b29c6d65ad6da83e4b0140138e43a3e84fd2
---
A lib/translationunits/MWReference.js
M lib/translationunits/index.js
M test/adaptation/AdaptationTests.json
3 files changed, 62 insertions(+), 1 deletion(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/services/cxserver 
refs/changes/33/372833/1

diff --git a/lib/translationunits/MWReference.js 
b/lib/translationunits/MWReference.js
new file mode 100644
index 000..b553c5d
--- /dev/null
+++ b/lib/translationunits/MWReference.js
@@ -0,0 +1,53 @@
+'use strict';
+
+const cxutil = require( '../util.js' ),
+   TranslationUnit = require( './TranslationUnit.js' );
+
+/**
+ * This class handles the `` wikitext tag.
+ *
+ * This tag rendered as span with typeof="mw:Extension/ref" and 
rel="dc:references" by parsoid.
+ * By default parsoid also adds data-mw.body.id that refers to the rendered 
element produced by
+ * the `` tag. But here we expect that data-mw.body.html is also 
provided so that
+ * it can be adapted without any dependencies to other sections of the article.
+ */
+class MWReference extends TranslationUnit {
+}
+
+MWReference.prototype.adapt = cxutil.async( function* () {
+   // TODO: This format is not decided yet. We do need to inform client 
about failed
+   // adaptations somehow.
+   // This will be reset later if adaptation is succesful
+   this.node.attributes[ 'data-cx' ] = JSON.stringify( {
+   adapted: false,
+   } );
+
+   let refData = JSON.parse( this.node.attributes[ 'data-mw' ] );
+   if ( !refData ) {
+   this.logger.log( 'error', 'Not-adapting a reference node 
without data-mw: ' + this.node.attributes.id );
+   return this.node;
+   }
+
+   let refBody = refData.body && refData.body.html;
+   if ( !refBody ) {
+   this.logger.log( 'debug', 'Not-adapting a reference node 
without data-mw.body.html: ' + this.node.attributes.id );
+   return this.node;
+   }
+
+   // TODO: Parse recursively instead of just MTing to handle templates
+   // Translate reference contents
+   let translatedRefBody = yield this.context.conf.mtClient.translate(
+   this.sourceLanguage, this.targetLanguage, refBody
+   );
+
+   refData.body.html = translatedRefBody;
+   this.node.attributes[ 'data-mw' ] = JSON.stringify( refData );
+   delete this.node.attributes[ 'data-cx' ];
+
+   return this.node;
+} );
+
+MWReference.matchTagNames = [ 'span' ];
+MWReference.matchRdfaTypes = [ 'dc:references', 'mw:Extension/ref' ];
+
+module.exports = MWReference;
diff --git a/lib/translationunits/index.js b/lib/translationunits/index.js
index c8ddd27..1a4f800 100644
--- a/lib/translationunits/index.js
+++ b/lib/translationunits/index.js
@@ -2,5 +2,6 @@
 
 module.exports = {
MWLink: require( './MWLink.js' ),
-   MWImage: require( './MWImage.js' )
+   MWImage: require( './MWImage.js' ),
+   MWReference: require( './MWReference.js' )
 };
diff --git a/test/adaptation/AdaptationTests.json 
b/test/adaptation/AdaptationTests.json
index a97d3d6..aebddfd 100644
--- a/test/adaptation/AdaptationTests.json
+++ b/test/adaptation/AdaptationTests.json
@@ -33,5 +33,12 @@
"to": "ml",
"source": "Philo's experiment inspired later investigators.",
"result": "Philo's experiment inspired later investigators."
+   },
+   {
+   "desc": "Reference adaptation test without a reference 
template",
+   "from": "en",
+   "to": "ca",
+   "source": "Hello world.[1]",
+   "result": "Món d'hola.[1]"
}
 ]

-- 
To view, visit https://gerrit.wikimedia.org/r/372833
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I1fb5b29c6d65ad6da83e4b0140138e43a3e84fd2
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/services/cxserver
Gerrit-Branch: master
Gerrit-Owner: Nikerabbit 

___
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits