Skip to content

Instantly share code, notes, and snippets.

@willprice76
willprice76 / SparkNlpApplication.java
Last active August 27, 2020 09:12
SpringBoot application class
package org.example.sparknlp;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SparkNlpApplication {
public static void main(String[] args) {
SpringApplication.run(SparkNlpApplication.class, args);
}
@willprice76
willprice76 / pom.xml
Last active August 26, 2020 12:38
Basic Spring Boot Web / Spark NLP POM
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
@willprice76
willprice76 / recipes.tsx
Created May 1, 2020 14:10
Using multiple reducers for different lifecycles
//component state
const [{ data:initialData, isBusy:isLoading, hasError:loadingFailed, errorMessage:loadingError }, dispatch] = useReducer(
createActionReducer<Recipe[]>(),
{ isBusy: true, hasError: false }
);
const [{ data:updatedData, isBusy:isUpdating, hasError:updateFailed, errorMessage:updateError }, dispatch] = useReducer(
createActionReducer<Recipe[]>(),
{ isBusy: false, hasError: false }
);
@willprice76
willprice76 / recipes.tsx
Created May 1, 2020 14:07
Updated to use generic reducer
//component state
const [{ data, isBusy, hasError, errorMessage }, dispatch] = useReducer(
createActionReducer<Recipe[]>(),
{ isBusy: true, hasError: false }
);
@willprice76
willprice76 / generic-reducer.ts
Last active May 1, 2020 14:03
Generic React reducer for all component lifecycles
export type ActionState<T> = {
data?: T;
isBusy: boolean;
hasError: boolean;
errorMessage?: string;
};
export type ActionResult<T> =
| { type: "busy" }
| { type: "success"; result: T }
@willprice76
willprice76 / recipe-reducer.ts
Last active May 1, 2020 13:53
Non-generic reducer for load lifecycle
import { Recipe } from "../api";
export type LoadRecipesActionState = {
recipe?: Recipe[];
isLoading: boolean;
loadingFailed: boolean;
loadingError?: string;
};
export type LoadRecipesActionResult =
@willprice76
willprice76 / recipes.tsx
Last active May 1, 2020 13:31
Managing React component state with useReducer
//component state
const [{ recipes, isLoading, loadingFailed, loadingError }, dispatch] = useReducer(
createLoadRecipesReducer(), { isLoading: true, loadingFailed: false }
);
//load data
useEffect(() => {
dispatch({ type: "busy" });
api.getRecipes().then(response => {
dispatch({ type: "success", result: response.data });
@willprice76
willprice76 / recipes.old.tsx
Last active May 1, 2020 20:14
Managing React component state with useState
export const RecipeList = () => {
//component state
const [isLoading, setIsLoading] = useState(true);
const [loadingFailed, setLoadingFailed] = useState(false);
const [loadingError, setLoadingError] = useState("");
const [recipes, setRecipes] = useState<Recipe[]>();
//load data
useEffect(() => {
@willprice76
willprice76 / LoadPipelineTest.java
Created February 6, 2020 17:09
Test class for loading Spark NLP pipeline from local FS
package test;
import org.apache.spark.ml.PipelineModel;
import org.apache.spark.sql.SparkSession;
import org.junit.jupiter.api.Test;
import java.nio.file.Paths;
public class LoadPipelineTest {
@willprice76
willprice76 / pom.xml
Created February 6, 2020 17:07
Minimal pom.xml for Loading Spark NLP Pipeline in Java
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>test-spark-nlp</groupId>
<artifactId>test</artifactId>
<version>1.0.0</version>