3

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)
3
  • We probably need to see the rest of your code to determine the problem. Commented Dec 1, 2014 at 20:48
  • 1
    Do you ever actually call main? Commented Dec 1, 2014 at 20:49
  • 1
    Just a general troubleshooting idea try putting in: import pdb;pdb.set_trace() at the beginning of main. Step through the code to see if anything of interest comes up. You can use next and one line functions while using pdb to inspect what is happening with the variables that are being passed. help while in pdb to see the other commands. Commented Dec 1, 2014 at 20:49

1 Answer 1

4

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()
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you. I was not calling main explicitly
Slightly more pythonic is calling main() within the following conditional if __name__ == '__main__': This makes it such that when your run the python file containing this, main will run. However, if you import that module elsewhere, main will not run. May not be necessary for your use case but definitely good to know.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.