Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to avoid NLTK's sentence tokenizer splitting on abbreviations?

I'm currently using NLTK for language processing, but I have encountered a problem of sentence tokenizing.

Here's the problem: Assume I have a sentence: "Fig. 2 shows a U.S.A. map." When I use punkt tokenizer, my code looks like this:

from nltk.tokenize.punkt import PunktSentenceTokenizer, PunktParameters
punkt_param = PunktParameters()
abbreviation = ['U.S.A', 'fig']
punkt_param.abbrev_types = set(abbreviation)
tokenizer = PunktSentenceTokenizer(punkt_param)
tokenizer.tokenize('Fig. 2 shows a U.S.A. map.')

It returns this:

['Fig. 2 shows a U.S.A.', 'map.']

The tokenizer can't detect the abbreviation "U.S.A.", but it worked on "fig". Now when I use the default tokenizer NLTK provides:

import nltk
nltk.tokenize.sent_tokenize('Fig. 2 shows a U.S.A. map.')

This time I get:

['Fig.', '2 shows a U.S.A. map.']

It recognizes the more common "U.S.A." but fails to see "fig"!

How can I combine these two methods? I want to use default abbreviation choices as well as adding my own abbreviations.

like image 568
joe wong Avatar asked Jan 15 '16 07:01

joe wong


People also ask

Which tokenizer is used to split the punctuation?

The punctuation-based tokenizer splits the given text based on punctuation and whitespace. The punctuation-based tokenizer will split the words having punctuations in them too like platform. s is the whole word but using punctuation tokenizer the word will convert into 'platform', '. ', 's'.

What does nltk's function Word_tokenize () do?

word_tokenize is a function in Python that splits a given sentence into words using the NLTK library.

How does Punkt sentence tokenizer work?

This tokenizer divides a text into a list of sentences by using an unsupervised algorithm to build a model for abbreviation words, collocations, and words that start sentences. It must be trained on a large collection of plaintext in the target language before it can be used.

How is str split () different from word tokenizer?

tokenize() ,which returns a list, will ignore empty string (when a delimiter appears twice in succession) where as split() keeps such string. The split() can take regex as delimiter where as tokenize does not.


1 Answers

I think lower case for u.s.a in abbreviations list will work fine for you Try this,

from nltk.tokenize.punkt import PunktSentenceTokenizer, PunktParameters
punkt_param = PunktParameters()
abbreviation = ['u.s.a', 'fig']
punkt_param.abbrev_types = set(abbreviation)
tokenizer = PunktSentenceTokenizer(punkt_param)
tokenizer.tokenize('Fig. 2 shows a U.S.A. map.')

It returns this to me:

['Fig. 2 shows a U.S.A. map.']
like image 109
Prashant Puri Avatar answered Sep 21 '22 12:09

Prashant Puri