mirror of
https://github.com/NexVeridian/wikidata-to-surrealdb.git
synced 2025-09-02 09:59:13 +00:00
swap to surrealdb thing
This commit is contained in:
parent
f4babefdba
commit
b838a5c326
2 changed files with 15 additions and 26 deletions
|
@ -23,8 +23,8 @@ async fn main() -> Result<(), Error> {
|
||||||
|
|
||||||
db.use_ns("wikidata").use_db("wikidata").await?;
|
db.use_ns("wikidata").use_db("wikidata").await?;
|
||||||
|
|
||||||
let _: Option<EntityMini> = db.delete(id.to_string()).await?;
|
let _: Option<EntityMini> = db.delete(&id).await?;
|
||||||
let _: Option<EntityMini> = db.create(id.to_string()).content(data.clone()).await?;
|
let _: Option<EntityMini> = db.create(&id).content(data.clone()).await?;
|
||||||
|
|
||||||
// println!("{:#?}", data);
|
// println!("{:#?}", data);
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|
37
src/utils.rs
37
src/utils.rs
|
@ -3,6 +3,7 @@ use serde::{Deserialize, Serialize};
|
||||||
use serde_json::from_reader;
|
use serde_json::from_reader;
|
||||||
use serde_json::Value;
|
use serde_json::Value;
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
|
use surrealdb::sql::Thing;
|
||||||
use wikidata::ClaimValueData;
|
use wikidata::ClaimValueData;
|
||||||
use wikidata::{ClaimValue, Entity, Lang, Pid, WikiId};
|
use wikidata::{ClaimValue, Entity, Lang, Pid, WikiId};
|
||||||
|
|
||||||
|
@ -14,28 +15,16 @@ pub async fn get_entity(path: &str) -> Result<Entity, Error> {
|
||||||
Ok(data)
|
Ok(data)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
|
||||||
pub struct Id {
|
|
||||||
pub entity_type: String,
|
|
||||||
pub id: u64,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Id {
|
|
||||||
pub fn to_string(&self) -> (String, String) {
|
|
||||||
(self.entity_type.clone(), self.id.to_string())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||||
pub struct EntityMini {
|
pub struct EntityMini {
|
||||||
// In English
|
// In English
|
||||||
pub label: String,
|
pub label: String,
|
||||||
pub claims: Vec<(Id, ClaimValueData)>,
|
pub claims: Vec<(Thing, ClaimValueData)>,
|
||||||
pub description: String,
|
pub description: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl EntityMini {
|
impl EntityMini {
|
||||||
pub fn from_entity(entity: Entity) -> (Id, Self) {
|
pub fn from_entity(entity: Entity) -> (Thing, Self) {
|
||||||
(
|
(
|
||||||
get_id(&entity),
|
get_id(&entity),
|
||||||
Self {
|
Self {
|
||||||
|
@ -46,14 +35,14 @@ impl EntityMini {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn flatten_claims(claims: Vec<(Pid, ClaimValue)>) -> Vec<(Id, ClaimValueData)> {
|
fn flatten_claims(claims: Vec<(Pid, ClaimValue)>) -> Vec<(Thing, ClaimValueData)> {
|
||||||
claims
|
claims
|
||||||
.iter()
|
.iter()
|
||||||
.flat_map(|(pid, claim_value)| {
|
.flat_map(|(pid, claim_value)| {
|
||||||
let mut flattened = vec![(
|
let mut flattened = vec![(
|
||||||
Id {
|
Thing {
|
||||||
id: pid.0,
|
id: pid.0.into(),
|
||||||
entity_type: "Property".to_string(),
|
tb: "Property".to_string(),
|
||||||
},
|
},
|
||||||
claim_value.data.clone(),
|
claim_value.data.clone(),
|
||||||
)];
|
)];
|
||||||
|
@ -61,9 +50,9 @@ impl EntityMini {
|
||||||
flattened.extend(claim_value.qualifiers.iter().map(
|
flattened.extend(claim_value.qualifiers.iter().map(
|
||||||
|(qualifier_pid, qualifier_value)| {
|
|(qualifier_pid, qualifier_value)| {
|
||||||
(
|
(
|
||||||
Id {
|
Thing {
|
||||||
id: qualifier_pid.0,
|
id: qualifier_pid.0.into(),
|
||||||
entity_type: "Property".to_string(),
|
tb: "Property".to_string(),
|
||||||
},
|
},
|
||||||
qualifier_value.clone(),
|
qualifier_value.clone(),
|
||||||
)
|
)
|
||||||
|
@ -75,15 +64,15 @@ impl EntityMini {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_id(entity: &Entity) -> Id {
|
fn get_id(entity: &Entity) -> Thing {
|
||||||
let (id, entity_type) = match entity.id {
|
let (id, tb) = match entity.id {
|
||||||
WikiId::EntityId(qid) => (qid.0, "Entity".to_string()),
|
WikiId::EntityId(qid) => (qid.0, "Entity".to_string()),
|
||||||
WikiId::PropertyId(pid) => (pid.0, "Property".to_string()),
|
WikiId::PropertyId(pid) => (pid.0, "Property".to_string()),
|
||||||
WikiId::LexemeId(lid) => (lid.0, "Lexeme".to_string()),
|
WikiId::LexemeId(lid) => (lid.0, "Lexeme".to_string()),
|
||||||
_ => todo!("Not implemented"),
|
_ => todo!("Not implemented"),
|
||||||
};
|
};
|
||||||
|
|
||||||
Id { id, entity_type }
|
Thing { id: id.into(), tb }
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_name(entity: &Entity) -> String {
|
fn get_name(entity: &Entity) -> String {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue