java中文關鍵詞提取,Java語言關鍵詞

本文目錄一覽:

java 如何在一篇文章中取出某個關鍵字

這個方法是取出了含有keyword的那一行,你要去關鍵字,只要返回true,直接取就O了!!忘樓主採納!

public void selectKeyWord(String keyWord){

String filepath = “”;

try {

DataInputStream dis = new DataInputStream(new FileInputStream(filepath));

while(dis.read()!=-1){

String str = dis.readUTF();

if(str.contains(keyWord)){

System.out.println(str);

}

}

} catch (FileNotFoundException e) {

System.out.println(“文件不存在”);

System.exit(0);

} catch (IOException e) {

e.printStackTrace();

}

}

java編程 求一個能夠從TXT文本中提取中文關鍵字並輸出的程序

以下這個程序就是 獲取到.txt中的中文,然後在控制台中輸出

希望這個能幫到你啊,望採納啊

package cn.ima_21;

import java.io.FileWriter;

import java.io.FileReader;

import java.io.IOException;

public class FileReaderDemo {

public static void main(String[] args) throws IOException {

// 創建對象

FileReader fr = new FileReader(“a.txt”);

//開發一般如下寫

int num = 0;

while((num=fr.read())!=-1){

System.out.print((char)num);

}

//釋放資源

fr.close();

}

}

如何用java實現文本的關鍵字提取

可以使用java API中的URL抓取流,也可以使用Apache的HttpClient等多種方法,最終得到的就是字元串咯,得到字元串就好辦了,使用正則匹配,將匹配的保存起來就可以了

java word中提取關鍵字

首先,要用Java的File類中函數(如open,read等,記不清了)打開word文檔,利用形式語言與自動機的原理,跳過word文檔的頭部,分析內容,利用String中的equals()方法匹配”java”字元串。

求一個提取文章關鍵詞的java程序

//直接粘貼就行。

import java.awt.BorderLayout;

import java.awt.EventQueue;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.io.File;

import java.io.IOException;

import java.io.PrintWriter;

import java.util.ArrayList;

import java.util.Scanner;

import javax.swing.BorderFactory;

import javax.swing.JButton;

import javax.swing.JFileChooser;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.JSplitPane;

import javax.swing.JTextArea;

import javax.swing.border.EtchedBorder;

import javax.swing.filechooser.FileFilter;

public class Application2 extends JFrame implements Cloneable{

public Application2(){

this.setDefaultCloseOperation(EXIT_ON_CLOSE);

this.setSize(800,700);

this.setLayout(new BorderLayout());

keyWords1=new String[]{“那麼”,”還是”,”sdf”};

keyWords2=new String[]{“所以”,”而且”,};

input=new JTextArea();

JPanel ip=new JPanel();

ip.setLayout(new BorderLayout());

ip.add(input,BorderLayout.CENTER);

ip.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED), “輸入文本”));

output1=new JTextArea();

JPanel o1p=new JPanel();

o1p.setLayout(new BorderLayout());

o1p.add(output1,BorderLayout.CENTER);

o1p.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED), “以下為”));

output2=new JTextArea();

JPanel o2p=new JPanel();

o2p.setLayout(new BorderLayout());

o2p.add(output2,BorderLayout.CENTER);

o2p.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED), “以下為”));

JSplitPane split1=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,o1p,o2p);

split1.setDividerLocation(350);

JSplitPane split2=new JSplitPane(JSplitPane.VERTICAL_SPLIT,ip,split1);

split2.setDividerLocation(300);

this.add(split2,BorderLayout.CENTER);

open=new JButton(“導入”);

open.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e){

JFileChooser chooser=new JFileChooser(“.”);

chooser.setMultiSelectionEnabled(false);

chooser.addChoosableFileFilter(new FileFilter(){

@Override

public boolean accept(File file) {

if(file.isDirectory())

return true;

int length=file.getName().length();

if(length5)

return false;

if(file.getName().substring(length-4).equals(“.txt”))

return true;

return false;

}

@Override

public String getDescription() {

return “文本文件”;

}

});

chooser.showOpenDialog(Application2.this);

File file=chooser.getSelectedFile();

if(file==null)

return;

try {

Scanner sc=new Scanner(file);

String text=””;

while(sc.hasNextLine())

text+=sc.nextLine()+”\n”;

input.setText(text);

String[] array=getSentences();

output1.setText(getKeySentences(keyWords1,array));

output2.setText(getKeySentences(keyWords2,array));

}catch (IOException e1) {

e1.printStackTrace();

}

}

});

save=new JButton(“導出”);

save.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e){

JFileChooser chooser=new JFileChooser(“.”);

chooser.setMultiSelectionEnabled(false);

chooser.addChoosableFileFilter(new FileFilter(){

@Override

public boolean accept(File file) {

if(file.isDirectory())

return true;

int length=file.getName().length();

if(length5)

return false;

if(file.getName().substring(length-4).equals(“.txt”))

return true;

return false;

}

@Override

public String getDescription() {

return “文本文件”;

}

});

chooser.showSaveDialog(Application2.this);

File file=chooser.getSelectedFile();

if(file==null)

return;

try {

PrintWriter pw=new PrintWriter(file);

pw.print(output1.getText());

pw.flush();

pw.print(output2.getText());

pw.flush();

}catch (IOException e1) {

e1.printStackTrace();

}

}

});

JPanel buttonPane=new JPanel();

buttonPane.add(open);

buttonPane.add(save);

this.add(buttonPane,BorderLayout.SOUTH);

}

public String[] getSentences(){

ArrayListString set=new ArrayListString();

int length=input.getText().length();

for(int i=0,last=0;ilength;i++){

String s=String.valueOf(input.getText().charAt(i));

if(s.equals(“\n”))

last=i+1;

if(s.equals(“.”)||s.equals(“,”)||s.equals(“。”)||s.equals(“。”)||s.equals(“!”)||s.equals(“?”)||s.equals(“?”)||s.equals(“!”)||s.equals(“,”)){

set.add(input.getText().substring(last,i)+s);

last=i+1;

}

}

return set.StringtoArray(new String[set.size()]);

}

public String getKeySentences(String[] key,String[] sentences){

String result=””;

A: for(int i=0;isentences.length;i++){

for (int k = 0; k key.length; k++)

if (sentences[i].contains(key[k].subSequence(0, key[k].length()))) {

result += sentences[i] + “\n”;

continue A;

}

}

return result;

}

private JTextArea input;

private JTextArea output1;

private JTextArea output2;

private JButton open;

private JButton save;

private String[] keyWords1;

private String[] keyWords2;

public static void main(String… args){

EventQueue.invokeLater(new Runnable(){

public void run(){

new Application2().setVisible(true);

}

});

}

}

原創文章,作者:FXJGD,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/316988.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
FXJGD的頭像FXJGD
上一篇 2025-01-09 12:15
下一篇 2025-01-09 12:15

相關推薦

  • Java JsonPath 效率優化指南

    本篇文章將深入探討Java JsonPath的效率問題,並提供一些優化方案。 一、JsonPath 簡介 JsonPath是一個可用於從JSON數據中獲取信息的庫。它提供了一種DS…

    編程 2025-04-29
  • java client.getacsresponse 編譯報錯解決方法

    java client.getacsresponse 編譯報錯是Java編程過程中常見的錯誤,常見的原因是代碼的語法錯誤、類庫依賴問題和編譯環境的配置問題。下面將從多個方面進行分析…

    編程 2025-04-29
  • Java Bean載入過程

    Java Bean載入過程涉及到類載入器、反射機制和Java虛擬機的執行過程。在本文中,將從這三個方面詳細闡述Java Bean載入的過程。 一、類載入器 類載入器是Java虛擬機…

    編程 2025-04-29
  • Java騰訊雲音視頻對接

    本文旨在從多個方面詳細闡述Java騰訊雲音視頻對接,提供完整的代碼示例。 一、騰訊雲音視頻介紹 騰訊雲音視頻服務(Cloud Tencent Real-Time Communica…

    編程 2025-04-29
  • Java Milvus SearchParam withoutFields用法介紹

    本文將詳細介紹Java Milvus SearchParam withoutFields的相關知識和用法。 一、什麼是Java Milvus SearchParam without…

    編程 2025-04-29
  • Java 8中某一周的周一

    Java 8是Java語言中的一個版本,於2014年3月18日發布。本文將從多個方面對Java 8中某一周的周一進行詳細的闡述。 一、數組處理 Java 8新特性之一是Stream…

    編程 2025-04-29
  • Java判斷字元串是否存在多個

    本文將從以下幾個方面詳細闡述如何使用Java判斷一個字元串中是否存在多個指定字元: 一、字元串遍歷 字元串是Java編程中非常重要的一種數據類型。要判斷字元串中是否存在多個指定字元…

    編程 2025-04-29
  • AES加密解密演算法的C語言實現

    AES(Advanced Encryption Standard)是一種對稱加密演算法,可用於對數據進行加密和解密。在本篇文章中,我們將介紹C語言中如何實現AES演算法,並對實現過程進…

    編程 2025-04-29
  • VSCode為什麼無法運行Java

    解答:VSCode無法運行Java是因為默認情況下,VSCode並沒有集成Java運行環境,需要手動添加Java運行環境或安裝相關插件才能實現Java代碼的編寫、調試和運行。 一、…

    編程 2025-04-29
  • 學習Python對學習C語言有幫助嗎?

    Python和C語言是兩種非常受歡迎的編程語言,在程序開發中都扮演著非常重要的角色。那麼,學習Python對學習C語言有幫助嗎?答案是肯定的。在本文中,我們將從多個角度探討Pyth…

    編程 2025-04-29

發表回復

登錄後才能評論