Search and formatting results

Search:

Scripts + Language Web Pages + Pages

Results:

... a scripting language originally ... creating dynamic web pages . It has ... graphics applications .... the target is a scripting language that ... d creates web pages as output ...

Suppose I need a value that represents the number of characters that can be resolved as a complement on both sides of the agreed terms, and another value that represents how many matches will be shown as a result (i.e. I want to see only the first 5 matches, nothing more).

How exactly do you do it?

This is quite an agnostic language, but I will implement the solution in a PHP environment, so please limit the answers to parameters that do not require a specific language or framework.

Here is my thought process: create an array of search words. Determine which search word has the lowest score for where it was in the body of the article. Collect that part of the body into another variable, and then remove this section from the body of the article. Return to step 1. You can even add a counter to each word by skipping it when the counter reaches 3 or so.

Important:

. , , . , 3. 3 1 2, .

" , ".

:

padding , .

+3
2

:

  • , / (PHP array)
  • .
  • , , ,
  • , ( )
  • , .
  • , , /

, :

function string GetSearchExcerpt(searchText, searchTerms, wordPadding = 0, searchLimit = 3)
{
  results = new array()
  startIndex = 0
  foreach (searchTerm in searchTerms) 
  {
    charIndex = searchText.FindByIndex(searchTerms, startIndex) // finds 1st position of searchTerm starting at startIndex
    results.Add(searchTerm, charIndex)
    startIndex = charIndex + 1
  }
  results = results.SortByValue()
  lastSearchTerm = ""
  searchTermCount = new array()
  outputText = ""
  foreach (searchTerm => charIndex in results)
  {
    searchTermCount[searchTerm]++
    if (searchTermCount[searchTerm] <= searchLimit)
    {
      // WordPadding is a simple function that moves left or right a given number of words starting at a specified character index and returns those words
      outputText += "..." + WordPadding(-wordPadding, charIndex) + "<strong>" + searchTerm + "</strong>" + WordPadding(wordPadding, charIndex)
    }
  }

  return outputText
}
+1

, Regex Find-Replace, .

, RegEx . , RegEx.

, , , .

-1

Source: https://habr.com/ru/post/1703925/


All Articles