Skip to content

Incorrect Pygments lexer guessed #8

Open

Description

@pombredanne

See pygments/pygments#1563

With the attached file (to rename to a .JAVA UPPERCASE extension) the lexer is guessed incorrectly as Python.
See Logger.JAVA.txt

When the extension is lowercased to '.java`, the lexer is guessed OK from the filename.
In all cases, the content-based guess is not correct

$ python
Python 3.6.10 (default, Jun 13 2020, 08:53:46) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pygments
>>> pygments.__version__
'2.7.1'
>>> from pygments import lexers 
>>> fn='Logger.JAVA'
>>> code=open(f).read()
>>> lexers.get_lexer_for_filename(fn, code)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/tmp/lib/python3.6/site-packages/pygments/lexers/__init__.py", line 210, in get_lexer_for_filename
    raise ClassNotFound('no lexer for filename %r found' % _fn)
pygments.util.ClassNotFound: no lexer for filename 'Logger.JAVA' found
>>> lexers.guess_lexer(code)
<pygments.lexers.Python2Lexer>
>>> fn='Logger.java'
>>> lexers.get_lexer_for_filename(fn, code)
<pygments.lexers.JavaLexer>

Activity

  1. thatch commented on Oct 19, 2020

    @thatch

    The constraints around guess_lexer solely on contents need a complete rewrite; for speed it does not actually even try to tokenize the source, and although it can return a float (that is then ranked) there is no real balancing among the lexers written by different people.

    I would suggest lowercasing before get_lexer_for_filename as a very reasonable workaround.

  2. ashiscs commented on Oct 19, 2020

    @ashiscs

    Hello I am new to this community
    Can I work on this issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions