query: # Most frequent n-grams from a set of 1000 COVID-19 publications
  SELECT DISTINCT ?Ngram ?Count ?Length ?CountTimesLength ?N ?ExamplePub ?ExamplePubTitle
  
  WITH
  { # Generating a list of regexes to look for the NumericValue-th word in a string                                                              
    SELECT ?Regex1 ?Regex2 ?Regex3 ?Regex4 ?NumericValue 
      { 
        ?NumberItem wdt:P5176 []; wdt:P1181 ?NumericValue . 
        FILTER( ?NumericValue > 0 ) 
        FILTER( ?NumericValue < 51)
        BIND( CONCAT( "^([^ ]+ ){", STR( ?NumericValue - 1 ),"}([^ ]+) .*") AS ?Regex1)
        BIND( CONCAT( "^([^ ]+ ){", STR( ?NumericValue + 1 ),"}([^ ]+) .*") AS ?Regex2) 
        BIND( CONCAT( "^([^ ]+ ){", STR( ?NumericValue + 3 ),"}([^ ]+) .*") AS ?Regex3) 
        BIND( CONCAT( "^([^ ]+ ){", STR( ?NumericValue + 5 ),"}([^ ]+) .*") AS ?Regex4) 
      }
  } AS %regexes 
  WITH
  { # Generating a list of titles to be analyzed
    SELECT ?Title ?Publication ?Seeds ?ClearTitleLength
     { # Choosing the entities whose titles are to be analyzed
        {
            SELECT ?Title ?Publication  WHERE {  
             hint:Query hint:optimizer "None".
  
           { SERVICE wikibase:mwapi {
            bd:serviceParam wikibase:api "Search";
                            wikibase:endpoint "www.wikidata.org";
                            mwapi:srsearch "\"malaria\" haswbstatement:P31=Q13442814".
            ?page_title wikibase:apiOutput mwapi:title.
          }
                                       }
                                    
          BIND(IRI(CONCAT(STR(wd:), ?page_title)) AS ?Publication)
          ?Publication wdt:P1476 ?Title.
  
          FILTER CONTAINS(LCASE(?Title), "pregnan").
          } LIMIT 1000
        }  
        BIND (REPLACE(STR(?Title),"[\\.:,;\\[\\]()$]","") AS ?ClearTitle) # remove some frequent special characters, including colons and semicolons
        BIND(STRLEN(?ClearTitle) AS ?ClearTitleLength) 
        FILTER(LANG(?Title)="en") 
        # Basic processing of the titles
        BIND (LCASE(CONCAT("::: ::: ::: ::: ::: ::: ::: ::: ", # add start codon of colons to assist with processing of n-grams at beginning of title
                              ?ClearTitle, 
                              " ;;; ;;; ;;; ;;; ;;; ;;; ;;; ;;;")) # add stop codon of semicolons to assist with processing of n-grams at end of title
                       AS ?Seeds )
     }
  } AS %titles 
  WITH
  { # Applying the regexes to the titles to extract ngrams (for n <= 8), and counting occurrences of the ngrams across titles
    SELECT 
      DISTINCT ?Ngram 
      (COUNT(DISTINCT ?Title) AS ?Count)
      (SAMPLE(DISTINCT ?Publication) AS ?ExamplePub)
      ?Length 
      (( ?Count * ?Length ) AS ?CountTimesLength)
        { 
          INCLUDE %regexes
          INCLUDE %titles
          BIND( 
            (CONCAT(
              REPLACE(?Seeds, ?Regex1, "$1"), " ", 
              REPLACE(?Seeds, ?Regex1, "$2"), " ", 
              REPLACE(?Seeds, ?Regex2, "$1"), " ", 
              REPLACE(?Seeds, ?Regex2, "$2"), " ", 
              REPLACE(?Seeds, ?Regex3, "$1"), " ", 
              REPLACE(?Seeds, ?Regex3, "$2"), " ", 
              REPLACE(?Seeds, ?Regex4, "$1"), " ", 
              REPLACE(?Seeds, ?Regex4, "$2")
            )
          ) AS ?NgramCandidate) 
                              
          BIND( 
            (REPLACE
             (REPLACE
              (REPLACE
               (REPLACE
                (STR(?NgramCandidate),"([;:])",""),
                "(^\\s+)",""),
               "(\\s+$)",""),
              "([ ]{2,})"," ")
            ) AS ?Ngram) 
  
          BIND(STRLEN(?Ngram) AS ?Length) 
          FILTER (?Length > 2 )  
          FILTER (?Length <= ?ClearTitleLength ) 
                  
  
        }
    GROUP BY ?Ngram ?Count ?ExamplePub ?Length ?CountTimesLength
    HAVING(?Count > 1)
  } AS %ngrams 
  WHERE {
    INCLUDE %ngrams 
    ?ExamplePub wdt:P1476 ?ExamplePubTitle.
    FILTER(LANG(?ExamplePubTitle)="en") 
    BIND(STRLEN(REPLACE(?Ngram, "\\S", "")) + 1 as ?N)
  }
  GROUP BY ?Ngram ?Count ?Length ?CountTimesLength ?N ?ExamplePub ?ExamplePubTitle
  ORDER BY DESC(?CountTimesLength) DESC(?Count) DESC(?Length)
  