Since a first word in a sentence does not have a space (whitespace) before (prefix) it. It is not counted as a standalone, i can patch jaydub any input on how this could be done?

Comments

jaydub’s picture

StatusFileSize
new1.58 KB

I see that this is problematic and includes the case where the word to filter is at the end of the content as well.

Can you try out the attached patch to see if that helps? This is against the development snapshot which should be updated with another commit by tomorrow at the latest.

jaydub’s picture

Status: Active » Needs review
Michsk’s picture

No it does not seem to work, don't know how to explain the error. It just doesn't show the content.

jaydub’s picture

well the patch worked for me so I'm gonna need more info from you as to how it did not work for you.

Michsk’s picture

mm, well i would love to give you more info but it just doesn't work. The content (text) that uses words that need to be replaced don't show. Also when using the test i just get:
Your test phrase 'test needs to be replaced' did not match any filters

When unchecking the standalone for the word 'test' i do get the restult

Your test phrase: 
  'test needs to be replaced' 
was filtered to: 
  'replaced needs to be replaced'
(As HTML): 
  'test needs to be replaced' 
was filtered to: 
  'replaced needs to be replaced'
Michsk’s picture

This is the setup i'm trieing to use. Basically it's word changed to a link, checking the upper and lowercase. Demo:

Home|<a href="home.com" title="Home">Home</a>
home|<a href="home.com" title="home">home</a>
Homes|<a href="home.com" title="Homes">Homes</a>
homes|<a href="home.com" title="homes">homes</a>	

And it returnes.

Your test phrase: 
  'Home or home might even be Homes and homes' 
was filtered to: 
  '<a href="home.com" title="Homes"></a><a href="home.com" title="Homes"></a>'
(As HTML): 
  'Home or home might even be Homes and homes' 
was filtered to: 
  ''
stevetweeddale’s picture

StatusFileSize
new2.31 KB

I had the same issue, got an error of 'empty regular expression' when using the above patch. After some poking around I've got it working locally for the first and last words in a field, so thought I'd post my version of the patch.

q0rban’s picture

StatusFileSize
new595 bytes

The attached patch fixed it for me.

q0rban’s picture

Title: Standalone doesn't count for first word of sentence. » Standalone doesn't count for first/last word of sentence.
q0rban’s picture

Version: 6.x-1.x-dev » 7.x-1.x-dev

Grr. ;)

capellic’s picture

The patch in #8 is a good start, but it over-corrects and only gets words at the beginning and at the end, not ones truly surrounded by white space. I'm not a regular expression guy as I'm sure it could have been dealt with one one patter, but here's what I ended up with:

$pattern = array('/(\W)'. preg_quote($word->words, '/') .'(\W)/i');
$pattern[] = '/(\W|^)' . preg_quote($word->words, '/') . '(\W|$)/i';
q0rban’s picture

Hmm, I'm not sure I understand. I'm using #8 with success. Can you provide some example strings that would not be a match using that method?

capellic’s picture

q0rban: It wasn't working with the following string (including the period) where "hello" was the word to be converted.

Hello hello hello.

q0rban’s picture

Status: Needs review » Needs work

Ah, yeah, so if you write the same word over and over, it only finds every other word. I bet if you put 'Hello hi hello hi hello.' it would match them all. Will work on this a bit more.

q0rban’s picture

Status: Needs work » Reviewed & tested by the community

You know what, though, now that I think about it, that's a bug with the current regex as well. I'm going to create a separate issue for that in hopes that this will get in ASAP.

q0rban’s picture

Status: Needs review » Reviewed & tested by the community
q0rban’s picture

Status: Reviewed & tested by the community » Needs review
StatusFileSize
new1.05 KB

Ok, let's try this on for size. Should resolve both issues.

dunx’s picture

Status: Reviewed & tested by the community » Needs review

Same with 6-1.1. Should I raise a new issue?

Michsk’s picture

No you don't have to create a new issue.

yngens’s picture

same issue here with 6.x-1.x-dev. subscribe.

q0rban’s picture

Title: Standalone doesn't count for first/last word of sentence. » Standalone doesn't count for first/last word of sentence or word repetition

I'm closing #1176872: Standalone doesn't account for word repetition and retitling this issue. The patch in #17 resolves both issues.

Michsk’s picture

So this has been committed?

csavio’s picture

You shouldn't need to back-reference the word-boundary (\b). It is not a character. I think you missed changing the replacements as well as there is nothing to back-reference due to \b being a word boundary appending the variables $1 and $2 in the preg_replace makes no sense.

if ($word->standalone) {
  $pattern = '/\b'. preg_quote($word->words, '/') .'\b/i';
}
if (!preg_match('/^</', $part)) {
  $new_string .= preg_replace($pattern, $replacement, $part);
}
$text = preg_replace($pattern, $replacement, $text);
csavio’s picture

Status: Needs review » Needs work

Patch should be done against the development version(s) and updated to remove the now unnecessary back-references.

q0rban’s picture

Csavio, thanks for the review. Can you please explain this a bit more? I'm feeling a bit dense. :)

You shouldn't need to back-reference the word-boundary (\b). It is not a character. I think you missed changing the replacements as well as there is nothing to back-reference due to \b being a word boundary appending the variables $1 and $2 in the preg_replace makes no sense.

Also, the patch was done against the development version. Are you meaning to say it needs to be re-rolled?

csavio’s picture

Yeah, you'll just want to re-roll your patch with the additional changes if it was against the development version. Sorry, that was a misunderstanding on my part.

The metacharacter \b is an anchor like the caret and the dollar sign. It matches at a position that is called a "word boundary". This match is zero-length. (Reference: http://www.regular-expressions.info/wordboundaries.html)

So, since \b is not a character it is the boundary of the word the parenthesis around the regular expression character (\b) make a reference to nothing then concatenate that nothing needlessly later.

Previous replacements:

$new_string .= preg_replace($pattern, "\${1}". $replacement ."\${2}", $part);
$text = preg_replace($pattern, "\${1}". $replacement ."\${2}", $text);

Previously the pattern was matching (\W). \W means anything that is not white space, so it was concatenating those characters back on to the replacement ("\${1}". $replacement ."\${2}"), which is probably why it was broken every other match.

What we should be doing after your update:

$new_string .= preg_replace($pattern, $replacement, $part);
$text = preg_replace($pattern, $replacement, $text);

Your change functionally works perfectly, it would just be confusing from a maintainability standpoint and a bit of extra process that doesn't need to happen.

q0rban’s picture

Ah, got it! Thanks so much for the clarification, it's nice to have someone looking at this that actually understands these things. When it comes to regex, I just keep trying things until it works. ;)

csavio’s picture

No problem. Thanks for taking the time to make the patch. I'd just happened to be bug fixing the repetitive word issue and found your other thread.

mxh’s picture

Issue summary: View changes
Status: Needs work » Closed (outdated)