### Setting Up libpython-clj-examples Repository
Source: https://github.com/mark-watson/clojure-ai-book-code/blob/main/nlp_libpython/INSTALL_MLW.txt
These commands clone the 'libpython-clj-examples' repository, install Git and Emacs (a text editor), and navigate into the cloned directory. This repository contains examples for integrating Python libraries with Clojure.
```Shell
git clone https://github.com/gigasquid/libpython-clj-examples.git
sudo apt-get install git emacs-nox
cd libpython-clj-examples/
```
--------------------------------
### Installing Clojure Project Dependencies
Source: https://github.com/mark-watson/clojure-ai-book-code/blob/main/docs_qa/README.md
This command installs the required dependencies for the Clojure project. It must be executed in the `../openai_api` directory to prepare the environment for running tests or the application.
```Clojure
lein install
```
--------------------------------
### Installing Clojure CLI Tools
Source: https://github.com/mark-watson/clojure-ai-book-code/blob/main/nlp_libpython/INSTALL_MLW.txt
These commands download the official Clojure CLI installer script, make it executable, and then run it with superuser privileges to install the Clojure command-line tools on the system.
```Shell
curl -o install-clojure https://download.clojure.org/install/linux-install-${CLOJURE_TOOLS_VERSION}.sh
chmod +x install-clojure
sudo ./install-clojure
```
--------------------------------
### Starting Clojure REPL for libpython-clj-examples
Source: https://github.com/mark-watson/clojure-ai-book-code/blob/main/nlp_libpython/INSTALL_MLW.txt
This command starts a Clojure Read-Eval-Print Loop (REPL) within the 'libpython-clj-examples' project directory. The REPL is an interactive environment for developing and testing Clojure code, especially useful for experimenting with Python interop.
```Shell
lein repl
```
--------------------------------
### Installing and Configuring Leiningen
Source: https://github.com/mark-watson/clojure-ai-book-code/blob/main/nlp_libpython/INSTALL_MLW.txt
This sequence of commands sets up Leiningen, the build automation and project management tool for Clojure. It downloads the Leiningen script, makes it executable, adds its directory to the system's PATH, and verifies the installation.
```Shell
mkdir bin
cd bin
wget https://raw.githubusercontent.com/technomancy/leiningen/stable/bin/lein
chmod +x lein
cd
vi .profile # edit to add: export PATH=$PATH:~/bin
lein
```
--------------------------------
### Installing Initial Python AI/ML Libraries
Source: https://github.com/mark-watson/clojure-ai-book-code/blob/main/nlp_libpython/INSTALL_MLW.txt
This command uses pip to install fundamental Python libraries for AI and machine learning: NumPy for numerical operations, NLTK for natural language processing, and SpaCy for advanced NLP.
```Shell
python3 -m pip install numpy nltk spacy
```
--------------------------------
### Starting Ollama REST API Service (Shell)
Source: https://github.com/mark-watson/clojure-ai-book-code/blob/main/ollama/README.md
This shell command starts the Ollama REST API server, making the installed large language models available for external applications to query. It should be executed in a dedicated terminal session to continuously serve API requests.
```Shell
ollama serve
```
--------------------------------
### Installing Aptitude and Searching Python Packages
Source: https://github.com/mark-watson/clojure-ai-book-code/blob/main/nlp_libpython/INSTALL_MLW.txt
This snippet installs 'aptitude', a more advanced package manager, and then uses it to search for Python-related packages. This can be useful for exploring available versions or dependencies.
```Shell
sudo apt-get -qq -y install aptitude
aptitude search python
```
--------------------------------
### Running Main Function in Clojure Project
Source: https://github.com/mark-watson/clojure-ai-book-code/blob/main/webscraping/README.md
This command runs the main function defined in `core.clj` using Leiningen, which typically contains a simple example demonstrating the library's usage. It's useful for quick demonstrations or initial setup verification.
```Shell
lein run
```
--------------------------------
### Updating System and Installing Core Dependencies (Python 3.8, OpenJDK 8)
Source: https://github.com/mark-watson/clojure-ai-book-code/blob/main/nlp_libpython/INSTALL_MLW.txt
These commands update the package list and install essential tools and libraries, including curl, wget, bzip2, OpenJDK 8 (headless), Python 3.8, its development libraries, and pip for package management. These are foundational for Clojure and Python development.
```Shell
sudo apt-get -qq update
sudo apt-get -qq -y install curl wget bzip2 openjdk-8-jdk-headless python3.8 libpython3.8 python3-pip
```
--------------------------------
### Installing Core Dependencies (Python 3.7, OpenJDK 8)
Source: https://github.com/mark-watson/clojure-ai-book-code/blob/main/nlp_libpython/INSTALL_MLW.txt
This command installs a specific set of core dependencies, including curl, wget, bzip2, OpenJDK 8 (headless), Python 3.7, its development libraries, and pip. This provides an alternative Python version for compatibility.
```Shell
sudo apt-get -qq -y install curl wget bzip2 openjdk-8-jdk-headless python3.7 libpython3.7 python3-pip
```
--------------------------------
### Installing Core Dependencies (Python 3.7, OpenJDK 11)
Source: https://github.com/mark-watson/clojure-ai-book-code/blob/main/nlp_libpython/INSTALL_MLW.txt
This command installs essential tools and libraries, specifically targeting OpenJDK 11 (headless) and Python 3.7 with its development libraries and pip. This provides a newer Java version while maintaining Python 3.7.
```Shell
sudo apt-get -qq -y install curl wget bzip2 openjdk-11-jdk-headless python3.7 libpython3.7 python3-pip
```
--------------------------------
### Running Clojure Project Tests
Source: https://github.com/mark-watson/clojure-ai-book-code/blob/main/docs_qa/README.md
This command executes the test suite for the Clojure project. It serves both as a way to verify functionality and as the primary 'usage' command, indicating that running tests is the main interaction described. It should be run in the project's root directory after dependencies are installed.
```Clojure
lein test
```
--------------------------------
### Searching OpenJDK Packages with Aptitude
Source: https://github.com/mark-watson/clojure-ai-book-code/blob/main/nlp_libpython/INSTALL_MLW.txt
This command uses 'aptitude' to search for available OpenJDK packages. This helps in identifying different Java Development Kit versions that can be installed on the system.
```Shell
aptitude search openjdk
```
--------------------------------
### Cleaning Up Clojure Installer Script
Source: https://github.com/mark-watson/clojure-ai-book-code/blob/main/nlp_libpython/INSTALL_MLW.txt
This command removes the downloaded Clojure installer script after the installation is complete, helping to keep the system clean and free of unnecessary files.
```Shell
rm install-clojure
```
--------------------------------
### Querying for Articles by Content in SPARQL
Source: https://github.com/mark-watson/clojure-ai-book-code/blob/main/semantic_web_jena/data/sparql_ask_test.txt
This SPARQL ASK query checks for the existence of any triple where an `article_uri` has any `predicate` with the literal value 'Trout Season Starts'. It's useful for determining if specific content exists within the knowledge base.
```SPARQL
PREFIX kb:
ASK
WHERE { ?article_uri ?predicate "Trout Season Starts" }
```
--------------------------------
### Installing Mistral Model with Ollama (Shell)
Source: https://github.com/mark-watson/clojure-ai-book-code/blob/main/ollama/README.md
This shell command initiates the download and installation of the Mistral large language model using the Ollama command-line interface. The initial execution may take time due to the model file download, but subsequent runs will be faster as the file is cached locally.
```Shell
ollama run mistral
```
--------------------------------
### Clojure Namespace Setup and spaCy Initialization
Source: https://github.com/mark-watson/clojure-ai-book-code/blob/main/nlp_libpython/README.md
This snippet defines the Clojure namespace `nlp-libpython-spacy.core`, requiring necessary `libpython-clj` components. It then loads the `spacy` and `QA` Python modules and initializes the `en_core_web_sm` spaCy model. It also includes a sample text and a function `text->tokens` for basic text tokenization.
```Clojure
(ns nlp-libpython-spacy.core
(:require [libpython-clj.require :refer
[require-python]]
[libpython-clj.python :as py
:refer
[py. py.-]]))
(require-python '[spacy :as sp])
(require-python '[QA :as qa]) ;; loads the file QA.py
(def nlp (sp/load "en_core_web_sm"))
(def test-text "John Smith worked for IBM in Mexico last year and earned $1 million in salary and bonuses.")
(defn text->tokens [text]
(map (fn [token] (py.- token text))
(nlp text)))
```
--------------------------------
### Setting Clojure Tools Version Environment Variable
Source: https://github.com/mark-watson/clojure-ai-book-code/blob/main/nlp_libpython/INSTALL_MLW.txt
This command sets an environment variable to specify the desired version of Clojure tools for subsequent installation steps. This ensures consistency and allows for easy version management.
```Shell
export CLOJURE_TOOLS_VERSION=1.10.1.507
```
--------------------------------
### Querying for Article Copyright Information in SPARQL
Source: https://github.com/mark-watson/clojure-ai-book-code/blob/main/semantic_web_jena/data/sparql_ask_test.txt
This SPARQL ASK query verifies if any `article_uri` has a `kb:copyright` property, indicating the presence of copyright information. It helps in identifying articles with associated copyright details.
```SPARQL
PREFIX kb:
ASK
WHERE { ?article_uri kb:copyright ?copyright_value }
```
--------------------------------
### Describing an Article and Predicates by Object Value in SPARQL
Source: https://github.com/mark-watson/clojure-ai-book-code/blob/main/semantic_web_jena/data/sparql_describe_test.txt
This SPARQL query describes an article URI and its associated predicates where the object of a triple matches the string 'Trout Season Starts'. It's a more general query than searching by a specific predicate like 'kb:title'.
```SPARQL
PREFIX kb:
DESCRIBE ?article_uri ?predicate
WHERE { ?article_uri ?predicate "Trout Season Starts" }
```
--------------------------------
### Querying Articles by kb:title in SPARQL
Source: https://github.com/mark-watson/clojure-ai-book-code/blob/main/semantic_web_jena/data/sparql_combine_rdfs_test.txt
This SPARQL query selects distinct article URIs and their associated `kb:title` values. It includes necessary prefix declarations for `kb` and `testnews` namespaces, and retrieves all subjects that have a `kb:title` property, demonstrating a basic graph pattern match.
```SPARQL
PREFIX kb:
PREFIX testnews:
SELECT DISTINCT ?article_uri1 ?object
WHERE {
?article_uri1 kb:title ?object .
}
```
--------------------------------
### Running Clojure Project Tests with Leiningen (Shell)
Source: https://github.com/mark-watson/clojure-ai-book-code/blob/main/ollama/README.md
This shell command executes the test suite for a Clojure project using the Leiningen build automation tool. It is typically run in a separate console after the Ollama service has been started to validate the integration and functionality of the LLM interactions.
```Shell
lein test
```
--------------------------------
### Running Tests for Clojure OpenAI API Library
Source: https://github.com/mark-watson/clojure-ai-book-code/blob/main/old_code/openai_api_old/README.md
This snippet demonstrates how to execute the test suite for the Clojure OpenAI API library using the Leiningen build tool. Running `lein test` compiles and runs all tests defined in the project, ensuring the library's functionality. It requires Leiningen to be installed and configured.
```Clojure
lein test
```
--------------------------------
### Defining RDF Prefixes and Property Sub-properties in Turtle
Source: https://github.com/mark-watson/clojure-ai-book-code/blob/main/semantic_web_jena/data/sparql_combine_rdfs_test.txt
This snippet defines common RDF prefixes for `knowledgebooks.com`, `testnews.com`, and `rdfs` namespaces. It also establishes a bidirectional sub-property relationship between `kb:title` and `testnews:title`, indicating they are equivalent or closely related properties for semantic interoperability.
```Turtle
@prefix kb: .
@prefix testnews: .
@prefix rdfs: .
kb:title rdfs:subPropertyOf testnews:title .
testnews:title rdfs:subPropertyOf kb:title .
```
--------------------------------
### Demonstrating Basic Question Answering with Hugging Face Transformers in Clojure
Source: https://github.com/mark-watson/clojure-ai-book-code/blob/main/nlp_libpython/README.md
This snippet demonstrates the basic usage of a Hugging Face Transformer model for question answering in Clojure. It defines a `context-text` and then uses the `qa` function to query it with different questions. The output includes the confidence `score`, `start` and `end` indices of the answer in the context, and the `answer` itself, showcasing the model's ability to infer meaning.
```Clojure
lp-libpython-spacy.core=> (def context-text "Since last
year, Bill lives in Seattle. He likes to skateboard.")
#'nlp-libpython-spacy.core/context-text
nlp-libpython-spacy.core=> (qa
"where does Bill call home?"
context-text)
{'score': 0.9626545906066895, 'start': 31, 'end': 38,
'answer': 'Seattle'}
nlp-libpython-spacy.core=> (qa
"what does Bill enjoy?"
context-text)
{'score': 0.9084932804107666, 'start': 52, 'end': 62,
'answer': 'skateboard'}
```
--------------------------------
### Defining an RDF Instance with Properties in Turtle
Source: https://github.com/mark-watson/clojure-ai-book-code/blob/main/semantic_web_jena/data/sparql_combine_rdfs_test.txt
This Turtle snippet defines an RDF individual `kb:oak_creek_flooding` with multiple properties: `kb:storyType` (value `kb:disaster`), `kb:summary` (value 'Oak Creek flooded last week affecting 5 businesses'), and `kb:title` (value 'Oak Creek Flood'). It demonstrates how to assert facts about an entity using a concise semicolon syntax for multiple properties.
```Turtle
kb:oak_creek_flooding kb:storyType kb:disaster ;
kb:summary "Oak Creek flooded last week affecting 5 businesses" ;
kb:title "Oak Creek Flood" .
```
--------------------------------
### Querying Articles by testnews:title in SPARQL
Source: https://github.com/mark-watson/clojure-ai-book-code/blob/main/semantic_web_jena/data/sparql_combine_rdfs_test.txt
This SPARQL query selects distinct article URIs and their associated `testnews:title` values. It includes necessary prefix declarations for `kb` and `testnews` namespaces, and retrieves all subjects that have a `testnews:title` property, showcasing how to query using a different namespace prefix for similar data.
```SPARQL
PREFIX kb:
PREFIX testnews:
SELECT DISTINCT ?article_uri1 ?object
WHERE {
?article_uri1 testnews:title ?object .
}
```
--------------------------------
### Querying Articles Sharing a Specific Literal Object in SPARQL
Source: https://github.com/mark-watson/clojure-ai-book-code/blob/main/semantic_web_jena/data/sparql_select_test.txt
This SPARQL query finds pairs of distinct articles (`?article_uri1`, `?article_uri2`) that are linked to the exact literal string 'Trout Season Starts' by any predicate. It demonstrates how to query for relationships based on a specific string value, useful for identifying all subjects related to a particular piece of information.
```SPARQL
PREFIX kb:
SELECT DISTINCT ?article_uri1 ?article_uri2 ?predicate1 ?predicate2
WHERE {
?article_uri1 ?predicate1 "Trout Season Starts" .
?article_uri2 ?predicate2 "Trout Season Starts" .
FILTER (!sameTerm(?article_uri1, ?article_uri2)) .
}
```
--------------------------------
### Combined spaCy and Transformer Question Answering with Knowledge Graph Integration in Clojure
Source: https://github.com/mark-watson/clojure-ai-book-code/blob/main/nlp_libpython/README.md
This example showcases a more advanced question answering system in Clojure, integrating both the spaCy library and Hugging Face Transformer models. The `spacy-qa-demo` function queries a question, dynamically fetches context text from a public Knowledge Graph (like DBPedia) based on entities found in the question, and then uses the Transformer model to extract the answer. The output includes the context text used, confidence score, and the extracted answer.
```Clojure
lp-libpython-spacy.core=> (spacy-qa-demo
"what is the population of Paris?")
* * context text: Paris (French pronunciation: [paʁi] ()) is the capital and most populous city of France, with an estimated population of 2,150,271 residents as of 2020, in an area of 105 square kilometres (41 square miles). Since the 17th century, Paris has been one of Europe's major centres of finance, diplomacy, commerce, fashion, science and arts.
The City of Paris is the centre and seat of government of the Île-de-France, or Paris Region ...
{'score': 0.9000497460365295, 'start': 122, 'end': 131,
'answer': '2,150,271'}
nlp-libpython-spacy.core=> (spacy-qa-demo
"where does Bill Gates Work?")
* * context text: William Henry Gates III (born October 28, 1955) is an American business magnate, software developer,
investor, and philanthropist. He is best known as the co-founder of Microsoft Corporation. During his career at Micro
soft, Gates held the positions of chairman, chief executive officer (CEO), president and chief software architect, while also being the largest individual shareholder until May 2014. He is one of the best-known entrepreneurs and pioneers of the microcomputer revolution of the 1970s and 1980s.
{'score': 0.3064478039741516, 'start': 213, 'end': 222,
'answer': 'Microsoft'}
```
--------------------------------
### Updating Python Package Management Tools and SpaCy Model
Source: https://github.com/mark-watson/clojure-ai-book-code/blob/main/nlp_libpython/INSTALL_MLW.txt
These commands ensure that pip, setuptools, and wheel are updated to their latest versions, which are crucial for reliable Python package management. It then updates SpaCy and downloads its small English language model, essential for NLP tasks.
```Shell
python3 -m pip install -U pip setuptools wheel
python3 -m pip install -U spacy
python3 -m spacy download en_core_web_sm
```
--------------------------------
### Running Tests with Leiningen (Shell)
Source: https://github.com/mark-watson/clojure-ai-book-code/blob/main/gemini_api/README.md
Illustrates the command to run tests for the Clojure project using Leiningen, a build automation tool for Clojure.
```Shell
lein test
```
--------------------------------
### Running Tests with Leiningen (Clojure)
Source: https://github.com/mark-watson/clojure-ai-book-code/blob/main/semantic_web_jena/README.md
This snippet demonstrates how to execute the project's test suite using Leiningen, the standard build automation tool for Clojure. Running this command verifies the library's functionality and ensures all components are working correctly.
```Clojure
lein test
```
--------------------------------
### Using Gemini API: Content Generation and Summarization (Clojure)
Source: https://github.com/mark-watson/clojure-ai-book-code/blob/main/gemini_api/README.md
Demonstrates how to require the `gemini-api.core` library and then use its `generate-content` function to create text and `summarize` function to condense text. These functions interact with the Gemini APIs.
```Clojure
(require '[gemini-api.core :as gemini])
(println (gemini/generate-content "Write a short poem about the ocean."))
(println (gemini/summarize "The quick brown fox jumps over the lazy dog."))
```
--------------------------------
### Running Clojure Tests (Shell)
Source: https://github.com/mark-watson/clojure-ai-book-code/blob/main/llm_bosquet/README.md
Executes the tests for the Clojure project using the 'clj' command-line tool, specifically targeting the ':test' alias defined in 'deps.edn'.
```Shell
clj -X:test
```
--------------------------------
### Running a Demo Program with Leiningen in Clojure
Source: https://github.com/mark-watson/clojure-ai-book-code/blob/main/knowledge_graph_navigator/README.md
This command runs the main demo program of the Clojure project using Leiningen. It is typically used to quickly demonstrate the library's core functionality or for simple execution.
```Clojure
lein run
```
--------------------------------
### Running Tests in Clojure Project
Source: https://github.com/mark-watson/clojure-ai-book-code/blob/main/webscraping/README.md
This command executes the test suite for the Clojure project using Leiningen. It's a standard way to verify the correctness of the library's functionality and ensure all components are working as expected.
```Shell
lein test
```
--------------------------------
### Main Function for NLP and QA Demos in Clojure
Source: https://github.com/mark-watson/clojure-ai-book-code/blob/main/nlp_libpython/README.md
This Clojure `-main` function serves as an entry point to demonstrate various NLP and QA functionalities. It calls `text->entities`, `text->tokens-and-pos`, `text->pos`, and `text->tokens` with a `test-text` (not provided in snippet context), and performs direct QA calls and `spacy-qa-demo` calls to illustrate the integrated system.
```Clojure
(defn -main
[& _]
(println (text->entities test-text))
(println (text->tokens-and-pos test-text))
(println (text->pos test-text))
(println (text->tokens test-text))
(qa "where does Bill call home?"
"Since last year, Bill lives in Seattle. He likes to skateboard.")
(qa "what does Bill enjoy?"
"Since last year, Bill lives in Seattle. He likes to skateboard.")
(spacy-qa-demo "what is the population of Paris?")
(spacy-qa-demo "where does Bill Gates Work?"))
```
--------------------------------
### Setting OpenAI API Key (Shell)
Source: https://github.com/mark-watson/clojure-ai-book-code/blob/main/llm_bosquet/README.md
Sets the OPENAI_API_KEY environment variable, which is required if using OpenAI models with the Bosquet library. Replace 'sk-......' with your actual API key.
```Shell
export OPENAI_API_KEY=sk-......
```
--------------------------------
### Python Wrapper for Hugging Face Question Answering Pipeline
Source: https://github.com/mark-watson/clojure-ai-book-code/blob/main/nlp_libpython/README.md
This Python script defines a `qa` pipeline using the Hugging Face Transformers library for question answering. It initializes the pipeline with a specific pre-trained model and tokenizer. The `answer` function takes a query and context, processes them with the pipeline, prints the result, and returns the answer, serving as a backend for Clojure calls.
```Python
from transformers import pipeline
qa = pipeline(
"question-answering",
model="NeuML/bert-small-cord19-squad2",
tokenizer="NeuML/bert-small-cord19qa"
)
def answer (query_text,context_text):
answer = qa({
"question": query_text,
"context": context_text
})
print(answer)
return answer
```
--------------------------------
### Defining RDF/OWL Triples for News Article Properties
Source: https://github.com/mark-watson/clojure-ai-book-code/blob/main/semantic_web_jena/data/sparql_combine_test.txt
This snippet defines RDF prefixes for knowledge books, test news, and OWL. It establishes an OWL `sameAs` relationship between `kb:title` and `testnews:title`, and then defines an instance `kb:oak_creek_flooding` with its type, summary, and title using the `kb:` prefix.
```RDF
@prefix kb: .
@prefix testnews: .
@prefix owl: .
kb:title owl:sameAs testnews:title .
kb:oak_creek_flooding kb:storyType kb:disaster ;
kb:summary "Oak Creek flooded last week affecting 5 businesses" ;
kb:title "Oak Creek Flood" .
```
--------------------------------
### Running Clojure Application with Leiningen
Source: https://github.com/mark-watson/clojure-ai-book-code/blob/main/deeplearning_dl4j/README.md
This command executes the main function of the Clojure application using Leiningen, the build automation tool for Clojure projects. It's the standard way to run Clojure applications from the command line, typically found in the project's root directory.
```Shell
lein run
```
--------------------------------
### Running Tests with Leiningen in Clojure
Source: https://github.com/mark-watson/clojure-ai-book-code/blob/main/nlp_opennlp/README.md
This snippet demonstrates how to execute the test suite for the `opennlp-clj` project using Leiningen, the build automation tool for Clojure. Running `lein test` will compile and run all tests defined in the project.
```Clojure
lein test
```
--------------------------------
### Defining RDF Triples for a Knowledge Base (RDF Turtle)
Source: https://github.com/mark-watson/clojure-ai-book-code/blob/main/semantic_web_jena/data/inferencing_1.txt
This snippet defines a set of RDF triples using Turtle syntax to establish a basic knowledge base. It declares common prefixes, defines `kb:Sibling` as a subclass of `rdfs:Class`, `kb:Brother` as a subclass of `kb:Sibling`, and asserts `person:mark` as an instance of `kb:Brother`. These triples are intended to be added to a new RDF repository to set up initial data and schema.
```RDF Turtle
@prefix rdf: .
@prefix rdfs: .
@prefix kb: .
@prefix person: .
kb:Sibling rdfs:subClassOf rdfs:Class .
kb:Brother rdfs:subClassOf kb:Sibling .
person:mark rdf:type kb:Brother .
```
--------------------------------
### Running Tests with Leiningen in Clojure
Source: https://github.com/mark-watson/clojure-ai-book-code/blob/main/openai_api/README.md
This command executes the test suite for the Clojure project using Leiningen, the build automation tool for Clojure. It's a standard way to verify the functionality and correctness of the library's code.
```Shell
lein test
```
--------------------------------
### Defining RDF Triples for a New Repository - RDF Turtle
Source: https://github.com/mark-watson/clojure-ai-book-code/blob/main/semantic_web_jena/data/inferencing_2.txt
This snippet defines RDF prefixes and asserts `rdfs:subPropertyOf` relationships for `kb:mother` and `kb:father` under `kb:parent`. It also includes a specific `kb:father` relationship between `person:ron` and `person:anthony`, establishing foundational knowledge for a new repository.
```RDF Turtle
@prefix rdf: .
@prefix rdfs: .
@prefix kb: .
@prefix person: .
kb:mother rdfs:subPropertyOf kb:parent .
kb:father rdfs:subPropertyOf kb:parent .
person:ron kb:father person:anthony .
```
--------------------------------
### Demonstrating spaCy NLP Functions with Clojure REPL
Source: https://github.com/mark-watson/clojure-ai-book-code/blob/main/nlp_libpython/README.md
This snippet illustrates a Clojure REPL session demonstrating the use of spaCy through libpython-clj for various NLP tasks. It shows how to define a test string and then apply functions to extract named entities, tokens with part-of-speech tags, only part-of-speech tags, and just tokens. The output for each function call is also provided, showcasing the results of the NLP processing.
```text
$ ~/Clojure-AI-Book-Code/nlp_libpython$ lein repl
nlp-libpython-spacy.core=> (def test-text "John Smith
worked for IBM in Mexico last year and earned $1
million in salary and bonuses.")
#'nlp-libpython-spacy.core/test-text
nlp-libpython-spacy.core=> (text->entities
test-text)
(["John Smith" "PERSON"] ["IBM" "ORG"] ["Mexico" "GPE"]
["last year" "DATE"] ["$1 million" "MONEY"])
nlp-libpython-spacy.core=> (text->tokens-and-pos
test-text)
(["John" "PROPN"] ["Smith" "PROPN"] ["worked" "VERB"]
["for" "ADP"] ["IBM" "PROPN"] ["in" "ADP"]
["Mexico" "PROPN"] ["last" "ADJ"] ["year" "NOUN"]
["and" "CCONJ"] ["earned" "VERB"] ["$" "SYM"]
["1" "NUM"] ["million" "NUM"] ["in" "ADP"]
["salary" "NOUN"] ["and" "CCONJ"] ["bonuses" "NOUN"]
["." "PUNCT"])
nlp-libpython-spacy.core=> (text->pos test-text)
("PROPN" "PROPN" "VERB" "ADP" "PROPN" "ADP" "PROPN"
"ADJ" "NOUN" "CCONJ" "VERB" "SYM" "NUM" "NUM"
"ADP" "NOUN" "CCONJ" "NOUN" "PUNCT")
nlp-libpython-spacy.core=> (text->tokens test-text)
("John" "Smith" "worked" "for" "IBM" "in" "Mexico"
"last" "year" "and" "earned" "$" "1" "million"
"in" "salary" "and" "bonuses" ".")
```
--------------------------------
### Running Tests with Leiningen in Clojure
Source: https://github.com/mark-watson/clojure-ai-book-code/blob/main/knowledge_graph_navigator/README.md
This command executes the test suite for the Clojure project using Leiningen. It ensures that all defined tests pass, verifying the correctness and functionality of the library.
```Clojure
lein test
```
--------------------------------
### Describing an Article by Title in SPARQL
Source: https://github.com/mark-watson/clojure-ai-book-code/blob/main/semantic_web_jena/data/sparql_describe_test.txt
This SPARQL query uses the DESCRIBE keyword to retrieve all information about a resource (an article URI) that has a specific title. It requires the 'kb' prefix to resolve the 'kb:title' predicate.
```SPARQL
PREFIX kb:
DESCRIBE ?article_uri
WHERE { ?article_uri kb:title "Trout Season Starts" }
```
--------------------------------
### Defining RDF Prefixes and Relationships
Source: https://github.com/mark-watson/clojure-ai-book-code/blob/main/semantic_web_jena/data/inferencing_4.txt
This snippet defines common RDF prefixes (rdf, rdfs) and custom knowledge base (kb) and person prefixes. It then asserts domain constraints for 'mother' and 'father' predicates using `rdfs:domain` and a specific family relationship between Mary and Susan.
```RDF
@prefix rdf: .
@prefix rdfs: .
@prefix kb: .
@prefix person: .
kb:mother rdfs:domain kb:Female .
kb:father rdfs:domain kb:Male .
person:mary kb:mother person:susan .
```
--------------------------------
### Demonstrating spaCy and QA Integration in Clojure
Source: https://github.com/mark-watson/clojure-ai-book-code/blob/main/nlp_libpython/README.md
This Clojure function demonstrates a complete workflow for question answering by first extracting entities from a natural language query using spaCy, then retrieving context text based on these entities from a knowledge graph (DBpedia), and finally using the `qa` function to answer the query. It showcases integration of NLP, knowledge graphs, and QA.
```Clojure
(defn spacy-qa-demo [natural-language-query]
(let [entity-map
{"PERSON" ""
"ORG"
""
"GPE" ""}
entities (text->entities natural-language-query)
get-text-fn
(fn [entity]
(clojure.string/join
" "
(for [entity entities]
(kgn/dbpedia-get-entity-text-by-name
(first entity)
(get entity-map (second entity))))))
context-text
(clojure.string/join
" "
(for [entity entities]
(get-text-fn entity)))
_ (println "* * context text:" context-text)
answer (qa natural-language-query context-text)]
answer))
```
--------------------------------
### Defining RDF Triples for a Knowledge Base in Turtle
Source: https://github.com/mark-watson/clojure-ai-book-code/blob/main/semantic_web_jena/data/inferencing_3.txt
This snippet defines a set of RDF triples using Turtle syntax. It establishes prefixes for common RDF/RDFS vocabularies and custom knowledge base (kb) and person vocabularies. It then defines domain constraints for 'mother' and 'father' properties and asserts facts about 'kate' being a 'Female' and having 'bill' as a father.
```Turtle
@prefix rdf: .
@prefix rdfs: .
@prefix kb: .
@prefix person: .
kb:mother rdfs:domain kb:Female .
kb:father rdfs:domain kb:Male .
person:kate rdf:type kb:Female .
person:kate kb:father person:bill .
```
--------------------------------
### Constructing Story Type Value and Same Topic Relationship (SPARQL)
Source: https://github.com/mark-watson/clojure-ai-book-code/blob/main/semantic_web_jena/data/sparql_construct_test.txt
This SPARQL CONSTRUCT query identifies distinct articles (`?article1`, `?article2`) that share a `story_type_value` of 'sports' or 'recreation'. It constructs two triples: one asserting the `story_type_value` as the subject of `?article1`, and another asserting that `?article2` is on the `kb:same_topic` as `?article1`.
```SPARQL
PREFIX kb:
CONSTRUCT { ?story_type_value kb:subject_of ?article1 .
?article2 kb:same_topic ?article1 .}
WHERE {
?article1 ?story_type ?story_type_value .
?article2 ?story_type ?story_type_value .
FILTER ((?article1 != ?article2) && ((?story_type_value = kb:sports) || (?story_type_value = kb:recreation)))
}
```
--------------------------------
### Querying News Article Titles Using kb:title (SPARQL)
Source: https://github.com/mark-watson/clojure-ai-book-code/blob/main/semantic_web_jena/data/sparql_combine_test.txt
This SPARQL query retrieves distinct article URIs and their titles using the `kb:title` property. It demonstrates a direct query against the knowledge base for article titles.
```SPARQL
PREFIX kb:
PREFIX kb:
SELECT DISTINCT ?article_uri1 ?object
WHERE {
?article_uri1 kb:title ?object .
}
```
--------------------------------
### Defining Knowledge Base Entries in Turtle
Source: https://github.com/mark-watson/clojure-ai-book-code/blob/main/semantic_web_jena/data/sparql_select_test.txt
This snippet defines several RDF triples using Turtle syntax, establishing a small knowledge base. Each entry represents an article with properties like `storyType`, `summary`, and `title`, demonstrating how to represent different types of events (disaster, sports, recreation) and their attributes. It showcases basic RDF subject-predicate-object statements and property lists.
```Turtle
@prefix kb: .
kb:oak_creek_flooding kb:storyType kb:disaster ;
kb:summary "Oak Creek flooded last week affecting 5 businesses" ;
kb:title "Oak Creek Flood" .
kb:bear_mountain_fire kb:storyType kb:disaster ;
kb:summary "The fire on Bear Mountain was caused by lightning" ;
kb:title "Bear Mountain Fire" .
kb:trout_season kb:storyType kb:sports , kb:recreation ;
kb:summary "Fishing was good the first day of trout season" ;
kb:title "Trout Season Starts" .
kb:jc_basketball kb:storyType kb:sports ;
kb:summary "Local JC Basketball team lost by 12 points last night" ;
kb:title "Local JC Lost Last Night" .
# new triple, without a summary:
kb:jc_bowling kb:storyType kb:sports ;
kb:title "JC Bowling Team to Open Season" .
```
--------------------------------
### REPL Interaction: Tokenizing Text with Clojure Wrapper Functions
Source: https://github.com/mark-watson/clojure-ai-book-code/blob/main/nlp_libpython/README.md
This snippet showcases the usage of Clojure wrapper functions, `text->tokens` and `text->tokens-and-pos`, in a REPL. These functions process input text using spaCy, demonstrating how to extract tokens and their corresponding parts-of-speech, returning them as Clojure data structures.
```Clojure
nlp-libpython-spacy.core=> (text->tokens "the cat ran")
("the" "cat" "ran")
nlp-libpython-spacy.core=> (text->tokens-and-pos
"the cat ran")
(["the" "DET"] ["cat" "NOUN"] ["ran" "VERB"])
```
--------------------------------
### Querying Direct Father Relationships - SPARQL
Source: https://github.com/mark-watson/clojure-ai-book-code/blob/main/semantic_web_jena/data/inferencing_2.txt
This SPARQL query retrieves all direct `kb:father` relationships from the knowledge base. It demonstrates a basic pattern match to find subjects and objects explicitly linked by the `kb:father` property.
```SPARQL
SELECT DISTINCT ?s ?o
WHERE { ?s kb:father ?o }
```
--------------------------------
### Querying for Brothers (SPARQL)
Source: https://github.com/mark-watson/clojure-ai-book-code/blob/main/semantic_web_jena/data/inferencing_1.txt
This SPARQL query retrieves all distinct subjects that are explicitly declared as `kb:Brother` instances in the knowledge base. It demonstrates a direct query for a specific type, returning only entities that directly match the `rdf:type kb:Brother` pattern.
```SPARQL
SELECT DISTINCT ?s
WHERE { ?s rdf:type kb:Brother }
```
--------------------------------
### Performing Question Answering with Transformer Model in Clojure
Source: https://github.com/mark-watson/clojure-ai-book-code/blob/main/nlp_libpython/README.md
This Clojure function acts as a wrapper to use a Transformer model for question answering. It takes a question and context text, prints the result to stdout, and returns a map containing the answer. It relies on a `qa/answer` function, which is likely a Python interop call to a pre-trained model.
```Clojure
(defn qa
"Use Transformer model for question answering"
[question context-text]
;; prints to stdout and returns a map:
(qa/answer question context-text))
```
--------------------------------
### Defining Leiningen Project for libpython-clj Integration
Source: https://github.com/mark-watson/clojure-ai-book-code/blob/main/nlp_libpython/README.md
This snippet defines the `project.clj` file for a Clojure project, configuring it to use `libpython-clj` for Python interoperability. It specifies dependencies, JVM options, and plugin configurations necessary for integrating Python libraries like spaCy and Hugging Face Transformers.
```Clojure
(defproject python_interop_deeplearning "0.1.0-SNAPSHOT"
:description
"Example using libpython-clj with spaCy"
:url
"https://github.com/gigasquid/libpython-clj-examples"
:license
{:name
"EPL-2.0 OR GPL-2+ WITH Classpath-exception-2.0"
:url "https://www.eclipse.org/legal/epl-2.0/"}
:jvm-opts ["-Djdk.attach.allowAttachSelf"
"-XX:+UnlockDiagnosticVMOptions"
"-XX:+DebugNonSafepoints"]
:plugins [[lein-tools-deps "0.4.5"]]
:middleware
[lein-tools-deps.plugin/resolve-dependencies-with-deps-edn]
:lein-tools-deps/config {:config-files [:project]
:resolve-aliases []}
:mvn/repos
{"central" {:url "https://repo1.maven.org/maven2/"}
"clojars" {:url "https://clojars.org/repo"}}
:dependencies [[org.clojure/clojure "1.10.1"]
[clj-python/libpython-clj "1.37"]
[clj-http "3.10.3"]
[com.cemerick/url "0.1.1"]
[org.clojure/data.csv "1.0.0"]
[org.clojure/data.json "1.0.0"]]
:main ^:skip-aot nlp-libpython-spacy.core
:target-path "target/%s"
:profiles
{:uberjar
{:aot :all
:jvm-opts
["-Dclojure.compiler.direct-linking=true"]}})
```
--------------------------------
### Querying Articles by Multiple Story Types with UNION in SPARQL
Source: https://github.com/mark-watson/clojure-ai-book-code/blob/main/semantic_web_jena/data/sparql_select_test.txt
This SPARQL query retrieves the URI, title, and summary for articles classified under either 'sports' or 'recreation' story types. It demonstrates the `UNION` keyword to combine results from multiple graph patterns, effectively performing an OR operation on conditions.
```SPARQL
PREFIX kb:
SELECT ?article_uri ?title ?summary
WHERE {
{ ?article_uri kb:storyType kb:sports } UNION { ?article_uri kb:storyType kb:recreation } .
?article_uri kb:title ?title .
?article_uri kb:summary ?summary .
}
```
--------------------------------
### Querying Parent Relationships with Inferencing - SPARQL
Source: https://github.com/mark-watson/clojure-ai-book-code/blob/main/semantic_web_jena/data/inferencing_2.txt
This SPARQL query retrieves all `kb:parent` relationships, leveraging the `rdfs:subPropertyOf` inferencing defined in the RDF triples. It will return not only explicit `kb:parent` assertions but also those inferred from `kb:father` and `kb:mother` relationships.
```SPARQL
SELECT DISTINCT ?s ?o
WHERE { ?s kb:parent ?o }
```
--------------------------------
### Querying Titles with Optional Summaries in SPARQL
Source: https://github.com/mark-watson/clojure-ai-book-code/blob/main/semantic_web_jena/data/sparql_select_test.txt
This SPARQL query retrieves the title of every article and, if available, its summary. It uses the `OPTIONAL` keyword to include the `summary` predicate without excluding articles that do not have a summary, ensuring all titles are returned regardless of summary presence.
```SPARQL
PREFIX kb:
SELECT DISTINCT ?title ?summary
WHERE { ?article_uri kb:title ?title .
OPTIONAL { ?article_uri kb:summary ?summary }
}
```
--------------------------------
### Constructing Article Subject Relationship for Sports/Recreation (SPARQL)
Source: https://github.com/mark-watson/clojure-ai-book-code/blob/main/semantic_web_jena/data/sparql_construct_test.txt
This SPARQL CONSTRUCT query identifies articles related to either 'sports' or 'recreation' and asserts that their associated `story_type` is the subject of the article. It establishes a direct relationship between the story type and the article based on these specific topics.
```SPARQL
PREFIX kb:
CONSTRUCT { ?story_type kb:subject_of ?article1 . }
WHERE {
{ ?article1 ?story_type kb:sports } UNION { ?article1 ?story_type kb:recreation } .
}
```
--------------------------------
### Constructing Article Subject Relationship with Distinct Articles (SPARQL)
Source: https://github.com/mark-watson/clojure-ai-book-code/blob/main/semantic_web_jena/data/sparql_construct_test.txt
This SPARQL CONSTRUCT query identifies articles related to 'sports' or 'recreation', similar to the previous query. Additionally, it introduces a second article (`?article2`) also related to these topics and filters to ensure `?article1` and `?article2` are distinct, although only `?article1` is used in the constructed triple.
```SPARQL
PREFIX kb:
CONSTRUCT { ?story_type kb:subject_of ?article1 . }
WHERE {
{ ?article1 ?story_type kb:sports } UNION { ?article1 ?story_type kb:recreation } .
{ ?article2 ?story_type kb:sports } UNION { ?article2 ?story_type kb:recreation } .
FILTER (?article1 != ?article2)
}
```
--------------------------------
### Querying Articles with Regex Matching Objects in SPARQL
Source: https://github.com/mark-watson/clojure-ai-book-code/blob/main/semantic_web_jena/data/sparql_select_test.txt
This SPARQL query identifies distinct pairs of articles (`?article_uri1`, `?article_uri2`) that share the same predicate and whose objects (`?o1`, `?o2`) both contain the substring 'Season'. It illustrates the use of `regex` filter for pattern matching on literal values, enabling flexible text-based searches within the knowledge base.
```SPARQL
PREFIX kb:
SELECT DISTINCT ?article_uri1 ?article_uri2 ?predicate1 ?predicate2
WHERE {
?article_uri1 ?predicate1 ?o1 .
?article_uri2 ?predicate2 ?o2 .
FILTER (!sameTerm(?article_uri1, ?article_uri2) && sameTerm(?predicate1, ?predicate2)) .
FILTER regex(?o1, "Season") .
FILTER regex(?o2, "Season") .
}
```
--------------------------------
### Querying for Siblings (SPARQL)
Source: https://github.com/mark-watson/clojure-ai-book-code/blob/main/semantic_web_jena/data/inferencing_1.txt
This SPARQL query retrieves all distinct subjects that are instances of `kb:Sibling`. Due to the `rdfs:subClassOf` inference (where `kb:Brother` is a subclass of `kb:Sibling`), this query will also return `person:mark` even though it's explicitly defined as `kb:Brother`, showcasing the inferencing capabilities of an RDF repository.
```SPARQL
SELECT DISTINCT ?s
WHERE { ?s rdf:type kb:Sibling }
```
--------------------------------
### Querying Titles with Optional Summary and Filtered Page Count in SPARQL
Source: https://github.com/mark-watson/clojure-ai-book-code/blob/main/semantic_web_jena/data/sparql_select_test.txt
This SPARQL query retrieves the title of every article, optionally its summary, and optionally a `page_count` if it exists and is greater than 1. It showcases nested `OPTIONAL` clauses and a `FILTER` within an optional block, allowing for conditional inclusion and filtering of data without losing other results.
```SPARQL
PREFIX kb:
SELECT DISTINCT ?title ?summary ?page_count
WHERE { ?article_uri kb:title ?title .
OPTIONAL { ?article_uri kb:summary ?summary } .
OPTIONAL { ?article_uri kb:page_count ?page_count . FILTER (?page_count > 1) } .
}
```
--------------------------------
### REPL Interaction: Checking spaCy Document Type in Clojure
Source: https://github.com/mark-watson/clojure-ai-book-code/blob/main/nlp_libpython/README.md
This snippet demonstrates interacting with the `nlp` function (a wrapper for spaCy's `nlp` pipeline) in a Clojure REPL. It shows how to call the function with a sample text and then verify that the returned value is a Python object, specifically a spaCy `Document`.
```Clojure
$ lein repl
nlp-libpython-spacy.core=> (nlp "The cat ran")
The cat ran
nlp-libpython-spacy.core=> (type (nlp "The cat ran"))
:pyobject
```
--------------------------------
### REPL Interaction: Inspecting spaCy Document Attributes with py/dir
Source: https://github.com/mark-watson/clojure-ai-book-code/blob/main/nlp_libpython/README.md
This snippet illustrates how to use `libpython-clj`'s `py/dir` function in a Clojure REPL to inspect the available methods and attributes of a Python object, specifically a spaCy `Document`. It highlights key attributes like `__iter__`, `lang`, `sentiment`, and `text`.
```Clojure
nlp-libpython-spacy.core=> (py/dir (nlp "The cat ran"))
["__iter__" "lang" "sentiment" "text" "to_json" ...]
```
--------------------------------
### Querying News Article Titles Using testnews:title with Inferencing (SPARQL)
Source: https://github.com/mark-watson/clojure-ai-book-code/blob/main/semantic_web_jena/data/sparql_combine_test.txt
This SPARQL query retrieves distinct article URIs and their titles using the `testnews:title` property. It is specifically noted to work when OWL inferencing is supported, leveraging the `owl:sameAs` relationship defined earlier to find titles that might be asserted under `kb:title` but queried via `testnews:title`.
```SPARQL
# works if OWL inferencing is supported:
SELECT DISTINCT ?article_uri1 ?object
WHERE {
?article_uri1 testnews:title ?object .
}
```
--------------------------------
### Constructing Story Type Value as Subject of Article (SPARQL)
Source: https://github.com/mark-watson/clojure-ai-book-code/blob/main/semantic_web_jena/data/sparql_construct_test.txt
This SPARQL CONSTRUCT query identifies articles (`?article1`, `?article2`) that share a common `story_type_value`. It filters these values to be 'sports' or 'recreation' and ensures the articles are distinct. The query then constructs a triple asserting that the `story_type_value` itself is the subject of `?article1`.
```SPARQL
PREFIX kb:
CONSTRUCT { ?story_type_value kb:subject_of ?article1 . }
WHERE {
?article1 ?story_type ?story_type_value .
?article2 ?story_type ?story_type_value .
FILTER ((?article1 != ?article2) && ((?story_type_value = kb:sports) || (?story_type_value = kb:recreation)))
}
```