I am creating a tree-like structure where every leaf node has 5 documents to it. To get the document of parent node all the documents of the child will be assigned to it.
我正在创建一个树状结构,每个叶节点都有5个文档。要获取父节点的文档,将为其分配子节点的所有文档。
For e.g. A is the parent node and B, C are child nodes both of whom have 5 documents each. So, the documents for A will be 5+5=10. Similarly, the parent of A will get 10 documents of A + no of a document of the sibling of A. We will repeat this until we reach the root node.
例如,A是父节点,B是子节点,两者都有5个文档。A的文档是5+5=10。类似地,A的父节点将获得A的兄弟节点的10个A + no的文档。
I want to store documents of A as a list of size 10 and similarly parent of A as total no of documents of their child. But it is storing it as a list of size 2 and under each list, there are 5 documents each. And the parent of A is also storing documents of A as a list of 3 not 3*5=15
what I want.
我想将A的文档存储为一个大小为10的列表,并将A的父文档存储为它们的子文档的总数。但它将它存储为一个大小为2的列表,在每个列表下,每个列表有5个文档。A的父结点也将A的文件存储为3而不是3*5=15。
How can I store the document at each node as total no of documents and not a list of lists? Below is the code which I am using.
如何将每个节点上的文档存储为no文档,而不是列表?下面是我正在使用的代码。
from anytree import Node, RenderTree
import pandas as pd
import numpy as np
class Node(Node):
Node.documents = None
Node.vector = None
### Creating tree by giving documnets to leaf ###
### Tree Creation ###
# L1
Finance = Node("Finance")
# L2
Credit_and_Lending = Node("Credit and Lending", parent=Finance)
# L3
Credit_Cards = Node("Credit Cards", parent=Credit_and_Lending)
Loans = Node("Loans", parent=Credit_and_Lending)
# L4
Low_Interest_and_No_Interest_Credit_Cards = Node("Low Interest & No Interest Credit Cards", parent=Credit_Cards, documents=[(fvc.loc[(fvc['keyword']=='low interest & no interest credit cards') & (fvc['organic_rank']==1)])['vocab'].tolist()[0]
, (fvc.loc[(fvc['keyword']=='low interest & no interest credit cards') & (fvc['organic_rank']==2)])['vocab'].tolist()[0]
, (fvc.loc[(fvc['keyword']=='low interest & no interest credit cards') & (fvc['organic_rank']==3)])['vocab'].tolist()[0]
, (fvc.loc[(fvc['keyword']=='low interest & no interest credit cards') & (fvc['organic_rank']==4)])['vocab'].tolist()[0]
, (fvc.loc[(fvc['keyword']=='low interest & no interest credit cards') & (fvc['organic_rank']==5)])['vocab'].tolist()[0]])
Rewards_Cards = Node("Rewards Cards", parent=Credit_Cards, documents=[(fvc.loc[(fvc['keyword']=='rewards cards') & (fvc['organic_rank']==1)])['vocab'].tolist()[0]
, (fvc.loc[(fvc['keyword']=='rewards cards') & (fvc['organic_rank']==2)])['vocab'].tolist()[0]
, (fvc.loc[(fvc['keyword']=='rewards cards') & (fvc['organic_rank']==3)])['vocab'].tolist()[0]
, (fvc.loc[(fvc['keyword']=='rewards cards') & (fvc['organic_rank']==4)])['vocab'].tolist()[0]
, (fvc.loc[(fvc['keyword']=='rewards cards') & (fvc['organic_rank']==5)])['vocab'].tolist()[0]])
Student_Credit_Cards = Node("Student Credit Cards", parent=Credit_Cards, documents=[(fvc.loc[(fvc['keyword']=='student credit cards') & (fvc['organic_rank']==1)])['vocab'].tolist()[0]
, (fvc.loc[(fvc['keyword']=='student credit cards') & (fvc['organic_rank']==2)])['vocab'].tolist()[0]
, (fvc.loc[(fvc['keyword']=='student credit cards') & (fvc['organic_rank']==3)])['vocab'].tolist()[0]
, (fvc.loc[(fvc['keyword']=='student credit cards') & (fvc['organic_rank']==4)])['vocab'].tolist()[0]
, (fvc.loc[(fvc['keyword']=='student credit cards') & (fvc['organic_rank']==5)])['vocab'].tolist()[0]])
Auto_Financing = Node("Auto Financing", parent=Loans, documents=[(fvc.loc[(fvc['keyword']=='auto financing') & (fvc['organic_rank']==1)])['vocab'].tolist()[0]
, (fvc.loc[(fvc['keyword']=='auto financing') & (fvc['organic_rank']==2)])['vocab'].tolist()[0]
, (fvc.loc[(fvc['keyword']=='auto financing') & (fvc['organic_rank']==3)])['vocab'].tolist()[0]
, (fvc.loc[(fvc['keyword']=='auto financing') & (fvc['organic_rank']==4)])['vocab'].tolist()[0]
, (fvc.loc[(fvc['keyword']=='auto financing') & (fvc['organic_rank']==5)])['vocab'].tolist()[0]])
Commercial_Lending = Node("Commercial Lending", parent=Loans, documents=[(fvc.loc[(fvc['keyword']=='commercial lending') & (fvc['organic_rank']==1)])['vocab'].tolist()[0]
, (fvc.loc[(fvc['keyword']=='commercial lending') & (fvc['organic_rank']==2)])['vocab'].tolist()[0]
, (fvc.loc[(fvc['keyword']=='commercial lending') & (fvc['organic_rank']==3)])['vocab'].tolist()[0]
, (fvc.loc[(fvc['keyword']=='commercial lending') & (fvc['organic_rank']==4)])['vocab'].tolist()[0]
, (fvc.loc[(fvc['keyword']=='commercial lending') & (fvc['organic_rank']==5)])['vocab'].tolist()[0]])
##### Visualizing the created tree #####
for pre, fill, node in RenderTree(Finance):
print("%s%s" % (pre, node.name))
##### Getting documents for parent nodes #####
def get_documents(node):
if node.documents is not None:
return node.documents
else:
child_nodes = node.children
lis = []
for child in child_nodes:
child_docs = get_documents(child)
lis.append(child_docs)
node.documents = lis
return lis
get_documents(Finance)
1 个解决方案
#1
2
You can use this syntax:
您可以使用以下语法:
lis = lis + child_docs
Instead of
而不是
lis.append(child_docs)
#1
2
You can use this syntax:
您可以使用以下语法:
lis = lis + child_docs
Instead of
而不是
lis.append(child_docs)