Exemplo n.º 1
0
def translate(str, name):
    if name == "google":
        return google(str, dst='zh-CN', proxies={'http': '127.0.0.1:1080'})
    elif name == "baidu":
        return baidu(str, dst='zh-CN')
    elif name == "youdao":
        return youdao(str, dst='zh-CN')
Exemplo n.º 2
0
def search_wiki(keyword=''):

    keyword = google(keyword, dst='en')
    # running the query
    searchResults = wikipedia.search(keyword)
    # If there is no result, print no result
    if not searchResults:
        # print("No result from Wikipedia")
        return 'ไม่พบคำว่า {} ที่ท่านต้องการค้นหา กรุณาค้นหาใหม่อีกครั้งค่ะ'.format(
            keyword)
    # Search for page... try block
    try:
        page = wikipedia.page(searchResults[0])
    except wikipedia.DisambiguationError as err:
        page = wikipedia.page(err.options[0])

    wikiTitle = page.title.encode('utf-8')
    wikiSummary = page.summary.encode('utf-8')
    res = google(wikiSummary, dst='th')
    return (res[0:180] + '......')
Exemplo n.º 3
0
from translation import Translation, baidu, google, youdao, iciba, bing

print(baidu('hello world!', dst='zh'))
print(youdao('hello world!', dst='zh-CN'))
print(iciba('hello world!', dst='zh'))
print(google('hello world!', dst='zh-CN'))
print(bing('hello world!', dst='zh-CHS'))
Exemplo n.º 4
0
    def format_text(self, text):
        if self.font_type in ['normal', 'handwritten']:
            text = unidecode(text)
        elif self.font_type == 'arabic':
            text = google(text, src='en', dst='ar')
            text = get_display(arabic_reshaper.reshape(text))
        elif self.font_type == 'chinese':
            text = google(text, src='en', dst='zh-CN')
        else:
            raise NotImplementedError

        width, height = self.width - 2 * self.padding, self.height - 2 * self.padding
        text = (text.upper() if self.uppercase else text).strip()
        if self.text_type == 'word':
            word_as_number = choice([True, False])
            if word_as_number:
                n_letter = randint(1, 5)
                result_text = str(randint(0, 10**n_letter - 1))
            else:
                words = text.split(' ')
                result_text = rand_choice(words)
                iteration = 1
                while (not str.isalnum(result_text)
                       or len(result_text) < 1) and iteration < 40:
                    result_text = rand_choice(words)
                    iteration += 1
                if not str.isalnum(result_text) or len(result_text) < 1:
                    result_text = words[0][:randint(4, 10)]
                line_width = self.font.getsize(result_text)[0]
                while line_width > width and len(result_text) > 2:
                    result_text = result_text[:-1]
                    line_width = self.font.getsize(result_text)[0]

        else:
            max_lines = 1 if self.uniline else self.n_max_lines
            result_text, lines = '', ''
            text_height, cur_idx, n_lines = 0, 0, -1
            while text_height <= height:
                result_text = lines
                n_lines += 1
                line = text[cur_idx:].lstrip()
                cur_idx += len(text[cur_idx:]) - len(
                    line)  # adjust cur_idx if stripped
                if len(line) == 0 or n_lines == max_lines:
                    break
                line_width = self.font.getsize(line)[0]
                avg_char_width = line_width / len(line)
                if line_width > width:
                    index = int(
                        width / avg_char_width
                    ) + 10  # take larger slice in case of small characters
                    cut = max(line[:index].rfind(' '), line.find(
                        ' '))  # in case no space found in slice (small width)
                    line = line[:cut].strip()
                    line_width = self.font.getsize(line)[0]
                while line_width > width:
                    if ' ' in line:  # remove word by word
                        line = line[:line.rfind(' ')].strip()
                    else:  # remove character by character
                        line = line[:-1]
                    line_width = self.font.getsize(line)[0]

                cur_idx += len(line) + 1
                if self.justified:
                    w_space = self.font.getsize(' ')[0]
                    n_spaces = line.count(' ')
                    n_spaces_to_add = (width - line_width) // w_space
                    if n_spaces > 0 and n_spaces_to_add > 0:
                        q, r = n_spaces_to_add // n_spaces + 1, n_spaces_to_add % n_spaces
                        if q < 5:
                            if q > 1:
                                line = line.replace(' ', q * ' ')
                            pos = 0
                            while r > 0:
                                space_idx = line[pos:].find(' ') + pos
                                line = line[:space_idx] + ' ' + line[space_idx:]
                                pos = space_idx + q + 1
                                r -= 1
                lines = '{}\n{}'.format(lines, line) if lines else line
                text_height = self.font.getsize_multiline(
                    lines, spacing=self.spacing)[1]

        if '\n' in result_text and self.justified:  # we dont want to justify the last line
            result_text, last_line = result_text.rsplit('\n', 1)
            last_line = ' '.join(last_line.split())
            result_text = '{}\n{}'.format(result_text, last_line)

        content_width, content_height = self.font.getsize_multiline(
            result_text, spacing=self.spacing)
        content_width += 2 * self.padding
        content_height += 2 * self.padding

        return result_text, content_width, content_height
Exemplo n.º 5
0
# -*- coding: utf-8 -*-

import time
from translation import baidu, google, youdao, iciba

fo = open("synset.txt", "r")
fw = open('synset-trans.txt', 'w')

print "File name: ", fo.name

ii = 0
for line in fo.readlines():
    line = line.strip()
    txtWords = (line[10:]).strip().encode('utf-8')
    idNum = line[:10].strip().encode('utf-8')
    trans = google(txtWords, dst='zh-TW')
    #trans = youdao(line[10:], dst = 'zh-CN')
    txtTrans = ("{} {}".format(idNum, trans.encode('utf-8')))
    print("{} {} --> {}".format(ii, line, txtTrans))
    fw.write(txtTrans + '\n')

    time.sleep(0.3)
    ii += 1

fo.close()
fw.close()

#print(google('This is a pen!', dst = 'zh-TW'))
Exemplo n.º 6
0
from translation import bing, baidu, iciba, google

print(bing('Hello World', dst = 'de'))
print(baid('Hello World', dst = 'de'))
print(iciba('Hello World' dst = 'de'))
print(google('Hello World', dst = 'de'))
bg    : Bulgarian,
uk    : Ukrainian,
hr    : Croatian,
bn    : Bengali,
sl    : Slovenian,
ht    : Haitian Creole,
da    : Danish,
fa    : Persian,
hi    : Hindi,
fi    : Finnish,
hu    : Hungarian,
ja    : Japanese,
ka    : Georgian,
te    : Telugu,
zh-TW : Chinese Traditional,
sq    : Albanian,
no    : Norwegian,
ko    : Korean,
kn    : Kannada,
mk    : Macedonian,
zh-CN : Chinese Simplified,
sk    : Slovak,
mt    : Maltese,
de    : German,
ms    : Malay,
sr    : Serbian
'''
from translation import baidu, google, youdao, iciba, bing
print(google('hello world!', dst='hi'))
#print(bing('hello world!', dst = 'hi'))
Exemplo n.º 8
0
from translation import Translation, baidu, google, youdao, iciba, bing

print(baidu('hello world!', dst = 'zh'))
print(youdao('hello world!', dst = 'zh-CN'))
print(iciba('hello world!', dst = 'zh'))
print(google('hello world!', dst = 'zh-CN'))
print(bing('hello world!', dst = 'zh-CHS'))