From 809ae9c73745f83e9e8e459fd3fb1fc73f74d225 Mon Sep 17 00:00:00 2001 From: gamerdonkey Date: Fri, 19 Aug 2022 05:18:00 +0000 Subject: [PATCH] Improving output to use spaces more properly around punctuation. --- web/script.js | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/web/script.js b/web/script.js index 2f3014c..5161f8d 100644 --- a/web/script.js +++ b/web/script.js @@ -33,13 +33,28 @@ function showArticle(article) { document.getElementById('title').innerHTML = article.title document.getElementById('link').innerHTML = `[ Original Article ]` let output = '' + let spaceBefore = false + const noSpaceBeforeTags = new Set(['.', ',', ')', ';', ':', '!', '?', "''", 'POS']) + const noSpaceBeforeTokens = new Set(["n't", ']']) + const noSpaceAfterTags = new Set(['(', '``']) + const noSpaceAfterTokens = new Set(['[']) article.tokens.forEach(function(token) { - let adlib_input = document.getElementById(`token_${token.id}`); - if(adlib_input && adlib_input.value) { - output += `${adlib_input.value} ` + + if(spaceBefore && !noSpaceBeforeTags.has(token.tag) && !noSpaceBeforeTokens.has(token.token)) { + output += ' ' + } + spaceBefore = true + + let adlibInput = document.getElementById(`token_${token.id}`); + if(adlibInput && adlibInput.value) { + output += `${adlibInput.value}` } else { - output += `${token.token} ` + output += `${token.token}` + } + + if(noSpaceAfterTags.has(token.tag) || noSpaceAfterTokens.has(token.token)) { + spaceBefore = false } }) document.getElementById('summary').innerHTML = output