-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode.py
135 lines (111 loc) · 3.84 KB
/
node.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
from __future__ import print_function
from alchemyapi import AlchemyAPI
import json
class node:
count = 0
def __init__(self, name, keywords = set([]), content = ' ', parents = [], children = []):
self.content = content
self.ID = self.count
node.count = node.count+1
self.name = name
self.parents = {}
self.children = {}
for parent in parents:
self.addParent(parent)
for child in children:
self.addChild(child)
self.keywords = keywords
self.setKeywords()
#self.writeFile()
def __str__ (self):
return self.name
def __repr__ (self):
return self.__str__()
def getName(self):
return self.name
def getParents(self):
return self.parents
def getChildren(self):
return self.children
def addParent(self, parent):
if parent.ID not in self.parents:
self.parents[parent.ID] = parent
parent.addChild(self)
if checkPath(parent):
self.removeParent(parent)
#self.writeFile()
def addChild(self, child):
if child.ID not in self.children:
self.children[child.ID] = child
child.addParent(self)
if checkPath(self):
self.removeChild(child)
#self.writeFile()
def getContent(self):
return self.content
def getKeywords(self):
return self.keywords
def setKeywords(self):
alchemyapi = AlchemyAPI()
response = alchemyapi.keywords('text',self.content, { 'sentiment':1 })
if response['status'] == 'OK':
for keyword in response['keywords']:
self.keywords.add(keyword['text'].encode('ascii','ignore'))
else:
print('Error in concept tagging call: ', response['statusInfo'])
self.keywords = set(["Automatic keyword generation failed"])
response = alchemyapi.concepts('text',self.content, { 'sentiment':1 })
if response['status'] == 'OK':
for keyword in response['concepts']:
self.keywords.add(keyword['text'].encode('ascii','ignore'))
else:
print('Error in concept tagging call: ', response['statusInfo'])
self.keywords = set(["Automatic keyword generation failed"])
#self.writeFile()
#def writeFile(self):
#f = open('web/topics/' + self.name + '.html', 'w')
#f.write("<html><head><title>Tree of Knowledge: " + self.name.title() + "</title></head><body><h1>" + self.name.title() + "</h1><br><br>" + self.content + "</body></html>")
#f.close()
def editContent(self, newContent):
self.content = newContent
self.setKeywords()
#self.writeFile()
def removeParent(self, oldParent):
if oldParent.ID in self.parents:
del self.parents[oldParent.ID]
oldParent.removeChild(self)
#self.writeFile()
def removeChild(self, oldChild):
if oldChild.ID in self.children:
del self.children[oldChild.ID]
oldChild.removeParent(self)
# self.writeFile()
def __hash__(self):
return self.ID
def deleteConnections(self):
for i in self.parents.values():
self.removeParent(i)
for i in self.children.values():
self.removeChild(i)
seen = {}
def checkPath( source):
seen = {}
#print seen
#print "CHECK", source.name
if len(source.children) != 0:
for i in source.children.values():
if DFS(i, source) == True:
return True
return False
def DFS(current, goal):
#print current.name, goal.name
if current.ID == goal.ID:
#print "Cycle Found"
return True
if len(current.children) != 0:
for o in current.children.values():
if not( o.ID in seen):
seen[o.ID] = o
if DFS(o, goal):
return True
return False