
    P                     d    S r SSKJr  SSKJr  SSKJr  SSKJr   " S S\5      r " S S	\5      r	g
)z5Contains a class to rate commands based on relevance.    )absolute_import)division)unicode_literals)lookupc                   D    \ rS rSrSrSrSrSrSrSr	S r
S rS	 rS
 rSrg)CommandRater   z3A class to rate the results of searching a command.      ?g      ?g      ?g?c                 F    X l         UR                  5       U l        Xl        g)zCreate a CommandRater.

Args:
  results: googlecloudsdk.command_lib.search_help.search_util
    .CommandSearchResult, class that holds results.
  command: dict, a json representation of a command.
N)_commandAllTerms_terms_results)selfresultscommands      3lib/googlecloudsdk/command_lib/help_search/rater.py__init__CommandRater.__init__'   s     M""$DKM    c                 R    SnXR                  5       -  nXR                  5       -  nU$ )a  Produce a simple relevance rating for a set of command search results.

Returns a float in the range (0, 1]. For each term that's found, the rating
is multiplied by a number reflecting how "important" its location is, with
command name being the most and flag or positional names being the second
most important, as well as by how many of the search terms were found.

Commands are also penalized if duplicate results in a higher release track
were found.

Returns:
  rating: float, the rating of the results.
r
   )_RateForLocation_RateForTermsFound)r   ratings     r   RateCommandRater.Rate3   s0     F
##%%F
%%''FMr   c                 (   SnU R                   R                  5       R                  5       nU H  nU[        R                  :X  a  XR
                  -  nM'  U[        R                  :X  a  XR                  -  nMK  UR                  [        R                  5      S   [        R                  [        R                  4;   aD  UR                  [        R                  5      S   [        R                  :X  a  XR                  -  nM  XR                  -  nM     U$ )z+Get a rating based on locations of results.r
   r   )r   FoundTermsMapvaluesr   NAME_COMMAND_NAME_MULTIPLIERPATH_PATH_MULTIPLIERsplitDOTFLAGSPOSITIONALS_ARG_NAME_MULTIPLIER_DEFAULT_MULTIPLIER)r   r   	locationslocations       r   r   CommandRater._RateForLocationF   s    F++-446I	V[[	 ///v{{"'''NN6::&q)fllF<N<N-OOvzz*2.&++=+++***  Mr   c                     SnU R                   R                  5       nU R                   H  nX2;  d  M
  XR                  -  nM     U$ )z@Get a rating based on how many of the searched terms were found.r
   )r   r   r   _NOT_FOUND_MULTIPLIER)r   r   r   terms       r   r   CommandRater._RateForTermsFoundV   sB    Fmm))+G		,,,  Mr   )r   r   r   N)__name__
__module____qualname____firstlineno____doc__r"   r)   r$   r*   r/   r   r   r   r   __static_attributes__ r   r   r   r      s;    ; ! 
& r   r   c                   *    \ rS rSrSrS rS rS rSrg)CumulativeRater`   z'Rates all found commands for relevance.c                     / U l         g)z Creates a cumulative rater.
    N_found_commands_and_results)r   s    r   r   CumulativeRater.__init__c   s     (*D$r   c                 <    U R                   R                  X45        g)zAdd a command that is a result.

Args:
  command: dict, a json representation of a command. MUST already be updated
    with the search results.
  result: search_util.CommandSearchResults, the results object that goes
    with this command.
N)r>   append)r   r   results      r   AddFoundCommandCumulativeRater.AddFoundCommandh   s     	$$++W,=>r   c                     U R                    H0  u  p[        X!5      R                  5       nX1[        R                  '   M2     g)z#Adds rating to every command found.N)r>   r   r   r   	RELEVANCE)r   r   r   r   s       r   RateAllCumulativeRater.RateAlls   s6     <<G-224f"(f =r   r=   N)	r2   r3   r4   r5   r6   r   rC   rG   r7   r8   r   r   r:   r:   `   s    /*
	?)r   r:   N)
r6   
__future__r   r   r   &googlecloudsdk.command_lib.help_searchr   objectr   r:   r8   r   r   <module>rL      s1     < &  ' 9D6 DN)f )r   