Documentation
Comments
Understand comment filters, smart comments, ordering rules, and techniques for adding, preserving, and coalescing PGN comments.
Documentation
Understand comment filters, smart comments, ordering rules, and techniques for adding, preserving, and coalescing PGN comments.
Positions appearing in a recorded game in an input PGN file may contain comments and additional comments may be added to matching positions during the evaluation of a CQL query which will be included when the game is written to the output PGN file. Comments are the primary mechanism by which matching positions and information related to matching filters are communicated. CQLi provides several facilities to inspect comments appearing in PGN games, remove existing comments, add new comments, and control the various types of comments that are added during the operation of CQLi.
Comment Filters
The
originalcommentfilter provides access to comments appearing in the processed PGN file, thecommentfilter allows insertion of new comments, and theremovecommentfilter allows the removal of original comments appearing in the PGN file.The
commentFilterThe
commentfilter behaves like thestrfilter except that the string formed by the concatenation of its arguments is used to form a comment at the current position. Thecommentfilter always yields atruevalue. Unless the–nosmartcommentsoption is used, Smart Comments ensures that a comment added with thecommentfilter will not be written to the output PGN file if the enclosing expression fails to match or the query ultimately does not match the position.The
originalcommentFilterThe
originalcommentfilter is used to inspect comments and annotations appearing in the original PGN game text.When
originalcommentis not followed by a string literal it yields a string containing the text of the original comment at the current position orNoneif there is no original comment. Note that the string returned byoriginalcommentreflects the comments appearing in the PGN text and are not affected bycommentorremovecommentfilters that have been evaluated, e.g. the original comment(s) may still be retrieved even after aremovecommentfilter has been evaluated for the same position.Multiple Comments in a Position
If the current position has multiple original comments, these will be combined into a single string, separated by newline characters, in the string returned by
originalcomment. Because newlines in original comments are replaced with a space character when being parsed, and the regular expression anchoring characters^and$will match the beginning or end of a line terminated with a newline, the~~filter may be used to check if one of multiple comments at a position consists entirely of some string. For example, if the a position contains the commentsABCand123, the query:will match the position. Since, by default, newlines are not matched with the ‘
.’ regular expression character, the query:will not match. To search for a pattern that may span multiple consecutive comments (and thus multiple lines in the string returned by
originalcomment), either explicitly include the newline character as in:or enable the ability of
.to match a newline within a group using the?s:syntax:Implicit Search with
originalcommentIf
originalcommentis immediately followed by a string literal that represents a Numeric Annotation Glyph (NAG), or a corresponding typographic annotation symbol recognized by CQLi, the filter yieldstrueif there is a respective annotation at the position andfalseif there is not. Iforiginalcommentis immediately followed by a string literal that does not represent such an annotation, the result has Boolean value and yieldstrueif there is an original comment at the current position and the provided string appears in the text of any of the original comments at the current position andfalseotherwise.NAGs and Symbolic Annotations with
originalcommentA NAG consists of a dollar sign (
$) followed by one or more digits to form a decimal numeric value between0and255. NAGs are the standard way to represent simple annotations in a PGN file. CQLi also recognizes several typographic annotation symbols that may follow a move in a PGN game, the list of supported symbols and their corresponding NAG values is provided in the table below.!?!!??!??!=+/==/++/--/++--+Examples
Given the following PGN game text:
the table below shows the results of several uses of
originalcommentat each position of the above game.1.e41...e52.d4originalcomment"pre-game""Best by test""??"Noneoriginalcomment ""truetruetruefalseoriginalcomment "a"truefalsefalsefalseoriginalcomment "$1"falsetruetruefalseoriginalcomment "!"falsetruetruefalseoriginalcomment "$113"falsefalsetruefalseoriginalcomment "="falsefalsefalsetrueoriginalcomment "??"falsefalsefalsefalse"??" in originalcommentfalsefalsetruefalseNote in particular that
originalcomment "$1"will match if the position contains either the NAG$1or the corresponding annotation symbol!. A comment whose text resembles a NAG or symbolic annotation is still a comment and will not be matched by the NAG-querying version oforiginalcomment, theinfilter may be used to check for the presence of a comment containing text that would be interpreted as a NAG byoriginalcomment. Positions may contain multiple NAGs but at most one symbolic annotation.The
removecommentFilterThe
removecommentfilter removes any original comments associated with the current position. Theremovecommentfilter always yieldstrue.Adroit use of
commentandremovecommentmay be employed to remove or replace part of an original comment. For example, to remove clock information embedded in comments that looks like[%clk 0:09:16], the following query may be used:To replace all instances of one string in a comment with another, e.g. all instances of “XX” with “Y”, use the query:
Note that
removecommentdoes not employ smart-comment-like semantics. The effects of a previously evaluatedremovecommentfilter will be realized even if the same position later fails to match.The
–noremovecommentoption may be used to prevent comment removal with theremovecommentfilter. In this case evaluation ofremovecommentfilters still yield atruevalue but the original comment is not removed when the PGN game is written to the output file.Comments Added by CQLi
There are six situations in which CQLi may add comments to a position which are described in the following sections.
User Comments
User comments are added by the
commentfilter. These comments always appear in the output PGN file, subject to the provisions of Smart Comments, unless the–silentoption or thesilentheader parameter is used. Thecommentfilter may be used to annotate a position with multiple comments.Sort Comments
Each
sortfilter appearing in a query will cause a corresponding sort comment to be inserted at the beginning of each matching game with the best value encountered for the filter in that game. Sort comments are not emitted forsortfilters using thequietkeyword parameter or when either the--silentoption or thesilentheader parameter is used.Header Comments
By default, every matching game contains a header comment which includes the game number of the game which is the index of the game in the input PGN file. The
–noheaderoption may be used to suppress these comments.Match Comments
By default, CQLi will annotate every matching position of a game with the comment
CQL. The–matchstringoption or thematchstringheader parameter may be used to change the string used to annotate matching positions. Specifying an empty match string will effectively suppress these comments. Match comments are also suppressed if the--silentor--quietoptions are used or if thesilentorquietparameters appear in the CQL header.Auxiliary Comments
Auxiliary comments are those added during the operation of the
consecutivemoves,echo,find, andlinefilters. See the descriptions of these filters for information about the comments they add. Auxiliary comments are suppressed if the--silentor--quietoptions are used or if thesilentorquietparameters appear in the CQL header.Position ID Comments
A position ID comment has the form
id:position-idwhereposition-idis the numeric position ID value of the position in which the comment appears. Position ID comments are added to variation positions to help identify them in the following situations:messageorcommentfilter.commentfilter is evaluated in a variation position.echofilter is a variation position.linefilter is a variation position.Position ID comments are suppressed if auxiliary comments are suppressed.
Comment Order
Multiple comments added to a single position will appear in the following order: header comment, match comment, sort comments, and user and auxiliary comments in the order in which they were added by the corresponding filters. Header and sort comments appear only at the initial position, before the first move. When multiple
sortfilters are present, sort comments appear in the order in which their respective filters appear in the CQL query. CQLi will never add more than one header comment to a game or more than one match comment to a position.If the game from the input PGN originally contained comments, all comments added by CQLi in a given position will appear after any original comments at that position.
For example, given the following input:
and the query:
the result will be emitted as:
Comment Coalescing
By default, multiple comments at a position will be written out as multiple distinct comments in the PGN file, each enclosed by a separate set of braces. If the
–coalescecommentsoption is used, multiple comments at a single position will be combined into a single comment with space characters separating each coalesced comment component. For example, the query:produces three comments at the initial position: one containing
AB, one containingX, and one containingY. By default, these comments would be represented in the PGN file as three separate comments:If comment coalescing is enabled, the combined comment will be represented as:
Separately written comments make it clear where each comment begins and ends but some chess software does not handle multiple comments well and may not recognize or preserve multiple comments.
Unique comments
By default, multiple comments with the same text at the same position are not written to the output PGN file. This deduplication occurs before comment coalescing and includes both original comments and comments added by CQLi. If there are duplicate original comments at the same position, all but one of them is removed, even if the duplicate comments were not adjacent. Duplicates between new and original comments are likewise removed. The
–nouniquecommentsoption may be used to allow such duplicate comments.Smart Comments
The Smart Comments mechanism ensures that only appropriate comments are added to the resulting matching games by suppressing unnecessary comments. Comments added by filters such as
comment,line, andfindare suppressed in the following cases:Position Does Not Match
Any comments added while evaluating the current position will be suppressed if the position ultimately does not match the query. For example:
will only add the comment “
Terminal position” to positions that have no children as all other positions will fail to match theterminalfilter which will suppress preceding comments for that position.Subsequent Filter Fails to Match
If a later filter in the compound expression containing a comment does not match, previous comments are elided even if the position ultimately matches the query. For example:
will only comment the end of the main line, even though the enclosing
iffilter will match all non-terminal positions.Enclosing Filter Does Not Match
If a comment is added by a filter that is enclosed by another filter that fails to match later, the comment is suppressed. For example:
will only comment on positions in which there are 3-4 absolute pins as the enclosing comparison will not otherwise match even though the query itself will match every position.
Best Values
The
min,max,sort,line, sortedecho, andconsecutivemovesfilters accumulate only those comments that are associated with the best value(s) encountered by those filters. The details of effect of this behavior are described below.minandmaxFiltersComments appearing in a
minormaxfilter will only be emitted for values that correspond to the lowest or highest argument values. For example:will emit the comments “
A” and “X”.sortFiltersComments appearing within a
sortfilter will only be emitted for the lowest or highest value evaluated for thesortfilter within a particular game. If multiple evaluations yield the same best value, only the comments associated with the first occurrence of the best value are kept unless the–keepallbestoption is used. For example:will only emit a comment for the first position encountered having the maximum number of moves of all evaluated positions.
lineFiltersComments appearing in a
linefilter are only emitted for the longest matching line found. For example:will only comment the checks associated with the longest line of checks found at the current position. If there are multiple matching lines of the longest length in a
linefilter, only the comments associated with one of the matching lines are kept unless the--keepallbestoption is used.Sorted
echoFiltersWhen the
echofilter is used as the target of asortfilter, only the comments associated with the largest matching value of theechofilter’s target are retained. If multiple evaluations of theechotarget filter yield the largest value, only the comments generated with the first evaluation of the largest value are retained unless the--keepallbestoption is used.The
consecutivemovesFilterComments appearing in the arguments to the
consecutivemovesfilter are only retained for the evaluation of the filter that yields the longest matching sequence.