I have a text file. Its content may be plain text or JavaScript source code. I need an efficient and high accuracy approach to detect if the text file contains plain text or JavaScript code.
My approach: I tried to extract syntax-specific keywords, punctuations, operators, regex based loop detection for this. I added all the values and divided it by the length of the file and got a specific ratio. Let's say the ratio is greater than 0.2 then the file is JavaScript else plain text. The problem I ran in was with regex based loop detection. It was taking too much time for file size > 1 Mega Bytes, hence I abandoned the approach.
Any resources or approaches would be highly appreciated. Thank you.