Seiten

  • Startseite
  • Impressum
  • Inhalt
  • MINT
  • Sprache
  • Soziales
  • Geist
  • Kunst
  • Gemischtes
  • Gedichte

Sonntag, 21. September 2014

Doppelbeitrag


Deutsch

Ei­gent­lich soll­ten die nach­fol­gen­den zwei Ein­zel­bei­träge wer­den, aber da sie bei­de et­was mit In­fo und Spra­che zu tun ha­ben, veröffent­li­che ich sie in ei­nem Rit­te.

Und zwar geht es um zwei Funk­tio­nen, translate() und nope(), die ers­te eher nütz­lich, die zwei­te eher in­ter­essant. Bei­piel:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
<html>
  <meta charset="UTF-8">
  <?php
    $t = translate("de", "en", "The small cat sees the big dog.");
    $n = nope("Hund", "deu");
    print_r($t);
    echo "<br><br>";
    print_r($n);
  ?>
</html>

Die er­zeug­te Aus­ga­be ist:
Die kleine Katze sieht den großen Hund.

Array (
  [pie] => *ḱwṓ
  [itc] =>
  [lat] => canis
  [fra] => chien
  [gem] => *hundaz
  [eng] => hound
  [deu] => Hund
)


Übersetzungsfunktion in PHP


Die Funk­ti­on translate(), wel­che auf Goo­gle-Über­set­zer zu­greift, lie­fert die Über­set­zung ei­nes über­ge­be­nen Tex­tes zurück. Als wei­te­re Pa­ra­me­ter müssen die Kürzel von Aus­gangs- und Ziel­spra­che über­ge­ben wer­den.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
<?php
  function translate($source, $target, $text){
    $text = urlencode($text);
    $str = "https://translate.google.com/?sl=$source&tl=$target&text=$text";
    $site = file_get_contents($str);
    $site = substr($site, strpos($site, '<span id=result_box'));
    $site = substr($site, 0, strpos($site, '</div>'));
    $site = strip_tags($site);
    return utf8_encode($site);
  }
?>


Zufall?


Die Funk­ti­on nope(), wel­che auf Wiktio­na­ry zu­greift, lie­fert al­le ver­wand­ten Wörter (Urin­do­ger­ma­nisch, Ita­lisch, La­tein, Französisch, Ur­ger­ma­nisch, Eng­lisch, Deutsch) zu ei­nem über­ge­be­nen Wort in ei­ner der Spra­chen La­tein, Französisch, Eng­lisch oder Deutsch. Als wei­te­rer Pa­ra­me­ter muss das Sprachkürzel zum Wort an­ge­ge­ben wer­den.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
<?php
  function nope($x, $index){
    $array = array(
      "pie" => "",
      "itc" => "",
      "lat" => "",
      "fra" => "",
      "gem" => "",
      "eng" => "",
      "deu" => "",
    );
    $array[$index] = $x;
    $text = $x;
    if($index == "eng" || $index == "deu"){
      $gem = getGEM($text, $index);
      if($gem == null){
        return $array;
      }
      $array["gem"] = $gem;
      if($index == "eng"){
        $deu = getFromGEM($gem, "deu");
        if($deu !== null){
          $array["deu"] = $deu;
        }
      }
      else {
        $eng = getFromGEM($gem, "eng");
        if($eng !== null){
          $array["eng"] = $eng;
        }
      }
      $pie = getPIEfromGEM($gem);
      if($pie == null){
        return $array;
      }
      $array["pie"] = $pie;
      $ilf = getITCLATFRAfromPIE($pie);
      if($ilf[0] !== null){
        $array["itc"] = $ilf[0];
      }
      if($ilf[1] !== null){
        $array["lat"] = $ilf[1];
      }
      if($ilf[2] !== null){
        $array["fra"] = $ilf[2];
      }
    }
    else {
      if($index == "lat"){
        $pie = getPIEfromLAT($text);
      }
      elseif($index == "fra"){
        $pie = getPIEfromFRA($text);
      }
      if($pie == null){
        return $array;
      }
      $array["pie"] = $pie;
      $ilf = getITCLATFRAfromPIE($pie);
      if($ilf[0] !== null){
        $array["itc"] = $ilf[0];
      }
      if($text == "fra" && $ilf[1] !== null){
        $array["lat"] = $ilf[1];
      }
      if($text == "lat" && $ilf[2] !== null){
        $array["fra"] = $ilf[2];
      }
      if($ilf[3] !== null){
        $array["gem"] = $ilf[3];
      }
      else {
        return $array;
      }
      $deu = getFromGEM($array["gem"], "deu");
      if($deu !== null){
        $array["deu"] = $deu;
      }
      $eng = getFromGEM($array["gem"], "eng");
      if($eng !== null){
        $array["eng"] = $eng;
      }
    }
    return $array;
  }
  function getPIEfromFRA($fra){
    $fra = urlencode($fra);
    $str = "http://en.wiktionary.org/wiki/$fra";
    $site = @file_get_contents($str);
    if($site == "" || str_replace("Wiktionary does not yet have an entry for", "", $site) !== $site){
      return null;
    }
    $chap = substr($site, strpos($site, '<h2><span class="mw-headline" id="French'));
    if(strlen($chap) == strlen($site)){
      return null;
    }
    $chap = substr($chap, strpos($chap, '<h3><span class="mw-headline" id="Etymology'));
    $chap = substr($chap, 0, strpos($chap, '<h3><span class="mw-headline" id="Pronunciation'));
    $back = substr($chap, strpos($chap, '<i class="Latinx mention" lang="ine-pro'));
    if(strlen($back) == strlen($chap)){
      $back = substr($chap, strpos($chap, '<i class="Latinx mention" lang="la'));
      if(strlen($back) == strlen($chap)){
        return null;
      }
      $back = substr($back, 0, strpos($back, '</i>')+4);
      $back = strip_tags($back);
      if(str_replace(" ", "", $back) !== $back){ $back = substr($back, 0, strpos($back, " ")); }
      return getPIEfromLAT($back);
    }
    $back = substr($back, 0, strpos($back, '</i>')+4);
    $back = strip_tags($back);
    if(str_replace(" ", "", $back) !== $back){ $back = substr($back, 0, strpos($back, " ")); }
    return $back;
  }
  function getPIEfromLAT($lat){
    $lat = urlencode($lat);
    $str = "http://en.wiktionary.org/wiki/$lat";
    $site = @file_get_contents($str);
    if($site == "" || str_replace("Wiktionary does not yet have an entry for", "", $site) !== $site){
      return null;
    }
    $chap = substr($site, strpos($site, '<h2><span class="mw-headline" id="Latin'));
    if(strlen($chap) == strlen($site)){
      return null;
    }
    $chap = substr($chap, strpos($chap, '<h3><span class="mw-headline" id="Etymology'));
    $chap = substr($chap, 0, strpos($chap, '<h3><span class="mw-headline" id="Pronunciation'));
    $back = substr($chap, strpos($chap, '<i class="Latinx mention" lang="ine-pro"'));
    if(strlen($back) == strlen($chap)){
      return null;
    }
    $back = substr($back, 0, strpos($back, '</i>')+4);
    $back = strip_tags($back);
    if(str_replace(" ", "", $back) !== $back){ $back = substr($back, 0, strpos($back, " ")); }
    return $back;
  }
  function getITCLATFRAfromPIE($pie){
    $pie = str_replace("*", "", $pie);
    $pie = urlencode($pie);
    $str = "http://en.wiktionary.org/wiki/Appendix:Proto-Indo-European/$pie";
    $site = @file_get_contents($str);
    if($site == "" || str_replace("Wiktionary does not yet have an entry for", "", $site) !== $site){
      return null;
    }
    $chap = substr($site, strpos($site, '<h4><span class="mw-headline" id="Descendants'));
    $chap = substr($chap, strpos($chap, '<ul>'));
    $chap = substr($chap, 0, strpos($chap, '<!--'));
    $chap = strip_tags(str_replace("<li>", "|", $chap));
    $all = explode("|", $chap);
    $arr = array(null, null, null, null);
    for($i = 0; $i < count($all); $i++){
      $parts = explode(":", $all[$i]);
      if($parts[0] == "Italic"){
        $a = str_replace(html_entity_decode("&#10;"), "", substr($parts[1], 1, strlen($parts[1])-2));
        if(str_replace(" ", "", $a) !== $a){ $a = substr($a, 0, strpos($a, " ")); }
        $arr[0] = $a;
      }
      else if($parts[0] == "Latin"){
        $a = str_replace(html_entity_decode("&#10;"), "", substr($parts[1], 1, strlen($parts[1])-2));
        if(str_replace(" ", "", $a) !== $a){ $a = substr($a, 0, strpos($a, " ")); }
        $arr[1] = $a;
      }
      else if($parts[0] == "French"){
        $a = str_replace(html_entity_decode("&#10;"), "", substr($parts[1], 1, strlen($parts[1])-2));
        if(str_replace(" ", "", $a) !== $a){ $a = substr($a, 0, strpos($a, " ")); }
        $arr[2] = $a;
      }
      else if($parts[0] == "Germanic"){
        $a = str_replace(html_entity_decode("&#10;"), "", substr($parts[1], 1, strlen($parts[1])-2));
        if(str_replace(" ", "", $a) !== $a){ $a = substr($a, 0, strpos($a, " ")); }
        $arr[3] = $a;
      }
    }
    return $arr;
  }
  function getPIEfromGEM($gem){
    $gem = str_replace("*", "", $gem);
    $gem = urlencode($gem);
    $str = "http://en.wiktionary.org/wiki/Appendix:Proto-Germanic/$gem";
    $site = @file_get_contents($str);
    if($site == "" || str_replace("Wiktionary does not yet have an entry for", "", $site) !== $site){
      return null;
    }
    $chap = substr($site, strpos($site, '<h3><span class="mw-headline" id="Etymology'));
    $chap = substr($chap, 0, strpos($chap, '<h3><span class="mw-headline" id="Pronunciation'));
    $back = substr($chap, strpos($chap, 'Proto-Indo-European</a></span> <i class="Latinx mention" lang="ine-pro')+31);
    if(strlen($back) == strlen($chap)){
      return null;
    }
    $back = substr($back, 0, strpos($back, '</i>')+4);
    $back = strip_tags($back);
    if(str_replace(" ", "", $back) !== $back){ $back = substr($back, 0, strpos($back, " ")); }
    return $back;
  }
  function getFromGEM($gem, $to){
    $gem = str_replace("*", "", $gem);
    $gem = urlencode($gem);
    $str = "http://en.wiktionary.org/wiki/Appendix:Proto-Germanic/$gem";
    $site = @file_get_contents($str);
    if($site == "" || str_replace("Wiktionary does not yet have an entry for", "", $site) !== $site){
      return null;
    }
    $chap = substr($site, strpos($site, '<h4><span class="mw-headline" id="Descendants'));
    $chap = substr($chap, strpos($chap, '<ul>'));
    $chap = substr($chap, 0, strpos($chap, '<!--'));
    $chap = strip_tags(str_replace("<li>", "|", $chap));
    $all = explode("|", $chap);
    $deng = str_replace(array("eng", "deu"), array("English", "German"), $to);
    for($i = 0; $i < count($all); $i++){
      $parts = explode(":", $all[$i]);
      if($parts[0] == $deng || $parts[0] == "New High ".$deng){
        $a = str_replace(html_entity_decode("&#10;"), "", substr($parts[1], 1, strlen($parts[1])-2));
        if(str_replace(" ", "", $a) !== $a){ $a = substr($a, 0, strpos($a, " ")); }
        return $a;
      }
    }
    return null;
  }
  function getGEM($text, $from){
    $text = urlencode($text);
    $str = "http://en.wiktionary.org/wiki/$text";
    $site = @file_get_contents($str);
    if($site == "" || str_replace("Wiktionary does not yet have an entry for", "", $site) !== $site){
      return null;
    }
    $chap = substr($site, strpos($site, '<h2><span class="mw-headline" id="'.str_replace(array("deu", "eng"), array("German", "English"), $from)));
    if(strlen($chap) == strlen($site)){
      return null;
    }
    $chap = substr($chap, strpos($chap, '<h3><span class="mw-headline" id="Etymology'));
    $chap = substr($chap, 0, strpos($chap, '<h3><span class="mw-headline" id="Pronunciation'));
    $back = substr($chap, strpos($chap, '<i class="Latinx mention" lang="gem-pro'));
    if(strlen($back) == strlen($chap)){
      return null;
    }
    $back = substr($back, 0, strpos($back, '</i>')+4);
    $back = strip_tags($back);
    if(str_replace(" ", "", $back) !== $back){ $back = substr($back, 0, strpos($back, " ")); }
    return $back;
  }
?>

Keine Kommentare:

Kommentar veröffentlichen