Skip to content

Commit e074cae

Browse files
Androbinsushain97
authored andcommitted
Context size fixed (#248)
Deconflicts #78
1 parent b236765 commit e074cae

File tree

1 file changed

+38
-26
lines changed

1 file changed

+38
-26
lines changed

assets/js/translator.js

Lines changed: 38 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -350,23 +350,6 @@ if(modeEnabled('translation')) {
350350
return;
351351
}
352352

353-
// Obtaining context, (± config.SUGGESTIONS.context_size) words
354-
// fallback to complete text if this fails.
355-
var hashedWord = fromWord.hashCode() + fromWord + fromWord.hashCode();
356-
$('#wordGettingSuggested').text(hashedWord);
357-
358-
var splitText = $('#translatedText').text().split(' ');
359-
$('#wordGettingSuggested').text(fromWord);
360-
361-
var targetIndex = splitText.indexOf(hashedWord);
362-
var wrapLength = parseInt(config.SUGGESTIONS.context_size, 10);
363-
var begin = (targetIndex > wrapLength) ? (targetIndex - wrapLength) : 0;
364-
var ending = (splitText.length - targetIndex - 1 > wrapLength) ? (targetIndex + wrapLength + 1) : splitText.length;
365-
var context = splitText.slice(begin, ending).join(' ').replace(hashedWord, fromWord);
366-
if(!context) {
367-
context = $('#translatedText').attr('pristineText');
368-
}
369-
370353
$.ajax({
371354
url: config.APY_URL + '/suggest',
372355
type: 'POST',
@@ -375,7 +358,7 @@ if(modeEnabled('translation')) {
375358
'langpair': curSrcLang + '|' + curDstLang,
376359
'word': fromWord,
377360
'newWord': toWord,
378-
'context': context,
361+
'context': getContext(fromWord),
379362
'g-recaptcha-response': recaptchaResponse
380363
},
381364
success: function () {
@@ -460,6 +443,30 @@ if(modeEnabled('translation')) {
460443
});
461444
}
462445

446+
function getContext(fromWord) {
447+
var wrapLength = parseInt(config.SUGGESTIONS.context_size, 10);
448+
var mark = 'MEGAWORD!';
449+
var markedWord = mark + fromWord;
450+
var rawText = $('#translatedText').html();
451+
var cleanMarkedText = rawText.replace(/<span[^>]*id="wordGettingSuggested"[^>]*>/g, mark);
452+
cleanMarkedText = cleanMarkedText.replace(/<[/]?span[^>]*>/g, '');
453+
var splittedText = cleanMarkedText.replace(/\s+/g, ' ').split(' ');
454+
var targetIndex = -1;
455+
var wordCount = splittedText.length;
456+
for(var i = 0; i < wordCount && targetIndex === -1; i++) {
457+
if(splittedText[i].indexOf(markedWord) !== -1) {
458+
targetIndex = i;
459+
}
460+
}
461+
var beginning = (targetIndex > wrapLength) ? (targetIndex - wrapLength) : 0;
462+
var ending = (splittedText.length - targetIndex - 1 > wrapLength) ? (targetIndex + wrapLength + 1) : splittedText.length;
463+
var context = splittedText.slice(beginning, ending).join(' ').replace(mark, '');
464+
if(!context) {
465+
context = $('#translatedText').attr('pristineText');
466+
}
467+
return context;
468+
}
469+
463470
function getPairs() {
464471
var deferred = $.Deferred();
465472

@@ -793,22 +800,26 @@ function translateText(ignoreIfEmpty) {
793800
var placeholder = getDynamicLocalization('Suggest_Placeholder');
794801
$('#suggestedWordInput').attr('placeholder', placeholder);
795802
$('#translatedText').html(
796-
$('#translatedText').html().replace(/(^|\W|\d)(\*|@|#)(\w+)/g,
797-
'$1<span class="wordSuggestPopover text-danger" title="' +
798-
localizedTitle + '" style="cursor: pointer">$3</span>')
803+
$('#translatedText').html().replace(/(\*|@|#)([^\s0-9.,!?;:)_>"'«/»`+=-]+)/g,
804+
'<span class="wordSuggestPopover text-danger" title="' +
805+
localizedTitle + '" style="cursor: pointer">$2</span>')
799806
);
800807
}
801808

802-
$('#translatedTextClone').html($('#translatedText').attr('pristineText'));
803809
$('.wordSuggestPopover').click(function () {
810+
$('#translatedTextClone').html($('#translatedText').attr('pristineText'));
811+
var fromWord = $(this).html();
812+
$(this).attr('id', 'wordGettingSuggested');
813+
var context = getContext(fromWord);
814+
$('#translatedTextClone').html(context);
815+
804816
$('.wordSuggestPopover').removeAttr('id');
805817
$('.wordSuggestPopoverInline').removeAttr('id');
806-
$(this).attr('id', 'wordGettingSuggested');
807818

808819
$('#translatedTextClone').html(
809-
$('#translatedTextClone').html().replace(/(^|\W|\d)(\*|@|#)(\w+)/g,
810-
'$1<span class="wordSuggestPopoverInline text-danger" title="' +
811-
localizedTitle + '" style="cursor: pointer">$3</span>')
820+
$('#translatedTextClone').html().replace(/(\*|@|#)([^\s0-9.,!?;:)_>"'«/»`+=-]+)/g,
821+
'<span class="wordSuggestPopover text-danger" title="' +
822+
localizedTitle + '" style="cursor: pointer">$2</span>')
812823
);
813824

814825
$('.wordSuggestPopoverInline').click(function () {
@@ -826,6 +837,7 @@ function translateText(ignoreIfEmpty) {
826837
$('#suggestionTargetWord').html($(this).text().replace(/(\*|@|#)/g, ''));
827838

828839
$('#wordSuggestModal').modal();
840+
$(this).removeAttr('id');
829841
});
830842
}
831843
else {

0 commit comments

Comments
 (0)