Skip to content

Instantly share code, notes, and snippets.

@ikwattro
Created June 21, 2017 21:13
Show Gist options
  • Save ikwattro/255acfbc28970ce70e6f0f0a2e557073 to your computer and use it in GitHub Desktop.
Save ikwattro/255acfbc28970ce70e6f0f0a2e557073 to your computer and use it in GitHub Desktop.
from neo4j.v1 import GraphDatabase
from gensim import corpora, models
driver = GraphDatabase.driver("bolt://XXXXX:7687", auth=("neo4j", "XXXX"))
session = driver.session()
q = "MATCH (m:Memory) WHERE id(m) = 34452 \
MATCH (m)-[:HAS_CONTENT]->(c)-[:HAS_ANNOTATED_TEXT]->(at)-[:CONTAINS_SENTENCE]->(s)-[:HAS_TAG]->(tag) RETURN collect(tag.value) AS tags"
result = list(session.run(q))
t = result[0]["tags"]
tags = []
for i in t:
tags.append(t)
# print(tags)
dictionary = corpora.Dictionary(tags)
corpus = [dictionary.doc2bow(text) for text in tags]
ldamodel = models.ldamodel.LdaModel(corpus, num_topics=10, id2word = dictionary, passes=10)
topics = ldamodel.show_topics(num_words=10, formatted=False)
ldaTopics = []
for topic in topics[0][1]:
ldaTopics.append({'tag': topic[0], 'weight': topic[1]})
ldaQ = "MATCH (m:Memory) WHERE id(m) = 34452 " \
"UNWIND {topics} AS topic " \
"MATCH (t:Tag {id: topic.tag + '_en'}) " \
"MERGE (m)-[r:LDA_TOPIC_PREPASS]->(t) SET r.weight = topic.weight"
session.run(ldaQ, {'topics': ldaTopics}).consume()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment