googletrans stopped working with detecting all languages as English - python

The problem I have here is googletrans API suddenly stopped working, just like this:
result = translator.translate('祝您新年快乐', src='zh-cn', dest='en')
result.text
Output:
'祝您新年快乐'
It should return English but just printed the original text. Then I checked what goes wrong. I found that googletrans detect all languages as english, like this:
print(translator.detect('이 문장은 한글로 쓰여졌습니다.'))
print(translator.detect('祝您新年快乐'))
Output:
Detected(lang=en, confidence=1)
Detected(lang=en, confidence=1)
Finally I checked if those languages are available in the library. It is.
print(googletrans.LANGUAGES)
output:
{'af': 'afrikaans', 'sq': 'albanian', 'am': 'amharic', 'ar': 'arabic', 'hy': 'armenian', 'az': 'azerbaijani', 'eu': 'basque', 'be': 'belarusian', 'bn': 'bengali', 'bs': 'bosnian', 'bg': 'bulgarian', 'ca': 'catalan', 'ceb': 'cebuano', 'ny': 'chichewa', 'zh-cn': 'chinese (simplified)', 'zh-tw': 'chinese (traditional)', 'co': 'corsican', 'hr': 'croatian', 'cs': 'czech', 'da': 'danish', 'nl': 'dutch', 'en': 'english', 'eo': 'esperanto', 'et': 'estonian', 'tl': 'filipino', 'fi': 'finnish', 'fr': 'french', 'fy': 'frisian', 'gl': 'galician', 'ka': 'georgian', 'de': 'german', 'el': 'greek', 'gu': 'gujarati', 'ht': 'haitian creole', 'ha': 'hausa', 'haw': 'hawaiian', 'iw': 'hebrew', 'he': 'hebrew', 'hi': 'hindi', 'hmn': 'hmong', 'hu': 'hungarian', 'is': 'icelandic', 'ig': 'igbo', 'id': 'indonesian', 'ga': 'irish', 'it': 'italian', 'ja': 'japanese', 'jw': 'javanese', 'kn': 'kannada', 'kk': 'kazakh', 'km': 'khmer', 'ko': 'korean', 'ku': 'kurdish (kurmanji)', 'ky': 'kyrgyz', 'lo': 'lao', 'la': 'latin', 'lv': 'latvian', 'lt': 'lithuanian', 'lb': 'luxembourgish', 'mk': 'macedonian', 'mg': 'malagasy', 'ms': 'malay', 'ml': 'malayalam', 'mt': 'maltese', 'mi': 'maori', 'mr': 'marathi', 'mn': 'mongolian', 'my': 'myanmar (burmese)', 'ne': 'nepali', 'no': 'norwegian', 'or': 'odia', 'ps': 'pashto', 'fa': 'persian', 'pl': 'polish', 'pt': 'portuguese', 'pa': 'punjabi', 'ro': 'romanian', 'ru': 'russian', 'sm': 'samoan', 'gd': 'scots gaelic', 'sr': 'serbian', 'st': 'sesotho', 'sn': 'shona', 'sd': 'sindhi', 'si': 'sinhala', 'sk': 'slovak', 'sl': 'slovenian', 'so': 'somali', 'es': 'spanish', 'su': 'sundanese', 'sw': 'swahili', 'sv': 'swedish', 'tg': 'tajik', 'ta': 'tamil', 'te': 'telugu', 'th': 'thai', 'tr': 'turkish', 'uk': 'ukrainian', 'ur': 'urdu', 'ug': 'uyghur', 'uz': 'uzbek', 'vi': 'vietnamese', 'cy': 'welsh', 'xh': 'xhosa', 'yi': 'yiddish', 'yo': 'yoruba', 'zu': 'zulu'}
Can someone help here by explaning why this problem happened all of a sudden? It works just 30 minutes ago. It's weird it stopped working without changing anything.

According to the documentation googletrans, https://pypi.org/project/googletrans/, "is an unofficial library using the web API of translate.google.com".
They specifically state:
Due to limitations of the web version of google translate, this API does not guarantee that the library would work properly at all times (so please use this library if you don’t care about stability)
and suggest to use the official Google Translate API (click here).
For further reading I highly suggest the following sources:
GoogleTrans Python not translating
https://pypi.org/project/googletrans/
https://py-googletrans.readthedocs.io/en/latest/
If you decide to switch to the official API check out: https://cloud.google.com/translate/docs

Related

For Angel Broking SmartAPI, does anyone know how to store the NSE live stock market feed coming in JSON file into a dictionary while updating it?

I've been trying to create an algotrade bot in the Angel Broking SmartAPI. I'm getting live data from the API. I'm just confused as to how to store the live data feed that is thrown as output from the API.
FEED_TOKEN= feedToken
CLIENT_CODE=clientID
token="nse_cm|3063" # For Vedanta
task="mw"
ss = SmartWebSocket(FEED_TOKEN, CLIENT_CODE)
def on_message(ws, message):
print("Ticks: {}".format(message))
def on_open(ws):
print("on open")
ss.subscribe(task,token)
def on_error(ws, error):
print(error)
def on_close(ws):
print("Close")
def on_tick(ws, tick):
print("Ticks: {}".format(tick))
def on_connect():
print('Connected')
# Assign the callbacks.
ss._on_open = on_open
ss._on_connect = on_connect
ss._on_message = on_message
ss._on_error = on_error
ss._on_close = on_close
ss.connect()
This is the function that spits the output. Sample of the output is below.
Ticks: [{'e': 'nse_cm', 'ltp': '309.85', 'ltq': '56', 'ltt': 'NA', 'name': 'sf', 'tk': '3063'}]
Ticks: [{'name': 'tm', 'tvalue': '10/08/2021 10:41:55'}]
Ticks: [{'e': 'nse_cm', 'ltp': '309.90', 'ltq': '100', 'ltt': 'NA', 'name': 'sf', 'tk': '3063'}]
Ticks: [{'ap': '310.23', 'bp': '309.85', 'bq': '809', 'bs': '6725', 'c': '309.55', 'cng': '00.35', 'e': 'nse_cm', 'lo': '308.10', 'ltp': '309.90', 'ltq': '100', 'ltt': '10/08/2021 10:41:55', 'name': 'sf', 'nc': '00.1131', 'sp': '310.00', 'tbq': '754644', 'tk': '3063', 'to': '677822767.92', 'tsq': '1047420', 'v': '2184904'}]
Ticks: [{'name': 'tm', 'tvalue': '10/08/2021 10:41:56'}]
Ticks: [{'e': 'nse_cm', 'ltp': '309.85', 'ltq': '1', 'ltt': 'NA', 'name': 'sf', 'tk': '3063'}]
Ticks: [{'name': 'tm', 'tvalue': '10/08/2021 10:41:57'}]
Ticks: [{'e': 'nse_cm', 'ltp': '309.85', 'ltq': '95', 'ltt': 'NA', 'name': 'sf', 'tk': '3063'}]
Ticks: [{'name': 'tm', 'tvalue': '10/08/2021 10:41:58'}]
Ticks: [{'ap': '310.23', 'bp': '309.85', 'bq': '150', 'bs': '6725', 'c': '309.55', 'cng': '00.30', 'e': 'nse_cm', 'lo': '308.10', 'ltp': '309.85', 'ltq': '95', 'ltt': '10/08/2021 10:41:56', 'name': 'sf', 'nc': '00.0969', 'sp': '310.00', 'tbq': '748288', 'tk': '3063', 'to': '678133308.15', 'tsq': '1048198', 'v': '2185905'}]
Ticks: [{'e': 'nse_cm', 'ltp': '310.00', 'ltq': '200', 'ltt': 'NA', 'name': 'sf', 'tk': '3063'}]
Ticks: [{'name': 'tm', 'tvalue': '10/08/2021 10:41:59'}]
How do I store this data, where I don't know where is the output coming from in the API.
PS. This uses websockets, and you can find the details at
https://github.com/angelbroking-github/smartapi-python
Documentation Link:
https://smartapi.angelbroking.com/docs/WebSocketStreaming
If you want to store the data as a variable within the program itself you can append the json data to a list in the on_message function

Nested Python Object to CSV

I looked up "nested dict" and "nested list" but either method work.
I have a python object with the following structure:
[{
'id': 'productID1', 'name': 'productname A',
'option': {
'size': {
'type': 'list',
'name': 'size',
'choices': [
{'value': 'M'},
]}},
'variant': [{
'id': 'variantID1',
'choices':
{'size': 'M'},
'attributes':
{'currency': 'USD', 'price': 1}}]
}]
what i need to output is a csv file in the following, flattened structure:
id, productname, variantid, size, currency, price
productID1, productname A, variantID1, M, USD, 1
productID1, productname A, variantID2, L, USD, 2
productID2, productname A, variantID3, XL, USD, 3
i tried this solution: Python: Writing Nested Dictionary to CSV
or this one: From Nested Dictionary to CSV File
i got rid of the [] around and within the data and e.g. i used this code snippet from 2 and adapted it to my needs. IRL i can't get rid of the [] because that's simple the format i get when calling the API.
with open('productdata.csv', 'w', newline='', encoding='utf-8') as output:
writer = csv.writer(output, delimiter=';', quotechar = '"', quoting=csv.QUOTE_NONNUMERIC)
for key in sorted(data):
value = data[key]
if len(value) > 0:
writer.writerow([key, value])
else:
for i in value:
writer.writerow([key, i, value])
but the output is like this:
"id";"productID1"
"name";"productname A"
"option";"{'size': {'type': 'list', 'name': 'size', 'choices': {'value': 'M'}}}"
"variant";"{'id': 'variantID1', 'choices': {'size': 'M'}, 'attributes': {'currency': 'USD', 'price': 1}}"
anyone can help me out, please?
thanks in advance
list indices must be integers not strings
The following presents a visual example of a python list:
0 carrot.
1 broccoli.
2 asparagus.
3 cauliflower.
4 corn.
5 cucumber.
6 eggplant.
7 bell pepper
0, 1, 2 are all "indices".
"carrot", "broccoli", etc... are all said to be "values"
Essentially, a python list is a machine which has integer inputs and arbitrary outputs.
Think of a python list as a black-box:
A number, such as 5, goes into the box.
you turn a crank handle attached to the box.
Maybe the string "cucumber" comes out of the box
You got an error: TypeError: list indices must be integers or slices, not str
There are various solutions.
Convert Strings into Integers
Convert the string into an integer.
listy_the_list = ["carrot", "broccoli", "asparagus", "cauliflower"]
string_index = "2"
integer_index = int(string_index)
element = listy_the_list[integer_index]
so yeah.... that works as long as your string-indicies look like numbers (e.g. "456" or "7")
The integer class constructor, int(), is not very smart.
For example, x = int("3 ") will produce an error.
You can try x = int(strying.strip()) to get rid of leading and trailing white-space characters.
Use a Container which Allows Keys to be Strings
Long ago, before before electronic computers existed, there were various types of containers in the world:
cookie jars
muffin tins
carboard boxes
glass jars
steel cans.
back-packs
duffel bags
closets/wardrobes
brief-cases
In computer programming there are also various types of "containers"
You do not have to use a list as your container, if you do not want to.
There are containers where the keys (AKA indices) are allowed to be strings, instead of integers.
In python, the standard container which like a list, but where the keys/indices can be strings, is a dictionary
thisdict = {
"make": "Ford",
"model": "Mustang",
"year": 1964
}
thisdict["brand"] == "Ford"
If you want to index into a container using strings, instead of integers, then use a dict, instead of a list
The following is an example of a python dict which has state names as input and state abreviations as output:
us_state_abbrev = {
'Alabama': 'AL',
'Alaska': 'AK',
'American Samoa': 'AS',
'Arizona': 'AZ',
'Arkansas': 'AR',
'California': 'CA',
'Colorado': 'CO',
'Connecticut': 'CT',
'Delaware': 'DE',
'District of Columbia': 'DC',
'Florida': 'FL',
'Georgia': 'GA',
'Guam': 'GU',
'Hawaii': 'HI',
'Idaho': 'ID',
'Illinois': 'IL',
'Indiana': 'IN',
'Iowa': 'IA',
'Kansas': 'KS',
'Kentucky': 'KY',
'Louisiana': 'LA',
'Maine': 'ME',
'Maryland': 'MD',
'Massachusetts': 'MA',
'Michigan': 'MI',
'Minnesota': 'MN',
'Mississippi': 'MS',
'Missouri': 'MO',
'Montana': 'MT',
'Nebraska': 'NE',
'Nevada': 'NV',
'New Hampshire': 'NH',
'New Jersey': 'NJ',
'New Mexico': 'NM',
'New York': 'NY',
'North Carolina': 'NC',
'North Dakota': 'ND',
'Northern Mariana Islands':'MP',
'Ohio': 'OH',
'Oklahoma': 'OK',
'Oregon': 'OR',
'Pennsylvania': 'PA',
'Puerto Rico': 'PR',
'Rhode Island': 'RI',
'South Carolina': 'SC',
'South Dakota': 'SD',
'Tennessee': 'TN',
'Texas': 'TX',
'Utah': 'UT',
'Vermont': 'VT',
'Virgin Islands': 'VI',
'Virginia': 'VA',
'Washington': 'WA',
'West Virginia': 'WV',
'Wisconsin': 'WI',
'Wyoming': 'WY'
}
i could actually iterate this list and create my own sublist, e.g. e list of variants
data = [{
'id': 'productID1', 'name': 'productname A',
'option': {
'size': {
'type': 'list',
'name': 'size',
'choices': [
{'value': 'M'},
]}},
'variant': [{
'id': 'variantID1',
'choices':
{'size': 'M'},
'attributes':
{'currency': 'USD', 'price': 1}}]
},
{'id': 'productID2', 'name': 'productname B',
'option': {
'size': {
'type': 'list',
'name': 'size',
'choices': [
{'value': 'XL', 'salue':'XXL'},
]}},
'variant': [{
'id': 'variantID2',
'choices':
{'size': 'XL', 'size2':'XXL'},
'attributes':
{'currency': 'USD', 'price': 2}}]
}
]
new_list = {}
for item in data:
new_list.update(id=item['id'])
new_list.update (name=item['name'])
for variant in item['variant']:
new_list.update (varid=variant['id'])
for vchoice in variant['choices']:
new_list.update (vsize=variant['choices'][vchoice])
for attribute in variant['attributes']:
new_list.update (vprice=variant['attributes'][attribute])
for option in item['option']['size']['choices']:
new_list.update (osize=option['value'])
print (new_list)
but the output is always the last item of the iteration, because i always overwrite new_list with update().
{'id': 'productID2', 'name': 'productname B', 'varid': 'variantID2', 'vsize': 'XXL', 'vprice': 2, 'osize': 'XL'}
here's the final solution which worked for me:
data = [{
'id': 'productID1', 'name': 'productname A',
'variant': [{
'id': 'variantID1',
'choices':
{'size': 'M'},
'attributes':
{'currency': 'USD', 'price': 1}},
{'id':'variantID2',
'choices':
{'size': 'L'},
'attributes':
{'currency':'USD', 'price':2}}
]
},
{
'id': 'productID2', 'name': 'productname B',
'variant': [{
'id': 'variantID3',
'choices':
{'size': 'XL'},
'attributes':
{'currency': 'USD', 'price': 3}},
{'id':'variantID4',
'choices':
{'size': 'XXL'},
'attributes':
{'currency':'USD', 'price':4}}
]
}
]
for item in data:
for variant in item['variant']:
dic = {}
dic.update (ProductID=item['id'])
dic.update (Name=item['name'].title())
dic.update (ID=variant['id'])
dic.update (size=variant['choices']['size'])
dic.update (Price=variant['attributes']['price'])
products.append(dic)
keys = products[0].keys()
with open('productdata.csv', 'w', newline='', encoding='utf-8') as output_file:
dict_writer = csv.DictWriter(output_file, keys,delimiter=';', quotechar = '"', quoting=csv.QUOTE_NONNUMERIC)
dict_writer.writeheader()
dict_writer.writerows(products)
with the following output:
"ProductID";"Name";"ID";"size";"Price"
"productID1";"Productname A";"variantID1";"M";1
"productID1";"Productname A";"variantID2";"L";2
"productID2";"Productname B";"variantID3";"XL";3
"productID2";"Productname B";"variantID4";"XXL";4
which is exactly what i wanted.

Dynamically naming saved dataframes in loop

I'm attempting to use the GTab package to query Google Search trends data for every state in the US, but am having some trouble getting my loop to work.
For one state it's easy enough to do this, and new_query produces a dataframe.
t = gtab.GTAB()
t.set_options(pytrends_config={"geo": "US-NY", "timeframe": "2020-09-01 2020-10-01"})
query = t.new_query("weather")
To loop through I'm trying to use a dict to assign geo dynamically. However, I can't figure out how to do the same for the df name (query).
state_abbrevs = {
'Alabama': 'AL',
'Alaska': 'AK',
'Arizona': 'AZ',
'Arkansas': 'AR',
'California': 'CA',
'Colorado': 'CO',
'Connecticut': 'CT',
'Delaware': 'DE',
'District of Columbia': 'DC',
'Florida': 'FL',
'Georgia': 'GA',
'Guam': 'GU',
'Hawaii': 'HI',
'Idaho': 'ID',
'Illinois': 'IL',
'Indiana': 'IN',
'Iowa': 'IA',
'Kansas': 'KS',
'Kentucky': 'KY',
'Louisiana': 'LA',
'Maine': 'ME',
'Maryland': 'MD',
'Massachusetts': 'MA',
'Michigan': 'MI',
'Minnesota': 'MN',
'Mississippi': 'MS',
'Missouri': 'MO',
'Montana': 'MT',
'Nebraska': 'NE',
'Nevada': 'NV',
'New Hampshire': 'NH',
'New Jersey': 'NJ',
'New Mexico': 'NM',
'New York': 'NY',
'North Carolina': 'NC',
'North Dakota': 'ND',
'Northern Mariana Islands':'MP',
'Ohio': 'OH',
'Oklahoma': 'OK',
'Oregon': 'OR',
'Pennsylvania': 'PA',
'Puerto Rico': 'PR',
'Rhode Island': 'RI',
'South Carolina': 'SC',
'South Dakota': 'SD',
'Tennessee': 'TN',
'Texas': 'TX',
'Utah': 'UT',
'Vermont': 'VT',
'Virgin Islands': 'VI',
'Virginia': 'VA',
'Washington': 'WA',
'Washington DC' : 'DC',
'West Virginia': 'WV',
'Wisconsin': 'WI',
'Wyoming': 'WY'
}
for v in state_abbrevs.values():
t = gtab.GTAB()
t.set_options(pytrends_config={"geo": f"US-{v}", "timeframe": "2020-09-01 2020-10-01"})
query = t.new_query("weather")
I've tried using an f string but that produces SyntaxError: can't assign to literal.
I used two answers from here. I think your best option is just storing the DataFrames in a dictionary but this should work to create your query_* variables.
query_dict = {}
for n, v in enumerate(state_abbrevs.values()):
t = gtab.GTAB()
t.set_options(pytrends_config={"geo": f"US-{v}", "timeframe": "2020-09-01 2020-10-01"})
query = t.new_query("weather")
key = "query_" + str(n)
query_dict[key] = query
for k in query_dict.keys():
exec("%s = query_dict['%s']" % (k,k))

How to convert a string of strings into a list?

I have the string
"Abarth", "AC", "Aixam", "Ak", "Alfa%20Romeo", "Alpine", "Ariel", "Aston%20Martin", "Audi", "Austin", "Bac", "Beauford", "Bentley", "BMW", "Bristol", "Bugatti", "Buick", "Cadillac", "Caterham", "Chesil", "Chevrolet", "Chrysler", "Citroen", "Corvette", "Cupra", "Custom%20Vehicle", "Dacia", "Daewoo", "Daihatsu", "Daimler", "Datsun", "DAX", "Dodge", "DS%20AUTOMOBILES", "Ferrari", "Fiat", "Ford", "GMC", "Great%20Wall", "Holden", "Honda", "Humber", "Hummer", "Hyundai", "Infiniti", "Isuzu", "Iveco", "Jaguar", "Jeep", "Jensen", "Kia", "Koenigsegg", "KTM", "Lamborghini", "Lancia", "Land%20Rover", "Levc", "Lexus", "Leyland", "Lincoln", "Lotus", "Maserati", "Maybach", "Mazda", "McLaren", "Mercedes-Benz", "MG", "MINI", "Mitsubishi", "Morgan", "Morris", "Nissan", "Noble", "Opel", "Perodua", "Peugeot", "Pilgrim", "Plymouth", "Pontiac", "Porsche", "Proton", "Radical", "Raw", "Reliant", "Renault", "Replica", "Riley", "Robin%20Hood", "Rolls-Royce", "Rover", "Saab", "SEAT", "SKODA", "Smart", "Ssangyong", "Standard", "Subaru", "Sunbeam", "Suzuki", "Tesla", "Tiger", "Toyota", "Triumph", "TVR", "Vauxhall", "Volkswagen", "Volvo", "Westfield", "Yamaha", "Zenos"
which is stored by the variable "list_of_car_makes" in my python project
but I want to convert the string into a list, how would I do that?
when I use print(list_of_car_makes[0]) it prints the first character in the string, the ",
what i'd like to be able to print is Abarth when using that print function.
Okay you have other weird characters in there what I'm going to ignore (%20 is a space, but you can work out how to fix that)
Your issue to the letter would be solved by:
your_string_here.replace('"', '').split(',')
Try this:
makes = []
word = ""
for c in list_of_car_makes:
if c in " ,":
continue
if c == '"':
if word:
makes.append(word)
word = ""
continue
word += c
print(makes)
Output:
['Abarth', 'AC', 'Aixam', 'Ak', 'Alfa%20Romeo', 'Alpine', 'Ariel', 'Aston%20Martin', 'Audi', 'Austin', 'Bac', 'Beauford', 'Bentley', 'BMW', 'Bristol', 'Bugatti', 'Buick', 'Cadillac', 'Caterham', 'Chesil', 'Chevrolet', 'Chrysler', 'Citroen', 'Corvette', 'Cupra', 'Custom%20Vehicle', 'Dacia', 'Daewoo', 'Daihatsu', 'Daimler', 'Datsun', 'DAX', 'Dodge', 'DS%20AUTOMOBILES', 'Ferrari', 'Fiat', 'Ford', 'GMC', 'Great%20Wall', 'Holden', 'Honda', 'Humber', 'Hummer', 'Hyundai', 'Infiniti', 'Isuzu', 'Iveco', 'Jaguar', 'Jeep', 'Jensen', 'Kia', 'Koenigsegg', 'KTM', 'Lamborghini', 'Lancia', 'Land%20Rover', 'Levc', 'Lexus', 'Leyland', 'Lincoln', 'Lotus', 'Maserati', 'Maybach', 'Mazda', 'McLaren', 'Mercedes-Benz', 'MG', 'MINI', 'Mitsubishi', 'Morgan', 'Morris', 'Nissan', 'Noble', 'Opel', 'Perodua', 'Peugeot', 'Pilgrim', 'Plymouth', 'Pontiac', 'Porsche', 'Proton', 'Radical', 'Raw', 'Reliant', 'Renault', 'Replica', 'Riley', 'Robin%20Hood', 'Rolls-Royce', 'Rover', 'Saab', 'SEAT', 'SKODA', 'Smart', 'Ssangyong', 'Standard', 'Subaru', 'Sunbeam', 'Suzuki', 'Tesla', 'Tiger', 'Toyota', 'Triumph', 'TVR', 'Vauxhall', 'Volkswagen', 'Volvo', 'Westfield', 'Yamaha', 'Zenos']
To remove the %20 (and other codes, add this bit of code):
for j in range(len(makes)):
while "%" in makes[j]:
i = makes[j].find("%")
s = int(makes[j][i+1:i+3], base=16)
m = list(makes[j])
del m[i:i+3]
m.insert(i, chr(s))
makes[j] = ''.join(m)
Output with this code:
['Abarth', 'AC', 'Aixam', 'Ak', 'Alfa Romeo', 'Alpine', 'Ariel', 'Aston Martin', 'Audi', 'Austin', 'Bac', 'Beauford', 'Bentley', 'BMW', 'Bristol', 'Bugatti', 'Buick', 'Cadillac', 'Caterham', 'Chesil', 'Chevrolet', 'Chrysler', 'Citroen', 'Corvette', 'Cupra', 'Custom Vehicle', 'Dacia', 'Daewoo', 'Daihatsu', 'Daimler', 'Datsun', 'DAX', 'Dodge', 'DS AUTOMOBILES', 'Ferrari', 'Fiat', 'Ford', 'GMC', 'Great Wall', 'Holden', 'Honda', 'Humber', 'Hummer', 'Hyundai', 'Infiniti', 'Isuzu', 'Iveco', 'Jaguar', 'Jeep', 'Jensen', 'Kia', 'Koenigsegg', 'KTM', 'Lamborghini', 'Lancia', 'Land Rover', 'Levc', 'Lexus', 'Leyland', 'Lincoln', 'Lotus', 'Maserati', 'Maybach', 'Mazda', 'McLaren', 'Mercedes-Benz', 'MG', 'MINI', 'Mitsubishi', 'Morgan', 'Morris', 'Nissan', 'Noble', 'Opel', 'Perodua', 'Peugeot', 'Pilgrim', 'Plymouth', 'Pontiac', 'Porsche', 'Proton', 'Radical', 'Raw', 'Reliant', 'Renault', 'Replica', 'Riley', 'Robin Hood', 'Rolls-Royce', 'Rover', 'Saab', 'SEAT', 'SKODA', 'Smart', 'Ssangyong', 'Standard', 'Subaru', 'Sunbeam', 'Suzuki', 'Tesla', 'Tiger', 'Toyota', 'Triumph', 'TVR', 'Vauxhall', 'Volkswagen', 'Volvo', 'Westfield', 'Yamaha', 'Zenos']
I would suggest splitting by your delimiter then using str.split as you can provide it with characters to trim from the ends of a string.
[s.strip('"') for s in your_string.split(', ')]
You can even replace %20 with a space this way.
[s.strip('"').replace('%20', ' ') for s in your_string.split(', ')]
Try it here!

How do you code a translator with language user can choose

Im creating a webapp that takes an image, reads the text inside of it, and translates that text into another language. However right now you can't chnage the language the text is going to be changed into on the website. How would I do this
Right now I've got the source language as english and the target language as german (which is working). However, i've created a dictionary that has all languages and language codes supported by google translate, and ive put this in a dropdown form. how to I link the input from this back to python and make it the target language
PYTHON
def Lang_target():
language_targ={
'af': 'Afrikaans', 'sq':'Albanian', 'ar': 'Arabic', 'az': 'Azerbaijani',
'be': 'Belarusian', 'bn': 'Bengali', 'ca': 'Catalan', 'zh-CN': 'Chinese Simplified',
'zh-TW': 'Chinese Traditional', 'hr': 'Croatian', 'cs': 'Czech', 'da': 'Danish',
'nl': 'Dutch', 'en': 'English', 'eo': 'Esperanto', 'et': 'Estonian',
'tl': 'Filipino', 'fi': 'Finnish', 'fr': 'French', 'gl': 'Galician',
'ka': 'Georgian', 'de': 'German', 'el': 'Greek', 'gu': 'Gujarati',
'ht': 'Haitian Creole', 'iw': 'Hebrew', 'hi': 'Hindi', 'hu': 'Hungarian',
'is': 'Icelandic', 'ga': 'Irish', 'it': 'Italian', 'id': 'Indonesian',
'ja': 'Japanese', 'kn': 'Kannada', 'ko': 'Korean', 'la': 'Latin',
'lv': 'Latvian', 'lt': 'Lithuanian', 'mk': 'Macedonian', 'ms': 'Malay',
'mt': 'Maltese', 'no': 'Norwegian', 'fa': 'Persian', 'pl': 'Polish',
'pt': 'Portuguese', 'ro': 'Romanian', 'ru': 'Russian', 'sr': 'Serbian',
'sk': 'Slovak', 'es': 'Spanish', 'sl': 'Slovenian', 'sw': 'Swahili',
'sv': 'Swedish', 'ta': 'Tamil', 'te': 'Telugu', 'th': 'Thai',
'tr': 'Turkish', 'uk': 'Ukrainian', 'ur': 'Urdu', 'vi': 'Vietnamese',
'cy': 'Welsh', 'yi': 'Yiddish',
}
return language_targ
#app.route('/selectImage')
def selectImage():
fn = image_name()
language_target = Lang_target()
return render_template("selectImage.html", image_name=image_name, fn=fn, language_target=language_target)
#app.route('/getfileHelper', methods=['GET','POST'])
def getfileHelper():
if request.method == 'POST':
file = request.files['imgfile']
filename = secure_filename(file.filename) #from werkzeug import secure_filename
selectImage.html page
if file.filename == '':
flash("No file selected. Please select an image file")
return render_template('selectImage.html')
texts = detect_text('static/images/'+filename)
text_translations = [] #emty list for dictionary of original text and translation
for text in texts:
translate_client = translate.Client()
translate_text = text.description
source = 'en'
target = 'de'
translation = translate_client.translate(translate_text, source_language=source, target_language=target)
text_translations.append({'text':translate_text, 'translation':translation['translatedText']})
db_append(filename, translate_text, translation['translatedText'])
return render_template('home.html', filename=filename, text_translations=text_translations)
HTML
<form>
<select>
{% for x in language_target%}
<option> {{ language_target[x] }}</option>
{% endfor %}
</select>
<input type="submit" value="Submit">
</form>
If you add a name attribute to the select (for example, name="lang_target"), you can retrieve the value of the dropdown from the request in request.args["lang_target"] for GET (as you did not specify POST). I am not sure which application route performs the translations, but you should direct the request to that route.

Categories

Resources