0

I'm using the langdetect and it should return the probability/percentage of a certain language in a string which is something like [en:0.9999960343803843] for an English text. I want to check the language and the percentage and store them in variables to use them later but I can't do anything with it except printing it. the type seems to be <class 'langdetect.language.Language'>

lan="Otec matka syn."
lan=detect_langs(line)
print(lan)
print(type(lan[0]))

this code outputs

[pl:0.7142846922445223, fi:0.2857135474194883]
<class 'langdetect.language.Language'>

note: It's not json because i've tried json.loads(lan[0]) and an error says it should be a string not language

edit: as user696969 answered the solution was to save them in a dict

x=detect_langs(line)
lan={}
for lang in x:
    lan.update({lang.lang: lang.prob})
  • Based on the output shown by ```print(lan)``` detect_langs is evaluating line to be 99.9996% English. What did you want to do with the output? Can you show us what you'd like to be able to do using your example line? – itprorh66 Mar 26 '21 at 21:05
  • Also, take a look at [Python: How to determine the language?](https://stackoverflow.com/questions/39142778/python-how-to-determine-the-language) to assist you in getting desired results. – itprorh66 Mar 26 '21 at 21:07
  • Does this answer your question? [Python: How to determine the language?](https://stackoverflow.com/questions/39142778/python-how-to-determine-the-language) – itprorh66 Mar 26 '21 at 21:07
  • no it doesn't, I edited the example, sorry for picking a bad one. What i want to do with the line is based on the percentages that it gives. for example if more than 90% English save it on one file and if 60%<&90%< and 10%<&30% Spanish save it on another – Ahmad Wahbi Mar 27 '21 at 00:04

1 Answers1

2

Since they are language.Language object, you can convert each language data into dict type using the following code

from langdetect import detect_langs

line="Otec matka syn."
lan=[{lang.lang: lang.prob} for lang in detect_langs(line)]
print(lan)
print(type(lan[0]))

The expected output for lan would be

[{'fi': 0.8571392823357673}, {'pl': 0.14285943305652865}]

You can also store the entire list of languages into dictionary by replacing

lan=[{lang.lang: lang.prob} for lang in detect_langs(line)]

with

lan={lang.lang: lang.prob for lang in detect_langs(line)}

The expected output would be something like below

{'fi': 0.7142848220971209, 'pl': 0.2857147054811151}
tax evader
  • 2,082
  • 1
  • 7
  • 9
  • 1
    thank you it worked but I did a simpler solution inspired by your comment, never occurred to me that i can save them in a dict – Ahmad Wahbi Mar 27 '21 at 08:31