ChatGPT prompt word project for Java developers (7)

In today's era of information explosion, we are faced with a huge amount of text information, and it is difficult to spare enough time to read all the interesting content. However, the large language model provides us with an effective solution: through automatic text summarization, it helps us quickly obtain the core content of the article. This technique has been widely used in various software applications.

Taking ChatGPT as an example, it provides a simple web interface that allows users to easily summarize articles. This method greatly improves the reading efficiency and enables us to obtain more information in a limited time. This article will give you a detailed introduction on how to use programming technology to realize this function, so that you can easily navigate in the ocean of information.

Summarize the content of the article

In this article, we will use a practical example to illustrate how to use GPT technology for article summarization. Assuming you have limited time and cannot read the entire article, we can write keyword hints and let GPT generate a concise summary for us, with no more than 50 words. In this way, you can quickly understand the main content of the article and improve your reading efficiency.

public static void main(String[] args) {
    
    
    String text = "我说道:“爸爸,你走吧。”\n"
    + "他望车外看了看,说:“我买几个橘子去。你就在此地,不要走动。”\n"
    + "我看那边月台的栅栏外有几个卖东西的等着顾客。走到那边月台,须穿过铁道,须跳下去又爬上去。父亲是一个胖子,走过去自然要费事些。我本来要去的,他不肯,只好让他去。\n"
    + "我看见他戴着黑布小帽,穿着黑布大马褂,深青布棉袍,蹒跚地走到铁道边,慢慢探身下去,尚不大难。可是他穿过铁道,要爬上那边月台,就不容易了。"
    + "他用两手攀着上面,两脚再向上缩;他肥胖的身子向左微倾,显出努力的样子。这时我看见他的背影,我的泪很快地流下来了。我赶紧拭干了泪。怕他看见,也怕别人看见。\n"
    + "我再向外看时,他已抱了朱红的橘子往回走了。过铁道时,他先将橘子散放在地上,自己慢慢爬下,再抱起橘子走。到这边时,我赶紧去搀他。"
    + "他和我走到车上,将橘子一股脑儿放在我的皮大衣上。于是扑扑衣上的泥土,心里很轻松似的。过一会儿说:“我走了,到那边来信!”我望着他走出去。"
    + "他走了几步,回过头看见我,说:“进去吧,里边没人。”等他的背影混入来来往往的人里,再找不着了,我便进来坐下,我的眼泪又来了。\n";

    String prompt = "使用最多50个字总结三个反引号之间的文章。\n"
    + "```%s```";
    String response = getCompletion(String.format(prompt, text));
    System.out.println(response);
}

Run it to see the result:

The father buys oranges and the son sees them off. My father was fat, and it took a lot of effort to climb the railway. The son saw his father's back and wept. The father turned his head and told his son to go in and disappeared into the crowd. The son cries again.

This is a pretty good summary, albeit over the word limit.

Summary of Specific Goals

Sometimes when summarizing, there will be a very specific goal, for example: you are a fashion designer, and you may want to pay attention to the details related to clothing in the article. To achieve this, we can also modify the prompt words so that they generate a summary that is more applicable to a specific goal in your business.

public static void main(String[] args) {
    
    
    String text = "我说道:“爸爸,你走吧。”\n"
    + "他望车外看了看,说:“我买几个橘子去。你就在此地,不要走动。”\n"
    + "我看那边月台的栅栏外有几个卖东西的等着顾客。走到那边月台,须穿过铁道,须跳下去又爬上去。父亲是一个胖子,走过去自然要费事些。我本来要去的,他不肯,只好让他去。\n"
    + "我看见他戴着黑布小帽,穿着黑布大马褂,深青布棉袍,蹒跚地走到铁道边,慢慢探身下去,尚不大难。可是他穿过铁道,要爬上那边月台,就不容易了。"
    + "他用两手攀着上面,两脚再向上缩;他肥胖的身子向左微倾,显出努力的样子。这时我看见他的背影,我的泪很快地流下来了。我赶紧拭干了泪。怕他看见,也怕别人看见。\n"
    + "我再向外看时,他已抱了朱红的橘子往回走了。过铁道时,他先将橘子散放在地上,自己慢慢爬下,再抱起橘子走。到这边时,我赶紧去搀他。"
    + "他和我走到车上,将橘子一股脑儿放在我的皮大衣上。于是扑扑衣上的泥土,心里很轻松似的。过一会儿说:“我走了,到那边来信!”我望着他走出去。"
    + "他走了几步,回过头看见我,说:“进去吧,里边没人。”等他的背影混入来来往往的人里,再找不着了,我便进来坐下,我的眼泪又来了。\n";

    String prompt = "使用最多50个字总结三个反引号之间的文章,其中包括服装的细节。\n"
    + "```%s```";
    String response = getCompletion(String.format(prompt, text));
    System.out.println(response);
}

Run it to see the result:

Wearing a black cloth cap, a big mandarin jacket, and a dark green cotton robe, my father walked across the railway with difficulty and bought vermilion oranges. He put the orange on the child's fur coat and went away. The child looked at his back and burst into tears.

You can see that a different summary is generated, which includes clothing details for the father's black cloth cap, mandarin jacket, and dark blue cotton robe.

Extract article specific information

You may wish to focus only on the clothing-related details in the article and not on the article's story leave. We can modify the prompt words again so that they generate a summary that is more applicable to a specific goal in your business.

public static void main(String[] args) {
    
    
    String text = "我说道:“爸爸,你走吧。”\n"
            + "他望车外看了看,说:“我买几个橘子去。你就在此地,不要走动。”\n"
            + "我看那边月台的栅栏外有几个卖东西的等着顾客。走到那边月台,须穿过铁道,须跳下去又爬上去。父亲是一个胖子,走过去自然要费事些。我本来要去的,他不肯,只好让他去。\n"
            + "我看见他戴着黑布小帽,穿着黑布大马褂,深青布棉袍,蹒跚地走到铁道边,慢慢探身下去,尚不大难。可是他穿过铁道,要爬上那边月台,就不容易了。"
            + "他用两手攀着上面,两脚再向上缩;他肥胖的身子向左微倾,显出努力的样子。这时我看见他的背影,我的泪很快地流下来了。我赶紧拭干了泪。怕他看见,也怕别人看见。\n"
            + "我再向外看时,他已抱了朱红的橘子往回走了。过铁道时,他先将橘子散放在地上,自己慢慢爬下,再抱起橘子走。到这边时,我赶紧去搀他。"
            + "他和我走到车上,将橘子一股脑儿放在我的皮大衣上。于是扑扑衣上的泥土,心里很轻松似的。过一会儿说:“我走了,到那边来信!”我望着他走出去。"
            + "他走了几步,回过头看见我,说:“进去吧,里边没人。”等他的背影混入来来往往的人里,再找不着了,我便进来坐下,我的眼泪又来了。\n";

    String prompt = "使用最多50个字提取三个反引号之间的文章中服装信息。\n"
            + "```%s```";
    String response = getCompletion(String.format(prompt, text));
    System.out.println(response);
}

Run it to see the result:

Clothing information: black cloth cap, black cloth mandarin jacket, dark green cloth padded gown.

We can see that the clothing information in the article has been extracted.

Summarize long articles

Next, we will explore how to use GPT to effectively summarize longer articles. Due to the token size limit of a single request, we cannot send the content of the entire article to GPT for processing at one time. In order to solve this problem, we can adopt the segmentation processing strategy, split the article into several smaller parts, and then summarize each part separately.

First of all, we need to split the content of the article according to the logical structure to ensure that each part can independently express a complete meaning. Next, we send these split pieces to GPT to generate a summary for each part. In this process, we need to ensure that the summary of each part can accurately reflect the core ideas of the original text, while maintaining the fluency of language and the fullness of semantics.

Finally, we need to bring these summaries together to form a complete summary. To achieve this, we can again leverage the power of GPT to take these summaries as input and generate a comprehensive, condensed synthesis summary. Through this segmented processing and multiple summarization strategy, we can overcome the token size limitation of GPT when processing long articles, and achieve efficient summarization of longer content.

public static void main(String[] args) throws InterruptedException {
    
    
    String text =
            "我与父亲不相见已二年余了,我最不能忘记的是他的背影。那年冬天,祖母死了,父亲的差使也交卸了,正是祸不单行的日子,我从北京到徐州,打算跟着父亲奔丧回家。到徐州见着父亲,看见满院狼藉的东西,又想起祖母,不禁簌簌地流下眼泪。父亲说,“事已如此,不必难过,好在天无绝人之路!”\n"
                    + "回家变卖典质,父亲还了亏空;又借钱办了丧事。这些日子,家中光景很是惨淡,一半为了丧事,一半为了父亲赋闲。丧事完毕,父亲要到南京谋事,我也要回北京念书,我们便同行。\n"
                    + "到南京时,有朋友约去游逛,勾留了一日;第二日上午便须渡江到浦口,下午上车北去。父亲因为事忙,本已说定不送我,叫旅馆里一个熟识的茶房陪我同去。他再三嘱咐茶房,甚是仔细。但他终于不放心,怕茶房不妥帖;颇踌躇了一会。其实我那年已二十岁,北京已来往过两三次,是没有甚么要紧的了。他踌躇了一会,终于决定还是自己送我去。我两三回劝他不必去;他只说,“不要紧,他们去不好!”\n"
                    + "我们过了江,进了车站。我买票,他忙着照看行李。行李太多了,得向脚夫行些小费,才可过去。他便又忙着和他们讲价钱。我那时真是聪明过分,总觉他说话不大漂亮,非自己插嘴不可。但他终于讲定了价钱;就送我上车。他给我拣定了靠车门的一张椅子;我将他给我做的紫毛大衣铺好坐位。他嘱我路上小心,夜里警醒些,不要受凉。又嘱托茶房好好照应我。我心里暗笑他的迂;他们只认得钱,托他们直是白托!而且我这样大年纪的人,难道还不能料理自己么?唉,我现在想想,那时真是太聪明了!\n"
                    + "我说道,“爸爸,你走吧。”他望车外看了看,说,“我买几个橘子去。你就在此地,不要走动。”我看那边月台的栅栏外有几个卖东西的等着顾客。走到那边月台,须穿过铁道,须跳下去又爬上去。父亲是一个胖子,走过去自然要费事些。我本来要去的,他不肯,只好让他去。我看见他戴着黑布小帽,穿着黑布大马褂,深青布棉袍,蹒跚地走到铁道边,慢慢探身下去,尚不大难。可是他穿过铁道,要爬上那边月台,就不容易了。他用两手攀着上面,两脚再向上缩;他肥胖的身子向左微倾,显出努力的样子。这时我看见他的背影,我的泪很快地流下来了。我赶紧拭干了泪,怕他看见,也怕别人看见。我再向外看时,他已抱了朱红的橘子望回走了。过铁道时,他先将橘子散放在地上,自己慢慢爬下,再抱起橘子走。到这边时,我赶紧去搀他。他和我走到车上,将橘子一股脑儿放在我的皮大衣上。于是扑扑衣上的泥土,心里很轻松似的,过一会说,“我走了;到那边来信!”我望着他走出去。他走了几步,回过头看见我,说,“进去吧,里边没人。”等他的背影混入来来往往的人里,再找不着了,我便进来坐下,我的眼泪又来了。\n"
                    + "近几年来,父亲和我都是东奔西走,家中光景是一日不如一日。他少年出外谋生,独力支持,做了许多大事。那知老境却如此颓唐!他触目伤怀,自然情不能自已。情郁于中,自然要发之于外;家庭琐屑便往往触他之怒。他待我渐渐不同往日。但最近两年的不见,他终于忘却我的不好,只是惦记着我,惦记着我的儿子。我北来后,他写了一信给我,信中说道,“我身体平安,惟膀子疼痛利害,举箸提笔,诸多不便,大约大去之期不远矣。”我读到此处,在晶莹的泪光中,又看见那肥胖的,青布棉袍,黑布马褂的背影。唉!我不知何时再能与他相见!";

    String[] sections = text.split("\n");
    StringBuilder stringBuilder = new StringBuilder();
    for (int i = 0; i < sections.length; i++) {
    
    
        String prompt = "使用最多50个字总结三个反引号之间的内容。\n"
                + "```%s```";
        String response = getCompletion(String.format(prompt, sections[i]));
        System.out.println("第" + (i + 1) + "段的总结:" + response);
        stringBuilder.append(response).append('\n');
        Thread.sleep(5000);
    }
    String prompt = "使用最多100个字总结三个反引号之间的内容。\n"
            + "```%s```";
    String response = getCompletion(String.format(prompt, stringBuilder));
    System.out.println("汇总的总结:" + response);
}

Run it to see the result:

Summary of paragraph 1: The author misses his father, especially his back view. When the grandmother died and the father resigned, the author went home to reunite with his father. My father comforted the author, saying, "there is no such thing as a perfect road".
Summary of the second paragraph: The father sells the mortgage in order to repay the debt, attends the funeral, and is idle. The son accompanies Nanjing to seek work, and returns to Beijing to study by himself.
Summary of the third paragraph: The father sent his son to Nanjing, worried that the waiter would be inappropriate, and finally decided to see him off in person. The son is twenty years old, but the father is still worried.
Summary of the fourth paragraph: An older man is self-righteous and tells the young man what to do, but is finally sent to the train by the young man, secretly laughing at the young man's literal-mindedness.
Summary of paragraph 5: In order to buy oranges, my father crossed the railway and climbed hard, which made people feel distressed. The son looked at his father's back and shed tears, but he was afraid of being seen. Finally, when the father left, he asked his son to go in, which made people feel the deep affection.
Summary of paragraph 6: The father is old and depressed, emotionally unstable, and easily angered by family matters. But in the past two years, he has forgotten his son's badness, and only thinks about him and his grandson. When the son read his father's letter, his eyes were dim with tears, and he missed the fat figure.
Summary summary: The author misses his father and recalls his back and deep affection. The father continued to work hard for the family, and he was still worried when he sent his son to Nanjing. The father was old and depressed, but he forgot about his son's faults and only cared about him and his grandson.

By breaking articles into smaller parts, summarizing them separately, and then aggregating these summaries into a complete review, we can effectively use GPT to summarize longer articles. This method not only ensures the accuracy and completeness of the summary, but also gives full play to the advantages of GPT in natural language processing.

end

In today's era of information explosion, we are often faced with the processing of a large amount of text data. In order to help users quickly understand the key information in these texts, we can use GPT, an advanced natural language processing technology, to realize the text summarization function. Through the powerful generation capabilities of GPT, we can provide users with concise and clear summaries, allowing them to grasp the core content of the text in a short time. At the same time, if users are interested in a certain topic, they can also choose to read the original text in depth to obtain more detailed information. In this way, we not only improve the reading efficiency of users, but also enrich their knowledge system.

In the next post, we'll look at another capability of GPT: reasoning with text. Stay tuned.

General Catalog of "ChatGPT Prompt Words Project for Java Developers"

Guess you like

Origin blog.csdn.net/heihaozi/article/details/131391565