lunr/idf.py
raw| 1 | import math |
| 2 | |
| 3 | |
| 4 | def idf(posting, document_count): |
| 5 | """A function to calculate the inverse document frequency for a posting. |
| 6 | This is shared between the builder and the index. |
| 7 | """ |
| 8 | documents_with_term = 0 |
| 9 | for field_name in posting: |
| 10 | if field_name == "_index": |
| 11 | continue |
| 12 | documents_with_term += len(posting[field_name].keys()) |
| 13 | |
| 14 | x = (document_count - documents_with_term + 0.5) / (documents_with_term + 0.5) |
| 15 | return math.log(1 + abs(x)) |