kotman12 commented on code in PR #16998:
URL: https://github.com/apache/lucene/pull/16998#discussion_r4608993503


##########
lucene/monitor/src/java/org/apache/lucene/monitor/TermWeightor.java:
##########
@@ -21,18 +21,61 @@
 import java.util.HashSet;
 import java.util.Map;
 import java.util.Set;
+import java.util.function.DoubleUnaryOperator;
 import java.util.function.ToDoubleFunction;
 import org.apache.lucene.index.Term;
 import org.apache.lucene.util.BytesRef;
 
-/** Calculates the weight of a {@link Term} */
+/**
+ * Calculates the weight of a {@link Term}
+ *
+ * <p>Higher weights mark terms that are more useful for selecting queries, 
typically rarer or
+ * longer ones. {@link #negate(double)} turns a weight into the weight of the 
absence of the term,
+ * for negated clauses.
+ */
 public interface TermWeightor extends ToDoubleFunction<Term> {
 
   /** A default TermWeightor based on token length */
   TermWeightor DEFAULT = lengthWeightor(3, 0.3f);
 
-  /** Combine weightors by multiplication */
+  /** The smallest weight returned by the default {@link #negate(double)} */
+  double MIN_NEGATED_WEIGHT = 1e-6;
+
+  /**
+   * The weight of the absence of a term of weight {@code weight}, used for 
negated clauses
+   *
+   * <p>By default the weight is read as surprisal, {@code w = -ln p} where 
{@code p} is the share

Review Comment:
   I've changed the comment to refer to this as [information 
content](https://en.wikipedia.org/wiki/Information_contentbanshee) since that 
is wikipedia's primary term for it. Negating weights is easiest to think about 
when mapping from a common probability space (since it is `p` for a doc 
matching the query and then naturally `1-p` for it not matching). Then to get 
the "direction" right you invert the probability. Finally one can apply the 
natural log to this inverse probability to get the "surprisal" or "information 
content" in `nats`. I don't think taking the natural log here is strictly 
necessary but it has some nice properties (avoiding over/underflow, independent 
events are additive instead of multiplicative, and it is a standard).
    
   However, none of the existing weightors return information content so 
currently the negating weightor returns no meaningful units. It pretends it 
receives `nats` from the positive weightor when in reality it probably doesn't. 
Perhaps it makes sense to come up with a positive weightor that attempts to 
compute information content (with this change or a follow-up)?
   
   For instance, interpreting the _current_ default weightor as information 
content _incidentally_ results in a negative weightor that is very conservative 
and always just defers to positive weights over negative ones. But this is 
partly by chance because of how that weightor is currently calibrated. I think 
this default is probably fine but I've just added 
`testDefaultWeightsReadSensiblyAsInformationContent` to ensure that no one 
incidentally recalibrates the default weightor _too far_ without considering 
the effect on the negative weightor as it stands. Going too far in any 
direction could either result in a term that the negating weightor thinks is in 
a majority of documents (hard to make this claim based on term length) or in a 
term so rare that the negative weightor collapses to 0 (also unlikely based on 
just term length). 
   
   Here is a table of how the default weightor currently negates:
   
   | Term length | Weight w | Implied p = e^-w | Absence 1 - p | Negative 
weight -ln(1 - p) |
   |---|---|---|---|---|
   | 1 | 1.778 | 16.91% | 83.09% | 0.1852 |
   | 3 | 2.780 | 6.20% | 93.80% | 0.0640 |
   | 5 | 3.331 | 3.58% | 96.42% | 0.0364 |
   | 10 | 3.851 | 2.13% | 97.87% | 0.0215 |
   | 32+ | 4.000 | 1.83% | 98.17% | 0.0185 |



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to