countries)
29 | throws SQLException, ClassNotFoundException {
30 | out("\n" + GREEN + "Individual People:\n" + RESET);
31 | for (EntityAndDescription person : people) {
32 | out(" " + GREEN + String.format("%-25s", person.entityName) +
33 | PURPLE + " : " + removeBrackets(person.entityUri) + RESET);
34 | out(EntityDetail.personAsString(endpoint, person.entityUri));
35 | }
36 | out("\n" + CYAN + "Individual Companies:\n" + RESET);
37 | for (EntityAndDescription company : companies) {
38 | out(" " + CYAN + String.format("%-25s", company.entityName) +
39 | YELLOW + " : " + removeBrackets(company.entityUri) + RESET);
40 | out(EntityDetail.companyAsString(endpoint, company.entityUri));
41 | }
42 | out("\n" + GREEN + "Individual Cities:\n" + RESET);
43 | for (EntityAndDescription city : cities) {
44 | out(" " + GREEN + String.format("%-25s", city.entityName) +
45 | PURPLE + " : " + removeBrackets(city.entityUri) + RESET);
46 | out(EntityDetail.cityAsString(endpoint, city.entityUri));
47 | }
48 | out("\n" + GREEN + "Individual Countries:\n" + RESET);
49 | for (EntityAndDescription country : countries) {
50 | out(" " + GREEN + String.format("%-25s", country.entityName) +
51 | PURPLE + " : " + removeBrackets(country.entityUri) + RESET);
52 | out(EntityDetail.countryAsString(endpoint, country.entityUri));
53 | }
54 | out("");
55 | }
56 | }
57 |
--------------------------------------------------------------------------------
/kgn/src/main/java/com/knowledgegraphnavigator/Sparql.java:
--------------------------------------------------------------------------------
1 | package com.knowledgegraphnavigator;
2 |
3 | import com.markwatson.semanticweb.QueryResult;
4 | import com.markwatson.semanticweb.JenaApis;
5 | import static com.knowledgegraphnavigator.Log.sparql;
6 | import static com.knowledgegraphnavigator.Log.out;
7 |
8 | import java.sql.SQLException;
9 |
10 | public class Sparql {
11 | //static private String endpoint = "https://query.wikidata.org/bigdata/namespace/wdq/sparql";
12 | static private String endpoint = "https://dbpedia.org/sparql";
13 | public Sparql() {
14 | this.jenaApis = new JenaApis();
15 | }
16 |
17 | public QueryResult query(String sparqlQuery) throws SQLException, ClassNotFoundException {
18 | //out(sparqlQuery); // debug for now...
19 | sparql.append(sparqlQuery);
20 | sparql.append(("\n\n"));
21 | return jenaApis.queryRemote(endpoint, sparqlQuery);
22 | }
23 | private JenaApis jenaApis;
24 |
25 | public static void main(String[] args) throws Exception {
26 | Sparql sp = new Sparql();
27 | QueryResult qr = sp.query("select ?s ?p ?o where { ?s ?p ?o } limit 5");
28 | out(qr.toString());
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/kgn/src/main/java/com/knowledgegraphnavigator/Utils.java:
--------------------------------------------------------------------------------
1 | package com.knowledgegraphnavigator;
2 |
3 | public class Utils {
4 | static public String removeBrackets(String s) {
5 | if (s.startsWith("<")) return s.substring(1, s.length() - 1);
6 | return s;
7 | }
8 | }
9 |
--------------------------------------------------------------------------------
/langchain4j-ollama/.gitignore:
--------------------------------------------------------------------------------
1 | .idea
2 | .codegpt
3 |
4 |
--------------------------------------------------------------------------------
/langchain4j-ollama/Makefile:
--------------------------------------------------------------------------------
1 | run:
2 | mvn test -q # run test in quiet mode
3 |
4 |
--------------------------------------------------------------------------------
/langchain4j-ollama/README.md:
--------------------------------------------------------------------------------
1 | # WORK IN PROGRESS: LangChain4j Ollama Local LLM REST API Example
2 |
3 | Make sure Ollama serve is running:
4 |
5 | ollama serve
6 |
7 | Use the make file default target to run the test:
8 |
9 | make
--------------------------------------------------------------------------------
/langchain4j-ollama/pom.xml:
--------------------------------------------------------------------------------
1 |
3 | 4.0.0
4 | com.markwatson.langchain4j
5 | langchain4j-llm-client
6 | jar
7 | 1.0-SNAPSHOT
8 | langchain4j-llm-client
9 | http://maven.apache.org
10 |
11 |
12 | dev.langchain4j
13 | langchain4j
14 | 0.30.0
15 |
16 |
17 |
18 | dev.langchain4j
19 | langchain4j-open-ai
20 | 0.30.0
21 |
22 |
23 |
24 | junit
25 | junit
26 | 3.8.1
27 | test
28 |
29 |
30 | org.json
31 | json
32 | 20240303
33 |
34 |
35 |
36 |
--------------------------------------------------------------------------------
/langchain4j-ollama/src/main/java/com/markwatson/langchain4j_ollama/OllamaLlmLangChain4j.java:
--------------------------------------------------------------------------------
1 | package com.markwatson.langchain4j_ollama;
2 |
3 | import dev.langchain4j.model.chat.ChatLanguageModel;
4 | import dev.langchain4j.model.openai.OpenAiChatModel;
5 |
6 |
7 | import java.io.BufferedReader;
8 | import java.io.IOException;
9 | import java.io.InputStreamReader;
10 | import java.io.OutputStream;
11 | import java.net.HttpURLConnection;
12 | import java.net.URI;
13 | import java.net.URL;
14 | import java.net.URLConnection;
15 | import java.nio.file.Files;
16 | import java.nio.file.Path;
17 | import java.nio.file.Paths;
18 |
19 | import org.json.JSONObject;
20 |
21 | public class OllamaLlmLangChain4j {
22 |
23 | public static void main(String[] args) throws Exception {
24 | String prompt = "Translate the following English text to French: 'Hello, how are you?'";
25 | String completion = getCompletion(prompt, "mistral");
26 | System.out.println("completion: " + completion);
27 | }
28 |
29 | public static String getCompletion(String prompt, String modelName) throws Exception {
30 | System.out.println("\n\n**********\n\nprompt: " + prompt + ", modelName: " + modelName);
31 | String api_key = System.getenv("OPENAI_API_KEY");
32 |
33 | ChatLanguageModel model = OpenAiChatModel.withApiKey(api_key);
34 | String answer = model.generate(prompt);
35 |
36 | System.out.println(answer); // Hello! How can I assist you today?
37 | return answer;
38 | }
39 |
40 | /***
41 | * Utilities for using the Ollama LLM APIs
42 | */
43 |
44 | // read the contents of a file path into a Java string
45 | public static String readFileToString(String filePath) throws IOException {
46 | Path path = Paths.get(filePath);
47 | return new String(Files.readAllBytes(path));
48 | }
49 |
50 | public static String replaceSubstring(String originalString, String substringToReplace, String replacementString) {
51 | return originalString.replace(substringToReplace, replacementString);
52 | }
53 | public static String promptVar(String prompt0, String varName, String varValue) {
54 | String prompt = replaceSubstring(prompt0, varName, varValue);
55 | return replaceSubstring(prompt, varName, varValue);
56 | }
57 | }
58 |
59 |
60 |
61 |
62 |
--------------------------------------------------------------------------------
/langchain4j-ollama/src/test/java/com/markwatson/langchain4j_ollama/OllamaLlmLangChain4jTest.java:
--------------------------------------------------------------------------------
1 | package com.markwatson.langchain4j_ollama;
2 |
3 | import junit.framework.Test;
4 | import junit.framework.TestCase;
5 | import junit.framework.TestSuite;
6 |
7 | /**
8 | * Unit test for simple App.
9 | */
10 | public class OllamaLlmLangChain4jTest
11 | extends TestCase
12 | {
13 | /**
14 | * Create the test case
15 | *
16 | * @param testName name of the test case
17 | */
18 | public OllamaLlmLangChain4jTest( String testName )
19 | {
20 | super( testName );
21 | }
22 |
23 | /**
24 | * @return the suite of tests being tested
25 | */
26 | public static Test suite()
27 | {
28 | return new TestSuite( OllamaLlmLangChain4jTest.class );
29 | }
30 |
31 | /**
32 | * Rigourous Test :-)
33 | * @throws Exception
34 | */
35 | public void testCompletion() throws Exception {
36 | String r =
37 | OllamaLlmLangChain4j.getCompletion("Translate the following English text to French: 'Hello, how are you?'", "llama3.2:latest");
38 | System.out.println("\n\n&&&&&&&&&&\n\ncompletion: " + r);
39 | assertTrue( true );
40 | }
41 |
42 | public void testTwoShotTemplate() throws Exception {
43 | String input_text = "Mark Smith enjoys living in Berkeley California at 102 Dunston Street and use mjess@foobar.com for contacting him.";
44 | String prompt0 = OllamaLlmLangChain4j.readFileToString("../prompts/two-shot-2-var.txt");
45 | System.out.println("prompt0: " + prompt0);
46 | String prompt = OllamaLlmLangChain4j.promptVar(prompt0, "{input_text}", input_text);
47 | System.out.println("prompt: " + prompt);
48 | String r =
49 | OllamaLlmLangChain4j.getCompletion(prompt, "llama3:instruct");
50 | System.out.println("two shot extraction completion: " + r);
51 | assertTrue( true );
52 | }
53 |
54 | public void testSummarization() throws Exception {
55 | String input_text = "Jupiter is the fifth planet from the Sun and the largest in the Solar System. It is a gas giant with a mass one-thousandth that of the Sun, but two-and-a-half times that of all the other planets in the Solar System combined. Jupiter is one of the brightest objects visible to the naked eye in the night sky, and has been known to ancient civilizations since before recorded history. It is named after the Roman god Jupiter.[19] When viewed from Earth, Jupiter can be bright enough for its reflected light to cast visible shadows,[ and is on average the third-brightest natural object in the night sky after the Moon and Venus.";
56 | String prompt0 = OllamaLlmLangChain4j.readFileToString("../prompts/summarization_prompt.txt");
57 | System.out.println("prompt0: " + prompt0);
58 | String prompt = OllamaLlmLangChain4j.promptVar(prompt0, "{input_text}", input_text);
59 | System.out.println("prompt: " + prompt);
60 | String r =
61 | OllamaLlmLangChain4j.getCompletion(prompt, "llama3:instruct");
62 | System.out.println("summarization completion: " + r);
63 | assertTrue( true );
64 | }
65 | }
66 |
--------------------------------------------------------------------------------
/ner_dbpedia/Makefile:
--------------------------------------------------------------------------------
1 | test: install
2 | mvn test -q
3 |
4 | install:
5 | mvn install -DskipTest -Dmaven.test.skip=true -q
6 |
7 |
--------------------------------------------------------------------------------
/ner_dbpedia/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 | com.markwatson
8 | nerdbpedia
9 | 1.2-SNAPSHOT
10 |
11 | 11.0
12 | UTF-8
13 | UTF-8
14 |
15 |
16 |
17 |
18 | junit
19 | junit
20 | 4.13.1
21 | test
22 |
23 |
24 |
25 |
26 |
27 | org.apache.maven.plugins
28 | maven-compiler-plugin
29 | 3.3
30 |
31 | 1.8
32 | 1.8
33 |
34 |
35 |
36 |
37 |
38 |
39 |
--------------------------------------------------------------------------------
/ner_dbpedia/src/.DS_Store:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mark-watson/Java-AI-Book-Code/f62e1231f4cf2de4641ed01518334696d4d07ff0/ner_dbpedia/src/.DS_Store
--------------------------------------------------------------------------------
/ner_dbpedia/src/main/.DS_Store:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mark-watson/Java-AI-Book-Code/f62e1231f4cf2de4641ed01518334696d4d07ff0/ner_dbpedia/src/main/.DS_Store
--------------------------------------------------------------------------------
/ner_dbpedia/src/main/resources/.DS_Store:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mark-watson/Java-AI-Book-Code/f62e1231f4cf2de4641ed01518334696d4d07ff0/ner_dbpedia/src/main/resources/.DS_Store
--------------------------------------------------------------------------------
/ner_dbpedia/src/main/resources/META-INF/file-manefest-for-text-resource-files.txt:
--------------------------------------------------------------------------------
1 | BroadcastNetworkNamesDbPedia.txt
2 | PeopleDbPedia.txt
3 | CityNamesDbpedia.txt
4 | PoliticalPartyNamesDbPedia.txt
5 | CompanyNamesDbPedia.txt
6 | TradeUnionNamesDbPedia.txt
7 | CountryNamesDbpedia.txt
8 | UniversityNamesDbPedia.txt
9 | MusicGroupNamesDbPedia.txt
10 |
--------------------------------------------------------------------------------
/ner_dbpedia/src/test/java/com/markwatson/ner_dbpedia/TextToDbpediaUrisTest.java:
--------------------------------------------------------------------------------
1 | package com.markwatson.ner_dbpedia;
2 |
3 | import junit.framework.Test;
4 | import junit.framework.TestCase;
5 | import junit.framework.TestSuite;
6 |
7 | public class TextToDbpediaUrisTest extends TestCase {
8 | /**
9 | * Create the test case
10 | *
11 | * @param testName name of the test case
12 | */
13 | public TextToDbpediaUrisTest(String testName)
14 | {
15 | super( testName );
16 | }
17 |
18 | /**
19 | * @return the suite of tests being tested
20 | */
21 | public static Test suite()
22 | {
23 | return new TestSuite( TextToDbpediaUrisTest.class );
24 | }
25 |
26 | /**
27 | * Test that is just for side effect printouts:
28 | */
29 | public void test1() throws Exception {
30 | String s = "PTL Satellite Network covered President Bill Clinton going to Guatemala and visiting the Coca Cola Company.";
31 | TextToDbpediaUris test = new TextToDbpediaUris(s);
32 | System.out.println(test);
33 | assert(true);
34 | }
35 |
36 | }
37 |
--------------------------------------------------------------------------------
/neuralnetworks/.gitignore:
--------------------------------------------------------------------------------
1 | test.neural
2 |
3 |
--------------------------------------------------------------------------------
/neuralnetworks/Makefile:
--------------------------------------------------------------------------------
1 | all_examples: 1H 2H 2H_momentum
2 |
3 | 1H:
4 | mvn install -DskipTests -q
5 | mvn test -Dtest=NeuralNetwork_1H_Test -q
6 |
7 | 2H:
8 | mvn install -DskipTests -q
9 | mvn test -Dtest=NeuralNetwork_2H_Test -q
10 |
11 |
12 | 2H_momentum:
13 | mvn install -DskipTests -q
14 | mvn test -Dtest=NeuralNetwork_2H_momentum_Test -q
15 |
16 |
17 | install:
18 | mvn install -DskipTest -Dmaven.test.skip=true -q
19 |
--------------------------------------------------------------------------------
/neuralnetworks/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 | NeuralNetworksFromScratch
8 | neuralnetworks
9 | 1.0-SNAPSHOT
10 |
11 |
12 |
13 | junit
14 | junit
15 | 4.13.1
16 | test
17 |
18 |
19 |
20 |
21 |
22 | org.apache.maven.plugins
23 | maven-compiler-plugin
24 | 3.3
25 |
26 | 1.8
27 | 1.8
28 |
29 |
30 |
31 |
32 |
33 |
--------------------------------------------------------------------------------
/neuralnetworks/src/main/java/com/markwatson/neuralnetworks/Graph.java:
--------------------------------------------------------------------------------
1 | package com.markwatson.neuralnetworks;
2 |
3 | import javax.swing.*;
4 | import java.awt.*;
5 |
6 | /**
7 | * Title: Graph
8 | * Description: Simple program to produce a graph for the book
9 | * Copyright: Copyright (c) Mark Watson
10 | * Company:
11 | * @author Mark Watson
12 | * @version 1.0
13 | */
14 |
15 |
16 | public class Graph extends JFrame {
17 | GraphPanel jPanel1;
18 | float[] data1;
19 | float[] data2;
20 |
21 | public Graph() {
22 | try {
23 | int size = 500;
24 | data1 = new float[size];
25 | data2 = new float[size];
26 | float xmin = -5;
27 | float xmax = 5;
28 | for (int i = 0; i < size; i++) {
29 | float x = i;
30 | x = xmin + x * (xmax - xmin) / (float) size;
31 | data1[i] = sigmoid(x);
32 | data2[i] = sigmoidP(x);
33 | }
34 | jbInit();
35 | } catch (Exception e) {
36 | e.printStackTrace();
37 | }
38 | }
39 |
40 | protected float sigmoid(float x) {
41 | return
42 | (float) (1.0f / (1.0f + Math.exp((double) (-x))));
43 | // (float)((1.0f/(1.0f+Math.exp((double)(-x))))-0.5f);
44 | }
45 |
46 | protected float sigmoidP(float x) {
47 | double z = sigmoid(x); // + 0.5f;
48 | return (float) (z * (1.0f - z));
49 | }
50 |
51 |
52 | public static void main(String[] args) {
53 | Graph untitled11 = new Graph();
54 | }
55 |
56 | private void jbInit() throws Exception {
57 | jPanel1 = new GraphPanel(data1, data2);
58 | jPanel1.setBackground(Color.white);
59 | this.setDefaultCloseOperation(3);
60 | this.getContentPane().add(jPanel1, BorderLayout.CENTER);
61 | setSize(550, 300);
62 | jPanel1.setVisible(true);
63 | setVisible(true);
64 | }
65 | }
66 |
--------------------------------------------------------------------------------
/neuralnetworks/src/main/java/com/markwatson/neuralnetworks/GraphPanel.java:
--------------------------------------------------------------------------------
1 | package com.markwatson.neuralnetworks;
2 |
3 |
4 | /**
5 | * Title:
6 | * Description:
7 | * Copyright: Copyright (c)
8 | * Company:
9 | * @author
10 | * @version 1.0
11 | */
12 |
13 | import javax.swing.*;
14 | import java.awt.*;
15 |
16 | public class GraphPanel extends java.awt.Canvas { // JPanel {
17 |
18 | public GraphPanel(float[] data1, float[] data2) {
19 | super();
20 | this.data1 = data1;
21 | this.data2 = data2;
22 | }
23 |
24 | Color black = new Color(0, 0, 0);
25 | float[] data1;
26 | float[] data2;
27 |
28 | public void paint(Graphics g) {
29 | if (data1 == null || data2 == null) return;
30 | int width = this.getWidth();
31 | int height = this.getHeight();
32 | System.out.println("height=" + height);
33 | float min = 99999999.9f;
34 | float max = -min;
35 | int maxindex = 0;
36 | float maxval = 0.0f;
37 | for (int i = 0; i < data1.length; i++) { // assume length of data1 and data2 are the same
38 | if (min > data1[i]) min = data1[i];
39 | if (max < data1[i]) max = data1[i];
40 | if (min > data2[i]) min = data2[i];
41 | if (max < data2[i]) max = data2[i];
42 | }
43 | System.out.println("min=" + min + ", max=" + max);
44 | g.setColor(Color.red);
45 | for (int i = 0; i < data1.length - 1; i++) {
46 | float y1 = height - 5 - 0.95f * height * ((data1[i] - min) / (max - min));
47 | float y2 = height - 5 - 0.95f * height * ((data1[i + 1] - min) / (max - min));
48 | //System.out.println("data["+i+"]="+data[i]+", y1="+y1+", y2="+y2);
49 | g.drawLine(i + 20, (int) y1, i + 21, (int) y2);
50 | y1 = height - 5 - 0.95f * height * ((data2[i] - min) / (max - min));
51 | y2 = height - 5 - 0.95f * height * ((data2[i + 1] - min) / (max - min));
52 | //System.out.println("data["+i+"]="+data[i]+", y1="+y1+", y2="+y2);
53 | g.drawLine(i + 20, (int) y1, i + 21, (int) y2);
54 | }
55 | float yzero = height - 5 - 0.95f * height * ((0.0f - min) / (max - min));
56 | g.setColor(black);
57 | g.drawLine(20, (int) yzero, data2.length + 19, (int) yzero);
58 | g.drawLine(width / 2, height / 2 - 118, width / 2, height / 2 + 118);
59 | g.drawString("Sigmoid", width / 2 + 100, height / 4 - 10);
60 | g.drawString("SigmoidP", width / 2 +60, 3 * height / 4 + 10);
61 | g.drawString("-5", 4, (int) yzero);
62 | g.drawString("5", width - 19, (int) yzero);
63 | g.drawString("1.0", width / 2 - 7, 12);
64 | g.drawString("0.0", width / 2 - 9, height - 5);
65 | }
66 | }
67 |
--------------------------------------------------------------------------------
/neuralnetworks/src/main/java/com/markwatson/neuralnetworks/Plot1DPanel.java:
--------------------------------------------------------------------------------
1 | package com.markwatson.neuralnetworks;
2 |
3 |
4 | import javax.swing.*;
5 | import java.awt.*;
6 |
7 | public class Plot1DPanel extends java.awt.Canvas { // JPanel {
8 |
9 | public Plot1DPanel(int num, float min, float max, float[] values) {
10 | super();
11 | this.num = num;
12 | this.min = min;
13 | this.max = max;
14 | this.values = values;
15 | colors = new Color[100];
16 | for (int i = 0; i < 100; i++) {
17 | float x = 1.0f - ((float) i) * 0.0096f;
18 | colors[i] = new Color(x, x, x);
19 | }
20 | this.setBackground(Color.white);
21 | }
22 |
23 | private int num;
24 | private float min, max;
25 | private float temp;
26 | private float[] values = null;
27 | private Color[] colors;
28 |
29 | //public void plot(float [] values) {
30 | //}
31 | public void paint(Graphics g) {
32 | //System.out.println("Plot1DPanel: values="+values);
33 | if (values == null) return;
34 | int delta_width = this.getWidth() / num;
35 | int delta_height = this.getHeight() / num;
36 | for (int i = 0; i < num; i++) {
37 | //System.out.println(this.toString() + ", values[" + i + "]=" + values[i]);
38 | temp = 100.0f * (values[i] - min) / (max - min);
39 | int ii = (int) temp;
40 | if (ii < 0) ii = 0;
41 | if (ii > 99) ii = 99;
42 | g.setColor(colors[ii]);
43 | g.fillRect(i * delta_width, 0, delta_width, delta_height);
44 | g.setColor(Color.black);
45 | g.drawRect(i * delta_width, 0, delta_width, delta_height);
46 | }
47 | }
48 | }
49 |
--------------------------------------------------------------------------------
/neuralnetworks/src/main/java/com/markwatson/neuralnetworks/Plot2DPanel.java:
--------------------------------------------------------------------------------
1 | package com.markwatson.neuralnetworks;
2 |
3 | import javax.swing.*;
4 | import java.awt.*;
5 |
6 | public class Plot2DPanel extends java.awt.Canvas { // JPanel {
7 |
8 | public Plot2DPanel(int num1, int num2, float min, float max, float[][] values) {
9 | super();
10 | this.num1 = num1;
11 | this.num2 = num2;
12 | this.min = min;
13 | this.max = max;
14 | this.values = values;
15 | colors = new Color[100];
16 | for (int i = 0; i < 100; i++) {
17 | float x = 1.0f - ((float) i) * 0.0096f;
18 | colors[i] = new Color(x, x, x);
19 | }
20 | }
21 |
22 | private int num1;
23 | private int num2;
24 | private float min, max;
25 | private float temp;
26 | private float[][] values = null;
27 | private Color[] colors;
28 |
29 | public void paint(Graphics g) {
30 | if (values == null) return;
31 | int delta_width = this.getWidth() / num1;
32 | int delta_height = this.getHeight() / num2;
33 | for (int i = 0; i < num1; i++) {
34 | for (int j = 0; j < num2; j++) {
35 | //System.out.println(this.toString() + ", values[" + i + "]=" + values[i]);
36 | temp = 100.0f * (values[i][j] - min) / (max - min);
37 | int ii = (int) temp;
38 | if (ii < 0) ii = 0;
39 | if (ii > 99) ii = 99;
40 | g.setColor(colors[ii]);
41 | g.fillRect(i * delta_width, j * delta_height, delta_width, delta_height);
42 | g.setColor(Color.black);
43 | g.drawRect((i * delta_width), (j * delta_height), delta_width, delta_height);
44 | }
45 | }
46 | }
47 | }
48 |
--------------------------------------------------------------------------------
/neuralnetworks/src/test/java/com/markwatson/neuralnetworks/NeuralNetwork_1H_Test.java:
--------------------------------------------------------------------------------
1 | package com.markwatson.neuralnetworks;
2 |
3 | import junit.framework.Test;
4 | import junit.framework.TestCase;
5 | import junit.framework.TestSuite;
6 |
7 | public class NeuralNetwork_1H_Test extends TestCase {
8 | /**
9 | * Create the test case
10 | *
11 | * @param testName name of the test case
12 | */
13 | public NeuralNetwork_1H_Test(String testName)
14 | {
15 | super( testName );
16 | }
17 |
18 | static float[] in1 = {0.1f, 0.1f, 0.9f};
19 | static float[] in2 = {0.1f, 0.9f, 0.1f};
20 | static float[] in3 = {0.9f, 0.1f, 0.1f};
21 |
22 | static float[] out1 = {0.9f, 0.1f, 0.1f};
23 | static float[] out2 = {0.1f, 0.1f, 0.9f};
24 | static float[] out3 = {0.1f, 0.9f, 0.1f};
25 |
26 | static float[] test1 = {0.1f, 0.1f, 0.9f};
27 | static float[] test2 = {0.1f, 0.9f, 0.1f};
28 | static float[] test3 = {0.9f, 0.1f, 0.1f};
29 |
30 | void test_recall(Neural_1H nn, float[] inputs) {
31 | float[] results = nn.recall(inputs);
32 | System.out.print("Test case: ");
33 | for (float input : inputs) System.out.print(pp(input) + " ");
34 | System.out.print(" results: ");
35 | for (float result : results) System.out.print(pp(result) + " ");
36 | System.out.println();
37 | }
38 |
39 |
40 | /**
41 | * @return the suite of tests being tested
42 | */
43 | public static Test suite()
44 | {
45 | return new TestSuite( NeuralNetwork_1H_Test.class );
46 | }
47 |
48 | /**
49 | * Test that is just for side effect printouts:
50 | */
51 | public void testTraining() {
52 | Neural_1H nn = new Neural_1H(3, 3, 3);
53 | nn.addTrainingExample(in1, out1);
54 | nn.addTrainingExample(in2, out2);
55 | nn.addTrainingExample(in3, out3);
56 | double error = 0;
57 | for (int i = 0; i < 10000; i++) {
58 | error += nn.train();
59 | if (i > 0 && (i % 1000 == 0)) {
60 | error /= 100;
61 | System.out.println("cycle " + i + " error is " + error);
62 | error = 0;
63 | }
64 | }
65 | System.out.println("Test results should rotate inputs:");
66 | test_recall(nn, test1);
67 | test_recall(nn, test2);
68 | test_recall(nn, test3);
69 | }
70 |
71 | public static String pp(float x) {
72 | String s = new String("" + x + "00");
73 | int index = s.indexOf(".");
74 | if (index > -1) s = s.substring(0, index + 3);
75 | if (s.startsWith("-") == false) s = " " + s;
76 | return s;
77 | }
78 | }
--------------------------------------------------------------------------------
/neuralnetworks/src/test/java/com/markwatson/neuralnetworks/NeuralNetwork_2H_Test.java:
--------------------------------------------------------------------------------
1 | package com.markwatson.neuralnetworks;
2 |
3 | import junit.framework.Test;
4 | import junit.framework.TestCase;
5 | import junit.framework.TestSuite;
6 |
7 | public class NeuralNetwork_2H_Test extends TestCase {
8 | /**
9 | * Create the test case
10 | *
11 | * @param testName name of the test case
12 | */
13 | public NeuralNetwork_2H_Test(String testName)
14 | {
15 | super( testName );
16 | }
17 |
18 | static float[] in1 = {0.1f, 0.1f, 0.9f};
19 | static float[] in2 = {0.1f, 0.9f, 0.1f};
20 | static float[] in3 = {0.9f, 0.1f, 0.1f};
21 |
22 | static float[] out1 = {0.9f, 0.1f, 0.1f};
23 | static float[] out2 = {0.1f, 0.1f, 0.9f};
24 | static float[] out3 = {0.1f, 0.9f, 0.1f};
25 |
26 | static float[] test1 = {0.1f, 0.1f, 0.9f};
27 | static float[] test2 = {0.1f, 0.9f, 0.1f};
28 | static float[] test3 = {0.9f, 0.1f, 0.1f};
29 |
30 | void test_recall(Neural_2H nn, float[] inputs) {
31 | float[] results = nn.recall(inputs);
32 | System.out.print("Test case: ");
33 | for (float input : inputs) System.out.print(pp(input) + " ");
34 | System.out.print(" results: ");
35 | for (float result : results) System.out.print(pp(result) + " ");
36 | System.out.println();
37 | }
38 |
39 |
40 | /**
41 | * @return the suite of tests being tested
42 | */
43 | public static Test suite()
44 | {
45 | return new TestSuite( NeuralNetwork_2H_Test.class );
46 | }
47 |
48 | /**
49 | * Test that is just for side effect printouts:
50 | */
51 | public void testTraining() {
52 | Neural_2H nn = new Neural_2H(3, 3, 3, 3);
53 | nn.addTrainingExample(in1, out1);
54 | nn.addTrainingExample(in2, out2);
55 | nn.addTrainingExample(in3, out3);
56 | double error = 0;
57 | for (int i = 0; i < 10000; i++) {
58 | error += nn.train();
59 | if (i > 0 && (i % 1000 == 0)) {
60 | error /= 100;
61 | System.out.println("cycle " + i + " error is " + error);
62 | error = 0;
63 | }
64 | }
65 | System.out.println("Test results should rotate inputs:");
66 | test_recall(nn, test1);
67 | test_recall(nn, test2);
68 | test_recall(nn, test3);
69 | }
70 |
71 | public static String pp(float x) {
72 | String s = new String("" + x + "00");
73 | int index = s.indexOf(".");
74 | if (index > -1) s = s.substring(0, index + 3);
75 | if (s.startsWith("-") == false) s = " " + s;
76 | return s;
77 | }
78 | }
--------------------------------------------------------------------------------
/neuralnetworks/src/test/java/com/markwatson/neuralnetworks/NeuralNetwork_2H_momentum_Test.java:
--------------------------------------------------------------------------------
1 | package com.markwatson.neuralnetworks;
2 |
3 | import junit.framework.Test;
4 | import junit.framework.TestCase;
5 | import junit.framework.TestSuite;
6 |
7 | public class NeuralNetwork_2H_momentum_Test extends TestCase {
8 | /**
9 | * Create the test case
10 | *
11 | * @param testName name of the test case
12 | */
13 | public NeuralNetwork_2H_momentum_Test(String testName)
14 | {
15 | super( testName );
16 | }
17 |
18 | static float[] in1 = {0.1f, 0.1f, 0.9f};
19 | static float[] in2 = {0.1f, 0.9f, 0.1f};
20 | static float[] in3 = {0.9f, 0.1f, 0.1f};
21 |
22 | static float[] out1 = {0.9f, 0.1f, 0.1f};
23 | static float[] out2 = {0.1f, 0.1f, 0.9f};
24 | static float[] out3 = {0.1f, 0.9f, 0.1f};
25 |
26 | static float[] test1 = {0.1f, 0.1f, 0.9f};
27 | static float[] test2 = {0.1f, 0.9f, 0.1f};
28 | static float[] test3 = {0.9f, 0.1f, 0.1f};
29 |
30 | void test_recall(Neural_2H_momentum nn, float[] inputs) {
31 | float[] results = nn.recall(inputs);
32 | System.out.print("Test case: ");
33 | for (float input : inputs) System.out.print(pp(input) + " ");
34 | System.out.print(" results: ");
35 | for (float result : results) System.out.print(pp(result) + " ");
36 | System.out.println();
37 | }
38 |
39 |
40 | /**
41 | * @return the suite of tests being tested
42 | */
43 | public static Test suite()
44 | {
45 | return new TestSuite( NeuralNetwork_2H_momentum_Test.class );
46 | }
47 |
48 | /**
49 | * Test that is just for side effect printouts:
50 | */
51 | public void testTraining() {
52 | Neural_2H_momentum nn = new Neural_2H_momentum(3, 3, 3, 3);
53 | nn.addTrainingExample(in1, out1);
54 | nn.addTrainingExample(in2, out2);
55 | nn.addTrainingExample(in3, out3);
56 | double error = 0;
57 | for (int i = 0; i < 10000; i++) {
58 | error += nn.train();
59 | if (i > 0 && (i % 1000 == 0)) {
60 | error /= 100;
61 | System.out.println("cycle " + i + " error is " + error);
62 | error = 0;
63 | }
64 | }
65 | System.out.println("Test results should rotate inputs:");
66 | test_recall(nn, test1);
67 | test_recall(nn, test2);
68 | test_recall(nn, test3);
69 | }
70 |
71 | public static String pp(float x) {
72 | String s = new String("" + x + "00");
73 | int index = s.indexOf(".");
74 | if (index > -1) s = s.substring(0, index + 3);
75 | if (s.startsWith("-") == false) s = " " + s;
76 | return s;
77 | }
78 | }
--------------------------------------------------------------------------------
/neuralnetworks/src/test/java/com/markwatson/neuralnetworks/xor.java:
--------------------------------------------------------------------------------
1 | package com.markwatson.neuralnetworks;
2 |
3 | import junit.framework.Test;
4 | import junit.framework.TestCase;
5 | import junit.framework.TestSuite;
6 |
7 | // 3/7/2004: note: someone asked for an XOR example... -Mark
8 |
9 | public class xor {
10 | static float FALSE = 0.01f;
11 | static float TRUE = 0.99f;
12 |
13 | static float[] in1 = {FALSE, FALSE};
14 | static float[] in2 = {FALSE, TRUE};
15 | static float[] in3 = {TRUE, FALSE};
16 | static float[] in4 = {TRUE, TRUE};
17 |
18 | static float[] out1 = {FALSE};
19 | static float[] out2 = {TRUE};
20 | static float[] out3 = {TRUE};
21 | static float[] out4 = {FALSE};
22 |
23 | static float[] test1 = {FALSE, TRUE};
24 | static float[] test2 = {TRUE, FALSE};
25 | static float[] test3 = {TRUE, TRUE};
26 |
27 | public static void main(String[] args) {
28 | Neural_1H nn = new Neural_1H(2, 3, 1);
29 | nn.addTrainingExample(in1, out1);
30 | nn.addTrainingExample(in3, out3);
31 | nn.addTrainingExample(in2, out2);
32 | nn.addTrainingExample(in4, out4);
33 | // loop until we get a low enough error:
34 | try_again: for (int iter = 0; iter < 500; iter++) {
35 | nn.randomizeWeights();
36 | System.out.println("\nStarting with a new random set of weights (iter="+iter+")");
37 | float weightJiggleFactor = 0.15f;
38 | double learningRate = 0.4; // 0.15;
39 | float error = 0;
40 | for (long i = 0; i < 2000000; i++) {
41 | nn.jiggleWeights(weightJiggleFactor);
42 | weightJiggleFactor *= 0.9999;
43 | nn.setLearningRate((float) learningRate);
44 | learningRate *= 0.999995;
45 | if (learningRate < 0.08f) learningRate = 0.08f;
46 | if (weightJiggleFactor < 0.0002f) weightJiggleFactor = 0.02f;
47 | error += nn.train();
48 | //if (error > 0.25f) weightJiggleFactor = 0.5f;
49 | //if (error > 0.31f) nn.randomizeWeights();
50 | if (i>0 && i % 100 == 0) {
51 | error /= 100;
52 | //System.out.println("cycle " + i + " error is " + error + ", learningRate=" + learningRate + ", weightJiggleFactor=" + weightJiggleFactor);
53 | // test to see if this set of initial random weights is
54 | // producing poor results - if so, bail and start with a new set
55 | // of weights:
56 | if (i > 30000 & error > 0.38f) continue try_again;
57 | if (i > 90000 & error > 0.28f) continue try_again;
58 | if (i >150000 & error > 0.21f) continue try_again;
59 | if (i >350000 & error > 0.18f) continue try_again;
60 | // if the error is low enough, simply stop training now:
61 | if (error < 0.1) break try_again;
62 | error = 0;
63 | }
64 | }
65 | }
66 | test_recall(nn, test1);
67 | test_recall(nn, test2);
68 | test_recall(nn, test3);
69 | }
70 |
71 | public static void test_recall(Neural_1H nn, float[] inputs) {
72 | float[] results = nn.recall(inputs);
73 | System.out.print("Test case: ");
74 | for (int i = 0; i < inputs.length; i++) System.out.print(pp(inputs[i]) + " ");
75 | System.out.print(" results: ");
76 | for (int i = 0; i < results.length; i++) System.out.print(pp(results[i]) + " ");
77 | System.out.println();
78 | }
79 |
80 | public static String pp(float x) {
81 | String s = new String("" + x + "00");
82 | int index = s.indexOf(".");
83 | if (index > -1) s = s.substring(0, index + 3);
84 | if (s.startsWith("-") == false) s = " " + s;
85 | return s;
86 | }
87 | }
88 |
--------------------------------------------------------------------------------
/nlp/.gitignore:
--------------------------------------------------------------------------------
1 | .classpath
2 | .project
3 | *.iml
4 | *.txt
5 |
--------------------------------------------------------------------------------
/nlp/Makefile:
--------------------------------------------------------------------------------
1 | names:
2 | mvn install -DskipTests -q
3 | mvn exec:java -Dexec.mainClass="com.markwatson.nlp.ExtractNames" -q
4 |
5 | autotagger:
6 | mvn install -DskipTests -q
7 | mvn exec:java -Dexec.mainClass="com.markwatson.nlp.AutoTagger" -q
8 |
9 | fasttag:
10 | mvn install -DskipTests -q
11 | mvn exec:java -Dexec.mainClass="com.markwatson.nlp.FastTag" -q
12 |
13 |
14 | install:
15 | mvn install -DskipTest -Dmaven.test.skip=true -q
16 |
--------------------------------------------------------------------------------
/nlp/README.md:
--------------------------------------------------------------------------------
1 | # Notes for Mark Watson's NLP library
2 |
3 | TBD: history
4 |
5 | ## ExtractNames - named entity recognition
6 |
7 | The file test_data/propernames.ser is a serialized object file containing first names, last names, and place names.
8 |
9 | Just for educational purposes, the class constructor writes these names to the text files firstnames.txt, lastnames.txt, and placenames.txt.
10 |
11 | These text files are not used for anything except for giving you an easy way to see what entity name data we are using in this example.
12 |
--------------------------------------------------------------------------------
/nlp/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 | MarkWatsonNlpLib
8 | nlplib
9 | 1.0-SNAPSHOT
10 |
11 |
12 |
13 | junit
14 | junit
15 | 4.13.1
16 | test
17 |
18 |
19 |
20 |
21 |
22 | org.apache.maven.plugins
23 | maven-compiler-plugin
24 | 3.3
25 |
26 | 1.8
27 | 1.8
28 |
29 |
30 |
31 |
32 |
33 |
--------------------------------------------------------------------------------
/nlp/src/main/java/com/markwatson/nlp/ComparableDocument.java:
--------------------------------------------------------------------------------
1 | package com.markwatson.nlp;
2 |
3 | import com.markwatson.nlp.util.NoiseWords;
4 | import public_domain.Stemmer;
5 |
6 | import java.io.File;
7 | import java.io.FileNotFoundException;
8 | import java.util.*;
9 |
10 | /**
11 | * This class stores stem count data for words in a document and provides
12 | * an API to compare the similarity between this document and another.
13 | *
14 | * @author Mark Watson
15 | *
16 | *
17 | * Copyright 1998-2012 by Mark Watson. All rights reserved.
18 | *
19 | * This software is can be used under either of the following licenses:
20 | *
21 | * 1. LGPL v3
22 | * 2. Apache 2
23 | *
24 | *
25 | */
26 | public class ComparableDocument {
27 | private ComparableDocument() { } // disable default constructor calls
28 | public ComparableDocument(File document) throws FileNotFoundException {
29 | this(new Scanner(document).useDelimiter("\\Z").next());
30 | }
31 | public ComparableDocument(String text) {
32 | // System.out.println("text:\n\n" + text + "\n\n");
33 | List stems = new Stemmer().stemString(text);
34 | for (String stem : stems) {
35 | if (!NoiseWords.checkFor(stem)) {
36 | stem_count++;
37 | if (stemCountMap.containsKey(stem)) {
38 | Integer count = stemCountMap.get(stem);
39 | stemCountMap.put(stem, 1 + count);
40 | } else {
41 | stemCountMap.put(stem, 1);
42 | }
43 | }
44 | // System.out.println(stem + " : " + stemCountMap.get(stem));
45 | }
46 | }
47 | public Map getStemMap() { return stemCountMap; }
48 | public int getStemCount() { return stem_count; }
49 | public float compareTo(ComparableDocument otherDocument) {
50 | long count = 0;
51 | Map map2 = otherDocument.getStemMap();
52 | Iterator iter = stemCountMap.keySet().iterator();
53 | while (iter.hasNext()) {
54 | String key = iter.next();
55 | Integer count1 = stemCountMap.get(key);
56 | Integer count2 = map2.get(key);
57 |
58 | if (count1!=null && count2!=null) {
59 | count += count1 + count2;
60 | //System.out.println(key);
61 | }
62 | }
63 | //System.out.println("stem_count="+stem_count);
64 | return (float) Math.sqrt(((float)(count*count) / (double)(stem_count * otherDocument.getStemCount()))) / 2f;
65 | }
66 | private Map stemCountMap = new HashMap();
67 | private int stem_count = 0;
68 | // throw away test program:
69 | public static void main(String[] args) throws FileNotFoundException {
70 | ComparableDocument news1 = new ComparableDocument(new File("test_data/news_1.txt"));
71 | ComparableDocument news2 = new ComparableDocument(new File("test_data/news_2.txt"));
72 | ComparableDocument econ1 = new ComparableDocument(new File("test_data/economy_1.txt"));
73 | ComparableDocument econ2 = new ComparableDocument(new File("test_data/economy_2.txt"));
74 | System.out.println("news 1 - news1: " + news1.compareTo(news1));
75 | System.out.println("news 1 - news2: " + news1.compareTo(news2));
76 | System.out.println("news 2 - news2: " + news2.compareTo(news2));
77 | System.out.println("news 1 - econ1: " + news1.compareTo(econ1));
78 | System.out.println("econ 1 - econ1: " + econ1.compareTo(econ1));
79 | System.out.println("news 1 - econ2: " + news1.compareTo(econ2));
80 | System.out.println("news 2 - econ2: " + news2.compareTo(econ2));
81 | System.out.println("econ 1 - econ2: " + econ1.compareTo(econ2));
82 | System.out.println("econ 2 - econ2: " + econ2.compareTo(econ2));
83 | }
84 | }
85 |
86 |
87 |
--------------------------------------------------------------------------------
/nlp/src/main/java/com/markwatson/nlp/util/NameValue.java:
--------------------------------------------------------------------------------
1 | package com.markwatson.nlp.util;
2 |
3 | public class NameValue {
4 | private K name = null;
5 | private V value = null;
6 | public NameValue(K k, V v) {
7 | this.name = k;
8 | this.value = v;
9 | }
10 | public K getName() {
11 | return this.name;
12 | }
13 | public V getValue() {
14 | return this.value;
15 | }
16 | public void setValue(V val) {
17 | this.value = val;
18 | }
19 | public String toString() {
20 | return "[NameValue: " + name + " : " + value + "]";
21 | }
22 | }
--------------------------------------------------------------------------------
/nlp/src/main/java/com/markwatson/nlp/util/NoiseWords.java:
--------------------------------------------------------------------------------
1 | package com.markwatson.nlp.util;
2 |
3 | import java.util.HashSet;
4 | import java.util.Set;
5 | import public_domain.Stemmer;
6 |
7 | public class NoiseWords {
8 | private static String[] words = {
9 | "the", "a", "an", "it", "or", "and", "he", "she",
10 | "with", "often", "to", "do", "that", "this", "is",
11 | "are", "one", "two", "since", "just", "start",
12 | "beyond", "could", "not", "be", "from", "on", "could",
13 | "as", "say", "said", "will", "if", "by", "on", "often",
14 | "little", "big", "did", "do", "about", "any", "such",
15 | "up", "s", "already", "than", "now", "gave", "less",
16 | "more", "another", "for", "other", "goes", "would",
17 | "of", "her", "how", "told", "meet", "without",
18 | "few", "has", "ask", "run", "across", "rather", "me",
19 | "sometme", "want", "d", "look", "perhaps", "come",
20 | "o", "us", "m", "seem", "i", "u", "t", "what",
21 | "but", "last", "who", "toward", "when", "thing",
22 | "got", "can", "with", "at", "off", "in", "much",
23 | "under", "why", "also", "take", "am", "great",
24 | "in", "top"
25 | };
26 | private static Set stems = new HashSet();
27 | static {
28 | Stemmer stemmer = new Stemmer();
29 | for (String word : words) {
30 | stems.add(stemmer.stemOneWord(word));
31 | }
32 | }
33 | public static boolean checkFor(String stem) {
34 | return stems.contains(stem);
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/nlp/src/main/java/com/markwatson/nlp/util/RunExternal.java:
--------------------------------------------------------------------------------
1 | package com.markwatson.nlp.util;
2 |
3 | import java.io.BufferedReader;
4 | import java.io.InputStreamReader;
5 |
6 | public class RunExternal {
7 |
8 | public static void main(String argv[]) {
9 | try {
10 | String line;
11 | Process p = Runtime.getRuntime().exec
12 | ("echo \"thhe dogg brked\" | /usr/local/bin/aspell -a list");
13 | BufferedReader input =
14 | new BufferedReader
15 | (new InputStreamReader(p.getInputStream()));
16 | while ((line = input.readLine()) != null) {
17 | System.out.println(line);
18 | }
19 | input.close();
20 | }
21 | catch (Exception err) {
22 | err.printStackTrace();
23 | }
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/nlp/test_data/dictionary/README:
--------------------------------------------------------------------------------
1 |
2 | This file contains the contents of the Ispell (ver 3.1.20) word list after
3 | being expand from there affix compressed form used by Ispell.
4 |
5 | Ispell can be found at http://fmg-www.cs.ucla.edu/geoff/ispell.html.
6 | This wordlist can be found at http://wordlist.sourceforge.net/
7 |
8 | These word lists are under the same copyright as Ispell itself:
9 |
10 | Copyright 1993, Geoff Kuenning, Granada Hills, CA
11 | All rights reserved.
12 |
13 | Redistribution and use in source and binary forms, with or without
14 | modification, are permitted provided that the following conditions
15 | are met:
16 |
17 | 1. Redistributions of source code must retain the above copyright
18 | notice, this list of conditions and the following disclaimer.
19 | 2. Redistributions in binary form must reproduce the above copyright
20 | notice, this list of conditions and the following disclaimer in the
21 | documentation and/or other materials provided with the distribution.
22 | 3. All modifications to the source code must be clearly marked as
23 | such. Binary redistributions based on modified source code
24 | must be clearly marked as modified versions in the documentation
25 | and/or other materials provided with the distribution.
26 | 4. All advertising materials mentioning features or use of this software
27 | must display the following acknowledgment:
28 | This product includes software developed by Geoff Kuenning and
29 | other unpaid contributors.
30 | 5. The name of Geoff Kuenning may not be used to endorse or promote
31 | products derived from this software without specific prior
32 | written permission.
33 |
34 | THIS SOFTWARE IS PROVIDED BY GEOFF KUENNING AND CONTRIBUTORS ``AS
35 | IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
36 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
37 | FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GEOFF
38 | KUENNING OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
39 | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
40 | BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
41 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
42 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
43 | LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
44 | ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
45 | POSSIBILITY OF SUCH DAMAGE.
46 |
--------------------------------------------------------------------------------
/nlp/test_data/propername.ser:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mark-watson/Java-AI-Book-Code/f62e1231f4cf2de4641ed01518334696d4d07ff0/nlp/test_data/propername.ser
--------------------------------------------------------------------------------
/ollama-llm-client/Makefile:
--------------------------------------------------------------------------------
1 | run:
2 | mvn test -q # run test in quiet mode
3 |
4 |
--------------------------------------------------------------------------------
/ollama-llm-client/README.md:
--------------------------------------------------------------------------------
1 | # Ollama Local LLM REST API Example
2 |
3 | Make sure Ollama serve is running:
4 |
5 | ollama serve
6 |
7 | Use the make file default target to run the test:
8 |
9 | make
--------------------------------------------------------------------------------
/ollama-llm-client/pom.xml:
--------------------------------------------------------------------------------
1 |
3 | 4.0.0
4 | com.markwatson.openai
5 | ollama-llm-client
6 | jar
7 | 1.0-SNAPSHOT
8 | ollama-llm-client
9 | http://maven.apache.org
10 |
11 |
12 | junit
13 | junit
14 | 3.8.1
15 | test
16 |
17 |
18 | org.json
19 | json
20 | 20240303
21 |
22 |
23 |
24 |
--------------------------------------------------------------------------------
/ollama-llm-client/src/main/java/com/markwatson/ollama/OllamaLlmClient.java:
--------------------------------------------------------------------------------
1 | package com.markwatson.ollama;
2 |
3 | import java.io.BufferedReader;
4 | import java.io.IOException;
5 | import java.io.InputStreamReader;
6 | import java.io.OutputStream;
7 | import java.net.HttpURLConnection;
8 | import java.net.URI;
9 | import java.net.URL;
10 | import java.net.URLConnection;
11 | import java.nio.file.Files;
12 | import java.nio.file.Path;
13 | import java.nio.file.Paths;
14 |
15 | import org.json.JSONObject;
16 |
17 | public class OllamaLlmClient {
18 |
19 | public static void main(String[] args) throws Exception {
20 | String prompt = "Translate the following English text to French: 'Hello, how are you?'";
21 | String completion = getCompletion(prompt, "mistral");
22 | System.out.println("completion: " + completion);
23 | }
24 |
25 | public static String getCompletion(String prompt, String modelName) throws Exception {
26 | System.out.println("prompt: " + prompt + ", modelName: " + modelName);
27 |
28 | // New JSON message format
29 | JSONObject message = new JSONObject();
30 | message.put("prompt", prompt);
31 | message.put("model", modelName);
32 | message.put("stream", false);
33 | URI uri = new URI("http://localhost:11434/api/generate");
34 | URL url = uri.toURL();
35 | //System.out.println("jsonBody: " + jsonBody);
36 | URLConnection connection = url.openConnection();
37 | connection.setDoOutput(true);
38 | connection.setRequestProperty("Content-Type", "application/json");
39 | // Send the JSON payload
40 | try (OutputStream os = connection.getOutputStream()) {
41 | byte[] input = message.toString().getBytes("utf-8");
42 | os.write(input, 0, input.length);
43 | }
44 |
45 | StringBuilder response;
46 | // Read the response from the server
47 | try (BufferedReader br = new BufferedReader(
48 | new InputStreamReader(connection.getInputStream(), "utf-8"))) {
49 | response = new StringBuilder();
50 | String responseLine;
51 | while ((responseLine = br.readLine()) != null) {
52 | response.append(responseLine.trim());
53 | }
54 | System.out.println(response.toString());
55 | }
56 |
57 | ((HttpURLConnection) connection).disconnect();
58 |
59 | JSONObject jsonObject = new JSONObject(response.toString());
60 | String s = jsonObject.getString("response");
61 | return s;
62 | }
63 |
64 | /***
65 | * Utilities for using the Ollama LLM APIs
66 | */
67 |
68 | // read the contents of a file path into a Java string
69 | public static String readFileToString(String filePath) throws IOException {
70 | Path path = Paths.get(filePath);
71 | return new String(Files.readAllBytes(path));
72 | }
73 |
74 | public static String replaceSubstring(String originalString, String substringToReplace, String replacementString) {
75 | return originalString.replace(substringToReplace, replacementString);
76 | }
77 | public static String promptVar(String prompt0, String varName, String varValue) {
78 | String prompt = replaceSubstring(prompt0, varName, varValue);
79 | return replaceSubstring(prompt, varName, varValue);
80 | }
81 | }
82 |
83 |
84 |
85 |
86 |
--------------------------------------------------------------------------------
/ollama-llm-client/src/test/java/com/markwatson/ollama/OllamaLlmClientTest.java:
--------------------------------------------------------------------------------
1 | package com.markwatson.ollama;
2 |
3 | import junit.framework.Test;
4 | import junit.framework.TestCase;
5 | import junit.framework.TestSuite;
6 |
7 | /**
8 | * Unit test for simple App.
9 | */
10 | public class OllamaLlmClientTest
11 | extends TestCase
12 | {
13 | /**
14 | * Create the test case
15 | *
16 | * @param testName name of the test case
17 | */
18 | public OllamaLlmClientTest( String testName )
19 | {
20 | super( testName );
21 | }
22 |
23 | /**
24 | * @return the suite of tests being tested
25 | */
26 | public static Test suite()
27 | {
28 | return new TestSuite( OllamaLlmClientTest.class );
29 | }
30 |
31 | /**
32 | * Rigourous Test :-)
33 | * @throws Exception
34 | */
35 | public void testCompletion() throws Exception {
36 | String r =
37 | OllamaLlmClient.getCompletion("Translate the following English text to French: 'Hello, how are you?'", "llama3:instruct");
38 | System.out.println("completion: " + r);
39 | assertTrue( true );
40 | }
41 |
42 | public void testTwoShotTemplate() throws Exception {
43 | String input_text = "Mark Johnson enjoys living in Berkeley California at 102 Dunston Street and use mjess@foobar.com for contacting him.";
44 | String prompt0 = OllamaLlmClient.readFileToString("../prompts/two-shot-2-var.txt");
45 | System.out.println("prompt0: " + prompt0);
46 | String prompt = OllamaLlmClient.promptVar(prompt0, "{input_text}", input_text);
47 | System.out.println("prompt: " + prompt);
48 | String r =
49 | OllamaLlmClient.getCompletion(prompt, "llama3:instruct");
50 | System.out.println("two shot extraction completion: " + r);
51 | assertTrue( true );
52 | }
53 |
54 | public void testSummarization() throws Exception {
55 | String input_text = "Jupiter is the fifth planet from the Sun and the largest in the Solar System. It is a gas giant with a mass one-thousandth that of the Sun, but two-and-a-half times that of all the other planets in the Solar System combined. Jupiter is one of the brightest objects visible to the naked eye in the night sky, and has been known to ancient civilizations since before recorded history. It is named after the Roman god Jupiter.[19] When viewed from Earth, Jupiter can be bright enough for its reflected light to cast visible shadows,[ and is on average the third-brightest natural object in the night sky after the Moon and Venus.";
56 | String prompt0 = OllamaLlmClient.readFileToString("../prompts/summarization_prompt.txt");
57 | System.out.println("prompt0: " + prompt0);
58 | String prompt = OllamaLlmClient.promptVar(prompt0, "{input_text}", input_text);
59 | System.out.println("prompt: " + prompt);
60 | String r =
61 | OllamaLlmClient.getCompletion(prompt, "llama3:instruct");
62 | System.out.println("summarization completion: " + r);
63 | assertTrue( true );
64 | }
65 | }
66 |
--------------------------------------------------------------------------------
/openai-llm-client/Makefile:
--------------------------------------------------------------------------------
1 | run:
2 | mvn test -q # run test in quiet mode
3 |
4 |
--------------------------------------------------------------------------------
/openai-llm-client/README.md:
--------------------------------------------------------------------------------
1 | # OpenAI API Example
2 |
3 | run test using default Makefile target:
4 |
5 | make
--------------------------------------------------------------------------------
/openai-llm-client/pom.xml:
--------------------------------------------------------------------------------
1 |
3 | 4.0.0
4 | com.markwatson.openai
5 | openai-client-example
6 | jar
7 | 1.0-SNAPSHOT
8 | openai-client-example
9 | http://maven.apache.org
10 |
11 |
12 | junit
13 | junit
14 | 3.8.1
15 | test
16 |
17 |
18 | org.json
19 | json
20 | 20240303
21 |
22 |
23 |
24 |
--------------------------------------------------------------------------------
/openai-llm-client/src/main/java/com/markwatson/openai/OpenAICompletions.java:
--------------------------------------------------------------------------------
1 | package com.markwatson.openai;
2 |
3 | import java.io.BufferedReader;
4 | import java.io.IOException;
5 | import java.io.InputStreamReader;
6 | import java.io.OutputStream;
7 | import java.net.HttpURLConnection;
8 | import java.net.URI;
9 | import java.net.URL;
10 | import java.net.URLConnection;
11 | import java.nio.file.Files;
12 | import java.nio.file.Path;
13 | import java.nio.file.Paths;
14 |
15 | import org.json.JSONArray;
16 | import org.json.JSONObject;
17 |
18 | public class OpenAICompletions {
19 |
20 | public static void main(String[] args) throws Exception {
21 | String prompt = "Translate the following English text to French: 'Hello, how are you?'";
22 | String completion = getCompletion(prompt);
23 | System.out.println("completion: " + completion);
24 | }
25 |
26 | public static String getCompletion(String prompt) throws Exception {
27 | System.out.println("prompt: " + prompt);
28 | String apiKey = System.getenv("OPENAI_API_KEY");
29 | String model = "gpt-4o-mini"; // Replace with the desired model
30 |
31 | // New JSON message format
32 | JSONObject message = new JSONObject();
33 | message.put("role", "user");
34 | message.put("content", prompt);
35 |
36 | JSONArray messages = new JSONArray();
37 | messages.put(message);
38 | //System.out.println("messages: " + messages.toString());
39 | JSONObject jsonBody = new JSONObject();
40 | jsonBody.put("messages", messages);
41 | jsonBody.put("model", model);
42 | URI uri = new URI("https://api.openai.com/v1/chat/completions");
43 | URL url = uri.toURL();
44 | //System.out.println("jsonBody: " + jsonBody);
45 | URLConnection connection = url.openConnection();
46 | connection.setDoOutput(true);
47 | connection.setRequestProperty("Content-Type", "application/json");
48 | connection.setRequestProperty("Authorization", "Bearer " + apiKey);
49 | // Send the JSON payload
50 | try (OutputStream os = connection.getOutputStream()) {
51 | byte[] input = jsonBody.toString().getBytes("utf-8");
52 | os.write(input, 0, input.length);
53 | }
54 |
55 | StringBuilder response;
56 | // Read the response from the server
57 | try (BufferedReader br = new BufferedReader(
58 | new InputStreamReader(connection.getInputStream(), "utf-8"))) {
59 | response = new StringBuilder();
60 | String responseLine;
61 | while ((responseLine = br.readLine()) != null) {
62 | response.append(responseLine.trim());
63 | }
64 | System.out.println(response.toString());
65 | }
66 |
67 | ((HttpURLConnection) connection).disconnect();
68 | JSONObject jsonObject = new JSONObject(response.toString());
69 | JSONArray choices = jsonObject.getJSONArray("choices");
70 | JSONObject messageObject = choices.getJSONObject(0).getJSONObject("message");
71 | String content = messageObject.getString("content");
72 | //System.out.println("content: " + content);
73 | return content;
74 | }
75 |
76 |
77 | /***
78 | * Utilities for using the OpenAI API
79 | */
80 |
81 | // read the contents of a file path into a Java string
82 | public static String readFileToString(String filePath) throws IOException {
83 | Path path = Paths.get(filePath);
84 | return new String(Files.readAllBytes(path));
85 | }
86 |
87 | public static String replaceSubstring(String originalString, String substringToReplace, String replacementString) {
88 | return originalString.replace(substringToReplace, replacementString);
89 | }
90 | public static String promptVar(String prompt0, String varName, String varValue) {
91 | String prompt = replaceSubstring(prompt0, varName, varValue);
92 | return replaceSubstring(prompt, varName, varValue);
93 | }
94 | }
95 |
--------------------------------------------------------------------------------
/openai-llm-client/src/test/java/com/markwatson/openai/OpenAICompletionsTest.java:
--------------------------------------------------------------------------------
1 | package com.markwatson.openai;
2 |
3 | import junit.framework.Test;
4 | import junit.framework.TestCase;
5 | import junit.framework.TestSuite;
6 |
7 | /**
8 | * Unit test for simple App.
9 | */
10 | public class OpenAICompletionsTest
11 | extends TestCase
12 | {
13 | /**
14 | * Create the test case
15 | *
16 | * @param testName name of the test case
17 | */
18 | public OpenAICompletionsTest( String testName )
19 | {
20 | super( testName );
21 | }
22 |
23 | /**
24 | * @return the suite of tests being tested
25 | */
26 | public static Test suite()
27 | {
28 | return new TestSuite( OpenAICompletionsTest.class );
29 | }
30 |
31 | /**
32 | * Rigourous Test :-)
33 | * @throws Exception
34 | */
35 | public void testCompletion() throws Exception
36 | {
37 | String r = OpenAICompletions.getCompletion("Translate the following English text to French: 'Hello, how are you?'");
38 | System.out.println("completion: " + r);
39 | assertTrue( true );
40 | }
41 |
42 | public void testTwoShotTemplate() throws Exception {
43 | String input_text = "Mark Johnson enjoys living in Berkeley California at 102 Dunston Street and use mjess@foobar.com for contacting him.";
44 | String prompt0 = OpenAICompletions.readFileToString("../prompts/two-shot-2-var.txt");
45 | System.out.println("prompt0: " + prompt0);
46 | String prompt = OpenAICompletions.promptVar(prompt0, "{input_text}", input_text);
47 | System.out.println("prompt: " + prompt);
48 | String r =
49 | OpenAICompletions.getCompletion(prompt);
50 | System.out.println("two shot extraction completion: " + r);
51 | assertTrue( true );
52 | }
53 |
54 | public void testSummarization() throws Exception {
55 | String input_text = "Jupiter is the fifth planet from the Sun and the largest in the Solar System. It is a gas giant with a mass one-thousandth that of the Sun, but two-and-a-half times that of all the other planets in the Solar System combined. Jupiter is one of the brightest objects visible to the naked eye in the night sky, and has been known to ancient civilizations since before recorded history. It is named after the Roman god Jupiter.[19] When viewed from Earth, Jupiter can be bright enough for its reflected light to cast visible shadows,[ and is on average the third-brightest natural object in the night sky after the Moon and Venus.";
56 | String prompt0 = OpenAICompletions.readFileToString("../prompts/summarization_prompt.txt");
57 | System.out.println("prompt0: " + prompt0);
58 | String prompt = OpenAICompletions.promptVar(prompt0, "{input_text}", input_text);
59 | System.out.println("prompt: " + prompt);
60 | String r =
61 | OpenAICompletions.getCompletion(prompt);
62 | System.out.println("summarization completion: " + r);
63 | assertTrue( true );
64 | }
65 | }
66 |
--------------------------------------------------------------------------------
/prompts/README.md:
--------------------------------------------------------------------------------
1 | #% Example prompts for OPenAI and Ollama examples
2 |
--------------------------------------------------------------------------------
/prompts/extraction_prompt.txt:
--------------------------------------------------------------------------------
1 | Given the two examples below, extract the names, addresses, and email addresses of individuals mentioned later as Process Text. Format the extracted information in JSON, with keys for "name", "address", and "email". If any information is missing, use "null" for that field.
2 |
3 | Example 1:
4 | Text: "John Doe lives at 1234 Maple Street, Springfield. His email is johndoe@example.com."
5 | Output:
6 | {
7 | "name": "John Doe",
8 | "address": "1234 Maple Street, Springfield",
9 | "email": "johndoe@example.com"
10 | }
11 |
12 | Example 2:
13 | Text: "Jane Smith has recently moved to 5678 Oak Avenue, Anytown. She hasn't updated her email yet."
14 | Output:
15 | {
16 | "name": "Jane Smith",
17 | "address": "5678 Oak Avenue, Anytown",
18 | "email": null
19 | }
20 |
21 | Process Text: "{input_text}"
22 | Output:
23 |
--------------------------------------------------------------------------------
/prompts/summarization_prompt.txt:
--------------------------------------------------------------------------------
1 | Summarize the following text: "{input_text}"
2 | Output:
3 |
--------------------------------------------------------------------------------
/prompts/two-shot-2-var.txt:
--------------------------------------------------------------------------------
1 | Given the two examples below, extract the names, addresses, and email addresses of individuals mentioned later as Process Text. Format the extracted information in JSON, with keys for "name", "address", and "email". If any information is missing, use "null" for that field. Be very concise in your output by providing only the output JSON.
2 |
3 | Example 1:
4 | Text: "John Doe lives at 1234 Maple Street, Springfield. His email is johndoe@example.com."
5 | Output:
6 | {
7 | "name": "John Doe",
8 | "address": "1234 Maple Street, Springfield",
9 | "email": "johndoe@example.com"
10 | }
11 |
12 | Example 2:
13 | Text: "Jane Smith has recently moved to 5678 Oak Avenue, Anytown. She hasn't updated her email yet."
14 | Output:
15 | {
16 | "name": "Jane Smith",
17 | "address": "5678 Oak Avenue, Anytown",
18 | "email": null
19 | }
20 |
21 | Process Text: "{input_text}"
22 | Output:
23 |
--------------------------------------------------------------------------------
/prompts/two-shot-2.txt:
--------------------------------------------------------------------------------
1 | Given the two examples below, extract the names, addresses, and email addresses of individuals mentioned later as Process Text. Format the extracted information in JSON, with keys for "name", "address", and "email". If any information is missing, use "null" for that field. Be very concise in your output by providing only the output JSON.
2 |
3 | Example 1:
4 | Text: "John Doe lives at 1234 Maple Street, Springfield. His email is johndoe@example.com."
5 | Output:
6 | {
7 | "name": "John Doe",
8 | "address": "1234 Maple Street, Springfield",
9 | "email": "johndoe@example.com"
10 | }
11 |
12 | Example 2:
13 | Text: "Jane Smith has recently moved to 5678 Oak Avenue, Anytown. She hasn't updated her email yet."
14 | Output:
15 | {
16 | "name": "Jane Smith",
17 | "address": "5678 Oak Avenue, Anytown",
18 | "email": null
19 | }
20 |
21 | Process Text: "Mark Johnson enjoys living in Berkeley California at 102 Dunston Street and use mjess@foobar.com for contacting him."
22 | Output:
23 |
--------------------------------------------------------------------------------
/search/.gitignore:
--------------------------------------------------------------------------------
1 | .idea
2 | saved-model.bin
3 | conf.json
4 | *.iml
5 | target
6 |
--------------------------------------------------------------------------------
/search/.settings/org.eclipse.jdt.apt.core.prefs:
--------------------------------------------------------------------------------
1 | eclipse.preferences.version=1
2 | org.eclipse.jdt.apt.aptEnabled=false
3 |
--------------------------------------------------------------------------------
/search/.settings/org.eclipse.jdt.core.prefs:
--------------------------------------------------------------------------------
1 | eclipse.preferences.version=1
2 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
3 | org.eclipse.jdt.core.compiler.compliance=1.8
4 | org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
5 | org.eclipse.jdt.core.compiler.processAnnotations=disabled
6 | org.eclipse.jdt.core.compiler.release=disabled
7 | org.eclipse.jdt.core.compiler.source=1.8
8 |
--------------------------------------------------------------------------------
/search/.settings/org.eclipse.m2e.core.prefs:
--------------------------------------------------------------------------------
1 | activeProfiles=
2 | eclipse.preferences.version=1
3 | resolveWorkspaceProjects=true
4 | version=1
5 |
--------------------------------------------------------------------------------
/search/Makefile:
--------------------------------------------------------------------------------
1 | chess:
2 | mvn install -q
3 | mvn exec:java -Dexec.mainClass="search.game.Chess" -q
4 |
5 | tictactoe:
6 | mvn install -q
7 | mvn exec:java -Dexec.mainClass="search.game.TicTacToe" -q
8 |
9 | graph:
10 | mvn install -q
11 | mvn exec:java -Dexec.mainClass="search.graph.GraphDepthFirstSearch" -q
12 |
13 | maze:
14 | mvn install -q
15 | mvn exec:java -Dexec.mainClass="search.maze.MazeBreadthFirstSearch" -q
16 |
17 |
18 | install:
19 | mvn install -DskipTest -Dmaven.test.skip=true -q
20 |
--------------------------------------------------------------------------------
/search/pom.xml:
--------------------------------------------------------------------------------
1 |
3 | 4.0.0
4 | com.markwatson
5 | search_examples
6 | jar
7 | 1.0-SNAPSHOT
8 | search_examples
9 | http://maven.apache.org
10 |
11 | 1.8
12 |
13 |
14 |
15 |
16 |
17 | junit
18 | junit
19 | 4.13.1
20 | test
21 |
22 |
23 | org.slf4j
24 | slf4j-api
25 | 1.7.12
26 |
27 |
28 | org.apache.commons
29 | commons-io
30 | 1.3.2
31 |
32 |
33 |
34 |
35 |
36 | org.apache.maven.plugins
37 | maven-compiler-plugin
38 | 2.3.2
39 |
40 | 1.8
41 | 1.8
42 |
43 |
44 |
45 |
46 |
47 |
48 |
--------------------------------------------------------------------------------
/search/src/main/java/search/game/ChessMove.java:
--------------------------------------------------------------------------------
1 | package search.game;
2 |
3 | public class ChessMove extends Move {
4 | public int from;
5 | public int to;
6 | }
7 |
--------------------------------------------------------------------------------
/search/src/main/java/search/game/ChessPosition.java:
--------------------------------------------------------------------------------
1 | package search.game;
2 |
3 | public class ChessPosition extends Position {
4 | final static public int BLANK = 0;
5 | final static public int HUMAN = 1;
6 | final static public int PROGRAM = -1;
7 | final static public int PAWN = 1;
8 | final static public int KNIGHT = 2;
9 | final static public int BISHOP = 3;
10 | final static public int ROOK = 4;
11 | final static public int QUEEN = 5;
12 | final static public int KING = 9;
13 | int [] board = new int[120];
14 | public String toString() {
15 | StringBuffer sb = new StringBuffer("[");
16 | for (int i=22; i<100; i++) {
17 | sb.append(""+board[i]+",");
18 | }
19 | sb.append("]");
20 | return sb.toString();
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/search/src/main/java/search/game/Move.java:
--------------------------------------------------------------------------------
1 | package search.game;
2 |
3 | abstract public class Move {
4 | }
5 |
--------------------------------------------------------------------------------
/search/src/main/java/search/game/Position.java:
--------------------------------------------------------------------------------
1 | package search.game;
2 |
3 | abstract public class Position {
4 | }
--------------------------------------------------------------------------------
/search/src/main/java/search/game/TicTacToeMove.java:
--------------------------------------------------------------------------------
1 | package search.game;
2 |
3 | public class TicTacToeMove extends Move {
4 | public int moveIndex;
5 | }
6 |
--------------------------------------------------------------------------------
/search/src/main/java/search/game/TicTacToePosition.java:
--------------------------------------------------------------------------------
1 | package search.game;
2 |
3 | public class TicTacToePosition extends Position {
4 | final static public int BLANK = 0;
5 | final static public int HUMAN = 1;
6 | final static public int PROGRAM = -1;
7 | int [] board = new int[9];
8 | public String toString() {
9 | StringBuffer sb = new StringBuffer("[");
10 | for (int i=0; i<9; i++) {
11 | sb.append(""+board[i]+",");
12 | }
13 | sb.append("]");
14 | return sb.toString();
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/search/src/main/java/search/game/out.txt:
--------------------------------------------------------------------------------
1 | Board position:
2 |
3 | BR BN BB BQ BK BB BN BR
4 | BP BP BP BP BP BP BP BP
5 | . . . .
6 | . . . .
7 | . . . .
8 | . . . .
9 | WP WP WP WP WP WP WP WP
10 | WR WN WB WQ WK WB WN WR
11 | Your move:
12 | enter a move like 'd2d4' or 'oo'
13 | s=d2d4
14 | From 35, to 55
15 | Board position:
16 |
17 | BR BN BB BQ BK BB BN BR
18 | BP BP BP BP BP BP BP BP
19 | . . . .
20 | . . . .
21 | . WP . .
22 | . . . .
23 | WP WP WP . WP WP WP WP
24 | WR WN WB WQ WK WB WN WR
25 | next element: 5.4
26 | next element: [4,2,3,5,9,3,2,4,7,7,1,1,1,0,1,1,1,1,7,7,0,0,0,0,0,0,0,0,7,7,0,0,0,1,0,0,0,0,7,7,0,0,0,0,0,0,0,0,7,7,0,0,0,0,-1,0,0,0,7,7,-1,-1,-1,-1,0,-1,-1,-1,7,7,-4,-2,-3,-5,-9,-3,-2,-4,]
27 | next element: [4,2,3,0,9,3,2,4,7,7,1,1,1,5,1,1,1,1,7,7,0,0,0,0,0,0,0,0,7,7,0,0,0,1,0,0,0,0,7,7,0,0,0,0,0,0,0,0,7,7,0,0,0,0,-1,0,0,0,7,7,-1,-1,-1,-1,0,-1,-1,-1,7,7,-4,-2,-3,-5,-9,-3,-2,-4,]
28 | next element: [4,2,3,0,9,3,2,4,7,7,1,1,1,5,1,1,1,1,7,7,0,0,0,0,0,0,0,0,7,7,0,0,0,1,0,0,0,0,7,7,0,0,0,0,0,0,0,0,7,7,0,0,0,0,-1,-5,0,0,7,7,-1,-1,-1,-1,0,-1,-1,-1,7,7,-4,-2,-3,0,-9,-3,-2,-4,]
29 | next element: [4,2,3,0,9,3,0,4,7,7,1,1,1,5,1,1,1,1,7,7,0,0,0,0,0,2,0,0,7,7,0,0,0,1,0,0,0,0,7,7,0,0,0,0,0,0,0,0,7,7,0,0,0,0,-1,-5,0,0,7,7,-1,-1,-1,-1,0,-1,-1,-1,7,7,-4,-2,-3,0,-9,-3,-2,-4,]
30 | next element: [4,2,3,0,9,3,0,4,7,7,1,1,1,5,1,1,1,1,7,7,0,0,0,0,0,2,0,0,7,7,0,0,0,1,0,0,0,0,7,7,-1,0,0,0,0,0,0,0,7,7,0,0,0,0,-1,-5,0,0,7,7,0,-1,-1,-1,0,-1,-1,-1,7,7,-4,-2,-3,0,-9,-3,-2,-4,]
31 | Board position:
32 |
33 | BR BN BB BQ BK BB BN BR
34 | BP BP BP BP . BP BP BP
35 | . . BP . .
36 | . . . .
37 | . WP . .
38 | . . . .
39 | WP WP WP . WP WP WP WP
40 | WR WN WB WQ WK WB WN WR
41 | Your move:
42 | enter a move like 'd2d4' or 'oo'
43 | s=g1f3
44 | From 28, to 47
45 | Board position:
46 |
47 | BR BN BB BQ BK BB BN BR
48 | BP BP BP BP . BP BP BP
49 | . . BP . .
50 | . . . .
51 | . WP . .
52 | . . . WN .
53 | WP WP WP . WP WP WP WP
54 | WR WN WB WQ WK WB . WR
55 | next element: 6.1999993
56 | next element: [4,2,3,5,9,3,0,4,7,7,1,1,1,0,1,1,1,1,7,7,0,0,0,0,0,2,0,0,7,7,0,0,0,1,0,0,0,0,7,7,0,0,0,0,0,0,0,0,7,7,0,0,0,0,-1,-5,0,0,7,7,-1,-1,-1,-1,0,-1,-1,-1,7,7,-4,-2,-3,0,-9,-3,-2,-4,]
57 | next element: [4,2,3,0,9,3,0,4,7,7,1,1,1,0,1,1,1,1,7,7,0,0,0,5,0,2,0,0,7,7,0,0,0,1,0,0,0,0,7,7,0,0,0,0,0,0,0,0,7,7,0,0,0,0,-1,-5,0,0,7,7,-1,-1,-1,-1,0,-1,-1,-1,7,7,-4,-2,-3,0,-9,-3,-2,-4,]
58 | next element: [4,2,3,0,9,3,0,4,7,7,1,1,1,0,1,1,1,1,7,7,0,0,0,5,0,2,0,0,7,7,0,-3,0,1,0,0,0,0,7,7,0,0,0,0,0,0,0,0,7,7,0,0,0,0,-1,-5,0,0,7,7,-1,-1,-1,-1,0,-1,-1,-1,7,7,-4,-2,-3,0,-9,0,-2,-4,]
59 | next element: [4,2,3,0,9,3,0,4,7,7,1,1,0,0,1,1,1,1,7,7,0,0,1,5,0,2,0,0,7,7,0,-3,0,1,0,0,0,0,7,7,0,0,0,0,0,0,0,0,7,7,0,0,0,0,-1,-5,0,0,7,7,-1,-1,-1,-1,0,-1,-1,-1,7,7,-4,-2,-3,0,-9,0,-2,-4,]
60 | next element: [4,2,3,0,9,3,0,4,7,7,1,1,0,0,1,1,1,1,7,7,0,0,1,5,0,2,0,0,7,7,0,-3,0,1,0,0,0,-5,7,7,0,0,0,0,0,0,0,0,7,7,0,0,0,0,-1,0,0,0,7,7,-1,-1,-1,-1,0,-1,-1,-1,7,7,-4,-2,-3,0,-9,0,-2,-4,]
61 | Board position:
62 |
63 | BR BN BB . BK BB BN BR
64 | BP BP BP BP . BP BP BP
65 | . . BP BQ .
66 | . . . .
67 | . WP . .
68 | . . . WN .
69 | WP WP WP . WP WP WP WP
70 | WR WN WB WQ WK WB . WR
71 | Your move:
72 | enter a move like 'd2d4' or 'oo'
73 | java.lang.NullPointerException
--------------------------------------------------------------------------------
/search/src/main/java/search/graph/AbstractGraphSearch.java:
--------------------------------------------------------------------------------
1 | package search.graph;
2 |
3 | /**
4 | * Graph search
5 | *
6 | *
7 | * Copyright 1998-2012 by Mark Watson. All rights reserved.
8 | *
9 | * This software is can be used under either of the following licenses:
10 | *
11 | * 1. LGPL v3
12 | * 2. Apache 2
13 | *
14 | */
15 | abstract public class AbstractGraphSearch {
16 |
17 | public void addNode(String name, int x, int y) {
18 | System.out.println("Adding node: " + name + ", " + x + ", " + y);
19 | nodeNames[numNodes] = name;
20 | node_x[numNodes] = x;
21 | node_y[numNodes] = y;
22 | numNodes++;
23 | }
24 |
25 | public int getNumNodes() { return numNodes; }
26 | public int getNumLinks() { return numLinks; }
27 |
28 | public String getNodeName(int index) {
29 | try {
30 | return nodeNames[index];
31 | } catch (Exception e) {
32 | System.out.println("Error in getNodeName: " + e);
33 | }
34 | return "no name"; // error condition
35 | }
36 |
37 | public int getNodeX(int index) {
38 | try {
39 | return node_x[index];
40 | } catch (Exception e) {
41 | System.out.println("Error in getNodePosition: " + e);
42 | }
43 | return 0; // error condition
44 | }
45 |
46 |
47 | public int getNodeY(int index) {
48 | try {
49 | return node_y[index];
50 | } catch (Exception e) {
51 | System.out.println("Error in getNodePosition: " + e);
52 | }
53 | return 0; // error condition
54 | }
55 |
56 | public int getLink1(int index) {
57 | return link_1[index];
58 | }
59 |
60 | public int getLink2(int index) {
61 | return link_2[index];
62 | }
63 |
64 | public void addLink(int node1, int node2) {
65 | link_1[numLinks] = node1;
66 | link_2[numLinks] = node2;
67 | int dist_squared =
68 | (node_x[node1] - node_x[node2]) * (node_x[node1] - node_x[node2]) +
69 | (node_y[node1] - node_y[node2]) * (node_y[node1] - node_y[node2]);
70 | lengths[numLinks] = (int)Math.sqrt(dist_squared);
71 | numLinks++;
72 | }
73 |
74 | public void addLink(String name1, String name2) {
75 | int index1 = -1, index2 = -1;
76 | for (int i=0; i
7 | * Copyright 1998-2012 by Mark Watson. All rights reserved.
8 | *
9 | * This software is can be used under either of the following licenses:
10 | *
11 | * 1. LGPL v3
12 | * 2. Apache 2
13 | *
14 | */
15 | public class BreadthFirstSearch extends AbstractGraphSearch {
16 |
17 | /** findPath - abstract method in super class */
18 | public int [] findPath(int start_node, int goal_node) { // return an array of node indices
19 | System.out.println("Entered BreadthFirstSearch.findPath(" +
20 | start_node + ", " + goal_node + ")");
21 | // data structures for depth first search:
22 | boolean [] alreadyVisitedFlag = new boolean[numNodes];
23 | int [] predecessor = new int[numNodes];
24 | IntQueue queue = new IntQueue(numNodes + 2);
25 |
26 | for (int i=0; i= (len - 1)) {
78 | tail = 0;
79 | } else {
80 | tail++;
81 | }
82 | }
83 | public int removeFromQueue() {
84 | int ret = queue[head];
85 | if (head >= (len - 1)) {
86 | head = 0;
87 | } else {
88 | head++;
89 | }
90 | return ret;
91 | }
92 | public boolean isEmpty() {
93 | return head == (tail + 1);
94 | }
95 | public int peekAtFrontOfQueue() {
96 | return queue[head];
97 | }
98 | }
99 |
100 | protected int [] connected_nodes(int node) {
101 | int [] ret = new int[AbstractGraphSearch.MAX];
102 | int num = 0;
103 |
104 | for (int n=0; n
7 | * Copyright 1998-2012 by Mark Watson. All rights reserved.
8 | *
9 | * This software is can be used under either of the following licenses:
10 | *
11 | * 1. LGPL v3
12 | * 2. Apache 2
13 | *
14 | */
15 | public class DepthFirstSearch extends AbstractGraphSearch {
16 |
17 | /** findPath - abstract method in super class */
18 | public int [] findPath(int start_node, int goal_node) { // return an array of node indices
19 | System.out.println("Entered DepthFirstSearch.findPath(" +
20 | start_node + ", " + goal_node + ")");
21 | path[0] = start_node;
22 | return findPathHelper(path, 1, goal_node);
23 | }
24 |
25 | public int [] findPathHelper(int [] path, int num_path, int goal_node) {
26 | System.out.println("Entered DepthFirstSearch.findPathHelper(...," +
27 | num_path + ", " + goal_node + ")");
28 | if (goal_node == path[num_path - 1]) {
29 | int [] ret = new int[num_path];
30 | for (int i=0; i
7 | * Copyright 1998-2012 by Mark Watson. All rights reserved.
8 | *
9 | * This software is can be used under either of the following licenses:
10 | *
11 | * 1. LGPL v3
12 | * 2. Apache 2
13 | *
14 | */
15 | public class AbstractSearchEngine {
16 | public AbstractSearchEngine(int width, int height) {
17 | maze = new Maze(width, height);
18 | initSearch();
19 | }
20 | public Maze getMaze() { return maze; }
21 | protected Maze maze;
22 | /**
23 | * We will use the Java type Location (fields width and height will
24 | * encode the coordinates in x and y directions) for the search path:
25 | */
26 | protected Location [] searchPath = null;
27 | protected int pathCount;
28 | protected int maxDepth;
29 | protected Location startLoc, goalLoc, currentLoc;
30 | protected boolean isSearching = true;
31 |
32 | protected void initSearch() {
33 | if (searchPath == null) {
34 | searchPath = new Location[1000];
35 | for (int i=0; i<1000; i++) {
36 | searchPath[i] = new Location();
37 | }
38 | }
39 | pathCount = 0;
40 | startLoc = maze.startLoc;
41 | currentLoc = startLoc;
42 | goalLoc = maze.goalLoc;
43 | searchPath[pathCount++] = currentLoc;
44 | }
45 |
46 | protected boolean equals(Location d1, Location d2) {
47 | return d1.x == d2.x && d1.y == d2.y;
48 | }
49 |
50 | public Location [] getPath() {
51 | Location [] ret = new Location[maxDepth];
52 | for (int i=0; i
7 | * Copyright 1998-2012 by Mark Watson. All rights reserved.
8 | *
9 | * This software is can be used under either of the following licenses:
10 | *
11 | * 1. LGPL v3
12 | * 2. Apache 2
13 | *
14 | */
15 | public class DepthFirstSearchEngine extends AbstractSearchEngine {
16 | public DepthFirstSearchEngine(int width, int height) {
17 | super(width, height);
18 | iterateSearch(startLoc, 1);
19 | }
20 |
21 | private void iterateSearch(Location loc, int depth) {
22 | if (isSearching == false) return;
23 | maze.setValue(loc.x, loc.y, (short)depth);
24 | Location [] moves = getPossibleMoves(loc);
25 | for (int i=0; i<4; i++) {
26 | if (moves[i] == null) break; // out of possible moves from this location
27 | searchPath[depth] = moves[i];
28 | if (equals(moves[i], goalLoc)) {
29 | System.out.println("Found the goal at " + moves[i].x +
30 | ", " + moves[i].y);
31 | isSearching = false;
32 | maxDepth = depth;
33 | return;
34 | } else {
35 | iterateSearch(moves[i], depth + 1);
36 | if (isSearching == false) return;
37 | }
38 | }
39 | return;
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/search/src/main/java/search/maze/Location.java:
--------------------------------------------------------------------------------
1 | package search.maze;
2 |
3 | /**
4 | * Copyright Mark Watson 2008-2012. All Rights Reserved.
5 | */
6 |
7 | public class Location {
8 | int x, y;
9 | public Location(int x, int y) { this.x = x; this.y = y; }
10 |
11 | public Location() {
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/search/src/main/java/search/maze/Maze.java:
--------------------------------------------------------------------------------
1 | package search.maze;
2 |
3 | /**
4 | * Class Maze - class for representing search space as a two-Locational maze
5 | */
6 | public class Maze {
7 | public static short OBSTICLE = -1;
8 | public static short START_LOC_VALUE = -2;
9 | public static short GOAL_LOC_VALUE = -3;
10 | private int width = 0;
11 | private int height = 0;
12 | public Location startLoc = new Location();
13 | public Location goalLoc = new Location();
14 | /**
15 | * The maze (or search space) data is stored as a short integer rather than
16 | * as a boolean so that bread-first style searches can use the array to store
17 | * search depth. A value of -1 indicates a barrier in the maze.
18 | */
19 | private short [][]maze;
20 | public Maze(int width, int height) {
21 | System.out.println("New maze of size " + width + " by " + height);
22 | this.width = width;
23 | this.height = height;
24 | maze = new short[width+2][height+2];
25 | for (int i=0; i .
2 | .
3 | .
4 | .
5 | .
6 | .
7 | .
8 | "Her Majesty's Secret Service" .
9 | "HMSS" .
10 | .
11 | .
12 | .
13 | .
14 | .
15 | "M" .
16 | .
17 | .
18 | .
19 | .
20 | "Q" .
21 | .
22 | .
23 | .
24 | "James Bond" .
25 | .
26 | .
27 | .
28 | "Darko Kerim" .
29 | .
30 |
--------------------------------------------------------------------------------
/semantic_web_apache_jena/data/inferencing_1.txt:
--------------------------------------------------------------------------------
1 | # triples to add to a new repository:
2 |
3 | @prefix rdf: .
4 | @prefix rdfs: .
5 | @prefix kb: .
6 | @prefix person: .
7 |
8 | kb:Sibling rdfs:subClassOf rdfs:Class .
9 |
10 | kb:Brother rdfs:subClassOf kb:Sibling .
11 |
12 | person:mark rdf:type kb:Brother .
13 |
14 | # sample queries to demonstrate inferencing:
15 |
16 | SELECT DISTINCT ?s
17 | WHERE { ?s rdf:type kb:Brother }
18 |
19 | SELECT DISTINCT ?s
20 | WHERE { ?s rdf:type kb:Sibling }
--------------------------------------------------------------------------------
/semantic_web_apache_jena/data/inferencing_2.txt:
--------------------------------------------------------------------------------
1 | # triples to add to a new repository:
2 |
3 | @prefix rdf: .
4 | @prefix rdfs: .
5 | @prefix kb: .
6 | @prefix person: .
7 |
8 | kb:mother rdfs:subPropertyOf kb:parent .
9 | kb:father rdfs:subPropertyOf kb:parent .
10 |
11 | person:ron kb:father person:anthony .
12 |
13 | # sample queries to demonstrate inferencing:
14 |
15 | SELECT DISTINCT ?s ?o
16 | WHERE { ?s kb:father ?o }
17 |
18 | SELECT DISTINCT ?s ?o
19 | WHERE { ?s kb:parent ?o }
--------------------------------------------------------------------------------
/semantic_web_apache_jena/data/inferencing_3.txt:
--------------------------------------------------------------------------------
1 | # triples to add to a new repository:
2 |
3 | @prefix rdf: .
4 | @prefix rdfs: .
5 | @prefix kb: .
6 | @prefix person: .
7 |
8 | kb:mother rdfs:domain kb:Female .
9 | kb:father rdfs:domain kb:Male .
10 |
11 | person:kate rdf:type kb:Female .
12 | person:kate kb:father person:bill .
13 |
14 |
--------------------------------------------------------------------------------
/semantic_web_apache_jena/data/inferencing_4.txt:
--------------------------------------------------------------------------------
1 | # triples to add to a new repository:
2 |
3 | @prefix rdf: .
4 | @prefix rdfs: .
5 | @prefix kb: .
6 | @prefix person: .
7 |
8 | kb:mother rdfs:domain kb:Female .
9 | kb:father rdfs:domain kb:Male .
10 |
11 | person:mary kb:mother person:susan .
--------------------------------------------------------------------------------
/semantic_web_apache_jena/data/news.n3:
--------------------------------------------------------------------------------
1 | @prefix kb: .
2 | @prefix rdfs: .
3 |
4 | kb:containsCity rdfs:subPropertyOf kb:containsPlace .
5 |
6 | kb:containsCountry rdfs:subPropertyOf kb:containsPlace .
7 |
8 | kb:containsState rdfs:subPropertyOf kb:containsPlace .
9 |
10 | kb:containsCity "Burlington" , "Denver" , "St. Paul" , "Chicago" , "Quincy" , "CHICAGO" , "Iowa City" ;
11 | kb:containsRegion "U.S. Midwest" , "Midwest" ;
12 | kb:containsCountry "United States" , "Japan" ;
13 | kb:containsState "Minnesota" , "Illinois" , "Mississippi" , "Iowa" ;
14 | kb:containsOrganization "National Guard" , "U.S. Department of Agriculture" , "White House" , "Chicago Board of Trade" , "Department of Transportation" ;
15 | kb:containsPerson "Dena Gray-Fisher" , "Donald Miller" , "Glenn Hollander" , "Rich Feltes" , "George W. Bush" ;
16 | kb:containsIndustryTerm "food inflation" , "food" , "finance ministers" , "oil" .
17 |
18 | kb:containsCity "Washington" , "FLINT" , "Baghdad" , "Arlington" , "Flint" ;
19 | kb:containsCountry "United States" , "Afghanistan" , "Iraq" ;
20 | kb:containsState "Illinois" , "Virginia" , "Arizona" , "Michigan" ;
21 | kb:containsOrganization "White House" , "Obama administration" , "Iraqi government" ;
22 | kb:containsPerson "David Petraeus" , "John McCain" , "Hoshiyar Zebari" , "Barack Obama" , "George W. Bush" , "Carly Fiorina" ;
23 | kb:containsIndustryTerm "oil prices" .
24 |
25 | kb:containsCity "WASHINGTON" ;
26 | kb:containsCountry "United States" , "Pakistan" , "Islamic Republic of Iran" ;
27 | kb:containsState "Maryland" ;
28 | kb:containsOrganization "University of Maryland" , "United Nations" ;
29 | kb:containsPerson "Ban Ki-moon" , "Gordon Brown" , "Hu Jintao" , "George W. Bush" , "Pervez Musharraf" , "Vladimir Putin" , "Steven Kull" , "Mahmoud Ahmadinejad" .
30 |
31 | kb:containsCity "Sao Paulo" , "Kuala Lumpur" ;
32 | kb:containsRegion "Midwest" ;
33 | kb:containsCountry "United States" , "Britain" , "Saudi Arabia" , "Spain" , "Italy" , "India" , "France" , "Canada" , "Russia" , "Germany" , "China" , "Japan" , "South Korea" ;
34 | kb:containsOrganization "Federal Reserve Bank" , "European Union" , "European Central Bank" , "European Commission" ;
35 | kb:containsPerson "Lee Myung-bak" , "Rajat Nag" , "Luiz Inacio Lula da Silva" , "Jeffrey Lacker" ;
36 | kb:containsCompany "Development Bank Managing" , "Reuters" , "Richmond Federal Reserve Bank" ;
37 | kb:containsIndustryTerm "central bank" , "food" , "energy costs" , "finance ministers" , "crude oil prices" , "oil prices" , "oil shock" , "food prices" , "Finance ministers" , "Oil prices" , "oil" .
38 |
--------------------------------------------------------------------------------
/semantic_web_apache_jena/data/rdfs_business.n3:
--------------------------------------------------------------------------------
1 | @prefix : .
2 | @prefix owl: .
3 | @prefix rdfs: .
4 | @prefix rdfs_sample_1: .
5 | @prefix xsd: .
6 |
7 | a owl:Ontology .
8 |
9 | :FreshVeggies a :Business .
10 | :HanksHardware a :Business .
11 | :MarkWatsonSoftware a :Business .
12 | :Amazon a :Business .
13 | :MarkWatsonSoftware :Customer :Amazon .
14 |
15 | :Business a owl:Class;
16 | rdfs:label "Business";
17 | rdfs:subClassOf :Organization .
18 |
19 | :Customer a owl:Class;
20 | rdfs:label "Customer";
21 | rdfs:subClassOf :Business .
22 |
23 | :Organization a owl:Class;
24 | rdfs:label "Organization" .
25 |
26 | :organizationName a owl:ObjectProperty;
27 | rdfs:domain :Organization;
28 | rdfs:range xsd:stringstring .
29 |
30 | xsd:stringstring a owl:Class .
31 |
--------------------------------------------------------------------------------
/semantic_web_apache_jena/data/rdfs_business.nt:
--------------------------------------------------------------------------------
1 | .
2 |
3 | .
4 |
5 | "Business" .
6 |
7 | .
8 |
9 | .
10 |
11 | "Customer" .
12 |
13 | .
14 |
15 | .
16 |
17 | .
18 |
19 | .
20 |
21 | .
22 |
23 | .
24 |
25 | "Organization" .
26 |
27 | .
28 |
29 | .
30 |
31 | .
32 |
33 | .
34 |
35 |
--------------------------------------------------------------------------------
/semantic_web_apache_jena/data/rdfs_sample_1.n3:
--------------------------------------------------------------------------------
1 | @prefix : .
2 | @prefix kb: .
3 | @prefix rdf: .
4 |
5 | kb:Business a :Class;
6 | :label "Business";
7 | :subClassOf kb:Organization .
8 |
9 | kb:Customer a :Class;
10 | :label "Customer";
11 | :subClassOf kb:Business .
12 |
13 | kb:Organization a :Class;
14 | :label "Organization" .
15 |
16 | kb:organizationName a rdf:Property;
17 | :domain kb:Organization;
18 | :range .
19 |
--------------------------------------------------------------------------------
/semantic_web_apache_jena/data/rdfs_sample_1.owl:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
--------------------------------------------------------------------------------
/semantic_web_apache_jena/data/rdfs_sample_2.n3:
--------------------------------------------------------------------------------
1 | #Processed by Id: cwm.py,v 1.197 2007/12/13 15:38:39 syosi Exp
2 | # using base file:///Users/markw/Documents/WORK/ruby_scripting_book/src/part2/data/zzzzz.rdf
3 |
4 | # Notation3 generation by
5 | # notation3.py,v 1.200 2007/12/11 21:18:08 syosi Exp
6 |
7 | # Base was: file:///Users/markw/Documents/WORK/ruby_scripting_book/src/part2/data/zzzzz.rdf
8 | @prefix : .
9 | @prefix owl: .
10 | @prefix rdfs: .
11 | @prefix rdfs_sample_1: .
12 | @prefix xsd: .
13 |
14 | a owl:Ontology .
15 |
16 | rdfs_sample_1:FreshVeggies a :Business .
17 |
18 | :Business a owl:Class;
19 | rdfs:label "Business";
20 | rdfs:subClassOf :Organization .
21 |
22 | :Customer a owl:Class;
23 | rdfs:label "Customer";
24 | rdfs:subClassOf :Business .
25 |
26 | :Organization a owl:Class;
27 | rdfs:label "Organization" .
28 |
29 | :organizationName a owl:ObjectProperty;
30 | rdfs:domain :Organization;
31 | rdfs:range xsd:stringstring .
32 |
33 | xsd:stringstring a owl:Class .
34 |
35 | #ENDS
36 |
--------------------------------------------------------------------------------
/semantic_web_apache_jena/data/rdfs_sample_2.owl:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
6 |
7 |
8 |
9 |
10 |
11 |
12 | ]>
13 |
14 |
15 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 | Business
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 | Customer
44 |
45 |
46 |
47 |
48 |
49 | Organization
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
--------------------------------------------------------------------------------
/semantic_web_apache_jena/data/sample_news.n3:
--------------------------------------------------------------------------------
1 | @prefix kb: .
2 |
3 | kb:oak_creek_flooding kb:storyType kb:disaster ;
4 | kb:summary "Oak Creek flooded last week affecting 5 businesses" ;
5 | kb:title "Oak Creek Flood" .
6 |
7 | kb:bear_mountain_fire kb:storyType kb:disaster ;
8 | kb:summary "The fire on Bear Mountain was caused by lightning" ;
9 | kb:title "Bear Mountain Fire" .
10 |
11 | kb:trout_season kb:storyType kb:sports , kb:recreation ;
12 | kb:summary "Fishing was good the first day of trout season" ;
13 | kb:title "Trout Season Starts" .
14 |
15 | kb:jc_basketball kb:storyType kb:sports ;
16 | kb:summary "Local JC Basketball team lost by 12 points last night" ;
17 | kb:title "Local JC Lost Last Night" .
18 |
--------------------------------------------------------------------------------
/semantic_web_apache_jena/data/sample_news.nt:
--------------------------------------------------------------------------------
1 | .
2 |
3 | "Oak Creek flooded last week affecting 5 businesses" .
4 |
5 | .
6 |
7 | "The fire on Bear Mountain was caused by lightening" .
8 |
9 | .
10 |
11 | .
12 |
13 | .
14 |
--------------------------------------------------------------------------------
/semantic_web_apache_jena/data/sparql_ask_test.txt:
--------------------------------------------------------------------------------
1 | PREFIX kb:
2 | ASK
3 | WHERE { ?article_uri ?predicate "Trout Season Starts" }
4 |
5 | PREFIX kb:
6 | ASK
7 | WHERE { ?article_uri kb:copyright ?copyright_value }
8 |
--------------------------------------------------------------------------------
/semantic_web_apache_jena/data/sparql_combine_rdfs_test.txt:
--------------------------------------------------------------------------------
1 | @prefix kb: .
2 | @prefix testnews: .
3 | @prefix rdfs: .
4 |
5 | kb:title rdfs:subPropertyOf testnews:title .
6 | testnews:title rdfs:subPropertyOf kb:title .
7 |
8 | kb:oak_creek_flooding kb:storyType kb:disaster ;
9 | kb:summary "Oak Creek flooded last week affecting 5 businesses" ;
10 | kb:title "Oak Creek Flood" .
11 |
12 | PREFIX kb:
13 | PREFIX kb:
14 |
15 | SELECT DISTINCT ?article_uri1 ?object
16 | WHERE {
17 | ?article_uri1 kb:title ?object .
18 | }
19 |
20 | SELECT DISTINCT ?article_uri1 ?object
21 | WHERE {
22 | ?article_uri1 testnews:title ?object .
23 | }
24 |
--------------------------------------------------------------------------------
/semantic_web_apache_jena/data/sparql_combine_test.txt:
--------------------------------------------------------------------------------
1 | @prefix kb: .
2 | @prefix testnews: .
3 | @prefix owl: .
4 |
5 | kb:title owl:sameAs testnews:title .
6 |
7 | kb:oak_creek_flooding kb:storyType kb:disaster ;
8 | kb:summary "Oak Creek flooded last week affecting 5 businesses" ;
9 | kb:title "Oak Creek Flood" .
10 |
11 | PREFIX kb:
12 | PREFIX kb:
13 |
14 | SELECT DISTINCT ?article_uri1 ?object
15 | WHERE {
16 | ?article_uri1 kb:title ?object .
17 | }
18 |
19 | # works if OWL inferencing is supported:
20 | SELECT DISTINCT ?article_uri1 ?object
21 | WHERE {
22 | ?article_uri1 testnews:title ?object .
23 | }
24 |
--------------------------------------------------------------------------------
/semantic_web_apache_jena/data/sparql_construct_test.txt:
--------------------------------------------------------------------------------
1 |
2 | PREFIX kb:
3 | CONSTRUCT { ?story_type kb:subject_of ?article1 . }
4 | WHERE {
5 | { ?article1 ?story_type kb:sports } UNION { ?article1 ?story_type kb:recreation } .
6 | }
7 |
8 | PREFIX kb:
9 | CONSTRUCT { ?story_type kb:subject_of ?article1 . }
10 | WHERE {
11 | { ?article1 ?story_type kb:sports } UNION { ?article1 ?story_type kb:recreation } .
12 | { ?article2 ?story_type kb:sports } UNION { ?article2 ?story_type kb:recreation } .
13 | FILTER (?article1 != ?article2)
14 | }
15 |
16 | PREFIX kb:
17 | CONSTRUCT { ?story_type kb:subject_of ?article1 . }
18 | WHERE {
19 | ?article1 ?story_type ?story_type_value .
20 | ?article2 ?story_type ?story_type_value .
21 | FILTER ((?article1 != ?article2) && ((?story_type_value = kb:sports) || (?story_type_value = kb:recreation)))
22 | }
23 |
24 | PREFIX kb:
25 | CONSTRUCT { ?story_type_value kb:subject_of ?article1 . }
26 | WHERE {
27 | ?article1 ?story_type ?story_type_value .
28 | ?article2 ?story_type ?story_type_value .
29 | FILTER ((?article1 != ?article2) && ((?story_type_value = kb:sports) || (?story_type_value = kb:recreation)))
30 | }
31 |
32 | PREFIX kb:
33 | CONSTRUCT { ?story_type_value kb:subject_of ?article1 .
34 | ?article2 kb:same_topic ?article1 .}
35 | WHERE {
36 | ?article1 ?story_type ?story_type_value .
37 | ?article2 ?story_type ?story_type_value .
38 | FILTER ((?article1 != ?article2) && ((?story_type_value = kb:sports) || (?story_type_value = kb:recreation)))
39 | }
40 |
--------------------------------------------------------------------------------
/semantic_web_apache_jena/data/sparql_describe_test.txt:
--------------------------------------------------------------------------------
1 | PREFIX kb:
2 | DESCRIBE ?article_uri
3 | WHERE { ?article_uri kb:title "Trout Season Starts" }
4 |
5 | PREFIX kb:
6 | DESCRIBE ?article_uri ?predicate
7 | WHERE { ?article_uri ?predicate "Trout Season Starts" }
8 |
--------------------------------------------------------------------------------
/semantic_web_apache_jena/data/sparql_select_test.txt:
--------------------------------------------------------------------------------
1 | @prefix kb: .
2 |
3 | kb:oak_creek_flooding kb:storyType kb:disaster ;
4 | kb:summary "Oak Creek flooded last week affecting 5 businesses" ;
5 | kb:title "Oak Creek Flood" .
6 |
7 | kb:bear_mountain_fire kb:storyType kb:disaster ;
8 | kb:summary "The fire on Bear Mountain was caused by lightning" ;
9 | kb:title "Bear Mountain Fire" .
10 |
11 | kb:trout_season kb:storyType kb:sports , kb:recreation ;
12 | kb:summary "Fishing was good the first day of trout season" ;
13 | kb:title "Trout Season Starts" .
14 |
15 | kb:jc_basketball kb:storyType kb:sports ;
16 | kb:summary "Local JC Basketball team lost by 12 points last night" ;
17 | kb:title "Local JC Lost Last Night" .
18 |
19 |
20 |
21 | PREFIX kb:
22 | SELECT DISTINCT ?article_uri1 ?article_uri2 ?predicate1 ?predicate2
23 | WHERE {
24 | ?article_uri1 ?predicate1 ?same_object .
25 | ?article_uri2 ?predicate2 ?same_object .
26 | FILTER (sameTerm(?predicate1, ?predicate2) && !sameTerm(?article_uri1, ?article_uri2)) .
27 | }
28 |
29 |
30 | PREFIX kb:
31 | SELECT DISTINCT ?article_uri1 ?article_uri2 ?predicate1 ?predicate2
32 | WHERE {
33 | ?article_uri1 ?predicate1 "Trout Season Starts" .
34 | ?article_uri2 ?predicate2 "Trout Season Starts" .
35 | FILTER (!sameTerm(?article_uri1, ?article_uri2)) .
36 | }
37 |
38 | PREFIX kb:
39 | SELECT DISTINCT ?article_uri1 ?article_uri2 ?predicate1 ?predicate2
40 | WHERE {
41 | ?article_uri1 ?predicate1 ?o1 .
42 | ?article_uri2 ?predicate2 ?o2 .
43 | FILTER (!sameTerm(?article_uri1, ?article_uri2) && sameTerm(?predicate1, ?predicate2)) .
44 | FILTER regex(?o1, "Season") .
45 | FILTER regex(?o2, "Season") .
46 | }
47 |
48 | PREFIX kb:
49 | SELECT ?article_uri ?title ?summary
50 | WHERE {
51 | { ?article_uri kb:storyType kb:sports } UNION { ?article_uri kb:storyType kb:recreation } .
52 | ?article_uri kb:title ?title .
53 | ?article_uri kb:summary ?summary .
54 | }
55 |
56 | PREFIX kb:
57 | SELECT DISTINCT ?article_uri ?title ?summary
58 | WHERE {
59 | { ?article_uri kb:storyType kb:sports } UNION { ?article_uri kb:storyType kb:recreation } .
60 | ?article_uri kb:title ?title .
61 | ?article_uri kb:summary ?summary .
62 | }
63 |
64 |
65 | # new triple, without a summary:
66 | kb:jc_bowling kb:storyType kb:sports ;
67 | kb:title "JC Bowling Team to Open Season" .
68 |
69 | PREFIX kb:
70 | SELECT DISTINCT ?title ?summary
71 | WHERE { ?article_uri kb:title ?title .
72 | OPTIONAL { ?article_uri kb:summary ?summary }
73 | }
74 |
75 | PREFIX kb:
76 | SELECT DISTINCT ?title ?summary ?page_count
77 | WHERE { ?article_uri kb:title ?title .
78 | OPTIONAL { ?article_uri kb:summary ?summary } .
79 | OPTIONAL { ?article_uri kb:page_count ?page_count . FILTER (?page_count > 1) } .
80 | }
81 |
82 |
83 |
84 |
--------------------------------------------------------------------------------
/semantic_web_apache_jena/jenaApisLog.log:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mark-watson/Java-AI-Book-Code/f62e1231f4cf2de4641ed01518334696d4d07ff0/semantic_web_apache_jena/jenaApisLog.log
--------------------------------------------------------------------------------
/semantic_web_apache_jena/pom.xml:
--------------------------------------------------------------------------------
1 |
3 | 4.0.0
4 | com.markwatson
5 | semanticweb
6 | 1.0.3-SNAPSHOT
7 |
8 | 11.0
9 | UTF-8
10 | UTF-8
11 |
12 |
13 |
14 |
15 |
16 | org.apache.jena
17 | apache-jena-libs
18 | 3.17.0
19 | pom
20 |
21 |
22 | org.apache.derby
23 | derby
24 | 10.17.1.0
25 |
26 |
27 | org.apache.derby
28 | derbytools
29 | 10.15.2.0
30 |
31 |
32 | org.apache.derby
33 | derbyclient
34 | 10.15.2.0
35 |
36 |
37 |
38 | commons-io
39 | commons-io
40 | 2.14.0
41 |
42 |
43 | junit
44 | junit
45 | 4.13.1
46 | test
47 |
48 |
49 | org.slf4j
50 | slf4j-api
51 | 1.7.30
52 |
53 |
54 | org.slf4j
55 | slf4j-log4j12
56 | 1.7.30
57 | test
58 |
59 |
60 |
61 |
62 |
63 | maven-compiler-plugin
64 | 3.8.0
65 |
66 | 11
67 |
68 |
69 |
70 | org.apache.maven.plugins
71 | maven-jar-plugin
72 | 3.1.2
73 |
74 |
75 |
76 | com.markwatson.semanticweb.JenaApis
77 |
78 |
79 |
80 |
81 |
82 |
83 | org.apache.maven.plugins
84 | maven-shade-plugin
85 | 2.4.3
86 |
87 |
88 | package
89 |
90 | shade
91 |
92 |
93 |
94 |
95 |
96 | com.markwatson.semanticweb.JenaApis
97 | 1.0
98 |
99 |
100 |
101 |
102 |
103 |
104 |
105 |
106 |
107 |
108 |
109 |
--------------------------------------------------------------------------------
/semantic_web_apache_jena/src/main/java/com/markwatson/semanticweb/Cache.java:
--------------------------------------------------------------------------------
1 | package com.markwatson.semanticweb;
2 |
3 | import java.sql.*;
4 |
5 | import org.apache.commons.lang3.SerializationUtils;
6 | import org.apache.derby.client.*;
7 | import java.util.ArrayList;
8 | import java.util.List;
9 | import java.util.Properties;
10 |
11 | public class Cache {
12 | public Cache() throws SQLException, ClassNotFoundException {
13 | Properties props = new Properties();
14 | String dbName = "./sparqlCache.db";
15 | conn = DriverManager.getConnection(protocol + dbName
16 | + ";create=true", props);
17 | conn.setAutoCommit(true);
18 | try {
19 | Statement s = conn.createStatement();
20 | s.execute("CREATE TABLE cache (query varchar(3000) PRIMARY KEY, result blob)");
21 | System.out.println("Created table 'cache'");
22 | } catch (Exception ex) {
23 | //System.out.println("Error (Cache()): " + ex.getMessage());
24 | //System.out.println("Table 'cache' already exists");
25 | }
26 | }
27 | public void saveQueryResultInCache (String query, byte [] result) {
28 | try {
29 | if (fetchResultFromCache(query) != null) {
30 | //System.out.println("Query is already in the cache");
31 | return;
32 | }
33 | Statement s = conn.createStatement();
34 | PreparedStatement ps = conn.prepareStatement(
35 | "insert into cache (query, result) values (?, ?)");
36 | ps.setString(1, query);
37 | ps.setBytes(2, result);
38 | ps.executeUpdate();
39 | } catch (Exception ex) {
40 | System.out.println("Error (saveQueryResultInCache): " + ex.getMessage());
41 | }
42 | }
43 |
44 | public byte [] fetchResultFromCache (String query) {
45 | try {
46 | Statement s = conn.createStatement();
47 | PreparedStatement ps = conn.prepareStatement(
48 | "select result from cache where query = ?");
49 | ps.setString(1, query);
50 | ResultSet rs = ps.executeQuery();
51 | if (!rs.next()) return null;
52 | return rs.getBytes(1);
53 | } catch (Exception ex) {
54 | System.out.println("Error (fetchResultFromCache): " + ex.getMessage());
55 | return null;
56 | }
57 | }
58 |
59 | private Connection conn = null;
60 | private String protocol = "jdbc:derby:";
61 |
62 | }
63 |
--------------------------------------------------------------------------------
/semantic_web_apache_jena/src/main/java/com/markwatson/semanticweb/QueryResult.java:
--------------------------------------------------------------------------------
1 | package com.markwatson.semanticweb;
2 |
3 | import java.io.Serializable;
4 | import java.util.ArrayList;
5 | import java.util.List;
6 |
7 | public class QueryResult implements Serializable {
8 | private QueryResult() { }
9 | public QueryResult(List variableList) {
10 | this.variableList = variableList;
11 | }
12 | public List variableList;
13 | public List> rows = new ArrayList();
14 | public String toString() {
15 | StringBuilder sb = new StringBuilder("[QueryResult vars:" + variableList + "\nRows:\n");
16 | for (List row : rows) {
17 | sb.append(" " + row + "\n");
18 | }
19 | return sb.toString();
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/semantic_web_apache_jena/src/main/resources/log4j.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
--------------------------------------------------------------------------------
/semantic_web_apache_jena/src/test/java/com/markwatson/semanticweb/JenaApisTest.java:
--------------------------------------------------------------------------------
1 | package com.markwatson.semanticweb;
2 |
3 | import junit.framework.Test;
4 | import junit.framework.TestCase;
5 | import junit.framework.TestSuite;
6 |
7 | public class JenaApisTest extends TestCase {
8 | /**
9 | * Create the test case
10 | *
11 | * @param testName name of the test case
12 | */
13 | public JenaApisTest(String testName)
14 | {
15 | super( testName );
16 | }
17 |
18 | /**
19 | * @return the suite of tests being tested
20 | */
21 | public static Test suite()
22 | {
23 | return new TestSuite( JenaApisTest.class );
24 | }
25 |
26 | /**
27 | * Test that is just for side effect printouts:
28 | */
29 | public void test1() throws Exception {
30 | JenaApis jenaApis = new JenaApis();
31 | // test remote SPARQL queries against DBPedia SPARQL endpoint
32 | QueryResult qrRemote = jenaApis.queryRemote(
33 | "https://dbpedia.org/sparql",
34 | "select ?p where { ?p . } limit 10");
35 | // "select distinct ?s { ?s ?p } LIMIT 10");
36 | System.out.println("qrRemote:" + qrRemote);
37 | System.out.println("Repeat query to test caching:");
38 | qrRemote = jenaApis.queryRemote(
39 | "https://dbpedia.org/sparql",
40 | "select distinct ?s { ?s ?p } LIMIT 10");
41 | System.out.println("qrRemote (hopefully from cache):" + qrRemote);
42 |
43 | jenaApis.loadRdfFile("data/rdfs_business.nt");
44 | jenaApis.loadRdfFile("data/sample_news.nt");
45 | jenaApis.loadRdfFile("data/sample_news.n3");
46 |
47 | QueryResult qr = jenaApis.query(
48 | "select ?s ?o where { ?s ?o } limit 15");
49 | System.out.println("qr:" + qr);
50 |
51 | jenaApis.saveModelToTurtleFormat("model_save.nt");
52 | jenaApis.saveModelToN3Format("model_save.n3");
53 | }
54 |
55 | /**
56 | * Test that is just for side effect printouts:
57 | */
58 | public void testOwlReasoning() throws Exception {
59 | JenaApis jenaApis = new JenaApis();
60 | jenaApis.loadRdfFile("data/news.n3");
61 |
62 | QueryResult qr = jenaApis.query(
63 | "prefix kb: \n" +
64 | "select ?s ?o where { ?s kb:containsCity ?o } ");
65 | System.out.println("qr:" + qr);
66 |
67 | qr = jenaApis.query(
68 | "prefix kb: \n" +
69 | "select ?s ?o where { ?s kb:containsPlace ?o }");
70 | System.out.println("qr:" + qr);
71 |
72 | qr = jenaApis.query( // count for each place name
73 | "prefix kb: \n" +
74 | "select ?o (count(*) as ?count) where { ?s kb:containsPlace ?o } " +
75 | "group by ?o");
76 | System.out.println("qr:" + qr);
77 | }
78 |
79 | }
--------------------------------------------------------------------------------