栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

匹配至少有一个单词的字符串

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

匹配至少有一个单词的字符串

假设您有一些数据(在Turtle中):

@prefix : <http://stackoverflow.com/q/20203733/1281433> .@prefix dc: <http://purl.org/dc/elements/1.1/> .:a dc:title "Great Gatsby" .:b dc:title "Boring Gatsby" .:c dc:title "Great Expectations" .:d dc:title "The Great Muppet Caper" .

然后,您可以使用类似以下的查询:

prefix : <http://stackoverflow.com/q/20203733/1281433>prefix dc: <http://purl.org/dc/elements/1.1/>select ?x ?title where {  # this is just in place of this.getTitle().  It provides a value for  # ?TITLE that is "Gatsby Strikes Again".  values ?TITLE { "Gatsby Strikes Again" }  # Select a thing and its title.  ?x dc:title ?title .  # Then filter based on whether the ?title matches the result  # of replacing the strings in ?TITLE with "|", and matching  # case insensitively.  filter( regex( ?title, replace( ?TITLE, " ", "|" ), "i" ))}

得到像

------------------------| x  | title|========================| :b | "Boring Gatsby" || :a | "Great Gatsby"  |------------------------

这样做特别整洁的是,由于您正在动态生成模式,因此甚至可以基于图形模式中的另一个值来进行创建。例如,如果您想要标题至少匹配一个单词的所有事物对,则可以执行以下操作:

prefix : <http://stackoverflow.com/q/20203733/1281433>prefix dc: <http://purl.org/dc/elements/1.1/>select ?x ?xtitle ?y ?ytitle where {  ?x dc:title ?xtitle .  ?y dc:title ?ytitle .  filter( regex( ?xtitle, replace( ?ytitle, " ", "|" ), "i" ) && ?x != ?y )}order by ?x ?y

要得到:

-----------------------------------------------------------------| x  | xtitle        | y  | ytitle        |=================================================================| :a | "Great Gatsby"| :b | "Boring Gatsby"          || :a | "Great Gatsby"| :c | "Great Expectations"     || :a | "Great Gatsby"| :d | "The Great Muppet Caper" || :b | "Boring Gatsby"          | :a | "Great Gatsby"|| :c | "Great Expectations"     | :a | "Great Gatsby"|| :c | "Great Expectations"     | :d | "The Great Muppet Caper" || :d | "The Great Muppet Caper" | :a | "Great Gatsby"|| :d | "The Great Muppet Caper" | :c | "Great Expectations"     |-----------------------------------------------------------------

当然, 非常 重要的一点是要注意,您现在正在根据数据提取生成模式,这意味着可以将数据放入系统中的人可能会使用非常昂贵的模式来阻止查询并导致拒绝-
服务。更为平凡的是,如果您的任何标题中包含会干扰正则表达式的字符,您都可能会遇到麻烦。一个有趣的问题是,如果某个东西的标题带有多个空格,则该模式变为

The|Words|With||Two|Spaces
,因为其中的空模式可能使
所有内容 匹配。这是一种有趣的方法,但是有 很多 警告。

通常,您可以如此处所示执行此操作,或者通过在代码中生成正则表达式(可以在其中转义等),或者可以使用支持某些基于文本的扩展名的SPARQL引擎(例如jena)
-text
,它将Apache
Lucene或Apache Solr添加到Apache Jena)。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/407188.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号