<jupyter_start><jupyter_text>Exporting Vectors and Metadata to tsvIn this Notebook we are going to export PyKeen created embeddings to tsv files expected by the [Embedding Projector](https://projector.tensorflow.org/) tool.  Initial Configuration  Clonning RepositoryCloning the git repository containing PyKeen embeddings. In this example we are going to clone embeddings create for the Industry 4.0 Standards Landscape Knowledge Graph.<jupyter_code>!git clone https://github.com/i40-Tools/I40KG-Embeddings.git<jupyter_output><empty_output><jupyter_text>Installing LibrariesInstall and import the [rdflib](https://github.com/RDFLib/rdflib) library, to execute SPARQL queries to limit the scope of the embeddings we would like to display.<jupyter_code>!pip install rdflib
import json
from rdflib import Graph
import pprint<jupyter_output><empty_output><jupyter_text>Defining Main VariablesWe use two main variables in the rest of the notebook:1.   **kg_path**: Correspont to the path of the knowledge graph, i.e., ***file-name.nt.*** Do not forget that you have to add /content/ folder at the begining of the path to find in the clonned repo in Colab.2.   **emb_path**: Correspont to the path containing the embedding output after PyKeen training phase, i.e., ***file-name.json***<jupyter_code>kg_path = "/content/I40KG-Embeddings/embeddings/sto/sto-enriched.nt"
emb_path = "/content/I40KG-Embeddings/embeddings/training_set_relatedTo/TransH/entities_to_embeddings.json"<jupyter_output><empty_output><jupyter_text>Exporting Vecs and Meta TSV Files  Creating labels and tokensFirst, we will run a SPARQL query to select the entities embeddings we would like to export for visualization on Embedding Projector. We create at the same time labels and tokens to be exported as TSV files.<jupyter_code>g = Graph()
g.parse(kg_path, format="nt")
len(g) # prints 2

#query to get all the standards from the sto.nt file
qres = g.query("""
        PREFIX owl: <http://www.w3.org/2002/07/owl#>
        PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
        PREFIX sto: <https://w3id.org/i40/sto#>
        
        select ?s where {
           ?s rdf:type sto:Standard .
         } limit 1000 
        """)

labels = []
tokens = []

#to get the corresponding embeddings of the frameworks/standards from the json file 
with open(emb_path,'rb') as f:
    array = json.load(f)
for row in qres:
    for key,value in array.items():
        if key == "%s" % row:
            labels.append(key.replace('https://w3id.org/i40/', '')) # Just to clean the namespace for better visualization in embedding projector
            tokens.append(array[key])

print(len(labels))
print(len(tokens))<jupyter_output><empty_output><jupyter_text>Saving vecs and meta TSV filesNow, based on the labels and tokens we create the TSV files.<jupyter_code>import io

out_v = io.open('vecs.tsv', 'w', encoding='utf-8')
out_m = io.open('meta.tsv', 'w', encoding='utf-8')

standards_size = len(labels)

for standard_num in range(1, standards_size):
  out_m.write(labels[standard_num] + "\n")
  out_v.write('\t'.join([str(x) for x in tokens[standard_num]]) + "\n")
out_v.close()
out_m.close()<jupyter_output><empty_output><jupyter_text>Downloading FilesIf you are working in colab, the following piece of code is required to download vecs and meta TSV files.<jupyter_code>try:
  from google.colab import files
except ImportError:
  pass
else:
  files.download('vecs.tsv')
  files.download('meta.tsv')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1072961373390558, 'fraction_numerical': 0.01201716738197425, 'mean_word_length': 4.602564102564102, 'pattern_counts': {'":': 0, '<': 28, '<?xml version=': 0, '>': 28, 'https://': 5, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '585248', 'license': 'MIT', 'n_tokens_mistral': 1168, 'n_tokens_neox': 1113, 'n_words': 351, 'path': 'notebooks/visualization/Embedding_Projector.ipynb', 'repo_name': 'collarad/PyKEEN', 'size': '3495'}