Text Language detection in python

dataframe, pandas, python

Solution

In your loop you're overwriting the entire column by doing this:

df['Languagereveiw'] = lang

If you want to do this in a for loop use `iteritems`:

for index, row in df['Review'].iteritems():
    lang = detect(row) #detecting each row
    df.loc[index, 'Languagereveiw'] = lang

however, you can just ditch the loop and just do

df['Languagereveiw'] = df['Review'].apply(detect)

Which is syntactic sugar to execute your func on the entire column

Regarding your latter question about converting from language code to full description:

'en' to 'english',

look at polyglot

this provides the facility to detect language, get the language code, and the full description

Problem

I am trying to detect the language of the text that may consist of an unknown number of languages. The following code gives me different languages as answer NOTE: I reduced the review becuase it was giving the error during post "" are not allowed ``` print(detect(كانت جميله وممتعة للأطفال اولا حيث اماكن اللعبر)) print(detect(的马来西亚)) print(detect(Vi havde 2 perfekte dage i Legoland Malaysia)) print(detect(Wij hebben alleen gekozen voor het waterpark maar daar ben je vrijs snel doorheen. Super leuke glijbanen en overal ruimte om te zitten en te liggen. Misschien volgende keer een gecombineerd ticket kopen met ook toegang tot waterpark)) print(detect(This is a park thats just ok, nothing great to write home about. There is barely any shade, the weather is always really hot so they need to take this into consideration. The atractions are just meh. I would only go if you are a fan of lego, for the sculptures are nice.)) ``` Here is the output ``` ar zh-cn da nl en ``` But using the following loop, all reviews give me 'en' as result ``` from langdetect import detect import pandas as pd df = pd.read_excel('data.xls') # lang = [] for r in df.Review: lang = detect(r) df['Languagereveiw'] = lang ``` the output is 'en' for all five rows. Need guidance that where is the missing chain? Here is the sample data Secondly, How can I get the complete name of languages i.e. English for 'en'

Original source