"The following function that computes the accuracy of the tagger on gold-standard data."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def accuracy(tagger, gold_data):\n",
" correct = 0\n",
" total = 0\n",
" for tagged_sentence in gold_data:\n",
" words, gold_tags = zip(*tagged_sentence)\n",
" pred_tags = tagger.predict(words)\n",
" for gold_tag, pred_tag in zip(gold_tags, pred_tags):\n",
" correct += int(gold_tag == pred_tag)\n",
" total += 1\n",
" return correct / total"
]
},
{
"cell_type": "markdown",
"metadata": {},
...
...
@@ -362,7 +380,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.10"
"version": "3.10.4"
},
"latex_envs": {
"bibliofile": "biblio.bib",
...
...
%% Cell type:markdown id: tags:
# Feature engineering for part-of-speech tagging
%% Cell type:markdown id: tags:
In this challenge, you will practice your skills in feature engineering, the task of identifying useful features for a machine learning system.
%% Cell type:markdown id: tags:
## The data set
%% Cell type:markdown id: tags:
The data for this challenge and their representation is the same as for lab L4 on dependency parsing, but for this lab we have converted the data into a simpler format: words and their part-of-speech tags are separated by tabs, sentences are separated by empty lines. The code in the next cell defines a container class for data with this format.
We load the training data and the development data for this lab:
%% Cell type:code id: tags:
``` python
train_data=Dataset('train.txt')
dev_data=Dataset('dev.txt')
```
%% Cell type:markdown id: tags:
Both data sets consist of **tagged sentences**. On the Python side of things, a tagged sentence is represented as a list of string pairs, where the first component of each pair represents a word token and the second component represents the word’s tag. The possible tags are listed and exemplified in the [Annotation Guidelines](http://universaldependencies.org/u/pos/all.html) of the Universal Dependencies Project.
%% Cell type:markdown id: tags:
## Baseline tagger
%% Cell type:markdown id: tags:
The baseline tagger that you will use in this lab is a pure Python implementation of the perceptron tagger that was presented in the video lectures on [Part-of-speech tagging with the perceptron](https://web.microsoftstream.com/video/1a846d64-57e9-41ba-a3ca-3ab74cf32039) and [The perceptron learning algorithm](https://web.microsoftstream.com/video/1a69c6d6-35e1-42dc-81ba-8b5e52208831). To understand what the code provided here does, and how it might be extended with new features, you should watch these two lectures.
The following function that computes the accuracy of the tagger on gold-standard data.
%% Cell type:code id: tags:
``` python
defaccuracy(tagger,gold_data):
correct=0
total=0
fortagged_sentenceingold_data:
words,gold_tags=zip(*tagged_sentence)
pred_tags=tagger.predict(words)
forgold_tag,pred_taginzip(gold_tags,pred_tags):
correct+=int(gold_tag==pred_tag)
total+=1
returncorrect/total
```
%% Cell type:markdown id: tags:
## Feature engineering
%% Cell type:markdown id: tags:
Your task now is to try to improve the performance of the perceptron tagger by adding new features. The only part of the code that you are allowed to change is the `featurize` method. Provide a short (ca. 150 words) report on what features you added and what results you obtained.
%% Cell type:markdown id: tags:
**⚠️ To claim the bonus points for this challenge, your submitted notebook must contain output demonstrating at least 91% accuracy on the development set.**
%% Cell type:code id: tags:
``` python
tagger=train_perceptron(train_data,n_epochs=3)
print('{:.4f}'.format(accuracy(tagger,dev_data)))
```
%% Cell type:markdown id: tags:
*TODO: Insert your report here*
%% Cell type:markdown id: tags:
## Chocolate Box Challenge
%% Cell type:markdown id: tags:
To participate in the [Chocolate Box Challenge](https://www.kaggle.com/t/ce6f010bfb99478fb20830735c6f4a03), run the next code cell to produce a file `submission.csv` and upload this file to Kaggle.
%% Cell type:code id: tags:
``` python
# Load the test data (without the tags)
test_data=Dataset('test-notags.txt')
# Generate submission.csv with results on both the dev data and the test data