3

Are there a site or a tool, which can go through a big text and replace every word with an antonym of it?

If there are none, then a tool, which translates into a hand-made language would help. I.e translator, which takes a custom dictionary as an input.

  • 8
    Words don't just simple antonyms, they're contextual, with some words having more than one antonym, and many words having none. To do this would require a very strong AI, more than I think probably exists yet. – curiousdannii Aug 11 at 10:00
  • 2
    @curiousdannii, words are complex, this is obvious, but how does this relate to my question? where do you see me asking for something, which works perfectly? The same you said could be said about a translation program before it started to exist. And very first auto-translators were disgusting, but still they helped people in a way. I have a specific question, which has a specific answer, please, don't add to my question something which was not there. – klm123 Aug 11 at 11:11
  • 3
    It's not a matter of doing it imperfectly, it's a matter of being able to do it at all. Though I get your point about machine translation, which would be comparably inaccurate. So maybe an incredibly crude auto-antonym program would be possible. – curiousdannii Aug 11 at 11:31
  • 1
    You want a natural way to leave no silence as its synonym? – Luke Sawczak Aug 11 at 12:04
  • 5
    To those voting to close this question as "language-specific grammar or usage question": it is not one. – LjL Aug 11 at 14:58
19
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.

  • 1
    Could make it marginally less crude (less "differ"ent?) by excluding stopwords from the process. – Luke Sawczak Aug 11 at 13:43
  • 4
    +1 for Oxen pushing a fractional cart, large turnips would unmake thee unjust as little evil and unmake him a future of it. – David Vogt Aug 11 at 14:53
  • Wouldn't if be better to read input from stdin/file? – val Aug 12 at 12:06
  • @val Sure, but that's a trivial change (just use fileinput.input()). – Draconis Aug 12 at 19:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.