⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 invertedindex.java

📁 常用机器学习算法,java编写源代码,内含常用分类算法,包括说明文档
💻 JAVA
字号:
/* Copyright (C) 2002 Univ. of Massachusetts Amherst, Computer Science Dept.   This file is part of "MALLET" (MAchine Learning for LanguagE Toolkit).   http://www.cs.umass.edu/~mccallum/mallet   This software is provided under the terms of the Common Public License,   version 1.0, as published by http://www.opensource.org.  For further   information, see the file `LICENSE' included with this distribution. *//**	 Simple, in-memory inverted index that stores a list of instances having each feature, but not	 a value associated with each.  Currently only works with FeatureVectors.   @author Andrew McCallum <a href="mailto:mccallum@cs.umass.edu">mccallum@cs.umass.edu</a> */package edu.umass.cs.mallet.base.types;import java.util.*;public class InvertedIndex{	InstanceList ilist;	ArrayList[] ii;	public InvertedIndex (InstanceList ilist)	{		int numFeatures = ilist.getDataAlphabet().size();		ii = new ArrayList[numFeatures];		for (int i = 0; i < ilist.size(); i++) {			Instance inst = ilist.getInstance(i);			if (!(inst.getData() instanceof FeatureVector))				throw new IllegalArgumentException (this.getClass().getName() +																						" currently only handles FeatureVector data");			FeatureVector fv = (FeatureVector) inst.getData ();			for (int fl = 0; fl < fv.numLocations(); fl++) {				if (fv.valueAtLocation(fl) != 0)					addEntry (fv.indexAtLocation(fl), inst);			}		}	}	private void addEntry (int featureIndex, Instance instance)	{		if (ii[featureIndex] == null)			ii[featureIndex] = new ArrayList(2);		ii[featureIndex].add (instance);	}	public InstanceList getInstanceList () { return ilist; }	public ArrayList getInstancesWithFeature (int featureIndex)	{		return ii[featureIndex];	}	public ArrayList getInstancesWithFeature (Object feature)	{		int index = ilist.getDataAlphabet().lookupIndex (feature, false);		if (index == -1)			throw new IllegalArgumentException ("Feature "+feature+" not contained in InvertedIndex");		return getInstancesWithFeature (index);	}	public int getCountWithFeature (int featureIndex)	{		ArrayList a = ii[featureIndex];		return a == null ? 0 : a.size();	}	public int getCountWithFeature (Object feature)	{		int index = ilist.getDataAlphabet().lookupIndex (feature, false);		if (index == -1)			throw new IllegalArgumentException ("Feature "+feature+" not contained in InvertedIndex");		ArrayList a = ii[index];		return a == null ? 0 : a.size();	}	}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -