Created
August 3, 2023 00:32
-
-
Save jamesmurdza/a51b13869c4917eef9beb1b895af1bfc to your computer and use it in GitHub Desktop.
Detect the programming language from a snippet
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function detectPopularLanguages(codeSnippet: string): string[] { | |
| const languageKeywords: { [key: string]: RegExp } = { | |
| JavaScript: /\b(?:var|let|const|function|if|else|for|while|switch|case|break|return)\b/g, | |
| Python: /\b(?:def|if|elif|else|for|while|break|continue|return)\b/g, | |
| Java: /\b(?:public|private|protected|class|void|static|if|else|for|while|switch|case|break|return)\b/g, | |
| CSharp: /\b(?:public|private|protected|class|void|static|if|else|for|while|switch|case|break|return)\b/g, | |
| C: /\b(?:int|float|char|void|if|else|for|while|switch|case|break|return)\b/g, | |
| Ruby: /\b(?:def|if|else|elsif|unless|end|for|while|case|when)\b/g, | |
| PHP: /\b(?:public|private|protected|function|if|else|for|while|switch|case|break|return)\b/g, | |
| Swift: /\b(?:class|func|var|let|if|else|for|while|switch|case|break|return)\b/g, | |
| TypeScript: /\b(?:let|const|function|interface|class|if|else|for|while|switch|case|break|return)\b/g, | |
| Go: /\b(?:package|func|var|const|if|else|for|switch|case|break|return)\b/g, | |
| Rust: /\b(?:fn|let|const|if|else|for|while|match|return)\b/g, | |
| Kotlin: /\b(?:fun|val|var|if|else|for|while|when)\b/g, | |
| Scala: /\b(?:val|var|def|if|else|for|while|match|case)\b/g, | |
| HTML: /<[a-z][\s\S]*?>/gi, | |
| CSS: /{[\s\S]*?}/gi, | |
| SQL: /\b(?:SELECT|INSERT|UPDATE|DELETE|FROM|WHERE|JOIN)\b/g, | |
| Shell: /\b(?:echo|export|cd|ls|grep|awk|sed|chmod)\b/g, | |
| Lua: /\b(?:function|if|else|for|while|repeat|until|end)\b/g, | |
| Perl: /\b(?:my|sub|if|else|for|while|foreach|return)\b/g, | |
| R: /\b(?:function|if|else|for|while|return)\b/g, | |
| }; | |
| const languageCount: { [key: string]: number } = {}; | |
| for (const language in languageKeywords) { | |
| const regex = languageKeywords[language]; | |
| const matches = codeSnippet.match(regex); | |
| if (matches && matches.length > 0) { | |
| languageCount[language] = matches.length; | |
| } | |
| } | |
| const sortedLanguages = Object.keys(languageCount).sort( | |
| (a, b) => languageCount[b] - languageCount[a] | |
| ); | |
| const topLanguages = sortedLanguages.slice(0, 20); | |
| return topLanguages; | |
| } | |
| // Example usage: | |
| const codeSnippet = ` | |
| fn factorial(n: u32) -> u32 { | |
| if n == 0 { | |
| 1 | |
| } else { | |
| n * factorial(n - 1) | |
| } | |
| } | |
| fn main() { | |
| let num = 5; | |
| let result = factorial(num); | |
| println!("Factorial of {} is: {}", num, result); | |
| } | |
| `; | |
| const topLanguages = detectPopularLanguages(codeSnippet); | |
| console.log("Top 20 Programming Languages:"); | |
| console.log(topLanguages); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment