<commit_before>#!/usr/bin/python2.7
# Copyright 2015 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import logging
import re

from google.appengine.api import search as appengine_search

import model
import script_variant

# The index name for full text search.
# This index contains person name and location.
PERSON_LOCATION_FULL_TEXT_INDEX_NAME = 'person_location_information'

# This is for ranking (person name match higher than location)
REPEAT_COUNT_FOR_RANK = 5

def make_or_regexp(query_txt):
    """
    Creates compiled regular expression for OR search.
    Args:
        query_txt: Search query

    Returns:
        query_word | query_word | ...
    """
    query_words = query_txt.split(' ')
    regexp = '|'.join([re.escape(word) for word in query_words if word])
    return re.compile(regexp, re.I)

def create_sort_expressions():
    """
    Creates SortExpression's for ranking.
    Returns:
        array of SortExpression
    """
    return [appengine_search.SortExpression(
        expression='_score',
        direction=appengine_search.SortExpression.DESCENDING,
        default_value=0.0
    )]

def enclose_in_double_quotes(query_txt):
    """
    Encloses each word in query_txt in double quotes.
    Args:
        query_txt: Search query

    Returns:
        '"query_word1" "query_word2" ...'
    """
    query_words = query_txt.split(' ')
    return '"' + '" "'.join([word for word in query_words if word]) + '"'

def search(repo, query_txt, max_results):
    """
    Searches person with index.
    Query_txt must match at least a part of person name.
    (It's not allowed to search only by location.)
    Args:
        repo: The name of repository
        query_txt: Search query
        max_results: The max number of results you want.(Maximum: 1000)

    Returns:
        - Array of <model.Person> in datastore
        - []: If query_txt doesn't contain a part of person name

    Raises:
        search.Error: An error occurred when the index name is unknown
                      or the query has syntax error.
    """
    #TODO: Sanitaize query_txt
    if not query_txt:
        return []

    # Remove double quotes so that we can safely apply enclose_in_double_quotes().

    romanized_query = script_variant.romanize_text(query_txt)
    query_txt = re.sub('"', '', romanized_query)
<commit_msg>removed<commit_after>#!/usr/bin/python2.7
# Copyright 2015 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import logging
import re

from google.appengine.api import search as appengine_search

import model
import script_variant

# The index name for full text search.
# This index contains person name and location.
PERSON_LOCATION_FULL_TEXT_INDEX_NAME = 'person_location_information'

# This is for ranking (person name match higher than location)
REPEAT_COUNT_FOR_RANK = 5

def make_or_regexp(query_txt):
    """
    Creates compiled regular expression for OR search.
    Args:
        query_txt: Search query

    Returns:
        query_word | query_word | ...
    """
    query_words = query_txt.split(' ')
    regexp = '|'.join([re.escape(word) for word in query_words if word])
    return re.compile(regexp, re.I)

def create_sort_expressions():
    """
    Creates SortExpression's for ranking.
    Returns:
        array of SortExpression
    """
    return [appengine_search.SortExpression(
        expression='_score',
        direction=appengine_search.SortExpression.DESCENDING,
        default_value=0.0
    )]

def enclose_in_double_quotes(query_txt):
    """
    Encloses each word in query_txt in double quotes.
    Args:
        query_txt: Search query

    Returns:
        '"query_word1" "query_word2" ...'
    """
    query_words = query_txt.split(' ')
    return '"' + '" "'.join([word for word in query_words if word]) + '"'

def search(repo, query_txt, max_results):
    """
    Searches person with index.
    Query_txt must match at least a part of person name.
    (It's not allowed to search only by location.)
    Args:
        repo: The name of repository
        query_txt: Search query
        max_results: The max number of results you want.(Maximum: 1000)

    Returns:
        - Array of <model.Person> in datastore
        - []: If query_txt doesn't contain a part of person name

    Raises:
        search.Error: An error occurred when the index name is unknown
                      or the query has syntax error.
    """
    if not query_txt:
        return []

    # Remove double quotes so that we can safely apply enclose_in_double_quotes().

    romanized_query = script_variant.romanize_text(query_txt)
    query_txt = re.sub('"', '', romanized_query)

{'fraction_non_alphanumeric': 0.08311229000884174, 'fraction_numerical': 0.006719717064544651, 'mean_word_length': 3.092619392185239, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '4002075', 'n_tokens_mistral': 1641, 'n_tokens_neox': 1603, 'n_words': 585}