Difference between revisions of "Sentence segmenting"

From Apertium
Jump to navigation Jump to search
Line 6: Line 6:


===NLTK Punkt===
===NLTK Punkt===

You will need to install NLTK and NLTK data. Unfortunately, they both only support Python versions 2.6-2.7.

;Prerequisites & installing NLTK:

If you do not have these, please install them in the following order:
* Setuptools: <code>sudo apt-get install python-setuptools</code>
* Pip: <code>sudo easy_install pip</code>
* (optional) Numpy: <code>sudo pip install -U numpy</code>
* PyYAML and NLTK: <code>run sudo pip install -U pyyaml nltk</code>

To be sure that it has been correctly installed, type <code>python</code>. Then, type <code>import nltk</code>. Nothing should be output. Type <code>quit()</code> to quit.

;Installing NLTK data

NLTK data is also commonly called nltk.data.

Once you have installed NLTK, you can run the NLTK Downloader to install nltk.data.
First, type <code>python</code> to start the Python interpreter. Then, type <code>import nltk</code>. Then, type <code>nltk.download()</code> to open the NLTK Downloader. To download the sentence tokenisation package, <code>nltk.tokenize.punkt</code>, type <code>d punkt</code>. If you want to get everything (optional but recommended), type <code>d all</code>. For English, you also need the "corpora" package.


;Training
;Training

Type <code>import nltki</code>. If you are tokenising a language that uses different punctuation than English (i.e. Armenian), then you need to set the PunktLanguageVars:
<pre>
armenian_punkt_vars = nltk.tokenize.punkt.PunktLanguageVars
armenian_punkt_vars.sent_end_chars=('։','՞','՜','.')
</pre>

Put the characters that are likely to end sentences in single quotation marks as arguments of the <code>sent_end_chars()</code> function above (second line). Depending on your language, you can change the variable <code>armenian_punkt_vars</code> to whatever is more appropriate.

Now, it's time to train the trainer!

If the language uses punctuation that is different from English, add the PunktLanguageVars variable as a second argument for PunktTrainer(), like this:
<pre>
trainer = nltk.tokenize.punkt.PunktTrainer(text, armenian_punkt_vars)
params = trainer.get_params()
sbd = nltk.tokenize.punkt.PunktSentenceTokenizer(params)
</pre>

* The variable <code>text</code> is the corpus that you want to train the trainer on.
* <code>armenian_punkt_vars</code> is your PunktLanguageVars variable.
* We'll use the <code>params</code> variable later.

If the language uses the same punctuation as English, you create the trainer slightly differently:
<pre>
trainer = nltk.tokenize.punkt.PunktTrainer(raw)
trainer.INCLUDE_ALL_COLLOCS = True
trainer.INCLUDE_ABBREV_COLLOCS = True
params = trainer.get_params()
</pre>

* <code>raw</code> is the (string) corpus you want to train the trainer on.
* <code>INCLUDE_ALL_COLLOCS</code> makes the trainer look for and remember abbreviations and initialisms.
* <code>INCLUDE_ABBREV_COLLOCS</code> makes the trainer look for and remember word pairs where the first word is an abbreviation. It has to be placed after <code>INCLUDE_ALL_COLLOCS</code>.
* We'll use the <code>params</code> variable later.

For every language, assuming that the variable <code>text</text> is the corpus you want to train your trainer on, do:

<pre>
trainer.train(text)
</pre>
You must do this step to get a usable trainer object.

Now your trainer has been trained!


;Usage
;Usage

If you want to tokenize English sentences, please skip ahead.

For non-English languages:

First, we must define a sentence boundary detector:
<pre>
sbd = PunktSentenceTokenizer(params)
</pre>
That is where we use the params variable that was created in the "Training" section.

Now, we will use the trainer to tokenize our text:

<pre>
for sentence in sbd.sentences_from_text(text, realign_boundaries=True):
</pre>

* <code>text</code> is the string of text that you want to tokenize

Inside the for loop, you can do anything with the newly tokenized sentence (stored in the <code>sentence</code> variable). For example, you could print them, separated by a newline:

<pre>
print sentence + "\n"
</pre>

Now you're finished!
<br />
'''Tokenize English sentences'''
<br />

Load the english pickle (provided):

<pre>
sent_detector = nltk.data.load('tokenizers/punkt/english.pickle')
</pre>
By default, that is the path to <code>english.pickle</code>. Change it if you install nltk_data to a different directory when you downloaded it.

Now, you can tokenize the sentences:

<pre>
sentences = sent_detector.tokenize(text.strip())
</pre>

* <code>text</code> is the string of text that you want to tokenize
* sentences (the created variable) is a list of the tokenized sentences (each value is a separate sentence)


===splitta===
===splitta===

Splitta only works with Python 2.5 or later. It doesn't work with Python 3.


;Training
;Training


Unfortunately, it only works for English. You can find the developer's email address [http://www.dgillick.com/ here].
;Usage

;Usage

You can download the latest version of splitta [http://code.google.com/p/splitta/downloads/list here].
Extract the files to a directory.

Download the SVM Light binaries from http://svmlight.joachims.org/ and extract all files any directory. You will need to know the file path of that directory.

Open "sbd.py" (in the splitta directory). Scroll to where is says "## globals". You will see these too lines:

<pre>
SVM_LEARN = '/u/dgillick/tools/svm_light/svm_learn'
SVM_CLASSIFY = '/u/dgillick/tools/svm_light/svm_classify'
</pre>

Replace the two file paths to wherever you put the files from SVM Light. For example, if you extracted them to the same directory where splitta is, then it would be:

<pre>
SVM_LEARN = 'svm_learn'
SVM_CLASSIFY = 'svm_classify'
</pre>

Open Terminal and cd to the directory where splitta is. The simplest command to tokenize paragraphs into sentences is:

<pre>python sbd.py -m model_nb corpus.txt</pre>

That is assuming that corpus.txt is your corpus file. Splitta provides a sample English corpus, called "sample.txt", for you to test with. It prints out:

<pre>
loading model from [model_nb/]... done!
reading [corpus.txt]
featurising... done!
NB classifying... done!
<pre>

Afterwards, the tokenized sentences are outputted to the screen.

You can specify an output file (the sentences will be outputted to the file) using the -o command:

<pre>python sbd.py -m model_nb -o output.txt corpus.txt</pre>


===MxTerminator===
===MxTerminator===

Get `jmx.tar.gz` from [ftp://ftp.cis.upenn.edu/pub/adwait/jmx/ here]. Extract it to an empty directory. Edit your CLASSPATH to include mxpost.jar:

<pre>
export CLASSPATH=/usr/home/<yourname>/<yourdir>/mxpost.jar
</pre>

Replace `<yourname>` with the name of the user and replace `<yourdir>` with the path to mxpost.jar

The general instructions (too minimalistic/unhelpful) are [file:///home/daniel/jmx/MXTERMINATOR.html here].


;Training
;Training


Create an empty directory. This will be the directory for your project. In the directory, you must place a data file. This should contain many sentences in the language that are split by newlines. Then, run this command (traindata is the file aforementioned):
;Usage

<pre>
./trainmxterminator projectdir traindata
</pre>

Note: According to the developer, it should work for utf-8

;Usage

Run the command below. Replace <code>projectdir</code> with the directory of your project (a sample project, <code>eos.project</code>, is included) and <code>textfile</code> with the raw text that you want to tokenize.
<pre>
./mxterminator projectdir < textfile
</pre>


===Sentrick===
===Sentrick===


'''WILL BE UPDATED!'''
URL: http://www.denkselbst.de/sentrick2/

[http://www.denkselbst.de/sentrick2/ Home]
<br />
[http://www.denkselbst.de/sentrick/faq.html FAQ] (mostly troubleshooting)
<br />
[http://www.denkselbst.de/sentrick2/ New usage page]


;Training
;Training


Get the developer's version
Some information [http://sentrick.git.sourceforge.net/git/gitweb.cgi?p=sentrick/sentrick;a=blob;f=doc/readme.txt;h=6ef6ea2789a2f41dbcf542f5f076977f2e519585;hb=HEAD here].

<pre>
git clone git://sentrick.git.sourceforge.net/gitroot/sentrick/sentrick
</pre>

Run <code>ant clean dist</code>. If it does not work, go to <code>common-targets.xml</code> (under <code>ant</code>) and comment out/delete everything between <code><target name="test" depends="compile.tests" description="run junit tests" ></code> and <code></target></code> (advice from developer; it helps).

If it complains about encoding, add the attribute <code>encoding="MacRoman"<code> to the two <code><java c></code> tags in <code>common-targets.xml</code>.

It will generate a folder called <code>dist</code>. In terminal, type <code>cd dist</code> to get to the dist directory.

You need some input data with segmented sentences separated by a newline. `editor.sh` and `snippetCollector.sh` (two minumalistic GUI tools) will save the boundary positions (punctuation that ends sentences) in a .bps file:

Type <code>sh editor.sh</code> to open the Sentrick Sentence Boundary Editor (aka editor.sh).

In the left panel, navigate to the input data aforementioned. You can change the encoding (not recommended) or how to detect the boundaries (which sentence boundary detector/sbd to use) using the two dropdown menus on the left. Then, press the "Save Boundary Positions" button on the right. It will generate a .bps file with the same name as your .txt input file in the same directory.

Put your .bps and .txt files into a directory.

snippetCollector accomplishes the same thing, it just assumes that you do not have an input file. You can input the text into the textbox on the left and press Segment. Then, press "Save txt,bps as...". It does not let you change the encoding.

Use tdgen.sh (already-built) to create four .pl files.

This is the usage:

<pre>
tdgen [txt,bps root directory] [encoding] [SbdProvider (for resources)] [training data output directory] <token id prefix>
</pre>

Here's an example command:

<pre>
sh tdgen.sh dir utf-8 de.denkselbst.sentrick.sbd.NoSbdProvider ../out
<pre>

dir is the directory of the .txt and the .bps files. de.denkselbst.sentrick.sbd.NoSbdProvider is the sbd. ../out is the output directory (where your 4 .pl files will end up).


;Usage
;Usage
[http://sourceforge.net/projects/sentrick/files/latest/download Download] the latest version of Sentrick. Currently, Sentrick comes with sentence tokenisation for English and German.

Extract the files into a directory. Sentrick needs two arguments to run: the input file that contains paragraphs that need to be tokenized (<code>-i</code>) and the file it will output the tokenized sentences to (<code>-o</code>). An example command is:

<pre>
./sentrick/bin/sentrick.sh -i input.txt -o output.txt
</pre>

You '''must''' run <code>sentrick.sh</code> from outside the bin directory.

By default, the encodings of both file will be UTF-8. Optionally, you can declare the encodings of the two files using <code>-ie</code> (for the input file) and <code>-oe</code> (for the output file). For example, if you want the input file to be encoded as UCS-2 and the output file to be encoded as UTF-32, you could use this code:

<pre>
./sentrick/bin/sentrick.sh -i input.txt -o output.txt -ie UCS-2 -oe UTF-32
</pre>

If you want to tell Sentrick what language the input file is in (it defaults to English), then you can use the <code>-l</code> argument. Currently, only English (en) and German (de) are supported.


[[Category:Documentation in English]]
[[Category:Documentation in English]]

Revision as of 23:20, 1 January 2013

This page gives a review and usage instructions for some sentence-segmenting tools.

Tools

NLTK Punkt

You will need to install NLTK and NLTK data. Unfortunately, they both only support Python versions 2.6-2.7.

Prerequisites & installing NLTK

If you do not have these, please install them in the following order:

  • Setuptools: sudo apt-get install python-setuptools
  • Pip: sudo easy_install pip
  • (optional) Numpy: sudo pip install -U numpy
  • PyYAML and NLTK: run sudo pip install -U pyyaml nltk

To be sure that it has been correctly installed, type python. Then, type import nltk. Nothing should be output. Type quit() to quit.

Installing NLTK data

NLTK data is also commonly called nltk.data.

Once you have installed NLTK, you can run the NLTK Downloader to install nltk.data. First, type python to start the Python interpreter. Then, type import nltk. Then, type nltk.download() to open the NLTK Downloader. To download the sentence tokenisation package, nltk.tokenize.punkt, type d punkt. If you want to get everything (optional but recommended), type d all. For English, you also need the "corpora" package.

Training

Type import nltki. If you are tokenising a language that uses different punctuation than English (i.e. Armenian), then you need to set the PunktLanguageVars:

armenian_punkt_vars = nltk.tokenize.punkt.PunktLanguageVars
armenian_punkt_vars.sent_end_chars=('։','՞','՜','.')

Put the characters that are likely to end sentences in single quotation marks as arguments of the sent_end_chars() function above (second line). Depending on your language, you can change the variable armenian_punkt_vars to whatever is more appropriate.

Now, it's time to train the trainer!

If the language uses punctuation that is different from English, add the PunktLanguageVars variable as a second argument for PunktTrainer(), like this:

trainer = nltk.tokenize.punkt.PunktTrainer(text, armenian_punkt_vars)
params = trainer.get_params()
sbd = nltk.tokenize.punkt.PunktSentenceTokenizer(params)
  • The variable text is the corpus that you want to train the trainer on.
  • armenian_punkt_vars is your PunktLanguageVars variable.
  • We'll use the params variable later.

If the language uses the same punctuation as English, you create the trainer slightly differently:

trainer = nltk.tokenize.punkt.PunktTrainer(raw) 
trainer.INCLUDE_ALL_COLLOCS = True 
trainer.INCLUDE_ABBREV_COLLOCS = True
params = trainer.get_params()
  • raw is the (string) corpus you want to train the trainer on.
  • INCLUDE_ALL_COLLOCS makes the trainer look for and remember abbreviations and initialisms.
  • INCLUDE_ABBREV_COLLOCS makes the trainer look for and remember word pairs where the first word is an abbreviation. It has to be placed after INCLUDE_ALL_COLLOCS.
  • We'll use the params variable later.

For every language, assuming that the variable text</text> is the corpus you want to train your trainer on, do:

trainer.train(text)

You must do this step to get a usable trainer object.

Now your trainer has been trained!

Usage

If you want to tokenize English sentences, please skip ahead.

For non-English languages:

First, we must define a sentence boundary detector:

sbd = PunktSentenceTokenizer(params)

That is where we use the params variable that was created in the "Training" section.

Now, we will use the trainer to tokenize our text:

for sentence in sbd.sentences_from_text(text, realign_boundaries=True):
  • text is the string of text that you want to tokenize

Inside the for loop, you can do anything with the newly tokenized sentence (stored in the sentence variable). For example, you could print them, separated by a newline:

print sentence + "\n"

Now you're finished!
Tokenize English sentences

Load the english pickle (provided):

sent_detector = nltk.data.load('tokenizers/punkt/english.pickle')

By default, that is the path to english.pickle. Change it if you install nltk_data to a different directory when you downloaded it.

Now, you can tokenize the sentences:

sentences = sent_detector.tokenize(text.strip())
  • text is the string of text that you want to tokenize
  • sentences (the created variable) is a list of the tokenized sentences (each value is a separate sentence)

splitta

Splitta only works with Python 2.5 or later. It doesn't work with Python 3.

Training

Unfortunately, it only works for English. You can find the developer's email address here.

Usage

You can download the latest version of splitta here. Extract the files to a directory.

Download the SVM Light binaries from http://svmlight.joachims.org/ and extract all files any directory. You will need to know the file path of that directory.

Open "sbd.py" (in the splitta directory). Scroll to where is says "## globals". You will see these too lines:

SVM_LEARN = '/u/dgillick/tools/svm_light/svm_learn'
SVM_CLASSIFY = '/u/dgillick/tools/svm_light/svm_classify'

Replace the two file paths to wherever you put the files from SVM Light. For example, if you extracted them to the same directory where splitta is, then it would be:

SVM_LEARN = 'svm_learn'
SVM_CLASSIFY = 'svm_classify'

Open Terminal and cd to the directory where splitta is. The simplest command to tokenize paragraphs into sentences is:

python sbd.py -m model_nb corpus.txt

That is assuming that corpus.txt is your corpus file. Splitta provides a sample English corpus, called "sample.txt", for you to test with. It prints out:

loading model from [model_nb/]... done!
reading [corpus.txt]
featurising... done!
NB classifying... done!
<pre>

Afterwards, the tokenized sentences are outputted to the screen.

You can specify an output file (the sentences will be outputted to the file) using the -o command:

<pre>python sbd.py -m model_nb -o output.txt corpus.txt

MxTerminator

Get `jmx.tar.gz` from here. Extract it to an empty directory. Edit your CLASSPATH to include mxpost.jar:

export CLASSPATH=/usr/home/<yourname>/<yourdir>/mxpost.jar

Replace `<yourname>` with the name of the user and replace `<yourdir>` with the path to mxpost.jar

The general instructions (too minimalistic/unhelpful) are [file:///home/daniel/jmx/MXTERMINATOR.html here].

Training

Create an empty directory. This will be the directory for your project. In the directory, you must place a data file. This should contain many sentences in the language that are split by newlines. Then, run this command (traindata is the file aforementioned):

./trainmxterminator projectdir traindata

Note: According to the developer, it should work for utf-8

Usage

Run the command below. Replace projectdir with the directory of your project (a sample project, eos.project, is included) and textfile with the raw text that you want to tokenize.

./mxterminator projectdir < textfile

Sentrick

WILL BE UPDATED!

Home
FAQ (mostly troubleshooting)
New usage page

Training

Get the developer's version

git clone git://sentrick.git.sourceforge.net/gitroot/sentrick/sentrick

Run ant clean dist. If it does not work, go to common-targets.xml (under ant) and comment out/delete everything between <target name="test" depends="compile.tests" description="run junit tests" > and </target> (advice from developer; it helps).

If it complains about encoding, add the attribute encoding="MacRoman" to the two <java c> tags in common-targets.xml.

It will generate a folder called dist. In terminal, type cd dist to get to the dist directory.

You need some input data with segmented sentences separated by a newline. `editor.sh` and `snippetCollector.sh` (two minumalistic GUI tools) will save the boundary positions (punctuation that ends sentences) in a .bps file:

Type sh editor.sh to open the Sentrick Sentence Boundary Editor (aka editor.sh).

In the left panel, navigate to the input data aforementioned. You can change the encoding (not recommended) or how to detect the boundaries (which sentence boundary detector/sbd to use) using the two dropdown menus on the left. Then, press the "Save Boundary Positions" button on the right. It will generate a .bps file with the same name as your .txt input file in the same directory.

Put your .bps and .txt files into a directory.

snippetCollector accomplishes the same thing, it just assumes that you do not have an input file. You can input the text into the textbox on the left and press Segment. Then, press "Save txt,bps as...". It does not let you change the encoding.

Use tdgen.sh (already-built) to create four .pl files.

This is the usage:

tdgen [txt,bps root directory] [encoding] [SbdProvider (for resources)] [training data output directory] <token id prefix>

Here's an example command:

sh tdgen.sh dir utf-8 de.denkselbst.sentrick.sbd.NoSbdProvider ../out
<pre>

dir is the directory of the .txt and the .bps files. de.denkselbst.sentrick.sbd.NoSbdProvider is the sbd. ../out is the output directory (where your 4 .pl files will end up).

;Usage 
[http://sourceforge.net/projects/sentrick/files/latest/download Download] the latest version of Sentrick. Currently, Sentrick comes with sentence tokenisation for English and German.

Extract the files into a directory. Sentrick needs two arguments to run: the input file that contains paragraphs that need to be tokenized (<code>-i</code>) and the file it will output the tokenized sentences to (<code>-o</code>). An example command is:

<pre>
./sentrick/bin/sentrick.sh -i input.txt -o output.txt

You must run sentrick.sh from outside the bin directory.

By default, the encodings of both file will be UTF-8. Optionally, you can declare the encodings of the two files using -ie (for the input file) and -oe (for the output file). For example, if you want the input file to be encoded as UCS-2 and the output file to be encoded as UTF-32, you could use this code:

./sentrick/bin/sentrick.sh -i input.txt -o output.txt -ie UCS-2 -oe UTF-32

If you want to tell Sentrick what language the input file is in (it defaults to English), then you can use the -l argument. Currently, only English (en) and German (de) are supported.