First of all, you must pray for purity as one of the prophets in the Old Testament said," As I knew I could not be continent unless God gave it, I prayed to the Lord". You must also practice devotion to Our Lady, the Virgin of virgins. Do the consecration to the Incarnate Wisdom according to the method of St. Louis de Montfort. You should also do fasting and mortification as Jesus said in the Gospel.
Another thing you can do, if you wear a medal, or crucifix; wrap the chain onto your non-dominant hand when you go to bed. Find a way to weave it into your fingers so that it doesn't cause circulation problems, but it can't come off by itself. Hold the crucifix in you hand as long as you're awake and pray and meditate until you fall asleep. If you wake up to any degree, you will notice the chain and remember why it's there. Resume praying until you fall asleep again or get up. If you don't become conscious enough notice the chain, then you're too out of it to be responsible.