Difference between revisions of "Unigram tagger"

From Apertium
Jump to navigation Jump to search
Line 1: Line 1:
<code>apertium-tagger</code> from “m5w/apertium”<ref name="_1">https://github.com/m5w/apertium</ref> supports all the unigram models from “A set of open-source tools for Turkish natural language processing.”<ref name="_2">http://coltekin.net/cagri/papers/trmorph-tools.pdf</ref>
+
<code>apertium-tagger</code> from “m5w/apertium”<ref name="apertium">https://github.com/m5w/apertium</ref> supports all the unigram models from “A set of open-source tools for Turkish natural language processing.”<ref name="trmorph-tools">http://coltekin.net/cagri/papers/trmorph-tools.pdf</ref>
 
==Installation==
 
==Installation==
First, install all prerequisites. See “If you want to add language data / do more advanced stuff.”<ref name="_3">[[Installation#If you want to add language data / do more advanced stuff]]</ref>
+
First, install all prerequisites. See “If you want to add language data / do more advanced stuff.”<ref name="prerequisites">[[Installation#If you want to add language data / do more advanced stuff]]</ref>
   
Then, replace <code>&lt;directory&gt;</code> with the directory you’d like to clone “m5w/apertium”<ref name="_1"/> into and clone the repository.
+
Then, replace <code>&lt;directory&gt;</code> with the directory you’d like to clone “m5w/apertium”<ref name="apertium"/> into and clone the repository.
   
 
<pre>
 
<pre>
Line 9: Line 9:
 
</pre>
 
</pre>
   
Then, configure your environment<ref name="_4">[[Minimal installation from SVN#Set up environment]]</ref> and finally configure, build, and install<ref name="_5">[[Minimal installation from SVN#Configure, build, and install]]</ref> “m5w/apertium.”<ref name="_1"/>
+
Then, configure your environment<ref name="stow">[[Minimal installation from SVN#Set up environment]]</ref> and finally configure, build, and install<ref name="autogen.sh">[[Minimal installation from SVN#Configure, build, and install]]</ref> “m5w/apertium.”<ref name="apertium"/>
   
 
==Usage==
 
==Usage==
Line 35: Line 35:
 
''Example 2.1.1:'' <code>handtagged.txt</code> '': a Hand-Tagged Corpus for'' <code>apertium-tagger</code>
 
''Example 2.1.1:'' <code>handtagged.txt</code> '': a Hand-Tagged Corpus for'' <code>apertium-tagger</code>
   
Then, replace <code>MODEL</code> with the unigram model from “A set of open-source tools for Turkish natural language processing”<ref name="_2"/> you’d like to use, replace <code>SERIALISED_BASIC_TAGGER</code> with the filename to which you’d like to write the model, and train the tagger.
+
Then, replace <code>MODEL</code> with the unigram model from “A set of open-source tools for Turkish natural language processing”<ref name="trmorph-tools"/> you’d like to use, replace <code>SERIALISED_BASIC_TAGGER</code> with the filename to which you’d like to write the model, and train the tagger.
   
 
<pre>
 
<pre>
Line 52: Line 52:
 
''Example 2.2.1:'' <code>raw.txt</code> '': Input for'' <code>apertium-tagger</code>
 
''Example 2.2.1:'' <code>raw.txt</code> '': Input for'' <code>apertium-tagger</code>
   
Replace <code>MODEL</code> with the unigram model from “A set of open-source tools for Turkish natural language processing”<ref name="_2"/> you’d like to use, replace <code>SERIALISED_BASIC_TAGGER</code> with the file to which you wrote the unigram model, and disambiguate the input.
+
Replace <code>MODEL</code> with the unigram model from “A set of open-source tools for Turkish natural language processing”<ref name="trmorph-tools"/> you’d like to use, replace <code>SERIALISED_BASIC_TAGGER</code> with the file to which you wrote the unigram model, and disambiguate the input.
   
 
<pre>
 
<pre>
Line 66: Line 66:
   
 
==Unigram Models==
 
==Unigram Models==
See section 5.3 of “A set of open-source tools for Turkish natural language processing.”<ref name="_2"/>
+
See section 5.3 of “A set of open-source tools for Turkish natural language processing.”<ref name="trmorph-tools"/>
 
===Model 1===
 
===Model 1===
See section 5.3.1 of “A set of open-source tools for Turkish natural language processing.”<ref name="_2"/>
+
See section 5.3.1 of “A set of open-source tools for Turkish natural language processing.”<ref name="trmorph-tools"/>
   
 
This model assigns each analysis string a score of
 
This model assigns each analysis string a score of
Line 139: Line 139:
   
 
score("a<a>") ==
 
score("a<a>") ==
2 ==
+
2 ==
 
2.000000000000000000
 
2.000000000000000000
score("a<b>") ==
+
score("a<b>") ==
3 ==
+
3 ==
 
3.000000000000000000
 
3.000000000000000000
score("a<c>") ==
+
score("a<c>") ==
1 ==
+
1 ==
 
1.000000000000000000
 
1.000000000000000000
^a<b>$
+
^a<b>$
 
</pre>
 
</pre>
 
====Training on Corpora with Ambiguous Lexical Units====
 
====Training on Corpora with Ambiguous Lexical Units====
Line 168: Line 168:
 
</math>
 
</math>
   
the tagger must effectively increment the frequency of both analysis strings by <code>0.500000000000000000</code> . However, the tagger can’t increment the analysis strings’ frequencies by a non-integral number because model 1 represents analysis strings’ frequencies as <code>std::size_t</code> <ref name="_6">http://en.cppreference.com/w/cpp/types/size_t</ref>.
+
the tagger must effectively increment the frequency of both analysis strings by <code>0.500000000000000000</code> . However, the tagger can’t increment the analysis strings’ frequencies by a non-integral number because model 1 represents analysis strings’ frequencies as <code>std::size_t</code> <ref name="std::size_t">http://en.cppreference.com/w/cpp/types/size_t</ref>.
   
 
Instead, the tagger multiplies all the stored analysis strings’ frequencies by this lexical unit’s size and increments the frequency of each of this lexical unit’s analysis strings by 1.
 
Instead, the tagger multiplies all the stored analysis strings’ frequencies by this lexical unit’s size and increments the frequency of each of this lexical unit’s analysis strings by 1.
Line 213: Line 213:
 
ysis "a<a>"
 
ysis "a<a>"
 
^a/a<a>/a<b>$
 
^a/a<a>/a<b>$
  +
^
^
 
 
</pre>
 
</pre>
   
 
====File Format====
 
====File Format====
The tagger represents this model as <code>std::map&lt;Analysis, std::size_t&gt; Model;</code> .<ref name="std::map">http://en.cppreference.com/w/cpp/container/map</ref><ref name="_6"/><ref name="_9">https://github.com/m5w/apertium/blob/master/apertium/analysis.h</ref>
+
The tagger represents this model as <code>std::map&lt;Analysis, std::size_t&gt; Model;</code> .<ref name="std::map">http://en.cppreference.com/w/cpp/container/map</ref><ref name="std::size_t"/><ref name="Analysis">https://github.com/m5w/apertium/blob/master/apertium/analysis.h</ref>
   
 
The tagger first serialises <code>Model.size()</code> , the number of unique analysis strings in the corpus. To reduce file size, the tagger only puts a number’s non-zero bytes, preceded by the number of non-zero bytes in the number, henceforth known as the number’s size.
 
The tagger first serialises <code>Model.size()</code> , the number of unique analysis strings in the corpus. To reduce file size, the tagger only puts a number’s non-zero bytes, preceded by the number of non-zero bytes in the number, henceforth known as the number’s size.
Line 234: Line 234:
   
 
===Model 2===
 
===Model 2===
See section 5.3.2 of “A set of open-source tools for Turkish natural language processing.”<ref name="_2"/>
+
See section 5.3.2 of “A set of open-source tools for Turkish natural language processing.”<ref name="trmorph-tools"/>
   
 
Consider Example 3.1.1: <code>handtagged.txt</code> .
 
Consider Example 3.1.1: <code>handtagged.txt</code> .
Line 240: Line 240:
 
The tag string <code>&lt;b&gt;</code> is twice as frequent as <code>&lt;a&gt;</code>. However, model 1 scores <code>b&lt;a&gt;</code> and <code>b&lt;b&gt;</code> equally because neither analysis string appears in the corpus.
 
The tag string <code>&lt;b&gt;</code> is twice as frequent as <code>&lt;a&gt;</code>. However, model 1 scores <code>b&lt;a&gt;</code> and <code>b&lt;b&gt;</code> equally because neither analysis string appears in the corpus.
   
This model splits each analysis string into a root, <math>r</math> , and the part of the analysis string that isn’t the root, <math>a</math> . An analysis string’s root is its first lemma. The <math>r</math> of <code>a&lt;b&gt;+c&lt;d&gt;</code> is <code>a</code> , and its <math>a</math> is <code>&lt;b&gt;+c&lt;d&gt;</code> . The tagger assigns each analysis string a score of <math>P(r|a)f(a)</math> with add-one smoothing. (Without additive smoothing, this model would be the same as model 1.)<ref name="_7"><math>\begin{align}\mathrm{score} &= \frac{(\mathrm{tokenCount\_r\_a})(\mathrm{tokenCount\_a})}{\mathrm{tokenCount\_a}}\\&= \mathrm{tokenCount\_r\_a} = \mathrm{tokenCount\_T}\end{align}</math></ref> The tagger assigns higher scores to unknown analysis strings with frequent <math>a</math> than to unknown analysis strings with infrequent <math>a</math> .
+
This model splits each analysis string into a root, <math>r</math> , and the part of the analysis string that isn’t the root, <math>a</math> . An analysis string’s root is its first lemma. The <math>r</math> of <code>a&lt;b&gt;+c&lt;d&gt;</code> is <code>a</code> , and its <math>a</math> is <code>&lt;b&gt;+c&lt;d&gt;</code> . The tagger assigns each analysis string a score of <math>P(r|a)f(a)</math> with add-one smoothing. (Without additive smoothing, this model would be the same as model 1.)<ref name="score"><math>\begin{align}\mathrm{score} &= \frac{(\mathrm{tokenCount\_r\_a})(\mathrm{tokenCount\_a})}{\mathrm{tokenCount\_a}}\\&= \mathrm{tokenCount\_r\_a} = \mathrm{tokenCount\_T}\end{align}</math></ref> The tagger assigns higher scores to unknown analysis strings with frequent <math>a</math> than to unknown analysis strings with infrequent <math>a</math> .
   
 
Given the lexical unit <code>^b/b&lt;a&gt;/b&lt;b&gt;$</code>, the tagger assigns the analysis string <code>b&lt;a&gt;</code> a score of
 
Given the lexical unit <code>^b/b&lt;a&gt;/b&lt;b&gt;$</code>, the tagger assigns the analysis string <code>b&lt;a&gt;</code> a score of
Line 283: Line 283:
   
 
===Model 3===
 
===Model 3===
See section 5.3.3 of “A set of open-source tools for Turkish natural language processing.”<ref name="_2"/>
+
See section 5.3.3 of “A set of open-source tools for Turkish natural language processing.”<ref name="trmorph-tools"/>
   
 
Consider Example 3.1.1: <code>handtagged.txt</code> .
 
Consider Example 3.1.1: <code>handtagged.txt</code> .

Revision as of 04:41, 18 January 2016

apertium-tagger from “m5w/apertium”[1] supports all the unigram models from “A set of open-source tools for Turkish natural language processing.”[2]

Installation

First, install all prerequisites. See “If you want to add language data / do more advanced stuff.”[3]

Then, replace <directory> with the directory you’d like to clone “m5w/apertium”[1] into and clone the repository.

git clone https://github.com/m5w/apertium.git <directory>

Then, configure your environment[4] and finally configure, build, and install[5] “m5w/apertium.”[1]

Usage

See apertium-tagger -h .

Training a Model on a Hand-Tagged Corpus

First, get a hand-tagged corpus as you would for any non-unigram model.

$ cat handtagged.txt
^a/a<a>$
^a/a<b>$
^a/a<b>$
^aa/a<a>+a<a>$
^aa/a<a>+a<b>$
^aa/a<a>+a<b>$
^aa/a<b>+a<a>$
^aa/a<b>+a<a>$
^aa/a<b>+a<a>$
^aa/a<b>+a<b>$
^aa/a<b>+a<b>$
^aa/a<b>+a<b>$
^aa/a<b>+a<b>$

Example 2.1.1: handtagged.txt : a Hand-Tagged Corpus for apertium-tagger

Then, replace MODEL with the unigram model from “A set of open-source tools for Turkish natural language processing”[2] you’d like to use, replace SERIALISED_BASIC_TAGGER with the filename to which you’d like to write the model, and train the tagger.

$ apertium-tagger -s 0 -u MODEL SERIALISED_BASIC_TAGGER handtagged.txt

Disambiguation

Either write your input to a file or pipe it to the tagger.

$ cat raw.txt
^a/a<a>/a<b>/a<c>$
^aa/a<a>+a<a>/a<a>+a<b>/a<b>+a<a>/a<b>+a<b>/a<a>+a<c>/a<c>+a<a>/a<c>+a<c>$

Example 2.2.1: raw.txt : Input for apertium-tagger

Replace MODEL with the unigram model from “A set of open-source tools for Turkish natural language processing”[2] you’d like to use, replace SERIALISED_BASIC_TAGGER with the file to which you wrote the unigram model, and disambiguate the input.

$ apertium-tagger -gu MODEL SERIALISED_BASIC_TAGGER raw.txt
^a/a<b>$
^aa/a<b>+a<b>$
$ echo '^a/a<a>/a<b>/a<c>$
^aa/a<a>+a<a>/a<a>+a<b>/a<b>+a<a>/a<b>+a<b>/a<a>+a<c>/a<c>+a<a>/a<c>+a<c>$' | \
apertium-tagger -gu MODEL SERIALISED_BASIC_TAGGER
^a/a<b>$
^aa/a<b>+a<b>$

Unigram Models

See section 5.3 of “A set of open-source tools for Turkish natural language processing.”[2]

Model 1

See section 5.3.1 of “A set of open-source tools for Turkish natural language processing.”[2]

This model assigns each analysis string a score of

with additive smoothing.

Consider the following corpus.

$ cat handtagged.txt
^a/a<a>$
^a/a<b>$
^a/a<b>$

Example 3.1.1: handtagged.txt : A Hand-Tagged Corpus for apertium-tagger

Given the lexical unit ^a/a<a>/a<b>/a<c>$ , the tagger assigns the analysis string a<a> a score of

The tagger then assigns the analysis string a<b> a score of

and the unknown analysis string a<c> a score of

If ./autogen.sh is passed the option --enable-debug , the tagger prints such calculations to standard error.

$ ./autogen.sh --enable-debug
$ make
$ echo '^a/a<a>/a<b>/a<c>$' | apertium-tagger -gu 1 SERIALISED_BASIC_TAGGER


score("a<a>") ==
2 ==
  2.000000000000000000
  score("a<b>") ==
3 ==
  3.000000000000000000
  score("a<c>") ==
1 ==
  1.000000000000000000
  ^a<b>$

Training on Corpora with Ambiguous Lexical Units

Consider the following corpus.

$ cat handtagged.txt
^a/a<a>$
^a/a<a>/a<b>$
^a/a<b>$
^a/a<b>$

Example 3.1.1.1: handtagged.txt : a Hand-Tagged Corpus for apertium-tagger

The tagger expects lexical units of 1 analysis string, or lexical units of size 1. However, the size of the lexical unit ^a/a<a>/a<b>$ is 2. For this lexical unit,

the tagger must effectively increment the frequency of both analysis strings by 0.500000000000000000 . However, the tagger can’t increment the analysis strings’ frequencies by a non-integral number because model 1 represents analysis strings’ frequencies as std::size_t [6].

Instead, the tagger multiplies all the stored analysis strings’ frequencies by this lexical unit’s size and increments the frequency of each of this lexical unit’s analysis strings by 1.

The tagger could then increment the analysis strings’ frequencies of another lexical unit of size 2 without multiplying any of the stored analysis strings’ frequencies. To account for this, the tagger stores the least common multiple of all lexical units’ sizes; only if the LCM isn’t divisible by a lexical unit’s size does the tagger multiply all the analysis strings’ frequencies.

After incrementing the analysis strings’ frequencies of the lexical unit ^a/a<a>/a<b>$, the tagger increments the analysis string a<b> of the lexical unit ^a/a<b>$ by

If the tagger gets another lexical unit of size 2, it would increment the frequency of each of the lexical unit’s analysis strings by

and if it gets a lexical unit of size 3, it would multiply all the analysis strings’ frequencies by 3 and then increment the frequency of each of the lexical unit’s analysis strings by

Each model supports functions to increment all their stored analysis strings’ frequencies, so models 2 and 3 support this algorithm as well.

TODO: If one passes the -d option to apertium-tagger , the tagger prints warnings about ambiguous analyses in corpora to stderr.

$ apertium-tagger -ds 0 -u 1 handtagged.txt
apertium-tagger: handtagged.txt: 2:13: unexpected analysis "a<b>" following anal
ysis "a<a>"
^a/a<a>/a<b>$
^

File Format

The tagger represents this model as std::map<Analysis, std::size_t> Model; .[7][6][8]

The tagger first serialises Model.size() , the number of unique analysis strings in the corpus. To reduce file size, the tagger only puts a number’s non-zero bytes, preceded by the number of non-zero bytes in the number, henceforth known as the number’s size.

[. . . .]

([. . .]).serialise(0x00000000, [. . .]); // 00
([. . .]).serialise(0x000000ff, [. . .]); // 01ff
([. . .]).serialise(0x0000ffff, [. . .]); // 02ffff
([. . .]).serialise(0x00ffffff, [. . .]); // 03ffffff
([. . .]).serialise(0xffffffff, [. . .]); // 04ffffffff

[. . . .]

Model 2

See section 5.3.2 of “A set of open-source tools for Turkish natural language processing.”[2]

Consider Example 3.1.1: handtagged.txt .

The tag string <b> is twice as frequent as <a>. However, model 1 scores b<a> and b<b> equally because neither analysis string appears in the corpus.

This model splits each analysis string into a root, , and the part of the analysis string that isn’t the root, . An analysis string’s root is its first lemma. The of a<b>+c<d> is a , and its is <b>+c<d> . The tagger assigns each analysis string a score of with add-one smoothing. (Without additive smoothing, this model would be the same as model 1.)[9] The tagger assigns higher scores to unknown analysis strings with frequent than to unknown analysis strings with infrequent .

Given the lexical unit ^b/b<a>/b<b>$, the tagger assigns the analysis string b<a> a score of

Note that counts the analysis string being scored. For example, the tagger would assign the known analysis string a<a> a score of

The tagger assigns the analysis string b<b> a score of

File Format

Model 3

See section 5.3.3 of “A set of open-source tools for Turkish natural language processing.”[2]

Consider Example 3.1.1: handtagged.txt .

The morpheme a<b> is twice as frequent as the morpheme a<a> . However, model 2 scores the analysis strings a<a>+a<a> and a<b>+a<a> equally because the of neither appears in the corpus.

This model splits each analysis string into an a first inflection, and a sequence of derivation-inflection pairs, The of the analysis string a<b>+c<d> is a , its is <b> , and its is c<d> , where its is c , and its is <d> . The tagger assigns each analysis string a score of with add-one smoothing. The tagger assigns higher scores to unknown analysis strings with frequent than to unknown analysis strings with infrequent

Given the lexical unit ^aa/a<a>+a<a>/a<b>+a<a>$ , the tagger assigns the analysis string a<a>+a<a> a score of

The tagger assigns the analysis string a<b>+a<a> a score of

File Format

Notes

  1. 1.0 1.1 1.2 https://github.com/m5w/apertium
  2. 2.0 2.1 2.2 2.3 2.4 2.5 2.6 http://coltekin.net/cagri/papers/trmorph-tools.pdf
  3. Installation#If you want to add language data / do more advanced stuff
  4. Minimal installation from SVN#Set up environment
  5. Minimal installation from SVN#Configure, build, and install
  6. 6.0 6.1 http://en.cppreference.com/w/cpp/types/size_t
  7. http://en.cppreference.com/w/cpp/container/map
  8. https://github.com/m5w/apertium/blob/master/apertium/analysis.h