Skip to content

Instantly share code, notes, and snippets.

@serenamm
Created January 11, 2018 18:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save serenamm/07ae50bda77fc4cef28b035d049c5f60 to your computer and use it in GitHub Desktop.
Save serenamm/07ae50bda77fc4cef28b035d049c5f60 to your computer and use it in GitHub Desktop.
'''
Baidu for English to Canto characters
Chineseconverter.com for Canto characters to Jyutping
'''
def to_jyutping(sentence):
browser = initialize_browser()
browser.get("http://fanyi.baidu.com/#en/yue")
input_english = browser.find_element_by_id("baidu_translate_input")
input_english.send_keys(sentence)
sleep(3)# If I don't do sleep then it sometimes doesn't find the element
chinese_characters = browser.find_element_by_xpath('//*[@id="main-outer"]/div/div/div/div[2]/div[1]/div[2]/div/div/div[1]/p[2]').text
browser.close
browser = initialize_browser()
browser.get("https://www.chineseconverter.com/cantonesetools/en/cantonese-to-jyutping")
input_characters = browser.find_element_by_id("text")
input_characters.send_keys(chinese_characters) # This gives the problem
convert_button = browser.find_element_by_xpath("/html/body/div[3]/div[1]/div[2]/div/div[2]/form/div[3]/div/button").click()
jyutping = browser.find_element_by_xpath('/html/body/div[3]/div[1]/div[2]/div/div[2]/form/div[1]/div[2]/div/div/div/div').text
return jyutping, chinese_characters
def main():
sentence = input('Enter your input: ')
jyutping, chinese_characters = to_jyutping(sentence)
print("Traditional characters: " + chinese_characters)
print("Cantonese Jyutping: " + jyutping)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment