All Downloads are FREE. Search and download functionalities are using the official Maven repository.

dev.langchain4j.image_to_diagram.DiagramOutputParser Maven / Gradle / Ivy

There is a newer version: 1.0-20241113
Show newest version
package dev.langchain4j.image_to_diagram;

import com.google.gson.Gson;
import java.util.regex.Pattern;

public class DiagramOutputParser {
    public Diagram.Element parse(String s) {
        // String pattern = "```json\n(.*?)\n```";
        String pattern = "```json\n(.*?)\n```";

        // Create a Pattern object
        Pattern jsonPattern = Pattern.compile(pattern, Pattern.DOTALL | Pattern.MULTILINE);

        // Create a Matcher object
        java.util.regex.Matcher matcher = jsonPattern.matcher(s);

        // Check if a match is found
        if (!matcher.find()) {
            throw new IllegalArgumentException("no diagram provided! in text\n" + s);
        }

        Gson gson = new Gson();
        return gson.fromJson(matcher.group(1), Diagram.Element.class);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy