【问题标题】:Xpath expression for first sentence in a pagraph段落中第一句的 Xpath 表达式
【发布时间】:2019-06-13 19:33:40
【问题描述】:

我正在寻找段落中第一句的 Xpath 表达式。

<p>
A federal agency is recommending that White House adviser Kellyanne Conway be 
removed from federal service saying she violated the Hatch Act on numerous 
occasions. The office is unrelated to Robert Mueller and his investigation.
</p>

结果应该是:

A federal agency is recommending that White House adviser Kellyanne Conway be 
removed from federal service saying she violated the Hatch Act on numerous 
occasions.

我尝试了一些方法无济于事。

$expression = '/html/body/div/div/div/div/p//text()';

我需要使用://p[ends-with 还是 substring-before?

【问题讨论】:

  • 您需要明确您使用的 XPath 版本。这种事情在 XPath 2.0 或更高版本中要容易得多。您指的是ends-with(),它需要 XPath 2.0,但您也提到 PHP,这表明您仅限于 1.0。

标签: php xml xpath xml-parsing domxpath


【解决方案1】:

您将无法通过 XPath 解析自然语言,但您可以按如下方式获取包含第一个句点的子字符串:

substring(/p,1,string-length(substring-before(/p,"."))+1)

请注意,如果在第一句结束之前有句号的缩写或其他词汇出现,如果第一句以另一种形式的标点符号结尾等,这可能不是“第一句”。


另外,更简洁:

concat(substring-before(/p, "."), ".")

学分:ThW 在 cmets 中的聪明想法。

【讨论】:

  • 我想我也可以explode() 它并抓取数组中的第一个元素。可能试图用 XPath 做太多事情。
  • 以上内容与您在一个简单的 XPath 中获得的一样好; explode() 不会更好。您需要使用 NLP 库在语义、句子级别而不是词汇、标点级别进行操作才能真正做到正确。
  • 我认为substring-before(/p,'.') 就足够了。获得句号的其余表达式可能会混淆 substring-before() 语义。
  • @Alejandro:确实,我最初只写了substring-before(/p,'.'),但后来我看到OP要求“结果应该是”输出包含句点,所以我想我会接受更进一步。不过你说得对,substring-before() 是最重要的。
  • 你可以再次追加点:concat(substring-before(/p, "."), ".")
【解决方案2】:

这不是在 Xpath 级别上执行此操作的真正好方法。 PHP 只有 Xpath 1.0 并且只支持基本的字符串操作。没有什么可以考虑语言环境/语言。然而,PHP 本身在ext/intl 中有一些东西。

所以使用DOM+Xpath作为字符串获取段落元素节点的文本内容,并从中提取第一句。

IntlBreakIterator 可以根据区域设置/语言特定规则拆分字符串。

$html = <<<'HTML'
<p>
A federal agency is recommending that White House adviser Kellyanne Conway be 
removed from federal service saying she violated the Hatch Act on numerous 
occasions. The office is unrelated to Robert Mueller and his investigation.
</p>
HTML;

$document = new DOMDocument();
$document->loadXML($html);
$xpath = new DOMXpath($document);

// fetch the first paragraph in the document as string
$summary = $xpath->evaluate('string((//p)[1])');
// create a break iterator for en_US sentences.
$breaker = IntlBreakIterator::createSentenceInstance('en_US');
// replace line breaks with spaces before feeding it to the breaker
$breaker->setText(str_replace(["\r\n", "\n"], '', $summary));

$firstSentence = '';
// iterate the sentences
foreach ($breaker->getPartsIterator() as $sentence) {
  $firstSentence = $sentence;
  // break after the first sentence
  break;
}

var_dump($firstSentence);

输出:

string(164) "A federal agency is recommending that White House adviser Kellyanne Conway be removed from federal service saying she violated the Hatch Act on numerous occasions. "

此外,DOMXpath 允许您注册 PHP 函数并从 Xpath 表达式中调用它们。如果您需要 Xpath 级别的逻辑(在条件中使用它们),这是可能的。

【讨论】:

  • 这是一个利用托管语言的有用补充。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-11
  • 1970-01-01
  • 2016-12-13
  • 2013-08-13
  • 2012-01-17
  • 2018-10-13
相关资源
最近更新 更多