language detection code in python

1.3k views Asked by At

So, we have built a language detection program in python that just detects different languages. Our code seems fine; there is no error but I am not getting the desired result. Whenever I run it on Eclipse, it runs and terminates giving us the running time and an "OK". It is supposed to print the language of the text written.

def compute_ratios(text):

   tokens = wordpunct_tokenize(text)
   words = [word.lower() for word in tokens]

   langratios = {}

   for language in stopwords.fileids():
       stopwords_set = set(stopwords.words(language))
       words_set = set (words)
       common_elements = words_set.intersection(stopwords_set)

   langratios[language] = len(common_elements)

   return langratios

def max_ratio(text):

  ratios = compute_ratios(text)

  mostLang = max(ratios , key=ratios.get)
  return mostLang

def main():

  text = "This is cool"
  x = max_ratio(text)
  print(x)
1

There are 1 answers

2
NPE On BEST ANSWER

Unlike in some other languages, main() is just like any other function in Python. If you want it to run, you have to explicitly call it:

def main():
  ...

main()