kg

package
v0.10.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 22, 2026 License: Apache-2.0 Imports: 10 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ConvertCmd = &cobra.Command{
	Use:   "convert",
	Short: "Convert between knowledge graph formats (e.g., TTL to NQ)",
	Long: `Convert between knowledge graph formats.

Supported inputs: .ttl (Turtle, via the knowledge-rdf parser), .nq
(N-Quads), .nt (N-Triples), .jsonld (JSON-LD). Supported outputs:
nquads (default), ntriples, jsonld.`,
	RunE: func(cmd *cobra.Command, args []string) error {

		f, err := os.Open(inFile)
		if err != nil {
			return fmt.Errorf("failed to open input file: %w", err)
		}
		defer f.Close()

		format := inFmt
		if format == "" {
			ext := filepath.Ext(inFile)
			switch ext {
			case ".ttl":

				format = "turtle"
			case ".nq":
				format = "nquads"
			case ".nt":
				format = "ntriples"
			case ".jsonld":
				format = "jsonld"
			default:
				return fmt.Errorf("unknown input extension %s, please specify --from (supported: .ttl, .nq, .nt, .jsonld)", ext)
			}
		}

		outF, err := os.Create(outFile)
		if err != nil {
			return fmt.Errorf("failed to create output file: %w", err)
		}
		defer outF.Close()

		// 4. Encoder
		var w quad.Writer
		switch outFmt {
		case "nquads", "ntriples":

			w = nquads.NewWriter(outF)
		case "jsonld":
			w = jsonld.NewWriter(outF)
		default:
			return fmt.Errorf("unsupported output format: %s", outFmt)
		}

		defer func() {
			if c, ok := w.(io.Closer); ok {
				c.Close()
			}
		}()

		// 5. Copy
		var n int
		if format == "turtle" || format == "ttl" {
			n, err = copyTurtle(w)
		} else {
			var r quad.Reader
			switch format {
			case "nquads", "ntriples":

				r = nquads.NewReader(f, false)
			case "jsonld":
				r = jsonld.NewReader(f)
			default:
				return fmt.Errorf("unsupported input format: %s (supported: turtle, nquads, ntriples, jsonld)", format)
			}
			n, err = quad.Copy(w, r)
		}
		if err != nil {
			return fmt.Errorf("conversion failed: %w", err)
		}

		cmd.Printf("Converted %d quads to %s\n", n, outFile)
		return nil
	},
}
View Source
var KgCmd = &cobra.Command{
	Use:   "kg",
	Short: "Knowledge Graph utilities",
	Run: func(cmd *cobra.Command, args []string) {
		cmd.Help()
	},
}

Functions

func AddCommands

func AddCommands(rootCmd *cobra.Command)

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL