Skip to content

Commit

Permalink
Merge pull request #7 from chriskonnertz/feature-keep-line-breaks
Browse files Browse the repository at this point in the history
New feature: Keep line breaks
  • Loading branch information
chriskonnertz authored Nov 1, 2017
2 parents a97cc47 + 3ecab9a commit f61350b
Show file tree
Hide file tree
Showing 8 changed files with 323 additions and 77 deletions.
20 changes: 18 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ $languageCode = $deepLy->detectLanguage('Hello world!');
This will return 'EN'. The language of the text has to be one of the supported languages or the result will be incorrect.
If you do not need the code of the language but its name, you may call the `$deepLy->getLangName($langCode)` method.

The API in general can handle and completely translate texts that contain parts with different languages,
if the language switch is not within a sentence. The `detectLanguage()` method will however
only return the code of _one_ language. It will throw an exception if it is unable to auto-detect the language.
This will rarely happen, it is more likely that the API will return a "false positive": It will rather detect the wrong
language than no language at all.

> An interactive PHP demo script is included. It is located at `demos/demo_detect.php`.
## Supported Languages
Expand All @@ -88,7 +94,7 @@ DeepL(y) supports these languages:
| NL | Dutch |
| PL | Polish |

> Note that auto detection only is possible for the source language.
> Note that auto detection only is possible for the source language.
DeepL says they will [add more languages](https://www.heise.de/newsticker/meldung/Maschinelles-Uebersetzen-Deutsches-Start-up-DeepL-will-230-Sprachkombinationen-unterstuetzen-3836533.html)
in the future, such as Chinese and Russian.
Expand All @@ -97,7 +103,7 @@ in the future, such as Chinese and Russian.

According to the DeepL.com website, the length of the text that has to be translated is limited to 5000 characters.
Per default DeepLy will throw an exception if the length limit is exceeded.
You may call `$deepLy->setValidateTextLength(false)` to disable that validation.
You may call `$deepLy->setValidateTextLength(false)` to disable this validation.

## HTTP Client

Expand Down Expand Up @@ -135,6 +141,16 @@ In Laravel 5.0-5.4 you manually have to register the service provider
in your `config/app.php` config file.

You can then access DeepLy like this: `$ping = \DeepLy::ping();`

## Demos

There are several demo scripts included in the `demos` folder:

* `demo_detect.php`: Demonstrates language detection. Write a text and the API will tell you which language it thinks it is.
* `demo_translate.php`: Demonstrates language translation. Write a text and the API will try to translate it to a language of your choice.
* `demo_split.php`: Demonstrates sentence detection. Write a text and the API will split it into sentences.
* `demo_ping.php`: Demonstrates DeepLy's `ping()` method by pinging the API.


## Current State

Expand Down
106 changes: 106 additions & 0 deletions demos/demo_split.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?php

/**
* Minimal class autoloader
*
* @param string $class Full qualified name of the class
*/
function miniAutoloader($class)
{
require __DIR__ . '/../src/' . $class . '.php';
}

// If the Composer autoloader exists, use it. If not, use our own as fallback.
$composerAutoloader = __DIR__.'/../vendor/autoload.php';
if (is_readable($composerAutoloader)) {
require $composerAutoloader;
} else {
spl_autoload_register('miniAutoloader');
}

$text = isset($_POST['text']) ? $_POST['text'] : null;

$deepLy = new ChrisKonnertz\DeepLy\DeepLy();

?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>DeepLy Demo - Split Text</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/framy/latest/css/framy.min.css">
<style>
body { padding: 20px }
h1 { margin-bottom: 40px }
h4 { margin-top: 40px }
form { margin-bottom: 20px }
textarea { resize: vertical; }
blockquote { margin-left: 0; margin-right: 0; }
div.success { border: 1px solid #4ce276; margin: 20px 0; padding: 10px; border-top-width: 10px }
div.error { border: 1px solid #f36362; margin: 20px 0; padding: 10px; border-top-width: 10px }
</style>
</head>
<body>
<h1>DeepLy Demo</h1>

<form method="POST">

<div class="form-element">
<label for="text">Text:</label>
<textarea id="text" class="form-field" name="text" rows="4"><?php echo $text !== null ? $text : 'Hello world!' ?></textarea>
</div>

<div id="ping-result"></div>

<input type="submit" value="Split Text" class="button">
</form>

<div class="block result">
<?php

if ($text !== null) {
try {
$result = $deepLy->splitText($text);
$result = implode('<br>', $result);

echo '<div class="success">Sentences: <blockquote><b>' . $result . '</b></blockquote></div>';
} catch (\Exception $exception) {
echo '<div class="error">'.$exception->getMessage().'</div>';
}
}

?>
</div>

<div class="block info">
<small>
This is not an official package.
It is 100% open source and non-commercial.
DeepL is a product from DeepL GmbH. More info:
<a href="https://www.deepl.com/publisher.html">www.deepl.com/publisher.html</a>
</small>
</div>

<script>
(
// Use DeepLy's ping method to check if the API server is reachable
function()
{
var request = new XMLHttpRequest();

request.addEventListener('readystatechange', function() {
if (request.readyState === XMLHttpRequest.DONE) {
if (request.status !== 200 || request.responseText !== '1') {
document.getElementById('ping-result').innerHTML =
'<div class="error"><b>WARNING:</b> API seems unreachable.</div>';
}
}
});

request.open('GET', 'demo_ping.php?simple=1', true);
request.send();
}
)();
</script>
</body>
</html>
2 changes: 1 addition & 1 deletion demos/demo_translate.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ function createSelect($name, array $options, $default = null)
try {
$result = $deepLy->translate($text, $to, $from);

echo '<div class="success">Translation: <blockquote><b>' . $result . '</b></blockquote></div>';
echo '<div class="success">Translation: <pre><b>' . $result . '</b></pre></div>';
} catch (\Exception $exception) {
echo '<div class="error">'.$exception->getMessage().'</div>';
}
Expand Down
Loading

0 comments on commit f61350b

Please sign in to comment.