`
finallyliuyu
  • 浏览: 8243 次
  • 性别: Icon_minigender_2
  • 来自: 北京
社区版块
存档分类
最新评论

报告ArrayList的一个错误,大家一起来研究下

阅读更多
代码目的:计算一段文本中重复出现的词的个数。
分两种情况:
1.文本在内存中
2.文本在硬盘文件上
方案利用ArrayList
声明一个类
public class Entity
{
String word;
float pValue;
public Entity()

pValue=0;
word="";

}

}

1.文本在内存中主类中有以下代码
String []words={"小","团圆","究竟","泄了","张爱玲","什么","秘密","2009年","04月","08日","09:56","天天","小","团圆","究竟","哈哈","张爱玲","全额","天涯","张爱玲"};
		ArrayList<Entity> enList=new ArrayList();
		for(String w: words)
		{  w=w.trim();
			Entity en=new Entity();
			en.word=w;
			en.pValue=1;
			enList.add(en);
			//System.out.println(w);
		}
	
		
		for(int i=0;i<enList.size()-1;i++)
		{ 
			
			if(!enList.get(i).word.isEmpty())
			{
				for(int j=i+1;j<enList.size();j++)
				{
					if(enList.get(i).word==enList.get(j).word)
					{
						enList.get(i).pValue++;
						enList.get(j).pValue=0;
						enList.get(j).word="";
					}
				}
			}
		}
		for(Entity e : enList)
		{
			System.out.println(e.word+e.pValue);
		}
	
System.out.println(enList.size());	
		
		



结果如下
小2.0团圆2.0究竟2.0泄了1.0张爱玲3.0什么1.0秘密1.02009年1.004月1.0



2.文本在硬盘文件中
主类代码如下
public static void main(String[] args) throws  FileNotFoundException,IOException
	{
		
		
		// TODO Auto-generated method stub;
		String result=CutText("E:/自然语言处理/KL/sb.txt");
		String []words=result.split("\\|");
		
	  
		ArrayList<Entity> enList=new ArrayList();
		for(String w: words)
		{  w=w.trim();
			Entity en=new Entity();
			en.word=w;
			en.pValue=1;
			enList.add(en);
			//System.out.println(w);
		}
	
		
		for(int i=0;i<enList.size()-1;i++)
		{ 
			
			if(!enList.get(i).word.isEmpty())
			{
				for(int j=i+1;j<enList.size();j++)
				{
					if(enList.get(i).word==enList.get(j).word)
					{
						enList.get(i).pValue++;
						enList.get(j).pValue=0;
						enList.get(j).word="";
					}
				}
			}
		}
		for(Entity e : enList)
		{
			System.out.println(e.word+e.pValue);
		}
	
System.out.println(enList.size());	
		
		
	
}
	

结果如下:

小1.0团圆1.0究竟1.0泄了1.0张爱玲1.0什么1.0秘密1.02009年1.004月1.0

可以看出当从硬盘中读文件后,该方法没有有效统计处文本中同一个词出现的个数
读取硬盘文件的函数以及分词函数如下
public static String GetFileText(String path) throws  FileNotFoundException,IOException
	{   
		InputStreamReader inStreamReader=new InputStreamReader(new FileInputStream(path));
		//String strFile1=
		BufferedReader bufReader=new BufferedReader(inStreamReader);
		String line;
		StringBuilder sb=new StringBuilder();
		while((line=bufReader.readLine())!=null)
		{
			sb.append(line+" ");
		}
		inStreamReader.close();
		bufReader.close();
		String strFile=sb.toString();
	  
		
		
		return strFile;
		
	}

	/*this function cut a piece of text in to words sequeces*/
	public static String CutText(String path)throws FileNotFoundException,IOException
	{
		
      String fileText=GetFileText(path);
     
		
		MMAnalyzer analyzer=new MMAnalyzer();
		String result =null;
		String spliter="|";
		try  	
        {
			result = analyzer.segment(fileText, spliter);	
		}  	
        catch (IOException e)  	
        { 	
        	e.printStackTrace(); 	
        } 	
		//System.out.print(result);
        return result;
		
	}


但是如果将:
	if(enList.get(i).word==enList.get(j).word)
					{
						enList.get(i).pValue++;
						enList.get(j).pValue=0;
						enList.get(j).word="";
					}

改为
	if([color=cyan]enList.get(i).word.equals(enList.get(j).word))[/color]
					{
						enList.get(i).pValue++;
						enList.get(j).pValue=0;
						enList.get(j).word="";
					}

则两种情况下都能正确统计出同一个词的出现次数这是为啥呢?
为了方便大家研究,我把这段代码上传,其中用到了jeasey分词器,和lucene.jar

分享到:
评论
4 楼 finallyliuyu 2010-03-08  
非常感谢你
3 楼 抛出异常的爱 2010-03-07  
finallyliuyu 写道
抛出异常的爱 写道
enList.get(i).word==enList.get(j).word

string pool

string pool是什么意思?
能详细说下嘛?我刚开始用java

他们能达成==不是由于他们的值相等
而是凑巧他们是从同一个池中得到的两个引用。。。。。

值等在java中只有equle.
==比的是引用。
2 楼 finallyliuyu 2010-03-07  
抛出异常的爱 写道
enList.get(i).word==enList.get(j).word

string pool

string pool是什么意思?
能详细说下嘛?我刚开始用java
1 楼 抛出异常的爱 2010-03-07  
enList.get(i).word==enList.get(j).word

string pool

相关推荐

Global site tag (gtag.js) - Google Analytics