From 2f6c602c88b116581795672fb20e1a5ea9e00178 Mon Sep 17 00:00:00 2001 From: Wolfgang Fahl Date: Mon, 18 Mar 2024 07:26:10 +0100 Subject: [PATCH] refactors --- pyproject.toml | 2 +- tests/test_ris.py | 71 ++++++++++++++++++++++++++++++++++++++++------- 2 files changed, 62 insertions(+), 11 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 67c4ad2..ecb3eb0 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -20,7 +20,7 @@ dependencies = [ "nicegui", "ngwidgets>=0.14.0", # https://pypi.org/project/pylodstorage/ - 'pyLodStorage>=0.9.0', + 'pyLodStorage>=0.9.2', # https://pypi.org/project/habanero/ 'habanero~=1.2.2', # https://pypi.org/project/search-engine-parser/ diff --git a/tests/test_ris.py b/tests/test_ris.py index 8042bf8..d2f2e6f 100644 --- a/tests/test_ris.py +++ b/tests/test_ris.py @@ -5,6 +5,7 @@ """ import json import re +from dataclasses import field from ngwidgets.basetest import Basetest from skg.ris import RIS_Entry import os @@ -14,15 +15,53 @@ from ez_wikidata.wdproperty import PropertyMapping, WdDatatype from lodstorage.sparql import SPARQL from lodstorage.query import QueryManager +from ngwidgets.yamlable import lod_storable +from typing import Dict -class STT_Paper: +class STT_Paper(RIS_Entry): """ Softwaretechnik Trend Paper """ - def __init__(self,ris_entry:RIS_Entry): - self.paper=ris_entry + volume:str=None + issue:str=None + + def __post_init__(self): + super().__post_init__() + self.initialize_record() + + @classmethod + def from_ris_entry(cls, ris_entry): + """ + Class method to create an STT_Paper instance from an RIS_Entry instance. + """ + # Initialize a new STT_Paper instance with RIS_Entry data + paper = cls(**ris_entry.to_dict()) + # Perform any additional initialization specific to STT_Paper + paper.initialize_record() + return paper + + @staticmethod + def from_dblp_lod(record: Dict) -> "STT_Paper": + ris_entry=RIS_Entry( + primary_title=record["title"], + #authors=lod["authors"], + year=record["year"], + #archivedWebpage=lod["archivedWebpage"], + #listedOnTocPage=lod["listedOnTocPage"], + ) + paper=STT_Paper.from_ris_entry(ris_entry) + paper.volume=record["volume"] + paper.issue=record["issue"] + #author_count=int(lod["author_count"]), + #paper=lod["paper"] + return paper + + def initialize_record(self): regex = r"Band (\d+), Heft (\d+)" - match = re.search(regex, self.paper.secondary_title) + if self.secondary_title: + match = re.search(regex, self.secondary_title) + self.volume = match.group(1) + self.issue = match.group(2) self.property_mappings=RIS_Entry.get_property_mappings() self.property_mappings.extend([ PropertyMapping( @@ -46,14 +85,21 @@ def __init__(self,ris_entry:RIS_Entry): ) ] ) - record=self.paper.to_dict() - record["lang_qid"]=self.paper.lang_qid - record["label"]=self.paper.primary_title + record=self.to_dict() + record["lang_qid"]=self.lang_qid + record["label"]=self.primary_title record["description"]="Paper in Software Technik Trends" - record["volume"] = match.group(1) - record["issue"] = match.group(2) self.record=record +@lod_storable +class STT: + papers: dict[str,STT_Paper]=field(default_factory=dict) + + def add_from_dblp_lod(self, lod: [Dict]): + for record in lod: + paper = STT_Paper.from_dblp_lod(record) + self.papers[paper.primary_title] = paper # Or merge based on your logic + class TestRis2Wikidata(Basetest): """ @@ -84,9 +130,14 @@ def test_dblp_stt(self): qm = QueryManager(lang="sparql", queriesPath=qYamlFile) query = qm.queriesByName["STT-Papers"] lod = sparql.queryAsListOfDicts(query.query) + stt=STT() + stt.add_from_dblp_lod(lod) debug=True if debug: print(f"found {len(lod)} STT papers") + for i,record in enumerate(lod,start=1): + if i<10: + print(json.dumps(record,indent=2,default=str)) def testPaper(self): @@ -96,7 +147,7 @@ def testPaper(self): # ParTeG - A Model-Based Testing Tool wd=Wikidata() wd.loginWithCredentials() - paper=STT_Paper(self.ris_dict[416]) + paper=STT_Paper.from_ris_entry(self.ris_dict[416]) print(json.dumps(paper.record,indent=2,default=str)) #return write=True