本文整理汇总了Java中com.hankcs.hanlp.corpus.io.IOUtil类的典型用法代码示例。如果您正苦于以下问题:Java IOUtil类的具体用法?Java IOUtil怎么用?Java IOUtil使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
IOUtil类属于com.hankcs.hanlp.corpus.io包,在下文中一共展示了IOUtil类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testExtract
import com.hankcs.hanlp.corpus.io.IOUtil; //导入依赖的package包/类
public void testExtract() throws Exception
{
List<File> fileList = FolderWalker.open(FOLDER);
Map<String, String> phraseMap = new TreeMap<String, String>();
int i = 0;
for (File file : fileList)
{
System.out.print(++i + " / " + fileList.size() + " " + file.getName() + " ");
String path = file.getAbsolutePath();
List<String> phraseList = MutualInformationEntropyPhraseExtractor.extract(IOUtil.readTxt(path), 3);
System.out.print(phraseList);
for (String phrase : phraseList)
{
phraseMap.put(phrase, file.getAbsolutePath());
}
System.out.println();
}
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("data/phrase.txt")));
for (Map.Entry<String, String> entry : phraseMap.entrySet())
{
bw.write(entry.getKey() + "\t" + entry.getValue());
bw.newLine();
}
bw.close();
}
示例2: load
import com.hankcs.hanlp.corpus.io.IOUtil; //导入依赖的package包/类
private static boolean load(String path)
{
String binPath = path + Predefine.BIN_EXT;
if (loadBin(binPath)) return true;
CONVERT = new char[Character.MAX_VALUE + 1];
for (int i = 0; i < CONVERT.length; i++)
{
CONVERT[i] = (char) i;
}
IOUtil.LineIterator iterator = new IOUtil.LineIterator(path);
while (iterator.hasNext())
{
String line = iterator.next();
if (line == null) return false;
if (line.length() != 3) continue;
CONVERT[line.charAt(0)] = CONVERT[line.charAt(2)];
}
logger.info("正在缓存字符正规化表到" + binPath);
IOUtil.saveObjectTo(CONVERT, binPath);
return true;
}
示例3: loadBin
import com.hankcs.hanlp.corpus.io.IOUtil; //导入依赖的package包/类
private static boolean loadBin(String path)
{
try
{
ObjectInputStream in = new ObjectInputStream(IOUtil.newInputStream(path));
CONVERT = (char[]) in.readObject();
in.close();
}
catch (Exception e)
{
logger.warning("字符正规化表缓存加载失败,原因如下:" + e);
return false;
}
return true;
}
示例4: saveDat
import com.hankcs.hanlp.corpus.io.IOUtil; //导入依赖的package包/类
static boolean saveDat(String path, TreeMap<String, Integer> map)
{
try
{
DataOutputStream out = new DataOutputStream(new BufferedOutputStream(IOUtil.newOutputStream(path)));
Collection<Integer> freqList = map.values();
out.writeInt(freqList.size());
for (int freq : freqList)
{
out.writeInt(freq);
}
trie.save(out);
out.close();
}
catch (Exception e)
{
logger.log(Level.WARNING, "在缓存" + path + "时发生异常", e);
return false;
}
return true;
}
示例5: saveDat
import com.hankcs.hanlp.corpus.io.IOUtil; //导入依赖的package包/类
/**
* 保存dat到磁盘
* @param map
* @return
*/
static boolean saveDat(TreeMap<String, Character> map)
{
try
{
DataOutputStream out = new DataOutputStream(IOUtil.newOutputStream(path + Predefine.VALUE_EXT));
out.writeInt(map.size());
for (Character character : map.values())
{
out.writeChar(character);
}
out.close();
}
catch (Exception e)
{
logger.warning("保存值" + path + Predefine.VALUE_EXT + "失败" + e);
return false;
}
return trie.save(path + Predefine.TRIE_EXT);
}
示例6: loadDat
import com.hankcs.hanlp.corpus.io.IOUtil; //导入依赖的package包/类
private EnumItem<NR>[] loadDat(String path)
{
byte[] bytes = IOUtil.readBytes(path);
if (bytes == null) return null;
NR[] nrArray = NR.values();
int index = 0;
int size = ByteUtil.bytesHighFirstToInt(bytes, index);
index += 4;
EnumItem<NR>[] valueArray = new EnumItem[size];
for (int i = 0; i < size; ++i)
{
int currentSize = ByteUtil.bytesHighFirstToInt(bytes, index);
index += 4;
EnumItem<NR> item = new EnumItem<NR>();
for (int j = 0; j < currentSize; ++j)
{
NR nr = nrArray[ByteUtil.bytesHighFirstToInt(bytes, index)];
index += 4;
int frequency = ByteUtil.bytesHighFirstToInt(bytes, index);
index += 4;
item.labelMap.put(nr, frequency);
}
valueArray[i] = item;
}
return valueArray;
}
示例7: saveDat
import com.hankcs.hanlp.corpus.io.IOUtil; //导入依赖的package包/类
private boolean saveDat(String path, EnumItem<NS>[] valueArray)
{
try
{
DataOutputStream out = new DataOutputStream(IOUtil.newOutputStream(path));
out.writeInt(valueArray.length);
for (EnumItem<NS> item : valueArray)
{
out.writeInt(item.labelMap.size());
for (Map.Entry<NS, Integer> entry : item.labelMap.entrySet())
{
out.writeInt(entry.getKey().ordinal());
out.writeInt(entry.getValue());
}
}
out.close();
}
catch (Exception e)
{
logger.warning("保存失败" + e);
return false;
}
return true;
}
示例8: load
import com.hankcs.hanlp.corpus.io.IOUtil; //导入依赖的package包/类
public boolean load(String path)
{
trie = new BinTrie<Byte>();
if (loadDat(path + Predefine.TRIE_EXT)) return true;
String line = null;
try
{
BufferedReader bw = new BufferedReader(new InputStreamReader(IOUtil.newInputStream(path)));
while ((line = bw.readLine()) != null)
{
trie.put(line, null);
}
bw.close();
}
catch (Exception e)
{
logger.warning("加载" + path + "失败," + e);
}
if (!trie.save(path + Predefine.TRIE_EXT)) logger.warning("缓存" + path + Predefine.TRIE_EXT + "失败");
return true;
}
示例9: loadDat
import com.hankcs.hanlp.corpus.io.IOUtil; //导入依赖的package包/类
private EnumItem<NT>[] loadDat(String path)
{
byte[] bytes = IOUtil.readBytes(path);
if (bytes == null) return null;
NT[] values = NT.values();
int index = 0;
int size = ByteUtil.bytesHighFirstToInt(bytes, index);
index += 4;
EnumItem<NT>[] valueArray = new EnumItem[size];
for (int i = 0; i < size; ++i)
{
int currentSize = ByteUtil.bytesHighFirstToInt(bytes, index);
index += 4;
EnumItem<NT> item = new EnumItem<NT>();
for (int j = 0; j < currentSize; ++j)
{
NT tag = values[ByteUtil.bytesHighFirstToInt(bytes, index)];
index += 4;
int frequency = ByteUtil.bytesHighFirstToInt(bytes, index);
index += 4;
item.labelMap.put(tag, frequency);
}
valueArray[i] = item;
}
return valueArray;
}
示例10: saveDat
import com.hankcs.hanlp.corpus.io.IOUtil; //导入依赖的package包/类
private boolean saveDat(String path, EnumItem<NT>[] valueArray)
{
try
{
DataOutputStream out = new DataOutputStream(IOUtil.newOutputStream(path));
out.writeInt(valueArray.length);
for (EnumItem<NT> item : valueArray)
{
out.writeInt(item.labelMap.size());
for (Map.Entry<NT, Integer> entry : item.labelMap.entrySet())
{
out.writeInt(entry.getKey().ordinal());
out.writeInt(entry.getValue());
}
}
out.close();
}
catch (Exception e)
{
logger.warning("保存失败" + e);
return false;
}
return true;
}
示例11: saveDat
import com.hankcs.hanlp.corpus.io.IOUtil; //导入依赖的package包/类
static boolean saveDat(String path, AhoCorasickDoubleArrayTrie<Pinyin[]> trie, Set<Map.Entry<String, Pinyin[]>> entrySet)
{
try
{
DataOutputStream out = new DataOutputStream(IOUtil.newOutputStream(path + Predefine.BIN_EXT));
out.writeInt(entrySet.size());
for (Map.Entry<String, Pinyin[]> entry : entrySet)
{
Pinyin[] value = entry.getValue();
out.writeInt(value.length);
for (Pinyin pinyin : value)
{
out.writeInt(pinyin.ordinal());
}
}
trie.save(out);
out.close();
}
catch (Exception e)
{
logger.warning("缓存值dat" + path + "失败");
return false;
}
return true;
}
示例12: load
import com.hankcs.hanlp.corpus.io.IOUtil; //导入依赖的package包/类
static boolean load(String path)
{
trie = new DoubleArrayTrie<String>();
if (loadDat(path + ".bi" + Predefine.BIN_EXT)) return true;
TreeMap<String, String> map = new TreeMap<String, String>();
for (String line : IOUtil.readLineListWithLessMemory(path))
{
String[] param = line.split(" ");
if (param[0].endsWith("@"))
{
continue;
}
String dependency = param[1];
map.put(param[0], dependency);
}
if (map.size() == 0) return false;
trie.build(map);
if (!saveDat(path, map)) logger.warning("缓存" + path + Predefine.BIN_EXT + "失败");
return true;
}
示例13: saveDat
import com.hankcs.hanlp.corpus.io.IOUtil; //导入依赖的package包/类
static boolean saveDat(String path, TreeMap<String, String> map)
{
Collection<String> dependencyList = map.values();
// 缓存值文件
try
{
DataOutputStream out = new DataOutputStream(IOUtil.newOutputStream(path + ".bi" + Predefine.BIN_EXT));
out.writeInt(dependencyList.size());
for (String dependency : dependencyList)
{
out.writeUTF(dependency);
}
if (!trie.save(out)) return false;
out.close();
}
catch (Exception e)
{
logger.warning("保存失败" + e);
return false;
}
return true;
}
示例14: saveTxtTo
import com.hankcs.hanlp.corpus.io.IOUtil; //导入依赖的package包/类
@Override
public boolean saveTxtTo(String path)
{
if (trie.size() == 0) return true; // 如果没有词条,那也算成功了
try
{
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(IOUtil.newOutputStream(path), "UTF-8"));
Set<Map.Entry<String, Item>> entries = trie.entrySet();
for (Map.Entry<String, Item> entry : entries)
{
bw.write(entry.getValue().toString());
bw.newLine();
}
bw.close();
}
catch (Exception e)
{
logger.warning("保存到" + path + "失败" + e);
return false;
}
return true;
}
示例15: saveTxtTo
import com.hankcs.hanlp.corpus.io.IOUtil; //导入依赖的package包/类
@Override
public boolean saveTxtTo(String path)
{
try
{
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(IOUtil.newOutputStream(path)));
bw.write(toString());
bw.close();
}
catch (Exception e)
{
logger.warning("在保存转移矩阵词典到" + path + "时发生异常" + e);
return false;
}
return true;
}