Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Mark units that are aliases, and use alias string for uString comparisons so sameUnits will allow plurals, etc. (#1193) #1194

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from

Conversation

dpvc
Copy link
Member

@dpvc dpvc commented Feb 12, 2025

This PR addresses the issue raised in #1193, where aliases for units are not accepted when sameUnits => 1 is specified.

Because the lib/Units.pm file removes the aliases array from the %Units::known_units hash, we can't currently determine what is an alias and what isn't, so this PR adds new hashes to Units.pm that the units context can use to identify aliases. One maps the original to the array of aliases for it, and one maps from aliases to the original that they are aliases for. The former isn't used in the units context, so can be removed if you want.

The changes to the units context itself are to include a new ustring property for units that are aliases of another unit, and changes to the uString() method to cause it to use the ustring value, if present when constructing the unit string. This is only used internally to compare the units for a correct and student answer, so this doesn't affect the string or TeX value that is produced, but does normalize the unit names for aliases so that any alias will match.

Copy link
Member

@pstaabp pstaabp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good and fixes the error.

@pstaabp pstaabp linked an issue Feb 13, 2025 that may be closed by this pull request
Copy link
Member

@drgrice1 drgrice1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks fine.

It seems that there is more work to do on adding aliases though, particularly for plural and full word forms. I noticed that things like "gram" and "grams" are not accepted, and there are lots of other units like that. Other forms have been added for some units, but not for many others.

Comment on lines +689 to +692
for ('litre', 'litres', 'liter', 'liters') {
$UNITS{$_} = $UNITS{L};
$ALIAS{$_} = 'L';
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are these extras added here instead of to the Units.pm file? I am curious as to why this is done differently for liters than for all other units that use the aliases key in Units.pm.

Copy link
Member Author

@dpvc dpvc Feb 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had originally not wanted to mess with the Units.pm file, so I added them here, mostly as an illustration of how to do that. You can't use aliases here, as the Units.pm file has already processed and removed the aliases properties, so we have to do it by hand here after the fact.

I think the Units.pm file does need a lot of work in terms of aliases, but that wasn't my priority here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see.

I didn't mean to say that you should handle the work on aliases in this pull request. I just wanted to point that out, and that we need to work on it.

Interestingly, I noticed that I get somewhat different behavior from trying to use grams with the parserNumberWithUnits.pl versus with the new contextUnits.pl macro. With the parserNumberWithUnits.pl macro the message is 'gra' is not defined in this context, while with the contextUnits.pl macro the message is 'rams' is not defined in this context. The legacy macro sees ms as a unit and has a problem with gra, but the new macro sees g as a unit and has a problem with rams.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the two worked very differently about how they determined the units. The old units context tried to remove units from the end of the student answer and parse the rest as a formula, so it was essentially right-to-left handling of the units. But the new contest integrates units into the parsing as actual MathObjects, so it is done left-to-right, as with all the parsing. When there is a run of characters like "grams", MathObjects will take the maximal substring that matches one of its defined values (constants, functions, etc.), so in this case, since "gr", "gra", "gram", and "grams" are not defined in Units.pm, the g is the longest match.

The units context currently defines squared and square so that you can do square meters or seconds squared. I suppose one could add milli, centi, deci, etc., so that you could use millimeters, decilitres and so on without having to add all the separate aliases.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Alias units in contextUnits.pl
3 participants