Skip to content

Instantly share code, notes, and snippets.

@johl
Last active December 16, 2015 23:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johl/5515041 to your computer and use it in GitHub Desktop.
Save johl/5515041 to your computer and use it in GitHub Desktop.
from __future__ import print_function
import json
import re
import sys
import urllib2
if len(sys.argv) < 2:
sys.exit('Abfrage mit: %s Substantiv' % sys.argv[0])
substantiv = sys.argv[1]
try:
r = urllib2.urlopen(
'http://de.wiktionary.org/w/api.php?' +
'format=json' +
'&action=query' +
'&prop=revisions' +
'&rvprop=content' +
'&titles=' +
substantiv
).read()
except urllib2.URLError, e:
sys.exit('Problem beim Zugriff auf Wiktionary.')
content = json.loads(r)
page = content['query']['pages'][content['query']['pages'].keys()[0]]
if 'revisions' not in page.keys():
sys.exit(
'Substantiv nicht im deutschsprachigen Wiktionary verzeichnet.'
)
wikitext = (page['revisions'][0]['*'])
match = re.search(
'===\s?{{Wortart\|Substantiv\|Deutsch}},\s?' +
'{{([mfn])([mfn])?([mfn])?}}' +
'(,\s?{{([mfn])}}(,\s?{{([mfn])}})?)*' +
'\s?===',
wikitext)
if match is None:
sys.exit('Kein Substantiv!')
genera = filter(lambda x: re.match('^[mfn]$', x),
filter(lambda x: x is not None, match.groups()))
m = ["der " + substantiv for genus in genera if genus == "m"]
f = ["die " + substantiv for genus in genera if genus == "f"]
n = ["das " + substantiv for genus in genera if genus == "n"]
artikel = m + f + n
map(print, artikel)
@johl
Copy link
Author

johl commented May 4, 2013

$ python migrationsschatten.py Knoblauch
der Knoblauch
$ python migrationsschatten.py Ketchup
der Ketchup
das Ketchup
$ python migrationsschatten.py Joghurt
der Joghurt
das Joghurt
die Joghurt
$ python migrationsschatten.py finden
Kein Substantiv!
$ python migrationsschatten.py djshdjhsdowhdowde
Substantiv nicht im deutschsprachigen Wiktionary verzeichnet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment