Advertisement
gruntfutuk

writeProgramme

Jan 17th, 2018
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.02 KB | None | 0 0
  1. from nltk import word_tokenize
  2. from nltk.corpus import stopwords
  3. import nltk
  4.  
  5.  
  6. def writeprogramme(operation):
  7.     with open("programme.py", "a") as op:
  8.         op.write(operation + "\n")
  9.  
  10.  
  11. program = "I want to make a program which can divide, add, divide and subtract"
  12. tokens = word_tokenize(program)
  13. stop_words = set(stopwords.words('english'))
  14. remove_tokens = ["I", "want", "make", "program", ","]
  15. clean_tokens = [token for token in tokens
  16.                 if token not in stop_words
  17.                 and token not in remove_tokens]
  18.  
  19. print(f'Tokens to process: {", ".join(clean_tokens)}')
  20.  
  21. variables = {'a': 'a = 64', 'b': 'b = 10.0'}
  22. operations = {'add': 'print(a + b)',
  23.               'subtract': 'print(a - b)',
  24.               'multiple': 'print(a * b)',
  25.               'divide': 'print(a / b)'}
  26.  
  27. for variable in variables.values():
  28.     print(f'Variable: {variable}')
  29.     writeprogramme(variable)
  30.  
  31. for token in clean_tokens:
  32.     print(f'print(operation): {operations[token]}')
  33.     writeprogramme(operations[token])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement