Skip to content

Commit

Permalink
Cleaning up
Browse files Browse the repository at this point in the history
  • Loading branch information
katjabercic committed Dec 1, 2023
1 parent 16a6afe commit 6582b31
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 19 deletions.
2 changes: 1 addition & 1 deletion web/concepts/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,11 @@ class Source(models.TextChoices):
def key():
SOURCES = [
Item.Source.WIKIDATA,
Item.Source.WIKIPEDIA_EN,
Item.Source.NLAB,
Item.Source.MATHWORLD,
Item.Source.PROOF_WIKI,
Item.Source.ENCYCLOPEDIA_OF_MATHEMATICS,
Item.Source.WIKIPEDIA_EN,
Item.Source.AGDA_UNIMATH,
]
return lambda item: SOURCES.index(item.source)
Expand Down
9 changes: 4 additions & 5 deletions web/concepts/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ def home(request):
"concepts": autocomplete_names,
"number_of_links": {
"wikidata": Item.objects.filter(source=Item.Source.WIKIDATA).count(),
"wikipedia_en": Item.objects.filter(source=Item.Source.WIKIPEDIA_EN).count(),
"wikipedia_en": Item.objects.filter(
source=Item.Source.WIKIPEDIA_EN
).count(),
"nlab": Item.objects.filter(source=Item.Source.NLAB).count(),
"mathworld": Item.objects.filter(source=Item.Source.MATHWORLD).count(),
"proof_wiki": Item.objects.filter(source=Item.Source.PROOF_WIKI).count(),
Expand Down Expand Up @@ -53,8 +55,5 @@ def redirect_item_to_concept(request, source, identifier):

def results(request, query):
concepts = Concept.objects.filter(name__contains=query)
context = {
"query": query,
"results": [concept.name for concept in concepts]
}
context = {"query": query, "results": [concept.name for concept in concepts]}
return render(request, "results.html", context)
2 changes: 1 addition & 1 deletion web/slurper/management/commands/clear_wikidata.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from concepts.models import Item
from django.core.management.base import BaseCommand
from wd_raw_item import WD_OTHER_SOURCES
from slurper.wd_raw_item import WD_OTHER_SOURCES


class Command(BaseCommand):
Expand Down
11 changes: 6 additions & 5 deletions web/slurper/source_wikidata.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import logging

import requests
from concepts.models import Item
from django.db.utils import IntegrityError
from slurper.wd_raw_item import WD_OTHER_SOURCES, BaseWdRawItem


class WikidataSlurper:
SPARQL_URL = "https://query.wikidata.org/sparql"

Expand Down Expand Up @@ -89,9 +91,7 @@ def save_items(self):

def save_links(self):
for json_item in self.raw_data:
BaseWdRawItem.raw_item(
self.source, json_item
).save_links()
BaseWdRawItem.raw_item(self.source, json_item).save_links()


SLURPERS = [
Expand All @@ -118,7 +118,8 @@ def save_links(self):

SLURPERS += [
WikidataSlurper(
source, f"\n ?item {source_property['wd_property']} ?{source_property['json_key']} .\n"
source,
f"\n ?item {property['wd_property']} ?{property['json_key']} .\n",
)
for source, source_property in WD_OTHER_SOURCES.items()
for source, property in WD_OTHER_SOURCES.items()
]
17 changes: 10 additions & 7 deletions web/slurper/wd_raw_item.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from typing import Optional

from concepts.models import Item, Link

WD_OTHER_SOURCES = {
Expand All @@ -17,12 +18,12 @@
Item.Source.ENCYCLOPEDIA_OF_MATHEMATICS: {
"wd_property": "wdt:P7554",
"json_key": "eomID",
}
},
}
# Wikipedia is dealt with elsewhere

class BaseWdRawItem:

class BaseWdRawItem:
def __init__(self, source, json_item):
self.source = source
self.raw = json_item
Expand Down Expand Up @@ -64,18 +65,18 @@ def _get_item_queryset(self):

def item_exists(self):
return self._get_item_queryset().exists()

def get_item(self) -> Optional[Item]:
return self._get_item_queryset().first()

def yield_switched_if_not_exists(self, source):
switched = self.switch_source_to(source)
if not switched.item_exists():
yield switched.to_item()

def save_link_to(self, source):
target = self.switch_source_to(source)
if target is not None:
if target is not None:
destinationItem = target.get_item()
if self.item is not None and destinationItem is not None:
Link.save_new(self.item, destinationItem, Link.Label.WIKIDATA)
Expand Down Expand Up @@ -130,6 +131,7 @@ def save_links(self):
if self.has_source(source):
self.save_link_to(source)


class WpENRawItem(BaseWdRawItem):
def __init__(self, json_item):
super().__init__(Item.Source.WIKIPEDIA_EN, json_item)
Expand Down Expand Up @@ -160,6 +162,7 @@ def save_links(self):
# link back to WD items
self.save_link_to(WdRawItem(self.raw))


class nLabRawItem(OtherWdRawItem):
def __init__(self, json_item):
super().__init__(Item.Source.NLAB, json_item)
Expand Down Expand Up @@ -189,4 +192,4 @@ def __init__(self, json_item):
super().__init__(Item.Source.ENCYCLOPEDIA_OF_MATHEMATICS, json_item)

def url(self):
return "https://encyclopediaofmath.org/wiki/" + self.identifier()
return "https://encyclopediaofmath.org/wiki/" + self.identifier()

0 comments on commit 6582b31

Please sign in to comment.