Search in sources :

Example 1 with Sememe

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;
}
Also used : DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) Attribute(javax.xml.stream.events.Attribute) Sememe(ruc.irm.similarity.word.hownet2.sememe.Sememe) GZIPInputStream(java.util.zip.GZIPInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) IOException(java.io.IOException) GZIPInputStream(java.util.zip.GZIPInputStream) StartElement(javax.xml.stream.events.StartElement) XMLEvent(javax.xml.stream.events.XMLEvent) XMLEventReader(javax.xml.stream.XMLEventReader) XMLInputFactory(javax.xml.stream.XMLInputFactory)

Example 2 with Sememe

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());
    }
}
Also used : DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) Sememe(ruc.irm.similarity.word.hownet2.sememe.Sememe)

Aggregations

DefaultMutableTreeNode (javax.swing.tree.DefaultMutableTreeNode)2 Sememe (ruc.irm.similarity.word.hownet2.sememe.Sememe)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 GZIPInputStream (java.util.zip.GZIPInputStream)1 XMLEventReader (javax.xml.stream.XMLEventReader)1 XMLInputFactory (javax.xml.stream.XMLInputFactory)1 Attribute (javax.xml.stream.events.Attribute)1 StartElement (javax.xml.stream.events.StartElement)1 XMLEvent (javax.xml.stream.events.XMLEvent)1