㈠ 如何用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);
}
});
}
}