Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #! /usr/bin/env python
- # coding:utf-8
- import itertools
- import codecs
- import sys
- try:
- import argparse
- except:
- print 'In order to use this program, please install python-argparse package!'
- sys.exit()
- #Setting argument parser and help messages
- argpar = argparse.ArgumentParser(description='Scrambler v1.0 is a string permutation tool that gets all words of the same letters, and/or providing all possibilities! It also filters the output using a text-based dictionary! It support UTF-8; most languages are supported including Arabic!',
- epilog='Scrambler v1.0 by Mohammed A. Mustafa <Anxious.Nut@Gmail.com>')
- argpar.add_argument('--word','-w', metavar='Input', dest='input', default=None, help='User input, the source word that gets scrambled!')
- argpar.add_argument('--raw','-r', metavar='True', dest='raw', nargs='?', default=False, help='Data output, output will not be filtered with a dictionary; every combination will be printed if this option is added!')
- argpar.add_argument('-d', metavar='Location', dest='dict', default='/usr/share/dict/words', help='Location of an optional dictionary that will be used for filtering the output; defaults to Linux English dictionary, /usr/share/dict/words!')
- argpar.add_argument('--output','-o', metavar='File', dest='output', default=None, help='Saves the output in a UTF-8 text file at the specified location!')
- args = argpar.parse_args()
- #Loading dictionary
- if args.raw != True:
- try:
- opener = codecs.open(args.dict,'r','utf-8')
- except:
- print 'Error: Dictionary at {0} is not available!'.format(args.dict)
- sys.exit()
- try:
- words_raw = opener.read()
- words = words_raw.splitlines()
- opener.close()
- except:
- print 'Error: Failed to read dictionary!'
- sys.exit()
- #Getting input from user
- if args.input != None:
- word = args.input
- else:
- try:
- word = raw_input('Enter a word: ')
- except:
- print '\nError: Noting was entered; exiting!'
- sys.exit()
- #Permuting
- try:
- perms = list(itertools.permutations(list(word.decode('utf-8'))))
- except:
- print '\nError: Permutation failed or canceled! Please report this error if this is encountered continually!'
- sys.exit()
- #Filtering with/without loaded dictionary
- matching = []
- if args.raw == False: #w/o dictionary
- try:
- for match in perms:
- if (u''.join(match) in words) and (u''.join(match) not in matching):
- matching.append(u''.join(match))
- except:
- print '\nError: Script failed!'
- sys.exit()
- else: #w/ dictionary
- for match in perms:
- try: matching.append(u''.join(match))
- except:
- print '\nError: Script failed!'
- sys.exit()
- #Providing output for user
- if len(matching) != 0 and (args.output == None):
- print u'\n'.join(matching)
- elif len(matching) != 0 and (args.output != None):
- try:
- op = codecs.open(args.output,'w','utf-8')
- op.write(u'\n'.join(matching))
- op.close()
- except:
- print 'Error: File at {0} is not accessible!'.format(args.output)
- else:
- print 'None matched!'
- ################################ Scrambler v1.0 ################################
- # Written by: Mohammed A. Mustafa (AnxiousNut)
- # Email: <Anxious.Nut@Gmail.com>
- # Blog: http://AnxiousNut.wordpress.com
- # License: GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
- ################################################################################
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement