如何通过 SPARQL 精确匹配单词
我的应用程序需要将正确的单词(即 C 或 C++)与 SPARQL 相匹配。我有以下 SPARQL 查询
String prolog = "PREFIX kb: <" + VBOOK.getURI() + ">";
String queryString = prolog
+ "\n"
+ "SELECT * "
+ "WHERE {?x kb:Price ?price. ?x kb:Currency ?currency. ?x kb:Title ?title. ?x kb:Count ?count. "
+ "OPTIONAL {?x kb:Description ?description}."
+ "FILTER regex(?title, \"" +title + "\")}";
这与我所有的书籍相匹配,甚至包括 C++、C# 和诸如 -Real Concepts in Embedded Systems 之类的书籍(因为“Concepts”一词有“C”)。
如果我将最后一行更改为
+ "FILTER regex(?title, \"" +"^"+title + "\")}";
并且标题例如为“C”,那么我的查询仅匹配那些标题以字母“C”开头的书籍。因此不会选择“Programming in ANSI C”之类的书籍。
我还尝试在最后一行进行以下更改
+ "FILTER regex(str(?title), \"" +title + "\")}";
即使这对我没有多大帮助。
即使书名不是以 C 开头,如何修改才能获取与“C”相关的书籍?
问候, 阿卡纳。
My application needs to match the correct word i.e. say C or C++ with SPARQL. I have the following SPARQL query
String prolog = "PREFIX kb: <" + VBOOK.getURI() + ">";
String queryString = prolog
+ "\n"
+ "SELECT * "
+ "WHERE {?x kb:Price ?price. ?x kb:Currency ?currency. ?x kb:Title ?title. ?x kb:Count ?count. "
+ "OPTIONAL {?x kb:Description ?description}."
+ "FILTER regex(?title, \"" +title + "\")}";
This matches me all books even C++,C# and books like -Real Concepts in Embedded Systems (because of the word 'Concepts' having 'C').
if I change the last line to
+ "FILTER regex(?title, \"" +"^"+title + "\")}";
and if title is for example 'C' then my query matches only those books whose titles start with letter 'C'.So books like 'Programming in ANSI C' are not selected.
I have also tried with the following change in the last line
+ "FILTER regex(str(?title), \"" +title + "\")}";
Even this has not helped me much.
How do I modify to get the books pertaining to 'C' even if the books title do not start with C?
Regards,
Archana.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这个问题归结为您的查询引擎支持哪些正则表达式功能。如果它支持
\b
字边界匹配,您可以通过将最后一行更改为来解决您的问题This question boils down to what regular expression features your query engine supports. If it supports
\b
word boundary matching, you can solve your problem by changing the last line to你需要正则表达式吗?
不要将 title 设置为变量,只需将其修复为 -
?x kb:Title "title" 。
即在您的代码中
String title = "C++";
"WHERE {?x kb:价格 ?价格。?x kb:货币 ?货币。?x kb:标题 \""+标题+"\" 。?x kb:计数 ?计数。"
Do you need regex at all?
Instead of making title a variable, simply fix it so -
?x kb:Title "title" .
i.e. in your code
String title = "C++";
"WHERE {?x kb:Price ?price. ?x kb:Currency ?currency. ?x kb:Title \""+title+"\" . ?x kb:Count ?count. "