from nltk.corpus import wordnet
try:
wordnet.synsets('test')
except LookupError:
import nltk
nltk.download('wordnet')
# For more information see: https://www.nltk.org/data.html
def anti(word, fallback=None):
for i in wordnet.synsets(word):
for j in i.lemmas():
for k in j.antonyms():
return k.name()
return fallback or word
def wordmap(text, morph=anti):
out = []
bits = [i.isalpha() for i in text]
diff = [0] + [j-i for i, j in zip(bits[:-1], bits[1:])]
pos = 0
while True:
end = diff.index(-1, pos)
word = text[pos:end]
out.append(morph(word))
try:
pos = diff.index(1, end)
except ValueError:
break
out.append(text[end:pos])
out.append(text[end:])
return ''.join(out)
if __name__ == '__main__':
import sys
assert len(sys.argv) == 2, "Provide a text to be translated."
print(wordmap(sys.argv[1]))
% python main.py 'At length it was so enormous that by itself it filled a whole cart, and two oxen were required to draw it, and the farmer had not the least idea what he was to do with the turnip, or whether it would be a fortune to him or a misfortune. At last he thought, "If thou sellest it, what wilt thou get for it that is of any importance, and if thou eatest it thyself, why the small turnips would do thee just as much good; it would be better to take it to the King, and make him a present of it.'
At length it was so enormous that by itself it filled a whole cart, and two oxen were required to draw it, and the farmer had not the least idea what he was to do with the turnip, or whether it would be a fortune to him or a misfortune. At last he thought, "If thou sellest it, what wilt thou get for it that is of any importance, and if thou eatest it thyself, why the small turnips would do thee just as much good; it would be better to take it to the King, and make him a present of it.
At length it differ so enormous that by itself it empty a fractional cart, and two oxen differ obviate to push it, and the farmer lack not the most idea what he differ to unmake with the turnip, or whether it would differ a fortune to him or a good_fortune. At first he forget, "If thou sellest it, what wilt thou leave for it that differ of any unimportance, and if thou eatest it thyself, why the large turnips would unmake thee unjust as little evil; it would differ worsen to give it to the queen, and unmake him a future of it.