<commit_before><?php

namespace Woe\EventBundle\Services;

use Doctrine\ORM\EntityManager;
use Woe\MapperBundle\Services\Mapper\TextNormalizer;

class Search
{
    /**
     * @var EntityManager
     */
    private $em;
    /**
     * @var TextNormalizer
     */
    private $normalizer;

    public function __construct(EntityManager $em, TextNormalizer $normalizer)
    {
        $this->em = $em;
        $this->normalizer = $normalizer;
    }

    public function getSearchResults($search_term)
    {
        $repository = $this->em->getRepository('WoeEventBundle:Event');
        $normalizedWords = $this->normalizer->normalize($search_term);

        list($normalizedWords, $dates) = $this->getDatesFromKeywords($normalizedWords);

        $events = empty($normalizedWords) ? $repository->findAllActiveSortedByDate()
                                          : $repository->findByKeywords($normalizedWords);

        return empty($dates) ? $events : $this->filterSearchResultsByDateKeywords($events, $dates);
    }

    /**
     * Separates dates keywords from regular keywords and converts them to DateTime objects
     *
     * @param $normalizedWords
     * @return array
     */
    protected function getDatesFromKeywords($normalizedWords)
    {
        $weekDays = array(
            'pirmadien' => ['monday'],
            'antradien' => ['tuesday'],
            'treciadien' => ['wednesday'],
            'ketvirtadien' => ['thursday'],
            'penktadien' => ['friday'],
            'sestadien' => ['saturday'],
            'sekmadien' => ['sunday'],
            'siandien' => ['today'],
            'rytoj' => ['tomorrow'],
            'poryt' => ['tomorrow +1 day'],
            'savaitgal' => ['saturday', 'sunday']
        );

        $dates = array();
        foreach ($weekDays as $weekDay => $dateFormats) {
            $position = array_search($weekDay, $normalizedWords);

            if ($position !== false) {
                array_splice($normalizedWords, $position, 1);
                foreach ($dateFormats as $dateFormat) {
                    $dates[] = new \DateTime($dateFormat);
                }
            }
        }

        return array($normalizedWords, $dates);
    }

    /**
     * Filter search results by date keywords
     *
     * @param array $events<commit_msg>Extracted field from weekDays variable<commit_after><?php

namespace Woe\EventBundle\Services;

use Doctrine\ORM\EntityManager;
use Woe\MapperBundle\Services\Mapper\TextNormalizer;

class Search
{
    protected $weekDays = array(
        'pirmadien' => ['monday'],
        'antradien' => ['tuesday'],
        'treciadien' => ['wednesday'],
        'ketvirtadien' => ['thursday'],
        'penktadien' => ['friday'],
        'sestadien' => ['saturday'],
        'sekmadien' => ['sunday'],
        'siandien' => ['today'],
        'rytoj' => ['tomorrow'],
        'poryt' => ['tomorrow +1 day'],
        'savaitgal' => ['saturday', 'sunday']
    );
    /**
     * @var EntityManager
     */
    private $em;
    /**
     * @var TextNormalizer
     */
    private $normalizer;

    public function __construct(EntityManager $em, TextNormalizer $normalizer)
    {
        $this->em = $em;
        $this->normalizer = $normalizer;
    }

    public function getSearchResults($search_term)
    {
        $repository = $this->em->getRepository('WoeEventBundle:Event');
        $normalizedWords = $this->normalizer->normalize($search_term);

        list($normalizedWords, $dates) = $this->getDatesFromKeywords($normalizedWords);

        $events = empty($normalizedWords) ? $repository->findAllActiveSortedByDate()
                                          : $repository->findByKeywords($normalizedWords);

        return empty($dates) ? $events : $this->filterSearchResultsByDateKeywords($events, $dates);
    }

    /**
     * Separates dates keywords from regular keywords and converts them to DateTime objects
     *
     * @param $normalizedWords
     * @return array
     */
    protected function getDatesFromKeywords($normalizedWords)
    {
        $dates = array();
        foreach ($this->weekDays as $weekDay => $dateFormats) {
            $position = array_search($weekDay, $normalizedWords);

            if ($position !== false) {
                array_splice($normalizedWords, $position, 1);
                foreach ($dateFormats as $dateFormat) {
                    $dates[] = new \DateTime($dateFormat);
                }
            }
        }

        return array($normalizedWords, $dates);
    }

    /**
     * Filter search results by date keywords
     *
     * @param array $events
{'fraction_non_alphanumeric': 0.13914174252275682, 'fraction_numerical': 0.0008669267446900737, 'mean_word_length': 2.2917261055634808, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 48, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6257341', 'n_tokens_mistral': 1358, 'n_tokens_neox': 1289, 'n_words': 258}