Ejemplo n.º 1
0
    def en2ch(self, q):
        import urllib.request
        from HandleJs import Py4Js
        # 利用 谷歌翻译 爬虫 进行翻译
        #  http://blog.csdn.net/yingshukun/article/details/53470424
        content = q
        js = Py4Js()
        tk = js.getTk(content)
        if len(content) > 4891:
            print("翻译的长度超过限制!!!")
            return

        param = {'tk': tk, 'q': content}

        result = requests.get(
            """http://translate.google.cn/translate_a/single?client=t&sl=en
                &tl=zh-CN&hl=zh-CN&dt=at&dt=bd&dt=ex&dt=ld&dt=md&dt=qca&dt=rw&dt=rm&dt=ss
                &dt=t&ie=UTF-8&oe=UTF-8&clearbtn=1&otf=1&pc=1&srcrom=0&ssel=0&tsel=0&kc=2""",
            params=param)

        # 返回的结果为Json,解析为一个嵌套列表
        # print(type(result))

        # print(type(result.json()))
        # print(len(result.json()))
        rst = ''
        for ii in range(0, len(result.json()[0]) - 1):
            rst = rst + result.json()[0][ii][0]

        return rst
Ejemplo n.º 2
0
def main(argv):
    if argv:
        process_argv(argv)
    settings = ["auto", "auto", "def"]
    print("\n----------Settings----------")
    print("sl: %s tl: %s mode: %s" % (settings[0], settings[1], settings[2]))

    js = Py4Js()

    while 1:
        content = input("\nTranslate/(S): ")

        if content in ("q", "exit"):
            break

        if content in ("S"):
            print("\n--------Old Settings--------")
            print("sl: %s tl: %s mode: %s" %
                  (settings[0], settings[1], settings[2]))

            print("\n------Change Settings-------")
            input_settings = input("sl tl mode: ")
            input_settings = input_settings.replace("cn", "zh-CN")
            input_settings = input_settings.replace("tw", "zh-TW")
            new_settings = input_settings.split()
            for i in range(len(new_settings)):
                settings[i] = new_settings[i]

            print("\n--------New Settings--------")
            print("sl: %s tl: %s mode: %s" %
                  (settings[0], settings[1], settings[2]))
            continue

        tk = js.getTk(content)
        translate(settings, content, tk)
Ejemplo n.º 3
0
    def __init__(self, parent=None):
        super(ShapeWidget, self).__init__(parent)
        self.setWindowTitle("不规则的,可以拖动的窗体实现例子")
        self.createMode = 'doodle_contour'
        # self.mypix()
        self.width  = 0
        self.height = 0
        self.radius = 0
        self.set_window(300,300,10)
        self.js = Py4Js()   

        #创建多行文本框
        self.textEdit1=QTextEdit()
        self.textEdit2=QLabel()
        self.textEdit2.setWordWrap(True)
        #创建两个按钮
        self.btnPress1=QPushButton('清空')
        self.btnPress2=QPushButton('翻译')

        #实例化垂直布局
        layout=QVBoxLayout()
        #相关控件添加到垂直布局中
        layout.addWidget(self.textEdit1)
        btn_layout = QHBoxLayout()
        btn_layout.addWidget(self.btnPress1)
        btn_layout.addWidget(self.btnPress2)
        layout.addLayout(btn_layout)
        layout.addWidget(self.textEdit2)

        #设置布局
        self.setLayout(layout)
        #将按钮的点击信号与相关的槽函数进行绑定,点击即触发
        self.btnPress1.clicked.connect(self.btnPress1_clicked)
        self.btnPress2.clicked.connect(self.btnPress2_clicked)
Ejemplo n.º 4
0
def on_press(key):

    global old

    #if str(key) == "'`'":

    #pastes = parse_byte()
    #old = old + ' ' + pastes

    if str(key) == "'`'":

        old = ' '

    if key is keyboard.Key.esc:

        pastes = old

        old = ''

        #sentences = pastes.split('.')
        sentences = re.split('[.?!;] ', pastes)

        for content in sentences:

            js = Py4Js()
            tk = js.getTk(content)
            translate(content, tk)
            print(content)
            print()

        print('````````````````````````````````````````````````')
Ejemplo n.º 5
0
def translate_title(title_list):
    title_chn_list = []
    for each in title_list:
        js = Py4Js()
        tk = js.getTk(each)
        title_chn = translate(each, tk)
        title_chn_list.append(title_chn)
    return title_chn_list
Ejemplo n.º 6
0
def translate_normal(content):    
    js = Py4Js()    

    tk = js.getTk(content)
    #print('english:' + content)
    cn_buf = translate_core(content,tk)
    #print('Chinese:' + cn_buf)
    return cn_buf
Ejemplo n.º 7
0
    def translate_normal(self, content, language):
        js = Py4Js()

        tk = js.getTk(content)
        #print('#英文#:' + content)
        cn_buf = self.translate_core(content, tk, language)
        #print('#中文#:' + cn_buf)

        return cn_buf
Ejemplo n.º 8
0
    def content_format(self, content):
        js = Py4Js()
        tk = js.getTk(content)

        content = urllib.parse.quote(content)

        url = "http://translate.google.cn/translate_a/single?client=t" + "&sl=en&tl=zh-CN&hl=zh-CN&dt=at&dt=bd&dt=ex&dt=ld&dt=md&dt=qca" + "&dt=rw&dt=rm&dt=ss&dt=t&ie=UTF-8&oe=UTF-8&clearbtn=1&otf=1&pc=1" + "&srcrom=0&ssel=0&tsel=0&kc=2&tk=%s&q=%s" % (
            tk, content)
        return url
Ejemplo n.º 9
0
def main():
    js = Py4Js()

    while 1:
        content = input("输入待翻译内容:")

        if content == 'q!':
            break

        tk = js.getTk(content)
        translate(content, tk)
Ejemplo n.º 10
0
def main():
    js = Py4Js()
    tl = "en"  # tl是要翻译的目标语种,值参照ISO 639-1标准,如果翻译成中文"zh/zh-CN简体中文"
    # 读取需要翻译的文件
    # with open('chinese.zh.js', encoding="utf-8") as file_obj:
    #     for line in file_obj:
    #         data = line.strip('\n')
    #         result = translate(data, js, tl)  # 执行翻译
    #         print(result)
    result = translate("A very good app. Easy to use, reliable and hasn't given any problems.", js, "zh")  # 执行翻译
    print(result)
Ejemplo n.º 11
0
def Translate(context):
    s = requests.Session()
    if (isChinese(context) == 'True'):
        targe_lang = "en"
    else:
        targe_lang = "zh-cn"
    js = Py4Js()
    tk = js.getTk(context)
    context = urllib.parse.quote(context)
    url="http://translate.google.cn/translate_a/single?client=t&sl=auto&tl=%s&hl=en&" \
        "dt=at&dt=bd&dt=ex&dt=ld&dt=md&dt=qca&dt=rw&dt=rm&dt=ss&dt=t&ie=UTF-8&oe=UTF-8&otf=1&" \
        "ssel=3&tsel=3&kc=3&tk=%s&q=%s"%(targe_lang,tk,context)

    headers = {
        'Accept':
        'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
        'Accept-Encoding':
        'gzip, deflate, sdch',
        'Accept-Language':
        'en-US,en;q=0.8;zh-CN;q=0.6,zh;q=0.4',
        'Connection':
        'keep-alive',
        'DNT':
        '1',
        'Host':
        'translate.google.cn',
        'Upgrade-Insecure-Requests':
        '1',
        'User-Agent':
        'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2987.133 Mobile Safari/537.36'
    }

    try:
        r = s.get(url, headers=headers)
        s.close()
    except request.exceptions.ConnectionError:
        print("Connect Error , jump the line\n")
        return "opp!!!"

    if (targe_lang == "zh-cn"):
        out = ""
        result = r.text.split('"')
        loop = len(result)
        for a in range(1, int(loop / 2 - 2)):
            if (result[a] == "en"):
                break
            if (isChinese(result[a]) == 'True'):
                out += result[a]
        return out
    else:
        result = r.text.split(',')[0]
        result = result.lstrip('[]')
        return result
Ejemplo n.º 12
0
def trans_en(ori_content):  # 构造url,进行翻译

    content = urllib.parse.quote(ori_content)
    js = Py4Js()
    tk = js.getTk(ori_content)
    url = "https://translate.google.cn/translate_a/single?client=webapp&sl=auto&tl=en&hl=zh-CN&dt=at&dt=bd&dt=ex&dt=ld&dt=md&dt=qca&dt=rw&dt=rm&dt=ss&dt=t&dt=gt&clearbtn=1&otf=1&pc=1&ssel=3&tsel=0&kc=2&tk=%s&q=%s" % (
        tk, content)
    en_content = open_url(url)
    end = en_content.find("\",")
    if end > 4:
        end_content = en_content[4:end]  #  最终翻译的结果
        return end_content
Ejemplo n.º 13
0
def main():
    js = Py4Js()
    # tl是要翻译的目标语种,值参照ISO 639-1标准,如果翻译成中文"zh/zh-CN简体中文"
    tl = "zh"
    # 读取需要翻译的文件
    with open('text.en.js', encoding="utf-8") as file_obj:
        for line in file_obj:
            data = line.split(":", 1)
            if len(data) == 2:
                tk = js.getTk(data[1])
                translate(data, tk, tl)
            else:
                print("Illegal row data")
Ejemplo n.º 14
0
    def google_translate(self, content):
        """
		功能:谷歌翻译
		参数:传入要翻译的内容
		"""
        self._content = content.replace('\n', ' ')  # 待翻译的文本,除去回车提升翻译质量
        self._result = ''  # 翻译的结果

        # 判断要翻译的内容是否超出上限
        if len(self._content) > 4891:
            return '超出文本上限,请缩减待翻译文本!'
        # print(self._content)
        # self._content = urllib.parse.quote(self._content)  # 将文本url编码

        # 判断英汉互译的方向
        isCharacter = lambda x: ((x >= 'a') and (x <= 'z')) or (
            (x >= 'A') and (x <= 'Z'))  #是字母返回True
        if isCharacter(self._content[0]):
            sl = 'en'  # 从英语
            tl = 'zh-CN'  # 到汉语
        else:
            sl = 'zh-CN'
            tl = 'en'
        # 计算文本的tk值
        js = Py4Js()  # 初始化计算类
        tk = js.getTk(self._content)  # 获得tk值

        # 生成get请求的url
        url = "http://translate.google.cn/translate_a/single?client=t"\
        "&sl=%s&tl=%s&hl=zh-CN&dt=at&dt=bd&dt=ex&dt=ld&dt=md&dt=qca"\
        "&dt=rw&dt=rm&dt=ss&dt=t&ie=UTF-8&oe=UTF-8&clearbtn=1&otf=1&pc=1"\
        "&srcrom=0&ssel=0&tsel=0&kc=2&tk=%s" % (sl, tl, tk)
        # print(url)
        # 进行post请求
        headers = {}
        headers[
            'User-Agent'] = 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.75 Safari/537.36'
        data = {}
        data['q'] = self._content
        data = urllib.parse.urlencode(data).encode("utf-8")
        # print(data)
        req = urllib.request.Request(url=url, data=data,
                                     headers=headers)  # post请求
        # print(req)
        reponse = urllib.request.urlopen(req)
        html = reponse.read().decode('utf-8')
        target = json.loads(html)
        for index in range(len(target[0])):
            # print('-----------------------------------')
            # print(index)
            print(target[0][index][0], end=' ')  #输出结果中查看其结构,然后选定索引方式
Ejemplo n.º 15
0
def main():
    js = Py4Js()

    while 1:
        content = input("输入待翻译内容:")

        if content == 'q!':
            break

        tk = js.getTk(content)
        translate_1 = translate(content, tk)
        print(translate_1)
        with open("ohp", "a") as f:
            f.write("|%s \t %s|\n" % (content, translate_1))
Ejemplo n.º 16
0
def main():
    js = Py4Js()
    while True:
        content = input("[1.英译汉 2.汉译英 q!退出翻译]输入翻译内容:")
        choose = input("选择翻译选项:")
        if choose == 'q!':
            print("End")
            break
        elif choose == '1':
            tk = js.getTk(content)
            en_to_zn(content,tk)
        elif choose == '2':
            tk = js.getTk(content)
            zn_to_en(content,tk)
Ejemplo n.º 17
0
def main():

    old = ''

    para = ''

    while 1:

        time.sleep(1)

        pastes = gettext().decode('UTF-8',
                                  'ignore').strip().strip(b'\x00'.decode())
        pastes = pastes.replace('\r\n', ' ')

        if pastes == 'error.':
            continue

        if pastes != old:

            old = pastes[:]

            para = para + pastes

            if para[-1] != '.' and para[-1] != '?' and para[-1] != '!':
                continue

            #sentences = pastes.split('.')

            #import pdb
            #pdb.set_trace()

            sentences = re.split('[.?!;] ', para)

            para = ''

            for content in sentences:

                if content == 'q!':
                    break

                js = Py4Js()
                tk = js.getTk(content)
                translate(content, tk)
                print(content)
                print()

            print()
            print()
            print()
Ejemplo n.º 18
0
def main():
    js = Py4Js()
    # tl是要翻译的目标语种,值参照ISO 639-1标准,如果翻译成中文"zh/zh-CN简体中文"
    tl = "en"
    # 读取需要翻译的文件
    with open('D:/python/ch-en.txt', encoding="utf-8") as file_obj:
        for line in file_obj:
            data = line.split('  ')
            if len(data) == 1:
                print(data[0])
                tk = js.getTk(data[0])
                translate(data, tk, tl)
                print('success' + '\n')
            else:
                print('lose')
Ejemplo n.º 19
0
def combine_translate(tp):
    global fo
    tp = list(tp)
    term_en = tp[1]
    tk = Py4Js().getTk(term_en)
    t = random.choice([0.5, 0.45, 0.35, 0.25, 0.15, 0.05])
    time.sleep(t)
    try:
        translation = translate(tk, term_en)
        if not isinstance(translation, list) or len(translation) < 9:
            logger.error("请求翻译服务失败,返回了错误的结果,请检查....")
            result = 'NULL'
        else:
            result = extract_translation(translation)
        tp.append(result)
        new_line = '\t'.join(tp)+'\n'
        return new_line
    except Exception as e:
        return
def main():
    js = Py4Js()
    ls = ''
    while (True):
        # 如果剪贴板正在被占用
        try:
            cs = getText()
        except:
            time.sleep(1)
            cs = getText()
        print('cs', cs)
        if cs and cs != ls:
            print('准备翻译')
            content = getText()
            tk = js.getTk(content)
            res = translate(tk, content)
            setText(res)
            time.sleep(1)
            ls = res
Ejemplo n.º 21
0
def main():    
    js = Py4Js()
    original='zh-CN'
    target='en'
    number="1"
    number=choose()
    if number!="1" and number!="2":
        print("what you input is'not the right option,and now press again")
        number=choose()
    if number=="2":
        original='en'
        target='zh-CN'
    while 1:    
        content = input("输入待翻译内容:")    
            
        if content == '~':    
            break    
            
        tk = js.getTk(content)    
        translate(original,target,content,tk)    
Ejemplo n.º 22
0
def translate(content):
    if len(content) > 4891:
        print('翻译的长度超过4891 word 限制!\nPlease submit a shorter string')

    # 按照谷歌翻译页JavaScript代码生成验证tk验证码
    js = Py4Js()
    tk = js.getTk(content)

    param = {'tk': tk, 'q': content}

    result = requests.get(
        """http://translate.google.cn/translate_a/single?client=t&sl=en
        &tl=zh-CN&hl=zh-CN&dt=at&dt=bd&dt=ex&dt=ld&dt=md&dt=qca&dt=rw&dt=rm&dt=ss
        &dt=t&ie=UTF-8&oe=UTF-8&clearbtn=1&otf=1&pc=1&srcrom=0&ssel=0&tsel=0&kc=2""",
        params=param)
    #    return(result.json())
    result = result.json()
    clear_result = ''
    for text in result[0][0:-1]:
        clear_result = clear_result + text[0]
    return (clear_result)
Ejemplo n.º 23
0
def google_translate(content):
    # content是要翻译的内容
    # tl是要翻译的目标语种,值参照ISO 639-1标准,如果翻译成中文"zh/zh-CN简体中文"
    js = Py4Js()
    tl = "zh-CN"
    tk = js.getTk(content)
    # print(content)
    review = []
    if len(content) < 4000:
        trans_text = translate(content, tk, tl)
        time.sleep(0.5)
    else:
        review = content.split(u'.')
        temp = []
        temp.append(google_translate(".".join(review[0:(len(review) // 2)])))
        time.sleep(0.5)
        temp.append(
            google_translate(".".join(review[(len(review) // 2):len(review)])))
        time.sleep(0.5)
        trans_text = ".".join(temp)
    return trans_text
Ejemplo n.º 24
0
def main():
    js = Py4Js()

    #while 1:
    #   content = input("输入待翻译内容:")

    #   if content == 'q!':
    #      break
    content = readContent('.\\source.txt')
    print(content)
    print(30 * '*')
    sentences = getSentences(content)
    rs = open('result.txt', 'w', encoding='utf-8')
    for sentence in sentences:
        tk = js.getTk(sentence)
        result = translate(sentence, tk)
        print(300 * '*')
        print(result)
        if result:
            rs.write(result)
        time.sleep(0.3)

    rs.close()
def translation_tool(content):
    js = Py4Js()
    tk = js.getTk(content)
    if len(content) > 4891:
        print("翻译的长度超过限制!!!")
        return

    param = {'tk': tk, 'q': content}

    result = requests.get(
        """http://translate.google.cn/translate_a/single?client=t&sl=en 
        &tl=zh-CN&hl=zh-CN&dt=at&dt=bd&dt=ex&dt=ld&dt=md&dt=qca&dt=rw&dt=rm&dt=ss 
        &dt=t&ie=UTF-8&oe=UTF-8&clearbtn=1&otf=1&pc=1&srcrom=0&ssel=0&tsel=0&kc=2""",
        params=param)

    # 返回的结果为Json,解析为一个嵌套列表
    RE = (result.json()[0])
    result = ""
    for i in RE:
        if type(i[0]) == type("i"):
            result += i[0].split()[0].strip("\n")
    print(type(result))
    return result
Ejemplo n.º 26
0
def main():
    js = Py4Js()
    content = test_content
    tk = js.getTk(content)
    res = translate(tk, content)
    print(res)
Ejemplo n.º 27
0
def zh2en(content):
    js = Py4Js()

    tk = js.getTk(content)
    return translate(content, tk, sl='zh-CN', tl='en')
Ejemplo n.º 28
0
def en2zh(content):
    js = Py4Js()

    tk = js.getTk(content)
    return translate(content, tk)
Ejemplo n.º 29
0
def main():
    print("main...")
    js = Py4Js()  # JS执行器
    app_num = 0
    while app_num < len(app_id):
        # hl用于定于国家
        url = "https://play.google.com/store/apps/details?id=" + app_id[app_num] + "&showAllReviews=true&hl=cn"
        print(url)
        page_num = 0
        driver.get(url)
        show_more_exist = 1

        # show all reviews
        # 翻页请求评论
        while show_more_exist:
            show_more_exist = search_show_more()
            if show_more_exist:
                print("app name %s page %d:" % (app_id[app_num], page_num))
                page_num += 1
                driver.execute_script(click_show_more)
            if page_num >= 3:
                break

        """ 点击更多按钮
        # click all full review buttons
        full_reviews_len = len(driver.find_elements_by_class_name(full_reviews_tag))
        if (full_reviews_len > 0):
            for i in range(0, full_reviews_len):
                click_full_reviews = "var a = document.getElementsByClassName(\"" + full_reviews_tag + "\"); a[" + str(
                    i) + "].click();"
                driver.execute_script(click_full_reviews)
        # """

        print("start to match result")
        count = 0
        reviews = driver.find_elements_by_class_name(all_reviews_tag)
        users = driver.find_elements_by_class_name(users_tag)
        dates = []  # 日期
        user_name = []  # 用户名称
        # stars = driver.find_elements_by_class_name(stars_tag)  # 星等
        for user in users:
            dates.append(user.find_elements_by_class_name(dates_tag)[0].text)  # 日期
            user_name.append(user.find_elements_by_class_name(users_name_tag)[0].text)  # 用户名称
            # stars.append(user.find_elements_by_class_name(star_tag)[0])
        pass

        print("start to build json for " + app_id[app_num])
        for review, date, name in zip(reviews, dates, user_name):
            count += 1
            # msg_cn = translate(review.text, js, 'zh')
            # ls = [name, review.text, msg_cn, date]
            ls = [name, review.text, date]
            ls = [i.replace('\t', ' ') for i in ls]  # 删除特殊符号
            print('\t'.join(ls))
        pass
        """
        if len(final_json) > 0:
            output = json.dumps(final_json)
            fp = open("json_file/" + app_ip[app_num] + ".json", "w")
            fp.write(output)
            fp.close()
        # """
        app_num += 1
        pass

    driver.quit()  # 退出
    pass
Ejemplo n.º 30
0
def abc():
    js = Py4Js()  # JS执行器
    msg_cn = translate('A very good app. Easy to use, reliable and hasn\'t given any problems.', js, 'zh')
    print(msg_cn)
    pass