Extract of UK Frigates and Their Precendents

This post shows how to access the wikipedia knowledge graph
wikipedia
scrape
Author

Luke Heley

Published

March 26, 2023

Objective

Extract list of UK Frigates and their precedents.

Data

We use wikidata to first extract the different types of warship

q <- '
SELECT ?ship_type ?ship_typeLabel WHERE {
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
  ?ship_type wdt:P31 wd:Q2235308;
    wdt:P279 wd:Q3114762.
  
  OPTIONAL {  }
}
  '

WikidataQueryServiceR::query_wikidata(q)
Rows: 55 Columns: 2
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (2): ship_type, ship_typeLabel

ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
# A tibble: 55 × 2
   ship_type                              ship_typeLabel  
   <chr>                                  <chr>           
 1 http://www.wikidata.org/entity/Q17205  aircraft carrier
 2 http://www.wikidata.org/entity/Q104843 cruiser         
 3 http://www.wikidata.org/entity/Q161705 frigate         
 4 http://www.wikidata.org/entity/Q170013 corvette        
 5 http://www.wikidata.org/entity/Q174736 destroyer       
 6 http://www.wikidata.org/entity/Q182531 battleship      
 7 http://www.wikidata.org/entity/Q188924 galley          
 8 http://www.wikidata.org/entity/Q207452 ship of the line
 9 http://www.wikidata.org/entity/Q629990 bireme          
10 http://www.wikidata.org/entity/Q640078 minelayer       
# … with 45 more rows

And subsequently different types of frigates operated by the Royal Navy

q <- '
SELECT ?ship_class ?ship_classLabel ?service_entry ?followed_by ?followed_byLabel ?operator ?operatorLabel ?image ?service_retirement ?follows ?followsLabel ?service_entryLabel ?service_retirementLabel ?imageLabel WHERE {
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
  ?ship_class wdt:P31 wd:Q559026; # instance of ship class
    wdt:P279 wd:Q106070703. # subclass of frigate
  ?ship_class wdt:P137 wd:Q172771. # operated by RN
  OPTIONAL { ?ship_class wdt:P729 ?service_entry. }
  OPTIONAL { ?ship_class wdt:P730 ?service_retirement. }
  OPTIONAL { ?ship_class wdt:P155 ?follows. }
  OPTIONAL { ?ship_class wdt:P156 ?followed_by. }
  OPTIONAL { ?ship_class wdt:P137 ?operator. }
  OPTIONAL { ?ship_class wdt:P18 ?image. }
}
'
rn_frigates <- WikidataQueryServiceR::query_wikidata(q)
Rows: 83 Columns: 14
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr  (10): ship_class, ship_classLabel, followed_by, followed_byLabel, opera...
dttm  (4): service_entry, service_retirement, service_entryLabel, service_re...

ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
rn_frigates |>
  dplyr::filter(operatorLabel == "Royal Navy") |>
  dplyr::select(from = ship_classLabel, 
                to = followed_byLabel, 
                value = service_entry) |>
  dplyr::distinct() |>
  dplyr::filter(!is.na(to)) |>
  igraph::graph_from_data_frame() |>
  visNetwork::visIgraph()

Next step. Extract data on type 26, type 23, type 22, type 21, leander class, rothesay-class, valour-class, whiteby-class and saisbury class.

Future intention is to compare to other nations where data are available.