Santhosh has uploaded a new change for review.
https://gerrit.wikimedia.org/r/119734
Change subject: ROT13 dummy translation service integration
......................................................................
ROT13 dummy translation service integration
Integrate ROT13 MT interface. This version of ROT13 destroy the html
in the input by converting tags also.
Change-Id: I8728b944585b650db810fce2d638da1fe12a9e97
---
M models/DataModelManager.js
M mt/CXMTInterface.js
A mt/providers/Rot13.js
3 files changed, 95 insertions(+), 9 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/mediawiki/services/cxserver
refs/changes/34/119734/1
diff --git a/models/DataModelManager.js b/models/DataModelManager.js
index 65b55bc..7e0fb14 100644
--- a/models/DataModelManager.js
+++ b/models/DataModelManager.js
@@ -9,8 +9,8 @@
'use strict';
-var CXSegmenter = require( __dirname + '/../segmentation/CXSegmenter.js'
).CXSegmenter;
-
+var CXSegmenter = require( __dirname + '/../segmentation/CXSegmenter.js'
).CXSegmenter,
+ CXMTInterface = require( __dirname + '/../mt/CXMTInterface.js'
).CXMTInterface;
/**
* CXDataModelManager
* @class
@@ -27,6 +27,7 @@
CXDataModelManager.prototype.init = function () {
var dataModelManager = this,
segmenter,
+ mtInterface,
PageLoader, pageloader;
// TODO: refactor this
@@ -39,6 +40,7 @@
PageLoader = require( __dirname +
'/../pageloader/PageLoader.js' ).PageLoader;
pageloader = new PageLoader(
dataModelManager.context.sourcePage );
pageloader.load().then( function ( data ) {
+ console.log( '[CX] Page fetched' );
dataModelManager.context.sourceText = data;
segmenter = new CXSegmenter(
dataModelManager.context.sourceText );
segmenter.segment();
@@ -54,10 +56,16 @@
dataModelManager.publish();
// TODO: Dispatch the context to a number of
task runners
// Once each task runners finish, publish.
- }, function ( /*jqXHR, textStatus, errorThrown*/ ) {
+ mtInterface = new CXMTInterface(
dataModelManager.context );
+ mtInterface.translate(
dataModelManager.dataModel.segments ).then( function ( translations ) {
+ dataModelManager.dataModel.mt =
translations;
+ dataModelManager.publish();
+ } );
+ }, function () {
console.error( '[CX] Error in retrieving the
page ' +
dataModelManager.context.sourcePage );
} );
+
}
} );
};
diff --git a/mt/CXMTInterface.js b/mt/CXMTInterface.js
index 3a7a1c2..73cef46 100644
--- a/mt/CXMTInterface.js
+++ b/mt/CXMTInterface.js
@@ -5,16 +5,16 @@
'use strict';
-var ROT13Service = require( __dirname+ '/providers/ROT13.js' ).ROT13Service;
+var Rot13Service = require( __dirname + '/providers/Rot13.js' ).Rot13Service;
/**
* @class CxTranslator
*/
-function CXMTInterface ( config ) {
- this.config = config;
+function CXMTInterface( context ) {
+ this.context = context;
}
-CXMTInterface.prototype.translate = function ( sourceLang, targetLang,
sourceText/*, onSuccess, onError */) {
- return ( new ROT13Service(this.config) ).translate( sourceLang,
targetLang, sourceText );
+CXMTInterface.prototype.translate = function ( segments ) {
+ return ( new Rot13Service( this.context ) ).translate( segments );
};
-module.exports = { 'CXMTInterface': CXMTInterface };
+module.exports.CXMTInterface = CXMTInterface;
diff --git a/mt/providers/Rot13.js b/mt/providers/Rot13.js
new file mode 100644
index 0000000..e84998e
--- /dev/null
+++ b/mt/providers/Rot13.js
@@ -0,0 +1,78 @@
+/**
+ * Rot13 Translation service
+ *
+ * A dummy interface to test the CX MT
+ */
+
+'use strict';
+
+var Q = require( 'q' ),
+ SAXParser = require( 'sax' ).SAXParser;
+
+/**
+ * @class Rot13Service
+ */
+function Rot13Service( config ) {
+ this.config = config;
+ this.parser = null;
+}
+
+function rot13( text ) {
+ return text.replace( /[a-zA-Z]/g, function ( c ) {
+ return String.fromCharCode( ( c <= 'Z' ? 90 : 122 ) >= ( c =
c.charCodeAt( 0 ) + 13 ) ? c : c - 26 );
+ } );
+}
+
+Rot13Service.prototype.translate = function ( segments ) {
+ var rot13 = this,
+ deferred = Q.defer(),
+ mt = {};
+
+ // Simulate a 1000 millisecond delay.
+ setTimeout( function () {
+ var translation,
+ segmentId;
+ for ( segmentId in segments ) {
+ rot13.parser = new SAXParser();
+ rot13.prepareParser();
+ // Wrap the source with <p> to make it valid dom
fragment
+ rot13.parser.write( '<p>' + segments[ segmentId
].source + '</p>' );
+ translation = rot13.parser.parsedText;
+ translation = translation.substr( 3, translation.length
- 7 );
+ mt[ segmentId ] = translation;
+ }
+ deferred.resolve( mt );
+ }, 1000 );
+
+ return deferred.promise;
+};
+
+Rot13Service.prototype.prepareParser = function () {
+ var parser = this.parser;
+ parser.parsedText = '';
+ /**
+ * Entity handler
+ */
+ function entity( str ) {
+ return str.replace( '"', '"' );
+ }
+
+ parser.onopentag = function ( tag ) {
+ var attrName;
+ parser.parsedText += '<' + tag.name;
+ for ( attrName in tag.attributes ) {
+ parser.parsedText += ' ' + attrName + '="' + entity(
tag.attributes[ attrName ] ) + '"';
+ }
+ parser.parsedText += '>';
+ };
+
+ parser.onclosetag = function ( tag ) {
+ parser.parsedText += '</' + tag + '>';
+ };
+
+ parser.ontext = function ( text ) {
+ parser.parsedText += rot13( text );
+ };
+};
+
+module.exports.Rot13Service = Rot13Service;
--
To view, visit https://gerrit.wikimedia.org/r/119734
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I8728b944585b650db810fce2d638da1fe12a9e97
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/services/cxserver
Gerrit-Branch: master
Gerrit-Owner: Santhosh <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits