In [2]:
import random
import numpy as np

def normalize(arr):
    s = sum(arr)

    if s == 0:
        s = 1
        arr[0] = 1
    
    for i, val in enumerate(arr):
        arr[i] = val/s
    return arr


def generate(width, height):
    matrix = []

    for i in range(height):
        matrix.append([])

        for j in range(width):
            matrix[i].append(float(random.randint(0, 1000))/1000)

        matrix[i] = normalize(matrix[i])

        matrix[i] = [round(x, 3) for x in matrix[i]]
        
    return np.matrix(matrix)

generate(4,4)


print normalize([.444,.45, .34, .0])


[0.35980551053484605, 0.3646677471636953, 0.2755267423014587, 0.0]

In [3]:
#input:num characters
#output: num personality matricies, and closeness vectors

def initialize(num):
    out_matricies = []
    
    for i in range(num * 2):
        out_matricies.append(generate(4,4))
    
    closeness_vectors = []
    for x in range(num):
        close_vector = []
        for i in range(num):
            if i == x:
                close_vector.append(float(0))
            else:
                close_vector.append(float(random.randint(0,1000))/1000)
        closeness_vectors.append(normalize(close_vector))
    return out_matricies, closeness_vectors

initialize(2)


Out[3]:
([matrix([[ 0.239,  0.165,  0.299,  0.297],
          [ 0.127,  0.105,  0.328,  0.44 ],
          [ 0.168,  0.201,  0.301,  0.33 ],
          [ 0.003,  0.271,  0.394,  0.332]]),
  matrix([[ 0.256,  0.381,  0.101,  0.261],
          [ 0.353,  0.054,  0.355,  0.239],
          [ 0.128,  0.111,  0.415,  0.346],
          [ 0.216,  0.364,  0.405,  0.015]]),
  matrix([[ 0.008,  0.185,  0.327,  0.48 ],
          [ 0.312,  0.187,  0.166,  0.335],
          [ 0.131,  0.424,  0.285,  0.16 ],
          [ 0.196,  0.153,  0.484,  0.166]]),
  matrix([[ 0.277,  0.326,  0.16 ,  0.237],
          [ 0.191,  0.36 ,  0.216,  0.233],
          [ 0.028,  0.508,  0.265,  0.199],
          [ 0.293,  0.061,  0.326,  0.32 ]])],
 [[0.0, 1.0], [1.0, 0.0]])

In [10]:
def translate(num):
    if num == 0:
        return "Happy"
    if num == 1:
        return "Sad"
    if num == 2:
        return "Angry"
    if num == 3:
        return "Fear"     
    
def translate_to_num(stringy):
    if stringy == "Happy":
        return 0
    if stringy == "Sad":
        return 1
    if stringy == "Angry":
        return 2
    if stringy == "Fear":
        return 3

In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:

Few things to keep aware of. People acessing things at the same time, take the earlier user's request, then discard the other guys request and then continue the chain.


In [5]:
#Intialization - creates db and then sends an empty response

s = """{
  "Details": {
    "Username" : "Anonymous",
    "Story"    : "Rabbit Story"
  },
  "Characters": [
    {
      "Name": "Rabbit",
      "Position": "Right",
      "Social"  : 0.3,
      "Emotion": "Happy"
    },

    {
      "Name": "Turtle",
      "Position": "Left",
      "Social"  : 0.2,
      "Emotion": "Sad"
    }
  ]
}
"""

In [6]:
import json
string = json.loads(s)

username = string['Details']['Username']
story    = string['Details']['Story']
num_char = len(string['Characters'])

matrices, closeness_vectors = initialize(num_char)

#Initialize DB
character_id = 0
for elem in string['Characters']:
    #DB[Character objs] puts the following
    
    char_id      = character_id
    name         = elem['Name']
    position     = elem['Position']
    socialbility = elem['Social']
    emotion      = translate_to_num(elem['Emotion'])
    personality  = matrices[character_id]
    impact       = matrices[character_id + num_char]
    c_vector     = closeness_vectors[character_id]
        
    print char_id
    print name
    print position
    print socialbility
    print emotion
    print personality
    print impact
    print c_vector
    print
    
    character_id += 1
        
return generate_response(username,story,0,0)


0
Rabbit
Right
0.3
0
[[ 0.096  0.168  0.389  0.347]
 [ 0.316  0.002  0.359  0.323]
 [ 0.518  0.306  0.038  0.138]
 [ 0.007  0.57   0.059  0.364]]
[[ 0.145  0.244  0.307  0.304]
 [ 0.009  0.295  0.482  0.215]
 [ 0.136  0.5    0.219  0.144]
 [ 0.67   0.099  0.167  0.064]]
[0.0, 1.0]

1
Turtle
Left
0.2
1
[[ 0.15   0.427  0.368  0.055]
 [ 0.234  0.234  0.336  0.196]
 [ 0.434  0.06   0.196  0.31 ]
 [ 0.167  0.284  0.371  0.178]]
[[ 0.197  0.006  0.037  0.76 ]
 [ 0.114  0.463  0.42   0.003]
 [ 0.258  0.269  0.367  0.106]
 [ 0.113  0.499  0.38   0.008]]
[1.0, 0.0]


In [ ]:


In [2]:
#Update Text Request
#update text in db
#send response back

s = {
  "Details": {
    "Story": "Rabbit Story",
    "Username": "Anonymous",
    "Frame_start": 1,
    "Frame_end": 1
  },

  "Frames": [ 
    {
      "Characters":[
        {
          "Name": "Rabbit",
          "Text": "Hello Turtle1"
        },

        {
          "Name": "Turtle",
          "Text": "Hello Rabbit1"
        }
      ]
    },

    {
      "Characters":[
        {
          "Name": "Rabbit",
          "Text": "Hello Turtle2"
        },
        {
          "Name": "Turtle",
          "Text": "Hello Rabbit2"
        }
      ]
    }
  ]
}

create a function that access the db w/ the name of the character and gets its char_id

process

for every frame, update the text in the db nothing markov

if frames out of bound bail


In [ ]:
import json
string = json.loads(s)

username   = string['Details']['Username']
story      = string['Details']['Story']
start      = string['Details']['Frame_start']
end        = string['Details']['Frame_end']

#get number of the last frame from db
if last_known_frame < int(end):
    #return malformed JSON
else:
    #return JSON wanted
    for elem in string['Frames']:
        for sub_elem in elem['Characters']:
            char_name = sub_elem['Name']
            text      = sub_elem['Text']
            #puts in db[story][start][char_name][TEXT ENTRY] = text 
            
        start += 1
        if start > end:
            break
        
return generate_response(username,story,start,end)

In [ ]:


In [ ]:
#continue past Story
#Check for continuing or querying

#continuing
#Samples the markov chain for each character in the frame and then updates the db
#then send the response back

#querying 
#query db and send info w/ text back
s = {
  "Details": {
    "Story": "Rabbit Story",
    "Username": "Anonymous",
    "Frame_start": 1,
    "Frame_end": 3
  }
}

In [1]:
import json
string = json.loads(s)

username   = string['Details']['Username']
story      = string['Details']['Story']
start      = string['Details']['Frame_start']
end        = string['Details']['Frame_end']

#run a for
    #check if frame is in db
        #continue
    #else:
        #pull num of characters in story something like db.story.characterobjs
        
        #for 2 characters chosen at random:
            #sample markov chain for one and update in db
#return generate_response(username, story, start, end)


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-1-df28afc0a3e3> in <module>()
      1 import json
----> 2 string = json.loads(s)
      3 
      4 username   = string['Details']['Username']
      5 story      = string['Details']['Story']

NameError: name 's' is not defined

In [7]:
import numpy as np
import json

total_influence = np.matrix([[0.0, 0.0, 0.0, 0.0],
                                     [0.0, 0.0, 0.0, 0.0],
                                     [0.0, 0.0, 0.0, 0.0],
                                     [0.0, 0.0, 0.0, 0.0]])
s =  json.dumps(total_influence.tolist())
print total_influence


[[ 0.  0.  0.  0.]
 [ 0.  0.  0.  0.]
 [ 0.  0.  0.  0.]
 [ 0.  0.  0.  0.]]

In [6]:
print np.matrix(json.loads(s))


[[ 0.  0.  0.  0.]
 [ 0.  0.  0.  0.]
 [ 0.  0.  0.  0.]
 [ 0.  0.  0.  0.]]

In [ ]: