Editorial Workflows

Korean to English translator

public workflow

Install Workflow...

This workflow contains at least one Python script. Only use it if you trust the person who shared this with you, and if you know exactly what it does.

I understand, install the workflow!

This is a workflow for Editorial, a Markdown and plain text editor for iOS. To download it, you need to view this page on a device that has the app installed.

Description: Korean to English tranlater by google translate.
The tranlated english results further correct it's grammar by Ginger

Shared by: dvm-shlee

Comments: Comment Feed (RSS)

There are no comments yet.

+ Add Comment

Workflow Preview
Selected Text ?
Entire Line(s)
ON
Empty Selection Output
  • No Output
  • All Text
  • Closest Word
Folded Text
  • Include
  • Replace with:
Translate Korean to English ?
Source Code
#coding: utf-8
import workflow
import sys
import re
import urllib
import urlparse
from urllib2 import urlopen
from urllib2 import Request

agent = {'User-Agent':
    "Mozilla/4.0 (\
    compatible;\
    MSIE 6.0;\
    Windows NT 5.1;\
    SV1;\
    .NET CLR 1.1.4322;\
    .NET CLR 2.0.50727;\
    .NET CLR 3.0.04506.30\
    )"}

def translatekoen(text):
    """Returns the translation using google translate
    source: https://github.com/mouuff/mtranslate
    """
    base_link = "http://translate.google.com/m?hl=%s&sl=%s&q=%s"
    if (sys.version_info[0] < 3):
        to_translate = urllib.quote_plus(text.encode('utf8'))
        link = base_link % ('en', 'ko', to_translate)
        request = Request(link, headers=agent)
        page = urlopen(request).read()
    else:
        to_translate = urllib.parse.quote(text)
        link = base_link % ('en', 'ko', to_translate)
        request = urllib.request.Request(link, headers=agent)
        page = urllib.request.urlopen(request).read() #.decode("utf-8")
    expr = r'class="t0">(.*?)<'
    result = re.findall(expr, page)
    result = re.sub("&#(\d+);",lambda x:unichr(int(x.group(1),10)),result[0])
    if (len(result) == 0):
        return ("")
    return(str(result))

action_in = workflow.get_input()
action_out = translatekoen(action_in)
workflow.set_output(action_out)
Grammar Checker ?
Source Code
#coding: utf-8
import workflow
import urllib
import urlparse
from urllib2 import URLError, HTTPError
import json

def get_ginger_url(text):
    """Get URL for checking grammar using Ginger.
    
    source: https://github.com/zoncoen/python-ginger
    
    @param text English text
    @return URL
    """
    API_KEY = "6ae0c3a0-afdc-4532-a810-82ded0054236"

    scheme = "http"
    netloc = "services.gingersoftware.com"
    path = "/Ginger/correct/json/GingerTheText"
    params = ""
    query = urllib.urlencode([
        ("lang", "US"),
        ("clientVersion", "2.0"),
        ("apiKey", API_KEY),
        ("text", text)])
    fragment = ""

    return(urlparse.urlunparse((scheme, netloc, path, params, query, fragment)))

def get_ginger_result(text):
    """Get a result of checking grammar.
    
    source: https://github.com/zoncoen/python-ginger
    
    @param text English text
    @return result of grammar check by Ginger
    """
    url = get_ginger_url(text)

    try:
        response = urllib.urlopen(url)
    except HTTPError as e:
        print("HTTP Error:", e.code)
    except URLError as e:
        print("URL Error:", e.reason)
    except IOError, (errno, strerror):
        print("I/O error (%s): %s" % (errno, strerror))
    try:
        result = json.loads(response.read().decode('utf-8'))
    except ValueError:
        print("Value Error: Invalid server response.")

    return(result)

def correct_en(text):
    if len(text) > 600:
        print("You can't check more than 600 characters at a time.")
    fixed_text = text
    results = get_ginger_result(text)

    # Correct grammar
    if(not results["LightGingerTheTextResult"]):
        return(text)

    # Incorrect grammar
    fixed_gap = 0
    for result in results["LightGingerTheTextResult"]:
        if(result["Suggestions"]):
            from_index = result["From"]
            to_index = result["To"] + 1
            suggest = result["Suggestions"][0]["Text"]
            fixed_text = fixed_text[:from_index-fixed_gap] + suggest + fixed_text[to_index-fixed_gap:]
            fixed_gap += to_index-from_index-len(suggest)
    return(str(fixed_text))


action_in = workflow.get_input()

#TODO: Generate the output...
action_out = correct_en(action_in)

workflow.set_output(action_out)
Request Text Input ?
Title
Manual Correction
Initial Text
Input
  • Single Line
  • Multiple Lines
Keyboard Options:
Replace Selected Text ?
Replacement Text
Input