> For the complete documentation index, see [llms.txt](https://notes.dizy.cc/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://notes.dizy.cc/natural-language-processing/information-retrieval.md).

# Information Retrieval

## TF/IDF

### Term Frequency (TF)

TF: number of times term t occurs in document (or alternative: number of terms divided by length of document)

$$TF(t, d)=Count(times\ of\ term\ t\ appearing\ in\ d)$$

### Inverse Document Frequency (IDF)

IDF: logarithm of number of documents (in corpus) divided by number of documents containing term t

$$IDF(t)=\log{{Count(documents\ in\ total)}\over{Count(documents\ containing\ term\ t)}}$$

### TF-IDF

$$TF\_IDF(t, d)=TF(t, d) \* IDF(t)$$

## Cosine Similarity

Cosine of the Angle Between the Vectors. Range is \[0, 1]. The higher the value, the more similar the vectors.

$$Cosine(v1, v2) = \frac{v\_1 \cdot v\_2}{\sqrt{{v\_1}^2} \cdot \sqrt{{v\_2}^2}}$$

### Example

$$v1 = \[0, 5, 0, 5, 0]$$

$$v2 = \[0, 7, 0, 9, 0]$$

$$Cosine(v1, v2) = {{0*0+5*7+0*0+5*9+0\*0}\over{\sqrt{0^2+5^2+0^2+5^2+0^2}+\sqrt{0^2+7^2+0^2+9^2+0^2}}} = 0.992$$


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://notes.dizy.cc/natural-language-processing/information-retrieval.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
