㈠ 如何用java實現文本的關鍵字提取
可以使用java API中的URL抓取流,也可以使用Apache的HttpClient等多種方法,最終得到的就是字元串咯,得到字元串就好辦了,使用正則匹配,將匹配的保存起來就可以了
㈡ java word中提取關鍵字
首先,要用Java的File類中函數(如open,read等,記不清了)打開word文檔,利用形式語言與自動機的原理,跳過word文檔的頭部,分析內容,利用String中的equals()方法匹配"java"字元串。
㈢ Java 提取網頁中的標題和關鍵字的正則表達式
title ?不就是標題么?
<title>這個標題?</title>
你直接split("title>").就行啊。然後再對第一個substr就行了啊。分為三個字元串么。
< 、 這個標題?</ 、後面還有一個其他的。
對索引 1 的字元串截取就行了。
㈣ 提取Java關鍵字
我寫了個Keywords.java類,代碼如下:
*************************************************************
importjava.awt.BorderLayout;
importjava.awt.Dimension;
importjava.awt.FlowLayout;
importjava.awt.GridLayout;
importjava.awt.Toolkit;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjava.io.BufferedReader;
importjava.io.File;
importjava.io.FileNotFoundException;
importjava.io.FileReader;
importjava.io.IOException;
importjava.util.StringTokenizer;
importjavax.swing.BorderFactory;
importjavax.swing.JButton;
importjavax.swing.JFileChooser;
importjavax.swing.JFrame;
importjavax.swing.JPanel;
importjavax.swing.JScrollPane;
importjavax.swing.JTextArea;
importjavax.swing.UIManager;
{
String[]keywords={"abstract","boolean","break","byte","case",
"catch","char","class","continue","default","do","double",
"else","extends","false","final","finally","float","for",
"if","implements","import","instanceof","int","interface",
"long","native","new","null","package","private","protected",
"public","return","short","static","super","switch",
"synchronized","this","throw","throws","transient","true",
"try","void","volatile","while","const","goto"};
JTextAreatext;
JTextArearesult;
publicKeywords(){
this.setTitle("計算關鍵字數");
//文本框
text=newJTextArea(6,50);
text.setLineWrap(true);
JScrollPanetextScroll=newJScrollPane(text);
text.setBorder(BorderFactory.createBevelBorder(1));
JPaneltextPanel=newJPanel(newBorderLayout());
textPanel.setBorder(BorderFactory.createTitledBorder("導入的文本"));
textPanel.add(textScroll);
//結果框
result=newJTextArea(6,50);
result.setLineWrap(true);
JScrollPaneresultScroll=newJScrollPane(result);
result.setBorder(BorderFactory.createBevelBorder(1));
JPanelresultPanel=newJPanel(newBorderLayout());
resultPanel.setBorder(BorderFactory.createTitledBorder("計算結果"));
resultPanel.add(resultScroll);
//導入文本和結果框
JPanelallPanel=newJPanel();
allPanel.setLayout(newGridLayout(2,1));
allPanel.add(textPanel);
allPanel.add(resultPanel);
//按鈕
JButtonimpButton=newJButton("導入文本");
JButtoncalcButton=newJButton("計算關鍵字數");
JPanelbuttonPanel=newJPanel(newFlowLayout());
buttonPanel.add(impButton);
buttonPanel.add(calcButton);
//添加
this.add(allPanel,BorderLayout.CENTER);
this.add(buttonPanel,BorderLayout.SOUTH);
//this.setSize(400,300);
this.pack();
Toolkittool=Toolkit.getDefaultToolkit();
Dimensionscreen=tool.getScreenSize();
this.setLocation(screen.width/2-this.getWidth()/2,screen.height
/2-this.getHeight()/2);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//導入文本
impButton.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEvente){
writeText();
}
});
//計算關鍵字數
calcButton.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEvente){
//for(Stringss:s){
//System.out.println(ss);
//}
String[]start=text.getText().trim().split(" ");
System.out.println("start:"+start.length);
//String[]rets=newString[start.length];
StringBufferret=newStringBuffer();
for(inti=0;i<start.length;i++){
StringTokenizerst=newStringTokenizer(start[i]);
intn=0;
while(st.hasMoreTokens()){
Stringcomp=st.nextToken();
for(Strings:keywords){
if(s.equals(comp)){
n++;
if(n==1){
ret.append(i+":");
ret.append(s);
}else{
ret.append(","+s);
}
}
}
}
if(n!=0){
ret.append(" ");
}
}
result.setText(ret.toString().trim());
}
});
}
//導入文本
publicvoidwriteText(){
try{
JFileChooserchooser=newJFileChooser();
chooser.showOpenDialog(this);
Filefile=chooser.getSelectedFile();
BufferedReaderbr=newBufferedReader(newFileReader(file));
Strings;
text.setText("");
while((s=br.readLine())!=null){
text.append(s+" ");
}
text.setText(text.getText().trim());
System.out.println(text.getText());
}catch(FileNotFoundExceptione){
e.printStackTrace();
}catch(IOExceptione2){
e2.printStackTrace();
}
}
publicstaticvoidmain(String[]args)throwsException{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
newKeywords();
}
}
*************************************************************
運行結果如下:
㈤ JAVA語言的關鍵字提取問題
能力有限用個最笨的辦法了
publicStringmatchName(StringfilePath){
BufferedReaderreader=newBufferedReader(newFileReader(filePath));
//讀取文本
StringBuffersb=newStringBuffer();
Stringstr;
while(null!=(str=reader.readLine())){
sb.append(str);
sb.append(" ");
}
Stringrex="項目名稱";
StringtotalStr=sb.toString();
//獲取rex第一次出現的位置
intfirst=totalStr.indexOf(rex);
//從該位置截取30長度的字元串
Stringresult=totalStr.substring(first,first+30);
//返回第一行
returnresult.split(" ")[0];
}
㈥ java的關鍵字有哪些
48個關鍵字:abstract、assert、boolean、break、byte、case、catch、char、class、continue、default、do、double、else、enum、extends、final、finally、float、for、if、implements、import、int、interface、instanceof、long、native、new、package、private、protected、public、return、short、static、strictfp、super、switch、synchronized、this、throw、throws、transient、try、void、volatile、while。
2個保留字(現在沒用以後可能用到作為關鍵字):goto、const。
3個特殊直接量:true、false、null。
㈦ 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代碼如何在百度輸入關鍵字查詢,再提取搜索結果呢
如果是想用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(length<5)
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(length<5)
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(){
ArrayList<String> set=new ArrayList<String>();
int length=input.getText().length();
for(int i=0,last=0;i<length;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.<String>toArray(new String[set.size()]);
}
public String getKeySentences(String[] key,String[] sentences){
String result="";
A: for(int i=0;i<sentences.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);
}
});
}
}