In [6]:
from matplotlib import pyplot
%matplotlib inline
import numpy as np
In [1]:
stringSize = 12 # defining string size
maxWordLength = 12
In [2]:
# lets consider 63 basic character, those occur the most
# character to integer mapping dictionary
charToInt = {'0':0,'1':1,'2':2,'3':3,'4':4,'5':5,'6':6,'7':7,'8':8,'9':9,'A':10,'a':11,'B':12,'b':13,'C':14,'c':15,'D':16,'d':17,'E':18,'e':19,'F':20,'f':21,'G':22,'g':23,'H':24,'h':25,'I':26,'i':27,'J':28,'j':29,'K':30,'k':31,'L':32,'l':33,'M':34,'m':35,'N':36,'n':37,'O':38,'o':39,'P':40,'p':41,'Q':42,'q':43,'R':44,'r':45,'S':46,'s':47,'T':48,'t':49,'U':50,'u':51,'V':52,'v':53,'W':54,'w':55,'X':56,'x':57,'Y':58,'y':59,'Z':60,'z':61 ,' ':62,'.':63}
In [3]:
# integer to character maping dictionary
intToChar = {v: k for k, v in charToInt.iteritems()}
In [4]:
def giveWordmatrix(word):
"""
will generate 2d matrix of the string, which will be an input to convolutional network
word : is a string given to function
"""
#2d matrix of size 100*63 initilaized with all cell having value "false"
tempMatrix = np.zeros((maxWordLength, 63),dtype=bool)
charNo=0
for charNo in range (0,len(word)):
if charNo<maxWordLength:
try:
try:
# for above defined 63 character, if character exists then "true" is placed in place
characterToIndex = int(word[charNo])
tempMatrix[charNo][characterToIndex]=True
charNo += 1
except:
characterToIndex = charToInt[word[charNo]]
tempMatrix[charNo][characterToIndex]=True
charNo += 1
except:
tempMatrix[charNo][0]=False
return tempMatrix
In [7]:
word = giveWordmatrix("carbon copy")
# genearting 2d matrix for mutated string
similarWord = giveWordmatrix("copy")
In [8]:
# visualizing original and muataed string
print ("word")
pyplot.imshow(word)#showing first image
pyplot.show()
print ("similar Word")
pyplot.imshow(similarWord) #showing first image
pyplot.show()
In [9]:
sentence = "The quick brown fox jumps over the lazy dog."
for eachword in sentence.split(" "):
print (eachword)
pyplot.imshow(giveWordmatrix(eachword))#showing first image
pyplot.show()
In [ ]: