Skip to content

Commit

Permalink
Dramatically improves success rate of suggestion linkability
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnathonKoster committed Sep 15, 2024
1 parent f12394f commit 803cf5b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
10 changes: 8 additions & 2 deletions resources/js/components/links/SuggestionsListing.vue
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
<span>{{ suggestion.phrase }}</span>
</div>
</template>
<template slot="cell-context.can_replace" slot-scope="{ row: suggestion }">
<span>{{ suggestion.context.can_replace ? '' : '' }}</span>
</template>
<template slot="cell-context.field_handle" slot-scope="{ row: suggestion }">
<span>{{ suggestion.context.field_handle ?? '' }}</span>
</template>
Expand All @@ -37,8 +40,9 @@
</template>
<template slot="actions" slot-scope="{ row: suggestion }">
<dropdown-list>
<dropdown-item text="Edit Entry" :redirect="editUrl"></dropdown-item>
<dropdown-item text="Accept Suggestion" @click="activeSuggestion = suggestion" v-if="suggestion.context.can_replace" />
<div class="divider" v-if="suggestion.context.can_replace"></div>
<div class="divider"></div>
<dropdown-item text="Ignore Suggestion" @click="ignoringSuggestion = suggestion" class="warning" />
</dropdown-list>
</template>
Expand Down Expand Up @@ -95,8 +99,9 @@ export default {
activeSuggestion: null,
columns: [
{ label: 'Phrase', field: 'phrase' },
{ label: 'Can Auto Apply', field: 'context.can_replace' },
{ label: 'Rank', field: 'score' },
{ label: 'URI', field: 'uri' }
{ label: 'Target URI', field: 'uri' }
],
suggestions: [],
};
Expand All @@ -109,6 +114,7 @@ export default {

this.$axios.get(cp_url(`seo-pro/links/${this.entry}/suggestions`)).then(response => {
this.suggestions = response.data;
console.log(this.suggestions);
this.loading = false;
});
},
Expand Down
21 changes: 14 additions & 7 deletions src/TextProcessing/Suggestions/SuggestionEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,11 @@ protected function getPhraseContext(array $contentMapping, string $phrase): Phra
$regex = '/([^.!?]*' . preg_quote($phrase, '/') . '[^.!?]*[.!?])|([^.!?]*' . preg_quote($phrase, '/') . '[^.!?]*$)/i';

if (preg_match($regex, $searchText, $matches)) {

$firstMatch = trim($matches[0]);

if (Str::contains($firstMatch, "\n")) {

$lines = explode("\n", $firstMatch);

$curLine = '';
Expand Down Expand Up @@ -94,20 +96,25 @@ protected function getPhraseContext(array $contentMapping, string $phrase): Phra
return $context;
}

/**
* Attempts to locate a target phrase within a value and capture the surrounding context.
*
* @param string $content The text to search within.
* @param string $phrase The value to search for within $content.
* @param int $surroundingWords The number of words to attempt to retrieve around the $phrase.
* @return string|null
*/
protected function getSurroundingWords(string $content, string $phrase, int $surroundingWords = 4): ?string
{
preg_match('/^(.*?)('.preg_quote($phrase, '/').')(.*)$/iu', $content, $matches);
$pattern = '/(?P<before>(?:[^\s\n]+[ \t]+){0,'.$surroundingWords.'})(?P<phrase>' . preg_quote($phrase, '/') . ')(?P<after>(?:[ \t]+[^\s\n]+){0,'.$surroundingWords.'})/iu';

preg_match($pattern, $content, $matches);

if (empty($matches)) {
return null;
}

$words = array_filter(array_slice(explode(' ', $matches[1]), -$surroundingWords));
$words[] = $phrase;

$words = array_merge($words, array_filter(array_slice(explode(' ', $matches[3]), 0, $surroundingWords)));

return implode(' ', $words);
return $matches['before'].$matches['phrase'].$matches['after'];
}

public function suggest(Entry $entry)
Expand Down

0 comments on commit 803cf5b

Please sign in to comment.