Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Use of common prefix in vocabulary #726

Closed
1 task done
nvitucci opened this issue Nov 6, 2023 · 4 comments
Closed
1 task done

Use of common prefix in vocabulary #726

nvitucci opened this issue Nov 6, 2023 · 4 comments

Comments

@nvitucci
Copy link

nvitucci commented Nov 6, 2023

Check for existing issues

  • Completed

Environment

  • OS: macOS 14.1
  • Install method: Homebrew
  • Vale version: 2.29.6

Describe the bug / provide steps to reproduce it

Although a similar issue of non-deterministic behaviour has already been raised, this is more of a request for advice than an actual bug (at least given the current behaviour).

Let's assume the following:

  • accept.txt vocabulary:
MyBrand
MyProduct
MyProduct Community
MyProduct Enterprise
  • .vale.ini:
StylesPath = styles

Vocab = Basic

[*.md]
BasedOnStyles = Vale
  • test.md document:
Use Myproduct.
Use Myproduct enterprise.
Use MyProduct enterprise.

When I run Vale, the output oscillates between this:

test.md:1:5:Vale.Terms:Use 'MyProduct' instead of 'Myproduct'.
test.md:2:5:Vale.Terms:Use 'MyProduct Enterprise' instead of 'Myproduct enterprise'.

and this:

test.md:1:5:Vale.Terms:Use 'MyProduct' instead of 'Myproduct'.
test.md:2:5:Vale.Terms:Use 'MyProduct' instead of 'Myproduct'.

Basically, the second line has two possible corrections and the third line is never detected as wrong. As I understand, this happens because MyProduct is a common prefix and the result depends on what rule is triggered first.

As a workaround, I put the MyProduct common prefix in a separate substitution rule and left only the two more specific lines in the vocabulary. This works, but of course it's not ideal, especially when this pattern is repeated many times and the overlapping elements expand over time. The other downside is that, since the term is not in the vocabulary, it has to be manually added as an exception to other rules (for example, a rule on capitalization of headings).

No amount of tinkering, with regex's and other combinations, to try and put all the definitions in the dictionary would get me around this, and I suspect it's really by design. Even trying to put all the definitions in the substitution rule has the same "competing rule" behaviour, unless I use a hack like this, which works but gives a less useful message as it doesn't capture the prefix:

swap:
  - myproduct: MyProduct
  - (?<=myproduct )enterprise: MyProduct Enterprise

Is this the best I can do or am I missing any useful Vale features? Or perhaps am I seeing it the wrong way? How would you tackle this problem?

@jdkato
Copy link
Member

jdkato commented Nov 6, 2023

I would do this in two steps.

First, add an entry to accept.txt that matches all acceptable forms of the term:

MyBrand
MyProduct(?: Community| Enterprise)?

This will ensure that correct uses of these terms don't raise alerts in other places (such as your capitalization rule).

Second, create a MyProduct.yml rule to enforce the correct usage:

extends: substitution
message: "Use '%s' instead of '%s'."
ignorecase: true
level: error
swap:
  - MyProduct(?! Community| Enterprise): MyProduct
  - MyProduct Community: MyProduct Community
  - MyProduct Enterprise: MyProduct Enterprise

Here's an example that showcases both parts:

Screenshot 2023-11-06 at 3 33 03 PM

@nvitucci
Copy link
Author

nvitucci commented Nov 7, 2023

Thanks for the answer. If I try this, though, the third line in my example (with MyProduct in the correct form but enterprise not) goes still undetected.

Anyway, why is it not possible to achieve this in the vocabulary alone?

@jdkato
Copy link
Member

jdkato commented Jan 13, 2024

In the next release (v3.0.4), this will work with just a vocabulary:

Use Myproduct.
Use Myproduct enterprise.
Use MyProduct enterprise.

@jdkato jdkato closed this as completed Jan 13, 2024
@nvitucci
Copy link
Author

I just tried it and it seems to work properly. Thank you very much!

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

No branches or pull requests

2 participants