cogent icon indicating copy to clipboard operation
cogent copied to clipboard

Automatically identify whether the AI prompt content is a string or a code

Open ddkwork opened this issue 11 months ago • 1 comments

Describe the feature

In addition, we need to use MD rendering and highlight syntax when the input prompt content is a block of code, and we need a scanning algorithm to automatically identify which programming language it is in order to set the highlight color

Relevant code

No response

ddkwork avatar Mar 10 '24 08:03 ddkwork

Yes, I will implement good MD code highlighting and rendering soon.

kkoreilly avatar Mar 10 '24 17:03 kkoreilly


package main

import (
	"fmt"
	"regexp"
)

func extractCodeBlocks(text string) {
	patterns := map[string]string{
		"Go":     `\bfunc\b|\bpackage\b|\bimport\b`,
		"C++":    `\bclass\b|\bint\b|\bvoid\b`,
		"Python": `\bdef\b|\bimport\b|\bfrom\b`,
		"Rust":   `\bfn\b|\buse\b|\bmod\b`,
	}

	for lang, pattern := range patterns {
		re := regexp.MustCompile(pattern)
		matches := re.FindAllStringIndex(text, -1)
		if len(matches) > 0 {
			fmt.Printf("%s code blocks found:\n", lang)
			for _, match := range matches {
				fmt.Printf("Start line: %d, End line: %d\n", getLineNumber(text, match[0]), getLineNumber(text, match[1]))
			}
		}
	}
}

func getLineNumber(text string, index int) int {
	lines := 1
	for i := 0; i < index; i++ {
		if text[i] == '\n' {
			lines++
		}
	}
	return lines
}

func main() {
	text := `
        package main

        import (
            "fmt"
        )

        func main() {
            fmt.Println("Hello, world!")
        }

        // C++ code block
        class MyClass {
            int val;
        };

        # Python code block
        def hello():
            print("Hello, world!")

        // Rust code block
        pub fn hello() {
            println!("Hello, world!");
        }
    `
	extractCodeBlocks(text)
}

ddkwork avatar Mar 16 '24 16:03 ddkwork

Just to clarify, the issue is not that we need to magically detect the language of the code blocks. It tells us the language of the code blocks, and I just need to connect our markdown renderer with our syntax highlighter to achieve the appropriate highlighting.

kkoreilly avatar Mar 16 '24 17:03 kkoreilly