<commit_before>package uk.ac.ebi.arrayexpress.utils.search;

import net.sf.saxon.s9api.XdmItem;
import net.sf.saxon.s9api.XdmNode;
import net.sf.saxon.s9api.XdmValue;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import uk.ac.ebi.arrayexpress.app.Application;
import uk.ac.ebi.arrayexpress.components.SaxonEngine;
import uk.ac.ebi.arrayexpress.utils.RegExpHelper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Search experiments based on full-text information stored in ExperimentText class
 */
public class ExperimentSearch
{
    // logging machinery
    private final Log log = LogFactory.getLog(getClass());

    // array of ExperimentText objects in the same order they exist in the document
    private List<ExperimentText> expText = new ArrayList<ExperimentText>();

    // mapping of experiment accession numbers to experiment text indices
    private Map<String, Integer> accessionIdx = new HashMap<String, Integer>();

    public boolean isEmpty()
    {
        return expText.isEmpty();
    }

    public void buildText( XdmNode experiments )
    {
        if (null != experiments && null != experiments.getUnderlyingNode()) {
            try {
                expText.clear();
                accessionIdx.clear();

                XdmValue exps = ((SaxonEngine)Application.getAppComponent("SaxonEngine")).evaluateXPath(experiments, "/experiments/experiment");
                if (null != exps) {
                    for (XdmItem expObj : exps) {
                        ExperimentText text = new ExperimentText().populateFromExperiment((XdmNode)expObj);
                        expText.add(text);
                        accessionIdx.put(text.accession, expText.size() - 1);
                    }
                }
            } catch ( Throwable x ) {
                log.error("Caught an exception:", x);
            }
        }
    }

    public boolean matchText( String textIdx, String keywords, boolean wholeWords )
    {
        int idx = Integer.parseInt(textIdx);
        return matchString(expText.get(idx).text, keywords, wholeWords);
    }

    public boolean matchAccession( String textIdx, String accession )
    {
        int idx = Integer.parseInt(textIdx);
        return expText.get(idx).accessions.contains(" ".concat(accession).concat(" ").toLowerCase());
    }

    public boolean matchSpecies( String textIdx, String species )
    {
        int idx = Integer.parseInt(textIdx);
        return expText.get(idx).species.contains(species.trim().toLowerCase());
    }

    public boolean matchArray( String textIdx, String array )
    {
        int idx = Integer.parseInt(textIdx);
        return expText.get(idx).array.contains(array.trim().toLowerCase());
    }

    public boolean matchExperimentType( String textIdx, String experimentType )
    {
        int idx = Integer.parseInt(textIdx);
        return expText.get(idx).experimentType.contains(experimentType.trim().toLowerCase());
    }

    public boolean matchUser( String textIdx, String user )
    {
        int idx = Integer.parseInt(textIdx);
        return expText.get(idx).users.contains(" ".concat(user).concat(" "));
    }

    public boolean isAccessible( String accession, String user )
    {
        if (user.equals("0")) {
            return true;
        }

        Integer idx = accessionIdx.get(accession.toLowerCase());
        return (null != idx) && matchUser(String.valueOf(idx), user);
    }

    public boolean doesPresent( String accession )
    {
        return accessionIdx.containsKey(accession.toLowerCase());
    }

    private String keywordToPattern( String keyword, boolean wholeWord )
    {<commit_msg>fix to prevent unnecessary exceptions<commit_after>package uk.ac.ebi.arrayexpress.utils.search;

import net.sf.saxon.s9api.XdmItem;
import net.sf.saxon.s9api.XdmNode;
import net.sf.saxon.s9api.XdmValue;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import uk.ac.ebi.arrayexpress.app.Application;
import uk.ac.ebi.arrayexpress.components.SaxonEngine;
import uk.ac.ebi.arrayexpress.utils.RegExpHelper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Search experiments based on full-text information stored in ExperimentText class
 */
public class ExperimentSearch
{
    // logging machinery
    private final Log log = LogFactory.getLog(getClass());

    // array of ExperimentText objects in the same order they exist in the document
    private List<ExperimentText> expText = new ArrayList<ExperimentText>();

    // mapping of experiment accession numbers to experiment text indices
    private Map<String, Integer> accessionIdx = new HashMap<String, Integer>();

    public boolean isEmpty()
    {
        return expText.isEmpty();
    }

    public void buildText( XdmNode experiments )
    {
        if (null != experiments && null != experiments.getUnderlyingNode()) {
            try {
                expText.clear();
                accessionIdx.clear();

                XdmValue exps = ((SaxonEngine)Application.getAppComponent("SaxonEngine")).evaluateXPath(experiments, "/experiments/experiment");
                if (null != exps) {
                    for (XdmItem expObj : exps) {
                        ExperimentText text = new ExperimentText().populateFromExperiment((XdmNode)expObj);
                        expText.add(text);
                        accessionIdx.put(text.accession, expText.size() - 1);
                    }
                }
            } catch ( Throwable x ) {
                log.error("Caught an exception:", x);
            }
        }
    }

    public boolean matchText( String textIdx, String keywords, boolean wholeWords )
    {
        if (null == textIdx) {
            return false;
        }
        Integer idx = Integer.parseInt(textIdx);
        return (null != idx) && matchString(expText.get(idx).text, keywords, wholeWords);
    }

    public boolean matchAccession( String textIdx, String accession )
    {
        if (null == textIdx) {
            return false;
        }
        Integer idx = Integer.parseInt(textIdx);
        return (null != idx) && expText.get(idx).accessions.contains(" ".concat(accession).concat(" ").toLowerCase());
    }

    public boolean matchSpecies( String textIdx, String species )
    {
        if (null == textIdx) {
            return false;
        }
        Integer idx = Integer.parseInt(textIdx);
        return (null != idx) && expText.get(idx).species.contains(species.trim().toLowerCase());
    }

    public boolean matchArray( String textIdx, String array )
    {
        if (null == textIdx) {
            return false;
        }
        Integer idx = Integer.parseInt(textIdx);
        return (null != idx) && expText.get(idx).array.contains(array.trim().toLowerCase());
    }

    public boolean matchExperimentType( String textIdx, String experimentType )
    {
        if (null == textIdx) {
            return false;
        }
        Integer idx = Integer.parseInt(textIdx);
        return (null != idx) && expText.get(idx).experimentType.contains(experimentType.trim().toLowerCase());
    }

    public boolean matchUser( String textIdx, String user )
    {
        if (null == textIdx) {
            return false;
        }
        Integer idx = Integer.parseInt(textIdx);
        return (null != idx) && expText.get(idx).users.contains(" ".concat(user).concat(" "));
    }

    public boolean isAccessible( String accession, String user )
    {
        if (user.equals("0")) {
            return true;
        }

        Integer idx = accessionIdx.get(accession.toLowerCase());
        return (null != idx) && matchUser(String.valueOf(idx), user);
    }

    public boolean doesPresent( String accession )
    {
        return accessionIdx.containsKey(accession.toLowerCase());
    }

    private String keywordToPattern( String keyword, boolean wholeWord )
    {
{'fraction_non_alphanumeric': 0.11097225693576605, 'fraction_numerical': 0.0012496875781054736, 'mean_word_length': 2.536456031816173, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2406150', 'n_tokens_mistral': 2213, 'n_tokens_neox': 2154, 'n_words': 518}