源程序如下:
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import javax.swing.event.*;
import java.io.*;
import java.lang.*;
import java.util.*;public class CHNSeg {
public static void main(String args[]){
new CHNWindow();
}
}
class Segment{//分词
//StringBuffer dicWord = new StringBuffer();
String wordTemp = null, w = "", resultStr = "";
String word[] = new String[20];
int i, maxLength, flag;
RandomAccessFile inAndOut = null;
Segment(){
try{
inAndOut = new RandomAccessFile("d.txt","rw");
i = 0;
while((wordTemp = inAndOut.readLine()) != null){
i ++;
word[i] = wordTemp;
}
}
catch(IOException e){};
}
String Seg(String s){
maxLength = 10;
flag = 0;//0为关键字未在词典中
//try{
//inAndOut = new RandomAccessFile("d.txt","rw");
/*while((wordTemp = inAndOut.readLine()) != null){
i ++;
word[i] = wordTemp;
}*/
StringBuffer resultStrTmp = new StringBuffer();
while(s != null){
int len = (int)s.length();
if(len > maxLength){
len = maxLength;
}
while(flag == 0 && len > 2){
w = s.substring(0,len);
for(int j = 0; j < word.length; j ++){
if(word[j] != w){
flag = 0;
len -= 2;
}
else flag = 1;
}
}
resultStrTmp.append(w + "/");
s = s.substring(len,s.length());
}
resultStr = resultStrTmp.toString();
//catch(IOException e){};
return resultStr;
}
}
//界面class CHNWindow extends JFrame implements ActionListener{
JTextField inputText, resultText1, resultText2;
JButton initBtn, resultBtn;
Box baseBox, boxV1, boxV2;
String str = "";
CHNWindow(){
this.setTitle("中文分词");
//inputText = new JTextArea();
//resultText = new JTextArea();
initBtn = new JButton("原始划分");
resultBtn = new JButton("人工辅助");
inputText = new JTextField(25);
resultText1 = new JTextField(25);
resultText2 = new JTextField(25);
boxV1 = Box.createVerticalBox();
boxV1.add(new JLabel(" "));
boxV1.add(Box.createVerticalStrut(18));
boxV1.add(initBtn);
boxV1.add(Box.createVerticalStrut(8));
boxV1.add(resultBtn);
boxV1.add(Box.createVerticalStrut(8));
boxV2 = Box.createVerticalBox();
boxV2.add(inputText);
boxV2.add(Box.createVerticalStrut(20));
boxV2.add(resultText1);
boxV2.add(Box.createVerticalStrut(14));
boxV2.add(resultText2);
boxV2.add(Box.createVerticalStrut(12));
baseBox = Box.createHorizontalBox();
baseBox.add(boxV2);
baseBox.add(Box.createHorizontalStrut(10));
// baseBox.add(boxV3);
// baseBox.add(Box.createHorizontalStrut(10));
baseBox.add(boxV1);
setLayout(new FlowLayout());
add(baseBox);
validate();
//
/*inputText.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
str = e.getActionCommand();
}
});*/
initBtn.addActionListener(this);
resultBtn.addActionListener(this);
setBounds(120,125,400,145);
setVisible(true);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
//Segment segment = new Segment();
}
//事件处理
public void actionPerformed(ActionEvent e){
str = inputText.getText();
Segment segment = new Segment();
JButton button = (JButton)e.getSource();
if(button == initBtn){
str = segment.Seg(str);
resultText1.setText(str);
}
else if(button == resultBtn){
resultText2.setText("null");
}
}
}
这是我自己编的简单的中文分词,还没细化。我已在相应的位置建立了d.txt文件(词典),每行一个单词。编译通过,但运行出现错误:java.lang.OutOfMemoryError: Java heap space 等等。想了好久也没想出来。希望无论是专业的还是业余的,能帮我找出问题之所在。更希望大家能对程序提供别的方面的建议。小弟在此有礼了。
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import javax.swing.event.*;
import java.io.*;
import java.lang.*;
import java.util.*;public class CHNSeg {
public static void main(String args[]){
new CHNWindow();
}
}
class Segment{//分词
//StringBuffer dicWord = new StringBuffer();
String wordTemp = null, w = "", resultStr = "";
String word[] = new String[20];
int i, maxLength, flag;
RandomAccessFile inAndOut = null;
Segment(){
try{
inAndOut = new RandomAccessFile("d.txt","rw");
i = 0;
while((wordTemp = inAndOut.readLine()) != null){
i ++;
word[i] = wordTemp;
}
}
catch(IOException e){};
}
String Seg(String s){
maxLength = 10;
flag = 0;//0为关键字未在词典中
//try{
//inAndOut = new RandomAccessFile("d.txt","rw");
/*while((wordTemp = inAndOut.readLine()) != null){
i ++;
word[i] = wordTemp;
}*/
StringBuffer resultStrTmp = new StringBuffer();
while(s != null){
int len = (int)s.length();
if(len > maxLength){
len = maxLength;
}
while(flag == 0 && len > 2){
w = s.substring(0,len);
for(int j = 0; j < word.length; j ++){
if(word[j] != w){
flag = 0;
len -= 2;
}
else flag = 1;
}
}
resultStrTmp.append(w + "/");
s = s.substring(len,s.length());
}
resultStr = resultStrTmp.toString();
//catch(IOException e){};
return resultStr;
}
}
//界面class CHNWindow extends JFrame implements ActionListener{
JTextField inputText, resultText1, resultText2;
JButton initBtn, resultBtn;
Box baseBox, boxV1, boxV2;
String str = "";
CHNWindow(){
this.setTitle("中文分词");
//inputText = new JTextArea();
//resultText = new JTextArea();
initBtn = new JButton("原始划分");
resultBtn = new JButton("人工辅助");
inputText = new JTextField(25);
resultText1 = new JTextField(25);
resultText2 = new JTextField(25);
boxV1 = Box.createVerticalBox();
boxV1.add(new JLabel(" "));
boxV1.add(Box.createVerticalStrut(18));
boxV1.add(initBtn);
boxV1.add(Box.createVerticalStrut(8));
boxV1.add(resultBtn);
boxV1.add(Box.createVerticalStrut(8));
boxV2 = Box.createVerticalBox();
boxV2.add(inputText);
boxV2.add(Box.createVerticalStrut(20));
boxV2.add(resultText1);
boxV2.add(Box.createVerticalStrut(14));
boxV2.add(resultText2);
boxV2.add(Box.createVerticalStrut(12));
baseBox = Box.createHorizontalBox();
baseBox.add(boxV2);
baseBox.add(Box.createHorizontalStrut(10));
// baseBox.add(boxV3);
// baseBox.add(Box.createHorizontalStrut(10));
baseBox.add(boxV1);
setLayout(new FlowLayout());
add(baseBox);
validate();
//
/*inputText.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
str = e.getActionCommand();
}
});*/
initBtn.addActionListener(this);
resultBtn.addActionListener(this);
setBounds(120,125,400,145);
setVisible(true);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
//Segment segment = new Segment();
}
//事件处理
public void actionPerformed(ActionEvent e){
str = inputText.getText();
Segment segment = new Segment();
JButton button = (JButton)e.getSource();
if(button == initBtn){
str = segment.Seg(str);
resultText1.setText(str);
}
else if(button == resultBtn){
resultText2.setText("null");
}
}
}
这是我自己编的简单的中文分词,还没细化。我已在相应的位置建立了d.txt文件(词典),每行一个单词。编译通过,但运行出现错误:java.lang.OutOfMemoryError: Java heap space 等等。想了好久也没想出来。希望无论是专业的还是业余的,能帮我找出问题之所在。更希望大家能对程序提供别的方面的建议。小弟在此有礼了。
解决方案 »
- java网络编程高手请进!为什么用java获取到的网卡的名称是乱码?我改了字符编码还是乱码,有什么办法可以解决吗?
- 深入jdk,同大家探讨一个问题关于jdbc
- 如何实现四级联动的下拉列表框(列表项从数据库中取出)
- 关于类的继承多态和静态函数的问题
- 如何利用反射机制 读取私有属性的值?
- c:\Documents and settings\sony>是什么意思?
- 一个菜鸟的jdbc问题
- 低级问题:.java文件交付用户怎么运行,难道也是DOS下:java xx.java?有没有.exe?
- 谁能提供JAVA常用词汇的一个下载地址?
- 【Java------能解决这个问题吗???】
- 帮忙解释下这是什么 异常
- 为什么会这样?
import java.awt.event.*;
import java.awt.*;
import javax.swing.event.*;
import java.io.*;
import java.lang.*;
import java.util.*; public class CHNSeg {
public static void main(String args[]){
new CHNWindow();
}
}
class Segment{//分词
//StringBuffer dicWord = new StringBuffer();
String wordTemp = null, w = "", resultStr = "";
String word[] = new String[20];
int i, maxLength, flag;
RandomAccessFile inAndOut = null;
Segment(){
try{
inAndOut = new RandomAccessFile("d.txt","rw");
i = 0;
while((wordTemp = inAndOut.readLine()) != null){
i ++;
word[i] = wordTemp;
}
}
catch(IOException e){};
}
String Seg(String s){ maxLength = 10;
flag = 0;//0为关键字未在词典中
//try{
//inAndOut = new RandomAccessFile("d.txt","rw");
/*while((wordTemp = inAndOut.readLine()) != null){
i ++;
word[i] = wordTemp;
}*/
StringBuffer resultStrTmp = new StringBuffer();
while(s != null){
int len = (int)s.length();
if(len > maxLength){
len = maxLength;
}
while(flag == 0 && len > 2){
w = s.substring(0,len);
for(int j = 0; j < word.length; j ++){
if(word[j] != w){
flag = 0;
len -= 2;
}
else flag = 1;
}
}
resultStrTmp.append(w + "/");
s = s.substring(len,s.length());
}
resultStr = resultStrTmp.toString();
//catch(IOException e){};
return resultStr;
}
}
//界面 class CHNWindow extends JFrame implements ActionListener{
JTextField inputText, resultText1, resultText2;
JButton initBtn, resultBtn;
Box baseBox, boxV1, boxV2;
String str = "";
CHNWindow(){ this.setTitle("中文分词");
//inputText = new JTextArea();
//resultText = new JTextArea();
initBtn = new JButton("原始划分");
resultBtn = new JButton("人工辅助");
inputText = new JTextField(25);
resultText1 = new JTextField(25);
resultText2 = new JTextField(25);
boxV1 = Box.createVerticalBox();
boxV1.add(new JLabel(" "));
boxV1.add(Box.createVerticalStrut(18));
boxV1.add(initBtn);
boxV1.add(Box.createVerticalStrut(8));
boxV1.add(resultBtn);
boxV1.add(Box.createVerticalStrut(8));
boxV2 = Box.createVerticalBox();
boxV2.add(inputText);
boxV2.add(Box.createVerticalStrut(20));
boxV2.add(resultText1);
boxV2.add(Box.createVerticalStrut(14));
boxV2.add(resultText2);
boxV2.add(Box.createVerticalStrut(12));
baseBox = Box.createHorizontalBox();
baseBox.add(boxV2);
baseBox.add(Box.createHorizontalStrut(10));
// baseBox.add(boxV3);
// baseBox.add(Box.createHorizontalStrut(10));
baseBox.add(boxV1);
setLayout(new FlowLayout());
add(baseBox);
validate();
//
/*inputText.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
str = e.getActionCommand();
}
});*/
initBtn.addActionListener(this);
resultBtn.addActionListener(this);
setBounds(120,125,400,145);
setVisible(true);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); //Segment segment = new Segment(); }
//事件处理
public void actionPerformed(ActionEvent e){
str = inputText.getText();
Segment segment = new Segment();
JButton button = (JButton)e.getSource();
if(button == initBtn){
str = segment.Seg(str);
resultText1.setText(str);
}
else if(button == resultBtn){
resultText2.setText("null");
}
}
}
词典d.txt格式如下(每行一个单词;已保存到相应位置):
我
是
小明在第一个文本框输入:我是小明
当单击“原始划分”按钮时,期望结果为:我/是/小明但是运行时报错:Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
at java.util.Arrays.copyOf(Unknown Source)
at java.lang.AbstractStringBuilder.expandCapacity(Unknown Source)
at java.lang.AbstractStringBuilder.append(Unknown Source)
at java.lang.StringBuffer.append(Unknown Source)
at Segment.Seg(CHNSeg.java:199) //指的是resultStrTmp.append(w + "/");
at CHNWindow.actionPerformed(CHNSeg.java:270) //指的是str = segment.Seg(str);
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)另外,该程序不涉及复杂情况分析,只是就上面的简单情况。希望大家能亲自编译解决。
虽然一楼没有给出问题之所在,但仍感谢。