Advertisement
Guest User

Untitled

a guest
Dec 20th, 2014
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.93 KB | None | 0 0
  1. from neo4jrestclient.client import GraphDatabase
  2. gdb = GraphDatabase("http://localhost:7474/")
  3.  
  4. #Defining the functions:
  5. def create_entity():
  6.     entities = gdb.labels.create('Entity')
  7.  
  8.     enter = raw_input('New Entry: ')
  9.  
  10.     entry = gdb.nodes.create(e = enter)
  11.     entities.add(entry)
  12.     return entry
  13.  
  14. def add_character(entity):
  15.     characters = gdb.labels.create('Character')
  16.     name = raw_input ('Character name: ')
  17.     gender = raw_input ('Character gender: ')
  18.     status = raw_input ('Is the character alive or dead? ')
  19.     char = gdb.nodes.create(name=name, gender=gender, status=status)
  20.     characters.add(char)
  21.     char.relationships.create('hasCharacter', entity)
  22.     return char
  23.  
  24. def display_existing_characters():
  25.     existing_characters = gdb.labels.get('Character').get()
  26.     for existing in existing_characters:
  27.         print str(existing.id) + " " + existing['name']
  28.        
  29.  
  30. def choose_existing_character(entity, question='Type the id number of the existing character: '):  #allows to choose from existing characters.
  31.     display_existing_characters()
  32.     name = raw_input (question)
  33.     character = gdb.node[name] #to link this entry to the one already there.
  34.     character.relationships.create('hasCharacter', entity)
  35.     return character
  36.  
  37. def add_location(entity):
  38.     location = gdb.labels.create('Place')
  39.  
  40.     pl = raw_input ('place: ')
  41.     gran = raw_input ('Granularity: ')
  42.     coor = raw_input ('Coordinates (as follows: lat, long): ')
  43.  
  44.     place = gdb.nodes.create(pl=pl, Granularity=gran, Coordinates=coor)
  45.     location.add(place)
  46.     place.relationships.create('inPlace', entity)
  47.     return place
  48.  
  49. def display_existing_location():
  50.     existing_location = gdb.labels.get('Place').get()
  51.     for existing in existing_location:
  52.         print str(existing.id) + " " + existing['pl']
  53.  
  54. def choose_existing_location(entity, question='Type the id number of the existing location: '): #allows to chose from existing locations.
  55.     display_existing_location()
  56.     pl = raw_input(question)
  57.     local = gdb.node[pl]
  58.     local.relationships.create('inPlace', entity)
  59.     return local
  60.  
  61.  
  62. def add_emotion(entity, Character):
  63.     emotions = gdb.labels.create('Emotions')
  64.  
  65.     List_of_emotions = ["Acceptance", "Anger", "Anticipation", "Anxiety", "Aversion", "Contempt", "Courage", "Dejection", "Desire", "Despair", "Disgust", "Distress", "Elation", "Expectancy", "Fear", "Guilt", "Grief", "Happiness", "Hate", "Hope", "Interest", "Joy", "Love", "Pain", "Panic", "Pleasure", "Rage", "Sadness", "Shame", "Sorrow", "Subjection", "Surprise", "Tender-Emotion", "Terror", "Wonder"]
  66.     print List_of_emotions
  67.     Emotion = raw_input ('Emotion (choose from list): ')
  68.     while Emotion not in List_of_emotions:
  69.             print 'Please choose from available list.'
  70.             Emotion = raw_input ('Emotion: ')
  71.     emotion = gdb.nodes.create(em = Emotion)
  72.     emotions.add(emotion)
  73.     emotion.relationships.create('isEmotion', entity)
  74.     emotion.relationships.create('hasCharacter', character)
  75.     emotion.relationships.create('Spatial_Emotion', place)
  76.     return emotion
  77.  
  78. def add_time(entity):
  79.     times = gdb.labels.create('Time')
  80.    
  81.     temporal = raw_input('Time: ')
  82.     gran = raw_input ('Granularity: ')
  83.     time = gdb.nodes.create(t = temporal, Granularity=gran)
  84.     times.add(time)
  85.     time.relationships.create('atTime', entity)
  86.     time.relationships.create('Spatial_Time', place)
  87.     return time
  88.  
  89. #Program starts, user inputs data:
  90. #Fetch character information:
  91.  
  92. ans = raw_input ('Entry? (1 for yes, 0 for no)')
  93. while ans != '0':                                   # 0 stops the program.
  94.     entity = create_entity()                        # Numbering the entries
  95.    
  96.     #New character Vs. Existing:
  97.     addition = raw_input ("New character? (yes/no)")
  98.     if addition == "yes":
  99.         character = add_character(entity)
  100.     else:
  101.         display_existing_characters()
  102.         character = choose_existing_character(entity)
  103.  
  104.     #Fetch relationship information:
  105.     connections = raw_input ('Does the character have a relationship with another character, in this specific instance? (yes/no)')
  106.  
  107.     if connections == "yes":
  108.         is_new = raw_input ('New character? (yes/no)')
  109.         if is_new == "yes":
  110.             related_char = add_character(entity)
  111.         else:
  112.             related_char = choose_existing_character(entity)
  113.         rel = raw_input ('What is the type of the relationship? ') #define the types and unify them; state them in this command
  114.         related_char.relationships.create(rel, character)  
  115.  
  116.     #New location Vs. Existing:
  117.     new_place = raw_input ('New Place? (yes/no)')
  118.     if new_place == "yes":
  119.         place = add_location(entity)
  120.     else:
  121.         display_existing_location()
  122.         place = choose_existing_location(entity)
  123.        
  124.     #Fetch emotion information:
  125.     emotional = raw_input (character.get('name') + ' Emotion? (yes/no)')
  126.     if emotional == "yes":
  127.         add_emotion(entity, character)
  128.     if connections == "yes":
  129.         emotional_related = raw_input (related_char.get('name') + ' Emotion? (yes/no)')
  130.         if emotional_related == "yes":
  131.             add_emotion(entity, related_char)
  132.  
  133.     #Fetch time information:
  134.     temporal = add_time(entity)
  135.    
  136.     #See if user wants to continue and act accordingly:
  137.     ans = raw_input ('Entry? (1 for yes, 0 for no)')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement