Tag: dictionary

Get dictionary from file/url – Python

Get dictionary from file:

This program get all word from any file

Used: import (sys, os, re) , if , try , for , except , else

get-word-from-file.png
To copy or see all commands you can click on Details button below

 

#!/usr/bin/python
”’
author: Hopeless
task: Write a program that takes a file a generates a wordlist
prog will get all words from any text file
”’

import sys, os, re

wordlist = []
# filename = raw_input(‘please enter filename to parse:’)

if len(sys.argv) > 1:
try:

filex = open(sys.argv[1],’r’)
for line in filex:
wordlist += re.findall(r'([0-9a-zA-Z_]{3,15})[\s\\\.]’,line)
#print wordlist

except Exception, e:
print e
exit(0)

filex.close()
wordset = set(wordlist)

print wordset
print ‘dic length is: ‘, len(wordset)

#save wordset to file -> newline for each item
realdic = open(‘realdic.txt’,’w’)
for word in wordset:
realdic.write(word+’\n’)

realdic.close()

 

hopeless@ubuntu:~/python$ wget https://en.wikipedia.org/wiki/Israel
hopeless@ubuntu:~/python$ ./baddic.py Israel
(all result save to file “realdic.txt”)


another way to get dictionary from file:

This program get all word from any file

Used: def , import (sys, os, re) , if , try , for , except , else

Screenshot from 2016-08-10 04:02:06.png
To copy or see all commands you can click on Details button below

 

#!/usr/bin/python
”’
author: Hopeless
task: prog will get all words from any text file
”’

import sys, os, re

if len(sys.argv) > 1:
try:
filex = open(sys.argv[1],’r’)

except Exception, e:
print e
exit(0)
# filename = raw_input(‘please enter filename to parse:’)

def text2dic(filename):
wordlist = []
for line in filename:
wordlist += re.findall(r'([0-9a-zA-Z_]{3,15})[\s\\\.]’,line)
wordset = set(wordlist)
return wordset

def savedic(listname, filename):
try:
realdic = open(filename,’w’)
for word in listname:
realdic.write(word+’\n’)
realdic.close()
except Exception, e:
print e
exit(0)

newdic = text2dic(filex)
savedic(newdic,’test1.dic’)

print newdic
print ‘dic length is: ‘, len(newdic)

filex.close()

 

hopeless@ubuntu:~/python$ wget https://en.wikipedia.org/wiki/Israel
hopeless@ubuntu:~/python$ ./dicfuncs.py Israel
(all result save to file “test1.dic”)


Get dictionary from url:

This program get all word from any url

Used: import (sys, os, re) , if , try , for , except , else

get-word-from-url.png
To copy or see all commands you can click on Details button below

#!/usr/bin/python

”’
author: Hopeless
task: prog will get all words from any url
”’

import sys, os, re
from urllib2 import urlopen
wordlist = []
# filename = raw_input(‘please enter filename to parse:’)

if len(sys.argv) > 1:
try:
urlf = urlopen(sys.argv[1],’r’)
for line in urlf:
wordlist += re.findall(r'([0-9a-zA-Z_]{3,15})[\s\\\.]’,line)
#print wordlist

except Exception, e:
print e
exit(0)

urlf.close()
wordset = set(wordlist)

print wordset
print ‘dic length is: ‘, len(wordset)

#save wordset to file -> newline for each item
realdic = open(‘realdic.txt’,’w’)
for word in wordset:
realdic.write(word+’\n’)

realdic.close()

hopeless@ubuntu:~/python$ ./passdicurl.py https://en.wikipedia.org/wiki/Israel
(all result save to file “realdic.txt”)


Results!
Screenshot from 2016-08-10 04:00:02.png