Package sentence :: Module sentence
[hide private]
[frames] | no frames]

Source Code for Module sentence.sentence

  1  #!/usr/bin/python 
  2  # -*- coding: utf-8 -*- 
  3   
  4  """ 
  5  @author: Eleftherios Avramidis 
  6  """ 
  7   
  8  from copy import deepcopy 
  9  import sys 
 10  import logging as log 
 11   
12 -class SimpleSentence(object):
13 """ 14 A simple (shallow) sentence object, which wraps both a sentence and its attributes 15 """ 16 17
18 - def __init__(self, string="", attributes={}):
19 """ 20 Initializes a simple (shallow) sentence object, which wraps both a sentence and its attributes 21 @param string: the string that the simple sentence will consist of 22 @type string: string 23 @param attributes: a dictionary of arguments that describe properties of the simple sentence 24 @type attributes: {String key, String value} 25 26 """ 27 28 #avoid tabs 29 self.string = string.replace("\t", " ") 30 #avoid getting a shallow reference to the attributes in the dict 31 self.attributes = deepcopy (attributes)
32 33 34 # def __gt__(self, other): 35 # return self.attributes["system"] > other.attributes["system"] 36 37 # def __lt__(self, other): 38 # return self.attributes["system"] < other.attributes["system"] 39
40 - def __eq__(self, other):
41 return (self.string == other.string and self.attributes == other.attributes)
42
43 - def get_string(self):
44 """ 45 Get the string of this simple sentence 46 @return: the text contained in the simple sentence 47 @rtype: String 48 """ 49 return self.string
50
51 - def get_attributes(self):
52 """ 53 Get the attributes of this sentence 54 @return: a dictionary of attributes that describe properties of the sentence 55 @rtype: dict 56 """ 57 return self.attributes
58
59 - def get_rank(self):
60 return self.attributes["rank"]
61
62 - def add_attribute(self, key, value):
63 self.attributes[key] = value
64
65 - def get_attribute(self, key, sub=None):
66 try: 67 return self.attributes[key] 68 except: 69 if sub: 70 return sub 71 else: 72 log.debug("Could not find attribute {}\n".format(key)) 73 raise AttributeError
74
75 - def add_attributes(self, attributes):
76 self.attributes.update(attributes)
77
78 - def rename_attribute(self, old_name, new_name):
79 self.attributes[new_name] = self.attributes[old_name] 80 del(self.attributes[old_name])
81
82 - def del_attribute(self, attribute):
83 del(self.attributes[attribute])
84
85 - def keep_only_attributes(self, attribute_names):
86 for name in self.attributes.keys(): 87 if name not in attribute_names: 88 del(self.attributes[name])
89 90
91 - def __str__(self):
92 return self.string + ": " + str(self.attributes)
93
94 - def merge_simplesentence(self, ss, attribute_replacements = {}):
95 """ 96 Add the attributes to the object SimpleSentence(). In place 97 @param attr: attributes of a simple sentence 98 @type attr: dict 99 """ 100 101 incoming_attributes = ss.get_attributes() 102 for incoming_attribute in incoming_attributes: 103 if incoming_attribute in attribute_replacements: 104 new_key = attribute_replacements[incoming_attribute] 105 new_value = incoming_attributes[incoming_attribute] 106 incoming_attributes[new_key] = new_value 107 del(incoming_attributes[incoming_attribute]) 108 109 self.attributes.update(incoming_attributes) 110 self.string = ss.string
111