> For the complete documentation index, see [llms.txt](https://emory.gitbook.io/nlp-essentials/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://emory.gitbook.io/nlp-essentials/distributional-semantics/homework.md).

# Homework

Create a [**distributional\_semantics.py**](https://github.com/emory-courses/nlp-essentials/blob/main/src/homework/distributional_semantics.py) file in the [src/homework/](https://github.com/emory-courses/nlp-essentials/tree/main/src/homework) directory.

## Task 1

Your task is to read word embeddings trained by [Word2Vec](/nlp-essentials/distributional-semantics/word2vec.md):

1. Define a function called `read_word_embeddings()` that takes a path to the file consisting of word embeddings, [word\_embeddings.txt](https://github.com/emory-courses/nlp-essentials/blob/main/dat/word_embeddings.txt).
2. Return a dictionary where the key is a word and the value is its corresponding embedding in [numpy.array](https://numpy.org/doc/stable/reference/generated/numpy.array.html).

Each line in the file adheres to the following format:

```
[WORD](\t[FLOAT]){50}
```

## Task 2

Your task is to retrieve a list of the most similar words to a given target word:

1. Define a function called `similar_words()` that takes the word embeddings from Task 1, a target word (string), and a threshold (float).
2. Return a list of tuples, where each tuple contains a word similar to the target word and the cosine similarity between them as determined by the embeddings. The returned list must only include words with similarity scores greater than or equal to the threshold, sorted in descending order based on the similarity scores.

## Task 3

Your task is to measure a similarity score between two documents:

1. Define a function called `document_similarity()` that takes the word embeddings and two documents (string). Assume that the documents are already tokenized.
2. For each document, generate a document embedding by averaging the embeddings of all words within the document.
3. Return the cosine similarity between the two document embeddings.

## Submission <a href="#submission" id="submission"></a>

Commit and push the **distributional\_semantics.py** file to your GitHub repository.

## Rubric

* Task 1: Read Word Embeddings (2.8 points)
* Task 2: Similar Words (3 points)
* Task 3: Document Similarity (3 points)
* Concept Quiz (3.2 points)


---

# 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://emory.gitbook.io/nlp-essentials/distributional-semantics/homework.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.
