1 '''
2 Created on 26/12/2013
3
4 @author: David Vilares
5 '''
6 from nltk.parse.dependencygraph import DependencyGraph
7
28
41
43 '''
44 An extension of the class L{nltk.parse.dependencygraph.DependencyGraph}
45 to include sentiment information
46 '''
47 ROOT_WORD = "ROOT_WORD"
48
49
50
57
58
65
68
69
72
74 """
75 @param node: A node of a L{nltk.parse.dependencygraph.DependencyGraph}
76 @return: dependency relation with head's node
77 """
78 return node[SentimentDependencyGraphNodeKeys.REL]
79
80
82 """
83 @param node: A node of a L{nltk.parse.dependencygraph.DependencyGraph}
84 @return: A list of children id's of node
85 """
86 return node[SentimentDependencyGraphNodeKeys.DEPS]
87
88
90 """
91 It obtains the adversative node identifier.
92 @precondition: The parameter tag must follow the regexp for adversative tags: tag:additional_information@id
93 @param tag: The tag of the artificial adversative node
94 @return: An integer
95 """
96 return int(node[SentimentDependencyGraphNodeKeys.TAG].split(":")[1].split("@")[1])
97
99 """
100 @param node: A node of a L{nltk.parse.dependencygraph.DependencyGraph}
101 @return: The fine PoS-tag of the node
102 """
103 return node[SentimentDependencyGraphNodeKeys.TAG].split('@')[0]
104
105
107 """
108 @param node: A node of a L{nltk.parse.dependencygraph.DependencyGraph}
109 @return: The coarse PoS-tag of the node
110 """
111 return node[SentimentDependencyGraphNodeKeys.TAG].split(':')[0]
112
113
116
118 """
119 @param node: A node of a L{nltk.parse.dependencygraph.DependencyGraph}
120 @return: The position in the sentence of the node. Zero is the root node
121 """
122 return node[SentimentDependencyGraphNodeKeys.ADDRESS]
123
124
133
135 """
136 @param node:A node of a L{nltk.parse.dependencygraph.DependencyGraph}
137 @return: The lexical category of the node
138 """
139 return self.get_tag(node).split(':')[0]
140
142 """
143 @param node: A node of a DependencyGraph
144 @return: True if is a leaf node, False otherwise
145 """
146 return self.get_deps(node) == []
147
149 """
150 @param node: A node of a L{nltk.parse.dependencygraph.DependencyGraph}
151 @return: True if is the root of the dependency graph, False otherwise
152 """
153 return self.get_address(node) == 0
154
156 """
157 @param node: A node of a L{nltk.parse.dependencygrpah.DependencyGraph}
158 @return True if it's a negation node, False otherwise.
159 """
160 word = self.get_word(node).lower()
161 rel = self.get_rel(node)
162
163 if (word == 'no' and rel in ['mod','neg']) or (word == 'nunca' and rel != 'S') or (word == 'sin'):
164 return True
165 return False
166
167
169 """
170 @param node: A node of a L{nltk.parse.dependencygraph.DependencyGraph}
171 @param dictionary: An instance of L{Dictionary}
172 @return: True if word is an intensifier, false otherwise
173 """
174 intensifier_rel = ['spec','espec','cc','sadv','f']
175 intensifier_categ = ['r','f']
176 lexical_category = self.get_lexical_category(node)
177 lemma = dictionary.get_lemma(lexical_category, self.get_word(node))
178 return (self.get_rel(node) in intensifier_rel
179 and self.get_lexical_category(node) in intensifier_categ
180 and dictionary.is_intensifier_term(lemma))
181
183 """
184 @param node: A node of a L{nltk.parse.dependencygraph.DependencyGraph}
185 @return True if the node is an emoticon, False otherwise
186 """
187 return self.get_rel(node) == 'art_rel_emoticon'
188
189
191 """
192 @param node: A node of a L{nltk.parse.dependencygraph.DependencyGraph}
193 @return: True if node was created artificially by L{src.model.parser.Parser}, False otherwise
194 """
195 return self.get_rel(node) == 'art_rel_adversative'
196
197
198
199
200
201
202
203
205
206 list_nodes = []
207 if self.is_leaf(node):
208 return [node]
209 else:
210 if not self.is_root_node(node):
211 list_nodes = [node]
212 list_children_node = [self.get_by_address(address)
213 for address in self.get_deps(node)]
214 for child_node in list_children_node:
215 list_nodes.extend(self._nodes_in_graph(child_node))
216 return list_nodes
217
218
220 """
221 @param A node of a L{SentimentDependencyGraph}
222 @return A string. The raw phrase which starts in the node.
223 """
224 sorted_nodes = sorted(self._nodes_in_graph(node), key= lambda d: d['address'],reverse=False);
225 return ' '.join([self.get_word(node) for node in sorted_nodes])
226
227
229 """
230 @param node: A node of a L{SentimentDependencyGraph}
231 @return An integer. The number of the nodes starting in node
232 """
233 number_nodes = 0;
234 if self.is_leaf(node):
235 return 1
236 else:
237 if not self.is_root_node(node):
238 number_nodes = 1
239 list_children_node = [self.get_by_address(address)
240 for address in self.get_deps(node)]
241 for child_node in list_children_node:
242 number_nodes+=self.number_of_nodes(child_node)
243 return number_nodes
244
245 - def level(self,address,level=1):
246 """
247 @param dg: A L{nltk.parse.dependencygraph.DependencyGraph} instance
248 @param address: An integer representing the identifier of a node of dg
249 @param level: Initial level of the node, before recursive calls of level function
250 @return:The level of a node in a dependency graph
251 """
252 try:
253 head_node = self.get_by_address(self._hd(address))
254 except:
255 return level
256 if self.is_root_node(head_node):
257 return level
258 else:
259 return self.level(self.get_address(head_node), level+1)
260
261
262
289