<commit_before>// Common modules
import '../../common'
import ko from 'knockout'

const api = require('../../get-api-data')
const browser = require('../../browser')
const endpoints = require('../../api')
const location = require('../../location/locationSelector')

function Families () {
  const self = this
  const currentLocation = location.getCurrentHubFromCookies()

  self.isSelectedSearchInput = ko.observable(false)
  self.showFilteredAdvice = ko.observable(false)
  self.advice = ko.observableArray([])
  self.filteredAdvice = ko.observableArray([])
  self.searchQuery = ko.observable('')

  self.isSelectedSearchInput.subscribe((newValue) => {
    if (!newValue) {
      self.showFilteredAdvice(false)
    } else if (newValue && self.searchQuery().trim().length && self.filteredAdvice().length) {
      self.showFilteredAdvice(true)
    }
  })

  self.searchQuery.subscribe(() => {
    self.search()
  })

  self.getAdvice = function () {
    browser.loading()
    api
    .data(`${endpoints.faqs}?location=${currentLocation.id}&tags=families&pageSize=100000&index=0`)
    .then((result) => {
      self.advice(result.data.items.map((x) => {
        return {
          id: ko.observable(x.id),
          body: ko.observable(x.body),
          locationKey: ko.observable(x.locationKey),
          parentScenario: ko.observable(x.parentScenario),
          sortPosition: ko.observable(x.sortPosition),
          tags: ko.observableArray(x.tags),
          title: ko.observable(x.title),
          breadcrumbs: ko.observable(`Families > ${x.parentScenario ? x.parentScenario.name + ' > ' : ''}${x.title}`)
        }
      }))
      browser.loaded()
    }, (_) => {
      browser.redirect('/500')
    })
  }

  self.filterByTitle = function (searchTermRegex) {
    let filteredByTitle = ko.observableArray(self.advice().filter((x) => {
                                                                    let result = x.title().toLowerCase().trim().match(searchTermRegex);
                                                                    return result != null && result.length
                                                                  })
                            .sort((a, b) => b.title().toLowerCase().trim().match(searchTermRegex).length
                                          - a.title().toLowerCase().trim().match(searchTermRegex).length))
    return filteredByTitle
  }

  self.filterByTag = function (searchTermRegex) {
    let filteredByTag = ko.observableArray(self.filteredAdvice().filter((x) => {
                                                                          let result = x.tags().filter((y) => y !== 'families').join(' ').replaceAll('-', ' ').match(searchTermRegex)
                                                                          return result != null && result.length
                                                                        })
                          .sort((a, b) => b.tags().filter((y) => y !== 'families').join(' ').replaceAll('-', ' ').match(searchTermRegex).length
                                        - a.tags().filter((y) => y !== 'families').join(' ').match(searchTermRegex).length))
    return filteredByTag
  }

  self.filterByContent = function (searchTermRegex) {
    let filteredByContent = ko.observableArray(self.filteredAdvice().filter((x) => {
                                                                              let result = x.body().toLowerCase().trim().match(searchTermRegex)
                                                                              return result != null && result.length
                                                                            })
                              .sort((a, b) => b.body().toLowerCase().trim().match(searchTermRegex).length
                                            - a.body().toLowerCase().trim().match(searchTermRegex).length))
    return filteredByContent
  }

  self.search = function () {
    let searchTerm = self.searchQuery().toLowerCase()

    if (searchTerm.trim()) {
      // Tokenize the search terms and remove empty spaces
      let tokens = searchTerm.toLowerCase().split(' ').filter((token) => token.trim() !== '');
      let searchTermRegex = new RegExp(tokens.join('|'), 'gim')

      let filteredByTitle = self.filterByTitle(searchTermRegex)
      self.filteredAdvice(self.advice().filter((x) => !filteredByTitle().some((y) => x.id() === y.id()) ))

      let filteredByTag = self.filterByTag(searchTermRegex)
      self.filteredAdvice(self.filteredAdvice().filter((x) => !filteredByTag().some((y) => x.id() === y.id()) ))

      let filteredByContent = self.filterByContent(searchTermRegex)
      self.filteredAdvice(filteredByTitle().concat(filteredByTag()).concat(filteredByContent()))
    }
    else {
      self.filteredAdvice([])
    }

    self.showFilteredAdvice(self.filteredAdvice().length)
  }

  self.getAdvice()
}

module.exports = Families
<commit_msg>Update search algorithm<commit_after>// Common modules
import '../../common'
import ko from 'knockout'

const api = require('../../get-api-data')
const browser = require('../../browser')
const endpoints = require('../../api')
const location = require('../../location/locationSelector')

function Families () {
  const self = this
  const currentLocation = location.getCurrentHubFromCookies()

  self.isSelectedSearchInput = ko.observable(false)
  self.showFilteredAdvice = ko.observable(false)
  self.advice = ko.observableArray([])
  self.filteredAdvice = ko.observableArray([])
  self.searchQuery = ko.observable('')

  self.isSelectedSearchInput.subscribe((newValue) => {
    if (!newValue) {
      self.showFilteredAdvice(false)
    } else if (newValue && self.searchQuery().trim().length && self.filteredAdvice().length) {
      self.showFilteredAdvice(true)
    }
  })

  self.searchQuery.subscribe(() => {
    self.search()
  })

  self.getAdvice = function () {
    browser.loading()
    api
    .data(`${endpoints.faqs}?location=${currentLocation.id}&tags=families&pageSize=100000&index=0`)
    .then((result) => {
      self.advice(result.data.items.map((x) => {
        return {
          id: ko.observable(x.id),
          body: ko.observable(x.body),
          locationKey: ko.observable(x.locationKey),
          parentScenario: ko.observable(x.parentScenario),
          sortPosition: ko.observable(x.sortPosition),
          tags: ko.observableArray(x.tags),
          title: ko.observable(x.title),
          breadcrumbs: ko.observable(`Families > ${x.parentScenario ? x.parentScenario.name + ' > ' : ''}${x.title}`)
        }
      }))
      browser.loaded()
    }, (_) => {
      browser.redirect('/500')
    })
  }

  self.sortByTitle = function (a, b, searchTermRegex) {
    let resA = a.title().toLowerCase().trim().match(searchTermRegex)
    resA = resA !== null ? resA.length : 0

    let resB = b.title().toLowerCase().trim().match(searchTermRegex)
    resB = resB !== null ? resB.length : 0

    if ((resB - resA) === 0) {
      return self.sortByTag(a, b, searchTermRegex)
    }
    return resB - resA
  }

  self.filterByTitle = function (searchTermRegex) {
    let filteredByTitle = ko.observableArray(self.advice().filter((x) => {
                                                                    let result = x.title().toLowerCase().trim().match(searchTermRegex);
                                                                    return result !== null && result.length
                                                                  })
                            .sort((a, b) => self.sortByTitle(a, b, searchTermRegex)))
    return filteredByTitle
  }

  self.sortByTag = function (a, b, searchTermRegex) {
    let resA = a.tags().filter((y) => y !== 'families').join(' ').match(searchTermRegex)
    resA = resA !== null ? resA.length : 0

    let resB = b.tags().filter((y) => y !== 'families').join(' ').replaceAll('-', ' ').match(searchTermRegex)
    resB = resB !== null ? resB.length : 0

    if ((resB - resA) === 0) {
      return self.sortByContent(a, b, searchTermRegex)
    }
    return resB - resA
  }

  self.filterByTag = function (searchTermRegex) {
    let filteredByTag = ko.observableArray(self.filteredAdvice().filter((x) => {
                                                                          let result = x.tags().filter((y) => y !== 'families').join(' ').replaceAll('-', ' ').match(searchTermRegex)
                                                                          return result !== null && result.length
                                                                        })
                          .sort((a, b) => self.sortByTag(a, b, searchTermRegex)))
    return filteredByTag
  }

  self.sortByContent = function (a, b, searchTermRegex) {
    let resContentA = a.body().toLowerCase().trim().match(searchTermRegex)
    resContentA = resContentA !== null ? resContentA.length : 0

    let resContentB = b.body().toLowerCase().trim().match(searchTermRegex)
    resContentB = resContentB !== null ? resContentB.length : 0

    return resContentB - resContentA
  }

  self.filterByContent = function (searchTermRegex) {
    let filteredByContent = ko.observableArray(self.filteredAdvice().filter((x) => {
                                                                              let result = x.body().toLowerCase().trim().match(searchTermRegex)
                                                                              return result !== null && result.length
                                                                            })
                              .sort((a, b) => self.sortByContent(a, b, searchTermRegex)))
    return filteredByContent
  }

  self.search = function () {
    let searchTerm = self.searchQuery().toLowerCase()

    if (searchTerm.trim()) {
      // Tokenize the search terms and remove empty spaces
      let tokens = searchTerm.toLowerCase().split(' ').filter((token) => token.trim() !== '');
      let searchTermRegex = new RegExp(tokens.join('|'), 'gim')

      let filteredByTitle = self.filterByTitle(searchTermRegex)
      self.filteredAdvice(self.advice().filter((x) => !filteredByTitle().some((y) => x.id() === y.id()) ))

      let filteredByTag = self.filterByTag(searchTermRegex)
      self.filteredAdvice(self.filteredAdvice().filter((x) => !filteredByTag().some((y) => x.id() === y.id()) ))

      let filteredByContent = self.filterByContent(searchTermRegex)
      self.filteredAdvice(filteredByTitle().concat(filteredByTag()).concat(filteredByContent()))
    }
    else {
      self.filteredAdvice([])
    }

    self.showFilteredAdvice(self.filteredAdvice().length)
  }

  self.getAdvice()
}

module.exports = Families

{'fraction_non_alphanumeric': 0.1487155534796824, 'fraction_numerical': 0.0026156001868285848, 'mean_word_length': 2.0056148231330715, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 56, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6868807', 'n_tokens_mistral': 2931, 'n_tokens_neox': 2847, 'n_words': 487}