1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-11-10 08:52:39 +01:00

Generate slightly shorter summaries in the typeahead browse dialog

Summary: Ref T11034. Try to produce a roughly-one-sentence summary instead of a roughly-one-paragraph summary for the browse dialog.

Test Plan:
  - Added unit tests, ran unit tests.
  - Wrote a longer summary for a project, browsed to it, saw a shorter summary.

Reviewers: chad

Reviewed By: chad

Maniphest Tasks: T11034

Differential Revision: https://secure.phabricator.com/D16892
This commit is contained in:
epriestley 2016-11-17 12:39:43 -08:00
parent d69a1b95e7
commit 79132311f4
4 changed files with 68 additions and 1 deletions

View file

@ -2896,6 +2896,7 @@ phutil_register_library_map(array(
'PhabricatorManiphestTaskTestDataGenerator' => 'applications/maniphest/lipsum/PhabricatorManiphestTaskTestDataGenerator.php',
'PhabricatorMarkupCache' => 'applications/cache/storage/PhabricatorMarkupCache.php',
'PhabricatorMarkupEngine' => 'infrastructure/markup/PhabricatorMarkupEngine.php',
'PhabricatorMarkupEngineTestCase' => 'infrastructure/markup/__tests__/PhabricatorMarkupEngineTestCase.php',
'PhabricatorMarkupInterface' => 'infrastructure/markup/PhabricatorMarkupInterface.php',
'PhabricatorMarkupOneOff' => 'infrastructure/markup/PhabricatorMarkupOneOff.php',
'PhabricatorMarkupPreviewController' => 'infrastructure/markup/PhabricatorMarkupPreviewController.php',
@ -7871,6 +7872,7 @@ phutil_register_library_map(array(
'PhabricatorManiphestTaskTestDataGenerator' => 'PhabricatorTestDataGenerator',
'PhabricatorMarkupCache' => 'PhabricatorCacheDAO',
'PhabricatorMarkupEngine' => 'Phobject',
'PhabricatorMarkupEngineTestCase' => 'PhabricatorTestCase',
'PhabricatorMarkupOneOff' => array(
'Phobject',
'PhabricatorMarkupInterface',

View file

@ -130,7 +130,7 @@ final class PhabricatorProjectDatasource
$description = idx($descriptions, $phid);
if (strlen($description)) {
$summary = PhabricatorMarkupEngine::summarize($description);
$summary = PhabricatorMarkupEngine::summarizeSentence($description);
$proj_result->addAttribute($summary);
}
}

View file

@ -607,6 +607,28 @@ final class PhabricatorMarkupEngine extends Phobject {
return array_values($files);
}
public static function summarizeSentence($corpus) {
$corpus = trim($corpus);
$blocks = preg_split('/\n+/', $corpus, 2);
$block = head($blocks);
$sentences = preg_split(
'/\b([.?!]+)\B/u',
$block,
2,
PREG_SPLIT_DELIM_CAPTURE);
if (count($sentences) > 1) {
$result = $sentences[0].$sentences[1];
} else {
$result = head($sentences);
}
return id(new PhutilUTF8StringTruncator())
->setMaximumGlyphs(128)
->truncateString($result);
}
/**
* Produce a corpus summary, in a way that shortens the underlying text
* without truncating it somewhere awkward.

View file

@ -0,0 +1,43 @@
<?php
final class PhabricatorMarkupEngineTestCase
extends PhabricatorTestCase {
public function testRemarkupSentenceSummmaries() {
$this->assertSentenceSummary(
'The quick brown fox. Jumped over the lazy dog.',
'The quick brown fox.');
$this->assertSentenceSummary(
'Go to www.help.com for details. Good day.',
'Go to www.help.com for details.');
$this->assertSentenceSummary(
'Coxy lummox gives squid who asks for job pen.',
'Coxy lummox gives squid who asks for job pen.');
$this->assertSentenceSummary(
'DEPRECATED',
'DEPRECATED');
$this->assertSentenceSummary(
'Never use this! It is deadly poison.',
'Never use this!');
$this->assertSentenceSummary(
"a short poem\nmeow meow meow\nmeow meow meow\n\n- cat",
'a short poem');
$this->assertSentenceSummary(
'WOW!! GREAT PROJECT!',
'WOW!!');
}
private function assertSentenceSummary($corpus, $summary) {
$this->assertEqual(
$summary,
PhabricatorMarkupEngine::summarizeSentence($corpus),
pht('Summary of: %s', $corpus));
}
}