Are there any existing tools for detecting whether a piece of text is source code or natural language? It does not need to identify the programming language nor the natural language. However, it would ideally be flexible for any programming and natural language.
For example, this piece of text would be identified as source code:
def fib(n):
a, b = 0, 1
while a < n:
print(a, end=' ')
a, b = b, a+b
print()
and this piece of text would be identified as natural language:
Hello! This is natural language.