POS tagging in German

Question:

I am using NLTK to extract nouns from a text-string starting with the following command:

tagged_text = nltk.pos_tag(nltk.Text(nltk.word_tokenize(some_string)))

It works fine in English. Is there an easy way to make it work for German as well?

(I have no experience with natural language programming, but I managed to use the python nltk library which is great so far.)

Asked By: Johannes Meier

||

Answers:

Natural language software does its magic by leveraging corpora and the statistics they provide. You’ll need to tell nltk about some German corpus to help it tokenize German correctly. I believe the EUROPARL corpus might help get you going.

See nltk.corpus.europarl_raw and this answer for example configuration.

Also, consider tagging this question with “nlp”.

Answered By: Mike Atlas

Part-of-Speech (POS) tagging is very specific to a particular [natural] language. NLTK includes many different taggers, which use distinct techniques to infer the tag of a given token in a given token. Most (but not all) of these taggers use a statistical model of sorts as the main or sole device to “do the trick”. Such taggers require some “training data” upon which to build this statistical representation of the language, and the training data comes in the form of corpora.

The NTLK “distribution” itself includes many of these corpora, as well a set of “corpora readers” which provide an API to read different types of corpora. I don’t know the state of affairs in NTLK proper, and if this includes any german corpus. You can however locate free some free corpora which you’ll then need to convert to a format that satisfies the proper NTLK corpora reader, and then you can use this to train a POS tagger for the German language.

You can even create your own corpus, but that is a hell of a painstaking job; if you work in a univeristy, you gotta find ways of bribing and otherwise coercing students to do that for you 😉

Answered By: mjv

I have written a blog-post about how to convert the German annotated TIGER Corpus in order to use it with the NLTK. Have a look at it here.

Answered By: Philipp

The Pattern library includes a function for parsing German sentences and the result includes the part-of-speech tags. The following is copied from their documentation:

from pattern.de import parse, split
s = parse('Die Katze liegt auf der Matte.')
s = split(s)
print s.sentences[0]

>>>   Sentence('Die/DT/B-NP/O Katze/NN/I-NP/O liegt/VB/B-VP/O'
     'auf/IN/B-PP/B-PNP der/DT/B-NP/I-PNP Matte/NN/I-NP/I-PNP ././O/O')

Update: Another option is spacy, there is a quick example in this blog article:

import spacy

nlp = spacy.load('de')
doc = nlp(u'Ich bin ein Berliner.')

# show universal pos tags
print(' '.join('{word}/{tag}'.format(word=t.orth_, tag=t.pos_) for t in doc))
# output: Ich/PRON bin/AUX ein/DET Berliner/NOUN ./PUNCT
Answered By: Suzana

Possibly you can use the Stanford POS tagger. Below is a recipe I wrote. There are python recipes for German NLP that I’ve compiled and you can access them on http://htmlpreview.github.io/?https://github.com/alvations/DLTK/blob/master/docs/index.html

#-*- coding: utf8 -*-

import os, glob, codecs

def installStanfordTag():
    if not os.path.exists('stanford-postagger-full-2013-06-20'):
        os.system('wget http://nlp.stanford.edu/software/stanford-postagger-full-2013-06-20.zip')
        os.system('unzip stanford-postagger-full-2013-06-20.zip')
    return

def tag(infile):
    cmd = "./stanford-postagger.sh "+models[m]+" "+infile
    tagout = os.popen(cmd).readlines()
    return [i.strip() for i in tagout]

def taglinebyline(sents):
    tagged = []
    for ss in sents:
        os.popen("echo '''"+ss+"''' > stanfordtemp.txt")
        tagged.append(tag('stanfordtemp.txt')[0])
    return tagged

installStanfordTag()
stagdir = './stanford-postagger-full-2013-06-20/'
models = {'fast':'models/german-fast.tagger',
          'dewac':'models/german-dewac.tagger',
          'hgc':'models/german-hgc.tagger'}
os.chdir(stagdir)
print os.getcwd()


m = 'fast' # It's best to use the fast german tagger if your data is small.

sentences = ['Ich bin schwanger .','Ich bin wieder schwanger .','Ich verstehe nur Bahnhof .']

tagged_sents = taglinebyline(sentences) # Call the stanford tagger

for sent in tagged_sents:
    print sent
Answered By: alvas

It seems to be a little late to answer the question, but it might be helpful for anyone who finds this question by googling like i did. So i’d like to share the things I found out.

The HannoverTagger might be a useful tool for this Task.
You can find tutorials here and here(german), but the second one is in german.

The Tagger seems to use the STTS Tagset, if you need a complete list of all Tags.

Answered By: Christoph
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.