mirror of
https://github.com/NexVeridian/wikidata-to-surrealdb.git
synced 2025-09-02 01:49:13 +00:00
ClaimData
This commit is contained in:
parent
60cbb47a10
commit
e0d2cef391
2 changed files with 47 additions and 17 deletions
|
@ -21,7 +21,7 @@ async fn main() -> Result<(), Error> {
|
||||||
|
|
||||||
db.use_ns("wikidata").use_db("wikidata").await?;
|
db.use_ns("wikidata").use_db("wikidata").await?;
|
||||||
|
|
||||||
let file = File::open("data/ex2.json")?;
|
let file = File::open("data/e.json")?;
|
||||||
let reader = BufReader::new(file);
|
let reader = BufReader::new(file);
|
||||||
|
|
||||||
for line in reader.lines() {
|
for line in reader.lines() {
|
||||||
|
|
62
src/utils.rs
62
src/utils.rs
|
@ -3,9 +3,41 @@ use serde::{Deserialize, Serialize};
|
||||||
use surrealdb::sql::Thing;
|
use surrealdb::sql::Thing;
|
||||||
use wikidata::{ClaimValue, ClaimValueData, Entity, Lang, Pid, WikiId};
|
use wikidata::{ClaimValue, ClaimValueData, Entity, Lang, Pid, WikiId};
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||||
|
pub enum ClaimData {
|
||||||
|
Thing(Thing),
|
||||||
|
ClaimValueData(ClaimValueData),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ClaimData {
|
||||||
|
fn from_cvd(cvd: ClaimValueData) -> Self {
|
||||||
|
match cvd {
|
||||||
|
ClaimValueData::Item(qid) => ClaimData::Thing(Thing {
|
||||||
|
id: qid.0.into(),
|
||||||
|
tb: "Entity".to_string(),
|
||||||
|
}),
|
||||||
|
ClaimValueData::Property(pid) => ClaimData::Thing(Thing {
|
||||||
|
id: pid.0.into(),
|
||||||
|
tb: "Property".to_string(),
|
||||||
|
}),
|
||||||
|
ClaimValueData::Lexeme(lid) => ClaimData::Thing(Thing {
|
||||||
|
id: lid.0.into(),
|
||||||
|
tb: "Lexeme".to_string(),
|
||||||
|
}),
|
||||||
|
_ => ClaimData::ClaimValueData(cvd),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||||
pub struct Claims {
|
pub struct Claims {
|
||||||
pub claims: Vec<(Thing, ClaimValueData)>,
|
pub claims: Vec<Claim>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||||
|
pub struct Claim {
|
||||||
|
pub id: Thing,
|
||||||
|
pub value: ClaimData,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||||
|
@ -18,12 +50,12 @@ pub struct EntityMini {
|
||||||
impl EntityMini {
|
impl EntityMini {
|
||||||
pub fn from_entity(entity: Entity) -> (Thing, (Thing, Claims), Self) {
|
pub fn from_entity(entity: Entity) -> (Thing, (Thing, Claims), Self) {
|
||||||
let thing_claim = Thing {
|
let thing_claim = Thing {
|
||||||
id: get_id(&entity).id,
|
id: get_id_entity(&entity).id,
|
||||||
tb: "Claims".to_string(),
|
tb: "Claims".to_string(),
|
||||||
};
|
};
|
||||||
|
|
||||||
(
|
(
|
||||||
get_id(&entity),
|
get_id_entity(&entity),
|
||||||
(
|
(
|
||||||
thing_claim.clone(),
|
thing_claim.clone(),
|
||||||
Self::flatten_claims(entity.claims.clone()),
|
Self::flatten_claims(entity.claims.clone()),
|
||||||
|
@ -41,23 +73,21 @@ impl EntityMini {
|
||||||
claims: claims
|
claims: claims
|
||||||
.iter()
|
.iter()
|
||||||
.flat_map(|(pid, claim_value)| {
|
.flat_map(|(pid, claim_value)| {
|
||||||
let mut flattened = vec![(
|
let mut flattened = vec![Claim {
|
||||||
Thing {
|
id: Thing {
|
||||||
id: pid.0.into(),
|
id: pid.0.into(),
|
||||||
tb: "Property".to_string(),
|
tb: "Property".to_string(),
|
||||||
},
|
},
|
||||||
claim_value.data.clone(),
|
value: ClaimData::from_cvd(claim_value.data.clone()),
|
||||||
)];
|
}];
|
||||||
|
|
||||||
flattened.extend(claim_value.qualifiers.iter().map(
|
flattened.extend(claim_value.qualifiers.iter().map(
|
||||||
|(qualifier_pid, qualifier_value)| {
|
|(qualifier_pid, qualifier_value)| Claim {
|
||||||
(
|
id: Thing {
|
||||||
Thing {
|
id: qualifier_pid.0.into(),
|
||||||
id: qualifier_pid.0.into(),
|
tb: "Property".to_string(),
|
||||||
tb: "Property".to_string(),
|
},
|
||||||
},
|
value: ClaimData::from_cvd(qualifier_value.clone()),
|
||||||
qualifier_value.clone(),
|
|
||||||
)
|
|
||||||
},
|
},
|
||||||
));
|
));
|
||||||
flattened
|
flattened
|
||||||
|
@ -67,7 +97,7 @@ impl EntityMini {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_id(entity: &Entity) -> Thing {
|
fn get_id_entity(entity: &Entity) -> Thing {
|
||||||
let (id, tb) = 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()),
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue