(data: dict)
| 52 | |
| 53 | |
| 54 | def insert_so_data(data: dict) -> None: |
| 55 | # Calculate embedding values for questions and answers |
| 56 | for q in data["items"]: |
| 57 | question_text = q["title"] + "\n" + q["body_markdown"] |
| 58 | q["embedding"] = embeddings.embed_query(question_text) |
| 59 | for a in q["answers"]: |
| 60 | a["embedding"] = embeddings.embed_query( |
| 61 | question_text + "\n" + a["body_markdown"] |
| 62 | ) |
| 63 | |
| 64 | # Cypher, the query language of Neo4j, is used to import the data |
| 65 | # https://neo4j.com/docs/getting-started/cypher-intro/ |
| 66 | # https://neo4j.com/docs/cypher-cheat-sheet/5/auradb-enterprise/ |
| 67 | import_query = """ |
| 68 | UNWIND $data AS q |
| 69 | MERGE (question:Question {id:q.question_id}) |
| 70 | ON CREATE SET question.title = q.title, question.link = q.link, question.score = q.score, |
| 71 | question.favorite_count = q.favorite_count, question.creation_date = datetime({epochSeconds: q.creation_date}), |
| 72 | question.body = q.body_markdown, question.embedding = q.embedding |
| 73 | FOREACH (tagName IN q.tags | |
| 74 | MERGE (tag:Tag {name:tagName}) |
| 75 | MERGE (question)-[:TAGGED]->(tag) |
| 76 | ) |
| 77 | FOREACH (a IN q.answers | |
| 78 | MERGE (question)<-[:ANSWERS]-(answer:Answer {id:a.answer_id}) |
| 79 | SET answer.is_accepted = a.is_accepted, |
| 80 | answer.score = a.score, |
| 81 | answer.creation_date = datetime({epochSeconds:a.creation_date}), |
| 82 | answer.body = a.body_markdown, |
| 83 | answer.embedding = a.embedding |
| 84 | MERGE (answerer:User {id:coalesce(a.owner.user_id, "deleted")}) |
| 85 | ON CREATE SET answerer.display_name = a.owner.display_name, |
| 86 | answerer.reputation= a.owner.reputation |
| 87 | MERGE (answer)<-[:PROVIDED]-(answerer) |
| 88 | ) |
| 89 | WITH * WHERE NOT q.owner.user_id IS NULL |
| 90 | MERGE (owner:User {id:q.owner.user_id}) |
| 91 | ON CREATE SET owner.display_name = q.owner.display_name, |
| 92 | owner.reputation = q.owner.reputation |
| 93 | MERGE (owner)-[:ASKED]->(question) |
| 94 | """ |
| 95 | neo4j_graph.query(import_query, {"data": data["items"]}) |
| 96 | |
| 97 | |
| 98 | # Streamlit |
no outgoing calls
no test coverage detected