Natural Language Toolkit (nltk)
http://www.nltk.org/
datei: projects.models.tree.py
Code: Alles auswählen
class tree(models.Model):
id_prev = models.IntegerField() #implizit FK=>tree.id
text = models.CharField(max_length=50)
class Meta:
ordering = ('id_prev', )
def __unicode__(self):
return self.tag
Code: Alles auswählen
insert into tree (id_prev, text) values (0, 'root');
// bekommt id=1 => root, MUSS vorhanden sein!!!!!!!!!!! sonst krachts.
insert into tree (id_prev, text) values (1, 'node_1');
// bekommt id=2
insert into tree (id_prev, text) values (2, 'node_1_1');
// bekommt id=3
insert into tree (id_prev, text) values (3, 'node_1_1_1');
// bekommt id=4
insert into tree (id_prev, text) values (0, 'node_2');
// bekommt id=5
insert into tree (id_prev, text) values (0, 'node_3');
// bekommt id=6
insert into tree (id_prev, text) values (5, 'node_2_1');
// bekommt id=7
Code: Alles auswählen
from nltk.tree import ParentedTree
from projects.models import tree
def render_tree(tree_id):
treedata = tree.objects.all()
map = {}
for node in treedata:
if node.id_prev == 0: #root, deshalb implizit FK (s.o.)
root_node = ParentedTree('<li id="%s_node_%s">%s' % (tree_id, node.id, node.text), [])
parent = root_node.root
map[node.id] = parent
continue
parent = map[node.id_prev]
leaf = ParentedTree('<li id="%s_node_%s">%s' % (tree_id, node.id, node.text), [])
parent.append(leaf)
map[node.id] = leaf
tree_string = root_node.pprint(margin=0, indent=0, nodesep='#').replace('\n', '')
tree_string = tree_string.replace('#)', '</li>')
tree_string = tree_string.replace('#', '<ul>')
tree_string = tree_string.replace(')', '</ul></li>')
tree_string = tree_string.replace('(', '')
tree_string = '<ul id="%s" class="treeview-black">%s</ul>' % (tree_id, tree_string)
return tree_string
hübsch machen mit jquery=>treeview:
jquery: http://jquery.com/
treeview: http://bassistance.de/jquery-plugins/jq ... -treeview/
javascript:
Code: Alles auswählen
$(document).ready(function()
{
$('#mytree').treeview();
}
tippfehler inclusive? dann postet eine korrigierte version :) :)