use of ruc.irm.similarity.word.hownet2.sememe.Sememe in project xsimilarity by iamxiatian.
the class SememeTreeUI method load.
/**
* 加载义原到Multimap中,并创建树的节点
*
* @return
* @throws IOException
*/
private static DefaultMutableTreeNode load() throws IOException {
/**
* 存放parentId和该parentId所隶属的义原,即Key为parentId,value为子义原
*/
Multimap<String, Sememe> sememes = ArrayListMultimap.create();
logger.info("Try to load hownet/sememe.xml.gz from resources");
InputStream input = SememeTreeUI.class.getResourceAsStream("/data/sememe.xml.gz");
input = new GZIPInputStream(input);
System.out.println("[" + SememeTreeUI.class.getSimpleName() + "]loading sememes into sememe tree...");
long time = System.currentTimeMillis();
try {
XMLInputFactory inputFactory = XMLInputFactory.newInstance();
XMLEventReader xmlEventReader = inputFactory.createXMLEventReader(input);
while (xmlEventReader.hasNext()) {
XMLEvent event = xmlEventReader.nextEvent();
if (event.isStartElement()) {
StartElement startElement = event.asStartElement();
if (startElement.getName().toString().equals("sememe")) {
String en = startElement.getAttributeByName(QName.valueOf("en")).getValue();
String cn = startElement.getAttributeByName(QName.valueOf("cn")).getValue();
String id = startElement.getAttributeByName(QName.valueOf("id")).getValue();
Attribute attr = startElement.getAttributeByName(QName.valueOf("define"));
String define = (attr == null ? null : attr.getValue());
Sememe sememe = new Sememe(id, en, cn, define);
int pos = id.lastIndexOf("-");
String parentId = "root";
if (pos > 0) {
parentId = id.substring(0, pos);
}
sememes.put(parentId, sememe);
}
}
}
input.close();
} catch (Exception e) {
throw new IOException(e);
}
time = System.currentTimeMillis() - time;
System.out.println("complete. time elapsed: " + (time / 1000) + "s");
DefaultMutableTreeNode top = new DefaultMutableTreeNode("知网义原层次关系树");
createNodes(sememes, top, "root");
return top;
}
use of ruc.irm.similarity.word.hownet2.sememe.Sememe in project xsimilarity by iamxiatian.
the class SememeTreeUI method createNodes.
private static void createNodes(Multimap<String, Sememe> sememes, DefaultMutableTreeNode top, String parentId) {
Collection<Sememe> children = sememes.get(parentId);
for (Sememe child : children) {
String text = child.getEnWord() + "|" + child.getCnWord();
if (child.getDefine() != null) {
text += " " + child.getDefine();
}
DefaultMutableTreeNode childNode = new DefaultMutableTreeNode(text);
top.add(childNode);
createNodes(sememes, childNode, child.getId());
}
}
Aggregations