gpt4 book ai didi

java - java中以下条件的模式

转载 作者:行者123 更新时间:2023-12-02 08:32:39 27 4
gpt4 key购买 nike

我想知道如何编写模式..

例如:

这个词是

   "AboutGoogle AdWords Drive traffic and customers to your site. Pay through Cheque,      Net Banking or Credit Card. Google Toolbar Add a search box to your browser. Google SMS To find out local information simply SMS to 54664. Gmail Free email with 7.2GB storage and less spam. Try Gmail today. Our ProductsHelp Help with Google Search, Services and ProductsGoogle Web Search Features Translation, I'm Feeling Lucky, CachedGoogle Services & Tools Toolbar, Google Web APIs, ButtonsGoogle Labs Ideas, Demos, ExperimentsFor Site OwnersAdvertising AdWords, AdSenseBusiness Solutions Google Search Appliance, Google Mini, WebSearchWebmaster Central One-stop shop for comprehensive info about how Google crawls and indexes websitesSubmit your content to Google Add your site, Google SitemapsOur CompanyPress Center News, Images, ZeitgeistJobs at Google Openings, Perks, CultureCorporate Info Company overview, Philosophy, Diversity, AddressesInvestor Relations Financial info, Corporate governanceMore GoogleContact Us FAQs, Feedback, NewsletterGoogle Logos Official Logos, Holiday Logos, Fan LogosGoogle Blog Insights to Google products and cultureGoogle Store Pens, Shirts, Lava lamps©2010 Google - Privacy Policy - Terms of Service"

我必须搜索一些词...

例如“谷歌洞察”

那么如何用java编写代码...

我只是写了一些小代码...

检查我的代码并回答我的问题...

该代码仅用于查找搜索词,它在哪里。

但我需要在搜索词前面显示一些词,并在搜索词后面显示一些词...

类似于谷歌搜索...

我的代码是

Pattern p = Pattern.compile("(?i)(.*?)"+search+"");
Matcher m = p.matcher(full);
String title="";
while (m.find() == true)
{
title=m.group(1);
System.out.println(title);
}

全文为原创内容,搜索s搜索词...

感谢并提前

最佳答案

最好的解决方案必须使用非常复杂的字符串搜索和索引算法。如果您不关心性能,那么这样的事情很容易实现:

import java.util.*;
public class SearchAndContext {
public static void main(String[] args) {
String text = "The path of the righteous man is beset on all sides by "
+ "the iniquities of the selfish and the tyranny of evil men. Blessed "
+ "is he, who in the name of charity and good will, shepherds the "
+ "weak through the valley of darkness, for he is truly his brother's "
+ "keeper and the finder of lost children. And I will strike down "
+ "upon thee with great vengeance and furious anger those who would "
+ "attempt to poison and destroy my brothers. And you will know my "
+ "name is the Lord when I lay my vengeance upon thee.";

List<String> words = Arrays.asList(text.split(" "));
final int W = 3;
final int N = words.size();
String[] queries = { "vengeance", "and", "monkeys" };
for (String query : queries) {
List<String> search = words;
System.out.println("Searching for " + query);
for (int idx = -1, pos; (pos = search.indexOf(query)) != -1; ) {
idx += (pos+1);
int left = Math.max(0, idx - W);
int right = Math.min(N, idx + W + 1);
System.out.println(words.subList(left, right));
search = search.subList(pos+1, search.size());
}
}
}
}

打印:

Searching for vengeance
[thee, with, great, vengeance, and, furious, anger]
[I, lay, my, vengeance, upon, thee.]
Searching for and
[of, the, selfish, and, the, tyranny, of]
[name, of, charity, and, good, will,, shepherds]
[his, brother's, keeper, and, the, finder, of]
[with, great, vengeance, and, furious, anger, those]
[attempt, to, poison, and, destroy, my, brothers.]
Searching for monkeys

如您所见,这会查找搜索查询的出现,并提供围绕“命中”的最多 W=3 个单词的上下文。

API 引用

关于java - java中以下条件的模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2935015/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com