title: Representing a couple of RDF-triples using tensor. How to programming this modeling process using Python?
body: <p>A question about representing a couple of RDF-triples using tensor.</p>
  
  <p>Scenario:</p>
  
  <p>A <a href="http://www.w3.org/TR/rdf-primer/" rel="nofollow">RDF-triple</a> is used to express simple statements about resources, formatting (subject, predicate, object).</p>
  
  <p>Suppose I have two predicates, one is play_for, the other is race_for, each of which contains n triples, as follows:</p>
  
  <p>1-st predicate: play_for; n triples:
  (Ray Allen, play_for, Boston Celtics), (Kobe Bryant, play_for, Lakers), ...
  For short, (A_i, play for, T_i) for i =1 to n.</p>
  
  <p>2-rd predicate: race_for; n triples:
  (Boston Celtics, race_for, NBA championship), (Lakers, race_for, NBA championship), ...
  For short, (T_i, race for, NBA) for i=1 to n.</p>
  
  <p>Tensor representation is one way to modeling this 2n triples. 
  I'm studying <a href="http://www.cip.ifi.lmu.de/~nickel/data/paper-icml2011.pdf" rel="nofollow">Maximilian Nickel's paper</a> to use tensor factorization to find the latent semantic structure of a dataset. And the first step is to represent the dataset using tensor.</p>
  
  <p>A tensor entry X_ijk = 1 denotes the fact that there exists a relation (i-th entity, k-th predicate, j-th entity). Otherwise, for non-existing and unknown relations, the entry is set to zero. For instance, this 2n triples can be modeled by a tensor as:</p>
  
  <pre><code> One slice:  (A_i, play for, T_i)
  
         A1, A2,...,An, T1, T2,...,Tn, NBA
   A1    0    0      0   1   0      0    0
   A2    0    0      0   0   1      0    0
   :
   An    0    0      0   0   0      1    0
   T1    0    0      0   0   0      0    0
   T2    0    0      0   0   0      0    0
   :
   Tn    0    0      0   0   0      0    0
   NBA   0    0      0   0   0      0    0
  
   The other slice: (T_i, race for, NBA)
  
        A1,  A2,...,An, T1, T2,...,Tn, NBA
   A1    0    0      0   0   0      0    0
   A2    0    0      0   0   0      0    0
   :
   An    0    0      0   0   0      0    0
   T1    0    0      0   0   0      0    1
   T2    0    0      0   0   0      0    1
   :
   Tn    0    0      0   0   0      0    1
   NBA   0    0      0   0   0      0    0
  </code></pre>
  
  <p>Assume the RDF-triples is stored in 'test.txt'. My question is how to programming this modeling process using Python. </p>
  
  <p>Here is what I think:</p>
  
  <p>The most difficult thing is how to get the coordinate of the RDF-triple corresponding to the position of non-zeros in the tensor. 
  At first, here is a list containing all entities:</p>
  
  <pre><code>T = ['A1',...,'An','T1',...'Tn','NBA']
  </code></pre>
  
  <p>For every RDF-triple (Subject_i, Predicate_k, Object_j) in the dataset, there is a coordinate (i,j,k) describe the position of X_ijk = 1 in a tensor. For instance, The coordinate of a existing RDF-triple (A_i, play for, T_i) is (5, 1, 13), which means X(5,13) = 1 in the first slice matrix. However, I don't know how to get this coordinate. Should I use dictionary to store the triple?  </p>
  
  <p>I don't quite familiar with Python, and I've tried to get the solution, but I have no idea about how to solve it. Any help would be greatly appreciated.</p>
  
  <p><strong>EDIT:</strong> For brevity and readability, I have deleted the description of RDF. </p>