Last active
December 6, 2017 07:42
-
-
Save Miss-Inputs/91d529b43f852a0fbac03c8994bf96ec to your computer and use it in GitHub Desktop.
Stupid hello world
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function translate([string][Parameter(valueFromPipeline=$true)]$text, $sourceLanguage, $targetLanguage){ | |
if($sourceLanguage -eq $targetLanguage){ | |
return $text | |
} | |
$url = "https://translate.googleapis.com/translate_a/single?client=gtx&sl=$([Uri]::escapeDataString($sourceLanguage))&tl=$([Uri]::escapeDataString($targetLanguage))&dt=t&q=$([Uri]::escapeDataString($text))" | |
$response = irm $url | |
return $response[0][0][0] | |
} | |
function isHelloWorld([string][Parameter(valueFromPipeline=$true)]$s){ | |
#This function sucks but I had a few beers so what you gonna do about it | |
switch -regex ($s -ireplace '[^\sA-Z]', ''){ | |
<#Translation engine might decide to return 'Hello, world!' 'Hello world!' 'Hello world' 'Hello world program' 'Hello everyone' and who | |
knows what else depending on what I use (Yandex and MyMemory are especially stupid) so just strip out everything but words and spaces#> | |
'^Hello\s+(world|everyone)(\s+program)?$'{ | |
return $true; | |
} | |
default { | |
return $false; | |
} | |
} | |
} | |
#This was just me making sure translate() and isHelloWorld() work | |
#$target = 'こんにちは 世界' #Konnichiwa sekai | |
#translate $target ja en | isHelloWorld | |
function makeRandomJapaneseString($length){ | |
$sb = new-object Text.StringBuilder; | |
$hiraganaRange = 0x3041..0x3093; #There are a few more up to 309f but those ones are weird so who cares | |
#Katakana is not used for this purpose | |
$kanjiRange = 0x4e00..0x9faf; | |
#Ignore the extended kanji because it looks like the kanjis in 'world' are in the normal range anyway | |
0..($length - 1) | % { | |
[void]$sb.append([char]($hiraganaRange + $kanjiRange | get-random)); | |
} | |
return "$sb"; | |
} | |
do { | |
$english = makeRandomJapaneseString 8 | translate -source ja -target en | |
#write-debug -debug $english #Most likely some random bullshit | |
} until (isHelloWorld $english); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment