Google Translate API: How to add newline without changing the phrase meaning - html

I am using google translate rest api to convert string to another language. There are some strings in which I want to insert '\n'. It is changing the meaning of the sentence
e.g.
String:35 seconds left
How it's displayed in my App:
35
seconds
left
Translation of the String: 35 ਸਕਿੰਟ ਬਾਕੀ
Required Output:
35
ਸਕਿੰਟ
ਬਾਕੀ
what I have tried:
q: 35 \n seconds \n left
output: 35 \ n ਸਕਿੰਟ \ n ਖੱਬੇ (here ਖੱਬੇ means left direction not what
actually required "remaining" i.e. ਬਾਕੀ)
q: 35 <br> seconds <br> left
output: 35 <br> ਸਕਿੰਟ <br> ਖੱਬੇ
q: 35 \n seconds \n left
output: <span class=\"e;notranslate\"e;>35, \\ n</span> ਸਕਿੰਟ <span class=\"e;notranslate\"e;>n</span> ਖੱਬਾ <span class=\"e;notranslate\"e;>\\</span>
I have also tried adding random number for line break
q: 35 123456 seconds 123456 left
output: 35 123456 ਸਕਿੰਟ 123456 ਬਾਕੀ
this is correct for the above string as I can easily replace 123456 with \n but for some of the other strings it breaks the meaning of the sentence.
However it works correctly on google
I'm using Rest api and I have tried specifying the format html

Translation is a complex task. What you're trying to do is enforce a specific syntax into a sentence, then translate it, and hope that the same syntax is applicable in the new language. This is not always the case, and would require the application to know the semantics of the inserted syntax (in your case linebreaks and numbers). You might want to read this article describing the issue in more detail.
Instead of trying to guess what you mean when you insert linebreaks into a sentence, it just assumes that you are starting a new one. This will cause each line to be translated individually, resulting in a non-optimal translation.
How I would approach this problem, would be to translate the string, e.g.
35 seconds left ➡️ 35 ਸਕਿੰਟ ਬਾਕੀ.
Then I would replace the spaces in output with linebreaks, resulting in the following:
35 ਸਕਿੰਟ ਬਾਕੀ ➡️ 35\nਸਕਿੰਟ\nਬਾਕੀ
Let me stretch that this is not general solution, but it probably work in many languages, since your use-case is rather simple, i.e. short sentences.

Related

Multiple HTM5 regex in text field

I'm trying to validate an entry in a text field via HTML5. Those are the rules:
It can be empty;
If not empty, it must be numeric and start with 21, 29 or 30;
If starts with 21, it must have 13 characters;
If starts with 29, it must have 15 characters;
If starts with 30, it must have 16 characters;
I've got it partially working when I use:
<input type="text" pattern="NULL|^(21)[0-9]{1,13}" />
However, if I try to concatenate those rules, It only works for the first one.
<input type="text" pattern="NULL|^(21)[0-9]{1,13}|^(29)[0-9]{1,15}|^(30)[0-9]{1,16}" />
I'm aware it can be done via a Javascript function, however I'd like to use regex if possible.
There are the two mentioned problems. First, to match an empty input that I assume it is what you want you need to use the ^$ constructor.
Then if you want to limit the required length of the input you need to state the proper length, not a variable one.
A regex like this one should work:
^$|^(21)[0-9]{11}$|^(29)[0-9]{13}$|^(30)[0-9]{14}$
The numbers inside the curly brackets indicate the length of the numeric characters behind the 21, 29 and 30. Those will depend on whether you are counting those as two characters or not for the proper length.
You can test it here and see some examples.
Use
<input type="text" pattern="(?:21\d{11}|29\d{13}|30\d{14})?" />
The HTML5 engine will wrap it with ^(?: and )$ automatically. The regex will look like ^(?:(?:21\d{11}|29\d{13}|30\d{14})?)$ and will match
^ - start of string
(?: - start of a non-capturing group:
21\d{11} - 21 and 11 digits
| - or
29\d{13} - 29 and 13 digits
| - or
30\d{14} - 30 and 14 digits
)? - end of the group, repeat 1 or 0 times
$ - end of string.

How to get rid of in AA

I am reading data (combination of letters and numbers) from an excel sheet and put it into a text field in target application, where the input should yield a unique item from a database.
However there (sometimes) is a whitespace behind the data in the excel cell, which results in a "no data found" when this whitespace is entered into the search field in target application. The whitespace does not seem to be a space though, since i am unable to trim that whitespace AA-internally. I guess it is a (or some similar html special character).
edit: confirmed to be a by now.
Q: How can i get rid of such characters AA internally?
Tried: Neither (a) Trim, (b) Replace " " ->"", nor (c) Replace " "->"" work.
Workaround: I am currently checking for the length of the data provided: if its longer than 10 chars i only take the leftmost 10 chars. This works here, since its a business rule for the data i am working with, but i am still interested in an original solution, since there may be upcoming cases, where no business rule will help me out.
AA Version: 11.3.1
Thankful for input...
Okay, since it's non-breaking spaces character, you can replace it using Regex in replace command.
Find: \u00a0
Options: Regular Expression.
Got rid of it using Replace Command with RegEx ticked:
[^a-z A-Z 0-9]

Regular Expression to match single, whole words from MySQL Database

I require a Regular Expression to match single words of [a-zA-Z] only with 6 or more characters.
The list of words:
a
27
it was good because
it
cat went to
four
eight is bigger
peter
pepper
objects
83
harrison99
adjetives
atom-bomb
The following would be matched
pepper
objects
adjectives
It isn't too bad if the following are matched too as I can strip out the duplicates after
it was good because
eight is bigger
atom-bomb
So far I have this
/[a-zA-z]{6,}/g
Which matches 6 character or more words according to RegEx 101 but when I use it in my MySQL database to filter a table on matches RegExp.
Currently it includes words containing punctuation and other non a-zA-Z characters. I don't want it to include results with numbers or punctuation.
In an ideal world I don't want it to include a row that contains more than one word as usually the other words are already duplicates.
Actual MySQL results
If a row contains a number or punctuation and/or contain one or more words I don't want it included.
I want only results that are single, whole words of 6+ characters
Drop the delimiters and add anchors for start/end:
SELECT 'abcdef' REGEXP '^[a-zA-Z]{6,}$'
-> 1
SELECT 'a abcdef' REGEXP '^[a-zA-Z]{6,}$'
-> 0
Also I changed [a-zA-z] in the character class to [a-zA-Z].
Instead of [a-zA-Z] you can also use [[:alpha:]] POSIX style bracket extension. So it becomes:
^[[:alpha:]]{6,}$
(As a side note: MySql uses Henry Spencer's implementation of regular expressions. if word boundaries needed, use [[:<:]]word[[:>:]] See manual for further syntax differences)

How to print Bar code 39 on star tsp future PRNT

Hi am working on the Star TSP100 futurePRNT, have got a sample example to print data which is working fine. They use the code "\x1b\x1d\x61\x1" to allign to center. Need to know which coding it is? And how to print Code - 39 barcode in using the code?
It's a hexadecimal code specified by the below lined document.
As an example I'll deconstruct this command:
1B = Ascii Escape Character. This denotes the beginning of the command.
1D = Ascii Group Separator Character. This is part of the command.
61 = Ascii numeral '1'. This is part of the command.
1 = Value. This is the only parameter of the command.
Consulting the linked specification, 1B 1D 61 a is the command to specify position alignment. The a is the parameter. This command specifically sets center horizontal alignment (page 3-32).
The command to specify a barcode can be found on pages 3-43 through 3-46. It follows a similar format, there will be a code specifying the task, then a number of variable parameters to select the correct format, and finally you'll send your data.
Latest Line Mode Specification: http://www.starasia.com/%5CDownload%5CManual%5Cstarline_cm_en.pdf

Code Golf: Pig Latin

Locked. This question and its answers are locked because the question is off-topic but has historical significance. It is not currently accepting new answers or interactions.
Challenge:
Take a sentence of input of any length and convert all the words in that sentence to pig latin. If you do not know what pig latin is please read Wikipedia: Pig Latin.
Specifications:
Assume all words are separated by spaces and all sentences either end with a exclamation, question mark or period.
Do not use the variant for vowels mentioned in Wikipedia.
For words such as bread and quiz it is perfectly acceptable for them to be readbay, uizqay instead of and eadbray and izquay.
Functions or methods are perfectly acceptable. In other words you do not need to take in user input, but you must display user output.
Assume no input contains a compound word.
Example:
Input: I am a happy man.
Output: Iway amway away appyhay anmay.
How to win:
The winner is the person who can write a program that will do the challenge with the least amount of characters.
sed - 53/55 45/47 chars
With the -r option (2+43):
s/\b[aeiou]\w*/w&/gi;s/\b(\w)(\w*)/\2\1ay/g
Without the -r option (47):
s/\b[aeiou]\w*/w&/gi;s/\b\(\w\)\(\w*\)/\2\1ay/g
C# 257 96 characters
Readable Version:
string.Join(" ",
args.Select(y =>
("aeiouAEIOU".Contains(y[0])
? y + "way"
: y.Substring(1) + y[0] + "ay")
)
);
Condensed
string.Join(" ",args.Select(y=>("aeiouAEIOU".Contains(y[0])?y+"way":y.Substring(1)+y[0]+"ay")));
Input:
LINQ helps me write good golf answers
Output:
INQLay elpshay emay riteway oodgay olfgay answersway
GolfScript - 60 53 52 51 49 46 chars
)](' '/{1/(."AEIOUaeiou"-!{\119}*"ay "}%));+\+
Ruby 1.9+: 63 62 chars
Just a quick answer, probably can be shortened more
p gets.gsub(/\w+/){|e|"#{e=~/^(qu|[^aeiou]+)/i?$'+$&:e+?w}ay"}
it handles the case of the qu (question => estionquay), and prints with double qoutes. 3 more bytes for getting rid of them (I say no specification about this)
Edit 1: If using Ruby 1.9 saves a character (?w), let's use it.
Perl 87, 56, 47 chars
works with punctuation.
Thanks to mobrule.
s/\b[aeiou]\w*/w$&/gi;s/\b(\w)(\w*)/\2\1ay/g
Usage :
echo 'I, am; a: happy! man.' | perl -p piglatin.pl
Output :
Iway, amway; away: appyhay! anmay.
Groovy, 117 100 91 85 83 79 chars
print args[0].replaceAll(/(?i)\b(\w*?)([aeiou]\w*)/,{a,b,c->c+(b?b:'w')+"ay"})
Readable version:
print args[0]
.replaceAll(
/(?i)\b(\w*?)([aeiou]\w*)/ ,
{
a, b, c ->
c + ( b ? b : 'w' ) + "ay"
})
Haskell: 244 199 222 214 chars
Solution gives reasonable capitalization to transformed words based on original capitalization. Now properly handles leading consonant clusters. Note: no newline included at end of last line.
import Data.Char
import Data.List
q(x:y,z)|w x=x%(z++toLower x:y++"ay")|0<1=x:y++z
q(_,z)=z++"way"
x%(y:z)|isUpper x=toUpper y:z|0<1=y:z
w=isAlpha
main=interact$(>>=q.break(`elem`"aeiouAEIOU")).groupBy((.w).(==).w)
Test Input:
Did the strapping man say: "I am Doctor X!"?
Test Output:
Idday ethay appingstray anmay aysay: "Iway amway Octorday Xay!"?
VB.NET: 106 chars
Assumes "s" is the input, and also Imports System.Text.RegularExpressions. (Interestingly, due to the need for the # string literal prefix and the trailing semi-colon, this VB.NET version beats the C# equivalent by 3 chars.)
Return Regex.Replace(Regex.Replace(s, "(?i)\b([aeiou]\S*)", "$1way"), "(?i)\b([^aeiou\s])(\S*)", "$2$1ay")
Python 3 — 107 106 chars
Not preserving capitalization, as allowed in the comment. But punctuations are preserved. Whitespaces and linebreaks are added for readability only (hence the ; after import re).
import re;
print(re.sub('(?i)\\b(qu|[^aeiou\W]*)(\w*)',
lambda m:m.group(2)+(m.group(1)or'w')+'ay',
input()))
3 chars can be removed (qu|) if we don't handle the "qu" words.
Example usage:
$ python3.1 x.py
The "quick brown fox" jumps over: the lazy dog.
eThay "ickquay ownbray oxfay" umpsjay overway: ethay azylay ogday.
Python 3 - 100 103 106 chars
(similar to KennyTM's; the regex makes the difference here.)
import re;print(re.sub('(?i)(y|qu|\w*?)([aeiouy]\w*)',lambda m:m.group(2)+(m.group(1)or'w')+'ay',input()))
Note: went from 100 to 103 characters because of modification of the regex to account for "qu".
Note 2: Turns out the 103-char version fails when "y" is used for a vowel sound. Bleh. (On the other hand, KennyTM's 106-char version also fails when "y" is used for a vowel sound, so whatever.)
Boo (.NET): 91 chars
Same concept as VB.NET answer, only using Boo to save a few keystrokes.
print /(?i)\b([^aeiou\s])(\S*)/.Replace(/(?i)\b([aeiou]\S*)/.Replace(s, "$1way"), "$2$1ay")
Oops... I just noticed that this doesn't handle the ending punctuation. Or really any punctuation. Oh well - neither do many of the other solutions.
Lua, 109 characters
print((io.read():gsub("(%A*)([^AEIOUaeiou]*)(%a+)",function(a,b,c)return a..c..b..(#b<1 and"way"or"ay")end)))
Input:
To be honest, I would say "No!" to that question.
Output:
oTay ebay onesthay, Iway ouldway aysay "oNay!" otay atthay uestionqay.
Perl, 70 characters
To get the ball rolling:
while(<>){for(split){s/^([^aeiou]+)(.*)/$2$1ay / or $_.='way ';print}}
I'm sure it can be improved somewhere.
Python - 107 chars
i=raw_input()
print" ".join(w+"way"if w[0]in"aeiouyAEIOUY"else w[1:]+w[0]+"ay"for w in i[:-1].split())+i[-1]
PHP 102 bytes
<?foreach(split(~ß,SENTENCE)as$a)echo($b++?~ß:'').(strpos(' aeuio',$a[0])?$a.w:substr($a,1).$a[0]).ay;
PHP with use of preg 80 bytes
<?=preg_filter('#\b(([aioue]\w*)|(\w)(\w*))\b#ie','"$2"?$2way:$4$3ay',SENTENCE);