From 623f1f1d8988c1317087bc6257edbb187d6b195a Mon Sep 17 00:00:00 2001 From: NexVeridian Date: Wed, 13 Dec 2023 05:57:47 +0000 Subject: [PATCH] flat --- src/main.rs | 3 +-- src/utils.rs | 30 ++++++++++++++++++++++++++++-- 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/src/main.rs b/src/main.rs index d92eb9c..212364c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,7 +3,6 @@ use anyhow::{Error, Result}; use dotenv_codegen::dotenv; use surrealdb::engine::remote::ws::Ws; use surrealdb::opt::auth::Root; -use surrealdb::sql::Thing; use surrealdb::Surreal; mod utils; @@ -11,7 +10,7 @@ use utils::*; #[tokio::main] async fn main() -> Result<(), Error> { - let data = get_entity("data/wiki.json").await?; + let data = get_entity("data/e.json").await?; let (id, data) = EntityMini::from_entity(data); let db = Surreal::new::("0.0.0.0:8000").await?; diff --git a/src/utils.rs b/src/utils.rs index d9a67eb..d88e1b1 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -3,9 +3,11 @@ use serde::{Deserialize, Serialize}; use serde_json::from_reader; use serde_json::Value; use std::fs::File; +use wikidata::ClaimValueData; use wikidata::{ClaimValue, Entity, Lang, Pid, WikiId}; pub async fn get_entity(path: &str) -> Result { + // From here - https://www.wikidata.org/wiki/Special:EntityData/P1476.json let mut file = File::open(path)?; let json: Value = from_reader(&mut file)?; let data = Entity::from_json(json).expect("Failed to parse JSON"); @@ -28,7 +30,7 @@ impl Id { pub struct EntityMini { // In English pub label: String, - pub claims: Vec<(Pid, ClaimValue)>, + pub claims: Vec<(Id, ClaimValueData)>, pub description: String, } @@ -38,11 +40,35 @@ impl EntityMini { get_id(&entity), Self { label: get_name(&entity), - claims: entity.claims.clone(), + claims: Self::flatten_claims(entity.claims.clone()), description: get_description(&entity).unwrap_or("".to_string()), }, ) } + fn flatten_claims(claims: Vec<(Pid, ClaimValue)>) -> Vec<(Id, ClaimValueData)> { + claims + .iter() + .flat_map(|(pid, claim_value)| { + let mut flattened = vec![( + Id { + id: pid.0, + entity_type: "Property".to_string(), + }, + claim_value.data.clone(), + )]; + for (qualifier_pid, qualifier_value) in &claim_value.qualifiers { + flattened.push(( + Id { + id: qualifier_pid.0, + entity_type: "Property".to_string(), + }, + qualifier_value.clone(), + )); + } + flattened + }) + .collect() + } } fn get_id(entity: &Entity) -> Id {