Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
*.log
src/*.pyc
src/**/*.pyc
.vscode/
*.pkl
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,20 @@ It's to you to teach it.
## Prerequisites

pyECTOR is written in Python so you need a Python interpreter
(version 2.5 or later) to execute pyECTOR. Python is installed by
(version 3 or later) to execute pyECTOR. Python is installed by
default in most Linux distributions. You can download Python from
the official Python website http://www.python.org.

## Obtaining pyECTOR

The latest pyECTOR version and online documentation can be found at
http://pyector.googlecode.com/
- https://github.com/parmentf/pyector/ (program)
- https://code.google.com/archive/p/pyector/ (documentation)

## Installation

Once you've downloaded the file, untar it like that:

```bash
tar -xvzf pyector-0.4.tar.gz .
```
Expand All @@ -43,11 +45,11 @@ grammatically. Be indulgent with it, consider it like a child,
learning to speak.

```bash
python ConceptNetwork.py
python3 ConceptNetwork.py
```

It allows one to play with its internal mechanism: the
[Concept Network](http://code.google.com/p/pyector/wiki/ConceptNetwork).
[Concept Network](https://code.google.com/archive/p/pyector/wikis/ConceptNetwork.wiki).

You can `@addnode` (add a node), `@addlink` (add a link between two
existing nodes), and even `@activate` a node, so you can
Expand All @@ -60,7 +62,7 @@ See [document on Concept Network Module](./doc/html/ConceptNetworkModule.html).
# Usage

```bash
python Ector.py [-p username][-n botname=Ector][-v|-q][-l logfilepath=ector.log][-s|-g][-h]
python3 Ector.py [-p username][-n botname=Ector][-v|-q][-l logfilepath=ector.log][-s|-g][-h]
```

Be aware that ECTOR does not know anything at the beginning (first launch).
Expand Down
62 changes: 31 additions & 31 deletions src/ConceptNetwork.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ def addState(self, state):
"""Add a state to the Concept Network"""
self.__hasType(state, "State")
stateId = state.id
if stateId in self.state.keys():
if stateId in list(self.state.keys()):
raise ConceptNetworkDuplicateState("The state (" + stateId + ") already exists!")
self.state[stateId] = state

Expand All @@ -248,11 +248,11 @@ def propagateActivations(self, state,
raise ConceptNetworkBadParameter("normalNumberComingLinks must be > 1")
# Set the old activation values as the current ones
# Increment age of the nodes
for symbol, nodeState in state.nodeState.iteritems():
for symbol, nodeState in state.nodeState.items():
self.__hasType(nodeState, "NodeState")
nodeState.ageActivationValues()

for (symbol, typeName), node in self.node.iteritems():
for (symbol, typeName), node in self.node.items():
influence = 0
nbIncomings = 0
nodeState = state.getNodeState(symbol, typeName)
Expand Down Expand Up @@ -295,12 +295,12 @@ def fastPropagateActivations(self, state,
memoryPerf: memory performance (the higher, the better)"""
influenceValues = {} # (symbol, type) => influence value
influenceNb = {} # (symbol, type) => influence nb
for _, nodeState in state.nodeState.iteritems():
for _, nodeState in state.nodeState.items():
nodeState.ageActivationValues()

## Fill influence table ##
# Get the nodes influenced by others
for (symbol, typeName), node in self.node.iteritems():
for (symbol, typeName), node in self.node.items():
if symbol:
ov = state.getNodeOldActivationValue(symbol, typeName)
#links = self.getLinksFrom(node)
Expand All @@ -324,7 +324,7 @@ def fastPropagateActivations(self, state,
+ 1

## For all nodes in the state ##
influenceValueKeys = influenceValues.keys()
influenceValueKeys = list(influenceValues.keys())
for (symbol, typeName) in state.nodeState:
nodeState = state.getNodeState(symbol, typeName)
oldAV = nodeState.getOldActivationValue()
Expand Down Expand Up @@ -377,9 +377,9 @@ def removeStatesExcept(self, stateId):

def showStates(self):
"""Show all states id"""
print "States (%d)" % (len(self.state))
print("States (%d)" % (len(self.state)))
for id in self.state:
print "\t%s" % (id)
print("\t%s" % (id))


class Node:
Expand Down Expand Up @@ -453,9 +453,9 @@ def addLabelingLink(self, link):

def show(self):
"""Display the node"""
print "%s (%s): %d" % (self.getSymbol().encode(ENCODING),
print("%s (%s): %d" % (self.getSymbol().encode(ENCODING),
self.getTypeName(),
self.getOcc())
self.getOcc()))


class Link:
Expand Down Expand Up @@ -511,20 +511,20 @@ def show(self, state):
"""Display the link, using state to compute labeled link weight."""
if self.label:
if not state:
print "%10s -(%10s %d)-> %10s" % (self.fro.getSymbol().encode(ENCODING),
print("%10s -(%10s %d)-> %10s" % (self.fro.getSymbol().encode(ENCODING),
self.label.getSymbol().encode(ENCODING),
self.getWeight() * 100,
self.to.getSymbol().encode(ENCODING))
self.to.getSymbol().encode(ENCODING)))
else:
print "%10s -(%10s %d)-> %10s" % (self.fro.getSymbol().encode(ENCODING),
print("%10s -(%10s %d)-> %10s" % (self.fro.getSymbol().encode(ENCODING),
self.label.getSymbol().encode(ENCODING),
self.getWeight(state) * 100,
self.to.getSymbol().encode(ENCODING))
self.to.getSymbol().encode(ENCODING)))
else:
print "%10s ------(%d, %d)-------> %10s" % (self.fro.getSymbol().encode(ENCODING),
print("%10s ------(%d, %d)-------> %10s" % (self.fro.getSymbol().encode(ENCODING),
self.getWeight() * 100,
self.getCoOcc(),
self.to.getSymbol().encode(ENCODING))
self.to.getSymbol().encode(ENCODING)))


class State:
Expand Down Expand Up @@ -591,7 +591,7 @@ def getNodeOldActivationValue(self, symbol, type="basic"):
def getAverageActivationValue(self):
"Get the average activation value"
activationValues = [nodeState.getActivationValue()
for _, nodeState in self.nodeState.iteritems()]
for _, nodeState in self.nodeState.items()]
nb = len(activationValues)
total = sum(activationValues)
if nb:
Expand All @@ -605,7 +605,7 @@ def getMaximumActivationValue(self, cn, typeNames):
typeNames: names of the types to take into account
cn: Concept Network containing the nodes"""
activationValues = [nodeState.getActivationValue()
for (_, typeName), nodeState in self.nodeState.iteritems()
for (_, typeName), nodeState in self.nodeState.items()
if typeName in typeNames]
return max(activationValues)

Expand All @@ -617,7 +617,7 @@ def getActivatedTypedNodes(self, cn, typeNames, threshold=90):

Return a list of tuples (node,activation value)"""
nodes = []
for nodeId, node in cn.node.iteritems():
for nodeId, node in cn.node.items():
(symbol, typeName) = nodeId
av = self.getNodeActivationValue(symbol, typeName)
if av > threshold:
Expand All @@ -634,18 +634,18 @@ def __hasType(self, obj, strType):

def checkNodes(self):
"Check that the nodes are NodeState s"
for _, nodeState in self.nodeState.iteritems():
for _, nodeState in self.nodeState.items():
self.__hasType(nodeState, "NodeState")

def showNodes(self):
"Print the node states"
print "oldav\tav\tage\tNode"
print("oldav\tav\tage\tNode")
for (symbol, typeName) in self.nodeState:
nodeState = self.nodeState[(symbol, typeName)]
print "%d\t%d\t%d\t%s(%s)" % (nodeState.getOldActivationValue(),
print("%d\t%d\t%d\t%s(%s)" % (nodeState.getOldActivationValue(),
nodeState.getActivationValue(),
nodeState.getAge(),
symbol.encode(ENCODING), typeName)
symbol.encode(ENCODING), typeName))

def clean(self):
"""Clean the state from the non-activated nodes"""
Expand Down Expand Up @@ -808,26 +808,26 @@ def main():
if line[:9] == "@addnode ":
node = Node(line[9:])
cn.addNode(node)
print "Node \"%s\" added" % (line[9:])
print("Node \"%s\" added" % (line[9:]))
elif line[:9] == "@addlink ":
params = line[9:].split()
if len(params) == 2:
try:
node1 = cn.getNode(params[0])
except ConceptNetworkUnknownNode:
print "The node \"%s\" does not exist!" % (params[0])
print("The node \"%s\" does not exist!" % (params[0]))
continue
try:
node2 = cn.getNode(params[1])
except ConceptNetworkUnknownNode:
print "The node \"%s\" does not exist!" % (params[1])
print("The node \"%s\" does not exist!" % (params[1]))
continue
print cn.addLink(node1, node2)
print(cn.addLink(node1, node2))
elif len(params) == 3:
node1 = cn.getNode(params[0])
node2 = cn.getNode(params[1])
node3 = cn.getNode(params[2])
print cn.addLink(node1, node2, node3)
print(cn.addLink(node1, node2, node3))
elif line[:10] == "@shownodes":
cn.showNodes()
elif line[:10] == "@showlinks":
Expand Down Expand Up @@ -857,11 +857,11 @@ def main():
file = open("state_1.pkl", "w")
pickle.dump(state, file, 0)
file.close()
print "Concept Network saved in \"%s\"" % (filename)
print("Concept Network saved in \"%s\"" % (filename))
elif line.startswith("@quit"):
return 0
elif line[:5] == "@help":
print """@help give this help
print("""@help give this help
@addnode name: add the node given
@addlink node1 node2 [label]: add a link from node1 to node2
@activate name [activation value]: activate a node from its name
Expand All @@ -870,7 +870,7 @@ def main():
@showlinks: show the links in the ConceptNetwork
@showstate: show the state of the nodes
@save: save the Concept Network and its state
@quit: quit without saving"""
@quit: quit without saving""")

if __name__ == "__main__":
import sys
Expand Down
8 changes: 4 additions & 4 deletions src/ConceptNetworkTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,15 +183,15 @@ def testDumpLoad(self):
conceptNetwork.addState(state)
state.setNodeActivationValue(100,"From")
conceptNetwork.fastPropagateActivations(state,2)
av = state.getNodeActivationValue("To1")
state.getNodeActivationValue("To1")

f = open("cntest.data","w")
f = open("cntest.data","wb")
try:
conceptNetwork.dump(f,0)
finally:
f.close()

f = open("cntest.data")
f = open("cntest.data", "rb")
try:
cnLoaded = pickle.load(f)
finally:
Expand Down Expand Up @@ -302,7 +302,7 @@ def testAging(self):
state = State(1)
conceptNetwork.addState(state)
state.setNodeActivationValue(100,"From","basic")
for i in range(0,51):
for _ in range(0,51):
conceptNetwork.fastPropagateActivations(state,2)
state.setNodeActivationValue(0, "From", "basic")
self.assertRaises(KeyError,state.nodeState.__getitem__,("From","basic"))
Expand Down
Loading