Last active
December 17, 2015 11:49
-
-
Save albertskog/5605279 to your computer and use it in GitHub Desktop.
Get the article of german nouns from Wiktionary
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>der die das?</title> | |
<meta http-equiv="content-type" content="text/html; charset=UTF-8" /> | |
<link rel="icon" href="favicon.ico" /> | |
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.js"></script> | |
<script> | |
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ | |
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), | |
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) | |
})(window,document,'script','//www.google-analytics.com/analytics.js','ga'); | |
ga('create', 'UA-5057680-6', 'dropboxusercontent.com'); | |
ga('send', 'pageview'); | |
</script> | |
<style type='text/css'>@import url('https://getbarometer.s3.amazonaws.com/assets/barometer/css/barometer.css');</style> | |
<script src='https://getbarometer.s3.amazonaws.com/assets/barometer/javascripts/barometer.js' type='text/javascript'></script> | |
<script type="text/javascript" charset="utf-8"> | |
BAROMETER.load('8GZwbeAaxhzabE39qQyEL'); | |
</script> | |
<style type="text/css"> | |
html, body { | |
font-family: helvetica; | |
text-align: center; | |
font-size: 60pt; | |
height: 100%; | |
width: 100%; | |
margin: 0; | |
padding: 0; | |
} | |
//p {border: 1px solid;} | |
a { | |
text-decoration: none; | |
color: inherit; | |
} | |
a:hover { | |
text-decoration: underline; | |
} | |
#top{ | |
height: 10%; | |
} | |
input, button { | |
font-size: 30pt; | |
font-family: inherit; | |
padding: 5px; | |
margin: 5px; | |
/* the following ensures they're all using the same box-model for rendering */ | |
-moz-box-sizing: content-box; /* or `border-box` */ | |
-webkit-box-sizing: content-box; | |
box-sizing: content-box; | |
} | |
#input{ | |
width: 300px; | |
height: 48px; | |
color: #888; | |
} | |
#search{ | |
cursor: pointer; | |
/*display: inline-block;*/ | |
width: 48px; | |
height: 48px; | |
border: 0; | |
background: url(right.png); | |
background-repeat: no-repeat; | |
vertical-align: bottom; | |
} | |
#article{ | |
font-weight: bold; | |
} | |
</style> | |
</head> | |
<body> | |
<p id = "top"></p> | |
<p id = "answer">der die das?</p> | |
<input type="text" id="input" value="Nutella"> | |
<button id="search"></button> | |
<script type="text/javascript"> | |
$("#input").keyup(function(event){ | |
if(event.keyCode == 13){ | |
$("#search").click(); | |
} | |
}); | |
var origValues = new Array(); | |
$("#input").click(function(){ | |
if( | |
origValues[this.name] == null | |
|| this.value == origValues[this.name] | |
) { | |
origValues[this.name] = this.value; | |
this.value = ''; | |
$(this).css('color','#000'); | |
} | |
}); | |
$("#search").click(function () { | |
//$('#answer').fadeTo(10, 0); | |
var rawInput = $('#input').val(); | |
//console.log(word); | |
var word = rawInput.toUpperCase().substr(0,1).concat( rawInput.toLowerCase().substr( 1, rawInput.length ) ) ; | |
$('#input').val(word); | |
var apiCallBase = 'https://de.wiktionary.org/w/api.php?action=query&format=json&prop=revisions&rvprop=content&indexpageids&callback=?&titles='; | |
var apiCall = apiCallBase.concat( word ); | |
$.getJSON( apiCall, (function parser(data) { | |
var answer = []; | |
if (data['query']['pageids'][0] == '-1'){ | |
answer.push( word.concat(' gibt es nicht') ); | |
} | |
else{ | |
var urlBase = 'http://de.wiktionary.org/wiki/'; | |
answer.push('<a href="' + urlBase + word + '">'); | |
var content = data['query']['pages'][data['query']['pageids'][0]]['revisions']['0']['*'].substr(0, 200); | |
//console.log(content); | |
var wortartPos = content.search('{{Wortart'); | |
//console.log(wortartPos); | |
var wortart = content.substr(wortartPos + 10, 3); | |
//console.log(wortart); | |
if (wortart != 'Sub'){ | |
answer.push( word.concat(' ist kein Substantiv') ); | |
} | |
else{ | |
var articles = {'m': 'der', 'f': 'die', 'n': 'das'}; | |
var article = articles[ content.substr(wortartPos + 34, 1) ]; | |
answer.push('<span id="article">' + article+ '</span>'); | |
var i = 1; | |
while ( article = articles[ content.substr(wortartPos + 34 + 7 * i++, 1) ] ) | |
{ | |
answer.push('/ <span id="article">' + article + '</span>'); | |
} | |
answer.push(word); | |
}; | |
}; | |
answer.push('</a>'); | |
$('#answer').html(answer.join(' '));//.fadeTo( 1, 10 ); | |
})); | |
}); | |
</script> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/opt/local/bin/python | |
import sys | |
import json | |
import urllib2 | |
word = sys.argv[1] | |
searchstring = 'http://de.wiktionary.org/w/api.php?action=query&format=json&prop=revisions&titles=%s&rvprop=content' % word | |
data = urllib2.urlopen(searchstring) | |
j = json.load(data) | |
t = str(j) | |
n = t.find('{{Wortart|Substantiv|Deutsch}}', 0, 200) | |
articles = {'m': 'der', 'f': 'die', 'n': 'das'} | |
if n > 0 : | |
print articles[t[n+34]] + ' ' + word | |
else: | |
print 'Word not found' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment