pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

用一個固定的Key對應一個HashSet創建一個HashMap.出

Create a HashMap with a fixed Key corresponding to a HashSet. point of departure(用一個固定的Key對應一個HashSet創建一個HashMap.出發點)
本文介紹了用一個固定的Key對應一個HashSet創建一個HashMap.出發點的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我的目標是創建一個以字符串為鍵、條目值為字符串的 HashSet 的哈希圖.

My aim is to create a hashmap with a String as the key, and the entry values as a HashSet of Strings.

輸出

現在的輸出如下所示:

Hudson+(surname)=[Q2720681], Hudson,+Quebec=[Q141445], Hudson+(given+name)=[Q5928530], Hudson,+Colorado=[Q2272323], Hudson,+Illinois=[Q2672022], Hudson,+Indiana=[Q2710584], Hudson,+Ontario=[Q5928505], Hudson,+Buenos+Aires+Province=[Q10298710], Hudson,+Florida=[Q768903]]

按照我的想法,應該是這樣的:

According to my idea, it should look like this:

[Hudson+(surname)=[Q2720681,Q141445,Q5928530,Q2272323,Q2672022]]

<小時>

目的是在維基數據中存儲一個特定的名稱,然后存儲與其相關的所有 Q 值的消歧,例如:


The purpose is to store a particular name in Wikidata and then all of the Q values associated with it's disambiguation, so for example:

這個是布什"的頁面.

我希望布什成為關鍵,然后對于所有不同的出發點,布什可以與維基數據的終端頁面相關聯的所有不同方式,我想存儲相應的Q 值"或唯一的字母數字標識符.

I want Bush to be the Key, and then for all of the different points of departure, all of the different ways that Bush could be associated with a terminal page of Wikidata, I want to store the corresponding "Q value", or unique alpha-numeric identifier.

我實際上正在做的是嘗試從維基百科消歧中抓取不同的名稱、值,然后在 wikidata 中查找與該值關聯的唯一字母數字標識符.

What I'm actually doing is trying to scrape the different names, values, from the wikipedia disambiguation and then look up the unique alpha-numeric identifier associated with that value in wikidata.

例如,使用 Bush 我們有:

For example, with Bush we have:

George H. W. Bush 
George W. Bush
Jeb Bush
Bush family
Bush (surname) 

相應的 Q 值為:

喬治 HW 布什 (Q23505)

喬治·W·布什(Q207)

杰布·布什 (Q221997)

布什家族 (Q2743830)

Bush family (Q2743830)

布什 (Q1484464)

Bush (Q1484464)

我的想法是數據結構應該按如下方式來解釋

關鍵:布什條目集: Q23505、Q207、Q221997、Q2743830、Q1484464

但我現在的代碼并沒有這樣做.

But the code I have now doesn't do that.

它為每個名稱和 Q 值創建一個單獨的條目.即

It creates a seperate entry for each name and Q value. i.e.

密鑰:杰布·布什條目集: Q221997

鑰匙:喬治·W·布什條目集: Q207

等等.

完整的代碼可以在 mygithub頁面,但我也會在下面總結一下.

The full code in all it's glory can be seen on my github page, but I'll summarize it below also.

這是我用來為我的數據結構添加值的方法:

This is what I'm using to add values to my data strucuture:

// add Q values to their arrayList in the hash map at the index of the appropriate entity
public static HashSet<String> put_to_hash(String key, String value) 
{
    if (!q_valMap.containsKey(key)) 
    {
        return q_valMap.put(key, new HashSet<String>() );
    }
    HashSet<String> list = q_valMap.get(key);
    list.add(value);
    return q_valMap.put(key, list);
}

這是我獲取內容的方式:

This is how I fetch the content:

    while ((line_by_line = wiki_data_pagecontent.readLine()) != null) 
    {
        // if we can determine it's a disambig page we need to send it off to get all 
        // the possible senses in which it can be used.
        Pattern disambig_pattern = Pattern.compile("<div class="wikibase-entitytermsview-heading-description ">Wikipedia disambiguation page</div>");
        Matcher disambig_indicator = disambig_pattern.matcher(line_by_line);
        if (disambig_indicator.matches()) 
        {
            //off to get the different usages
            Wikipedia_Disambig_Fetcher.all_possibilities( variable_entity );
        }
        else
        {
            //get the Q value off the page by matching
            Pattern q_page_pattern = Pattern.compile("<!-- wikibase-toolbar --><span class="wikibase-toolbar-container"><span class="wikibase-toolbar-item " +
                    "wikibase-toolbar ">\[<span class="wikibase-toolbar-item wikibase-toolbar-button wikibase-toolbar-button-edit"><a " +
                    "href="/wiki/Special:SetSiteLink/(.*?)">edit</a></span>\]</span></span>");

            Matcher match_Q_component = q_page_pattern.matcher(line_by_line);
            if ( match_Q_component.matches() ) 
            {
                String Q = match_Q_component.group(1);

                // 'Q' should be appended to an array, since each entity can hold multiple
                // Q values on that basis of disambig
                put_to_hash( variable_entity, Q );
            }
        }

    }

這就是我處理消歧頁面的方式:

and this is how I deal with a disambiguation page:

public static void all_possibilities( String variable_entity ) throws Exception
{
    System.out.println("this is a disambig page");
    //if it's a disambig page we know we can go right to the wikipedia


    //get it's normal wiki disambig page
    Document docx = Jsoup.connect( "https://en.wikipedia.org/wiki/" + variable_entity ).get();



        //this can handle the less structured ones. 
        Elements linx = docx.select( "p:contains(" + variable_entity + ") ~ ul a:eq(0)" );

        for (Element linq : linx) 
        {
            System.out.println(linq.text());
            String linq_nospace = linq.text().replace(' ', '+');
            Wikidata_Q_Reader.getQ( linq_nospace );

        }

}

我在想也許我可以傳遞 Key 值,但我真的不知道.我有點卡住了.也許有人可以看到我如何實現這個功能.

I was thinking maybe I could pass the Key value around, but I really don't know. I'm kind of stuck. Maybe someone can see how I can implement this functionality.

推薦答案

我不清楚你的問題是什么不起作用,或者你是否看到實際錯誤.但是,雖然您的基本數據結構想法(StringSetHashMap 是合理的,但添加"中有一個錯誤功能.

I'm not clear from your question what isn't working, or if you're seeing actual errors. But, while your basic data structure idea (HashMap of String to Set<String>) is sound, there's a bug in the "add" function.

public static HashSet<String> put_to_hash(String key, String value) 
{
    if (!q_valMap.containsKey(key)) 
    {
        return q_valMap.put(key, new HashSet<String>() );
    }
    HashSet<String> list = q_valMap.get(key);
    list.add(value);
    return q_valMap.put(key, list);
}

在第一次看到鍵的情況下(if (!q_valMap.containsKey(key))),它會為該鍵激活一個新的 HashSet,但它不會在返回之前添加 value 給它.(并且返回的值是該鍵的舊值,因此它將為空.)因此您將丟失每個術語的 Q 值.

In the case where a key is seen for the first time (if (!q_valMap.containsKey(key))), it vivifies a new HashSet for that key, but it doesn't add value to it before returning. (And the returned value is the old value for that key, so it'll be null.) So you're going to be losing one of the Q-values for every term.

對于像這樣的多層數據結構,我通常特例只是中間結構的激活,然后在單個代碼路徑中進行添加和返回.我認為這會解決它.(我也將它稱為 valSet 因為它是一個集合而不是一個列表.而且沒有必要每次都將集合重新添加到地圖中;它是一個引用類型并被添加第一次遇到那個鍵.)

For multi-layered data structures like this, I usually special-case just the vivification of the intermediate structure, and then do the adding and return in a single code path. I think this would fix it. (I'm also going to call it valSet because it's a set and not a list. And there's no need to re-add the set to the map each time; it's a reference type and gets added the first time you encounter that key.)

public static HashSet<String> put_to_hash(String key, String value) 
{
    if (!q_valMap.containsKey(key)) {
        q_valMap.put(key, new HashSet<String>());
    } 
    HashSet<String> valSet = q_valMap.get(key);
    valSet.add(value);
    return valSet;
}

還要注意,您返回的 Set 是對該鍵的實時 Set 的引用,因此在調用者中修改它時需要小心,如果你正在做多線程,你會遇到并發訪問問題.

Also be aware that the Set you return is a reference to the live Set for that key, so you need to be careful about modifying it in callers, and if you're doing multithreading you're going to have concurrent access issues.

或者只使用 Guava Multimap 這樣您就不必擔心自己編寫實現.

Or just use a Guava Multimap so you don't have to worry about writing the implementation yourself.

這篇關于用一個固定的Key對應一個HashSet創建一個HashMap.出發點的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Convert List of Strings into Map using Java-8 Streams API(使用 Java-8 Streams API 將字符串列表轉換為 Map)
Getting data from JSON(從 JSON 獲取數據)
java linkedhashmap iteration(javalinkedhashmap迭代)
Converting a list of objects to Map(將對象列表轉換為 Map)
HttpMessageConverter exception : RestClientException: Could not write request: no suitable HttpMessageConverter found(HttpMessageConverter 異常:RestClientException:無法寫入請求:找不到合適的 HttpMessageConverter) - IT屋-程序員
Best way to order an HashMap by key in Java?(在 Java 中按鍵排序 HashMap 的最佳方法?)
主站蜘蛛池模板: 生产自动包装秤_颗粒包装秤_肥料包装秤等包装机械-郑州鑫晟重工科技有限公司 | 锥形螺带干燥机(新型耙式干燥机)百科-常州丰能干燥工程 | 郑州墨香品牌设计公司|品牌全案VI设计公司 | 手机存放柜,超市储物柜,电子储物柜,自动寄存柜,行李寄存柜,自动存包柜,条码存包柜-上海天琪实业有限公司 | 纯水设备_苏州皙全超纯水设备水处理设备生产厂家 | 深圳3D打印服务-3D打印加工-手板模型加工厂-悟空打印坊 | 天津试验仪器-电液伺服万能材料试验机,恒温恒湿标准养护箱,水泥恒应力压力试验机-天津鑫高伟业科技有限公司 | 房间温控器|LonWorks|海思 | 除尘器布袋骨架,除尘器滤袋,除尘器骨架,电磁脉冲阀膜片,卸灰阀,螺旋输送机-泊头市天润环保机械设备有限公司 | 新材料分散-高速均质搅拌机-超声波分散混合-上海化烁智能设备有限公司 | 全自动五线打端沾锡机,全自动裁线剥皮双头沾锡机,全自动尼龙扎带机-东莞市海文能机械设备有限公司 | 电动液压篮球架_圆管地埋式篮球架_移动平箱篮球架-强森体育 | 自动气象站_农业气象站_超声波气象站_防爆气象站-山东万象环境科技有限公司 | 阻燃剂-氢氧化镁-氢氧化铝-沥青阻燃剂-合肥皖燃新材料 | 螺杆泵_中成泵业| 数码管_LED贴片灯_LED数码管厂家-无锡市冠卓电子科技有限公司 | 医学模型生产厂家-显微手术模拟训练器-仿真手术模拟训练系统-北京医教科技 | 常州企业采购平台_常州MRO采购公司_常州米孚机电设备有限公司 | 常州企业采购平台_常州MRO采购公司_常州米孚机电设备有限公司 | 除甲醛公司-甲醛检测-广西雅居环境科技有限公司 | 蒜肠网-动漫,二次元,COSPLAY,漫展以及收藏型模型,手办,玩具的新媒体.(原变形金刚变迷TF圈) | 武汉刮刮奖_刮刮卡印刷厂_为企业提供门票印刷_武汉合格证印刷_现金劵代金券印刷制作 - 武汉泽雅印刷有限公司 | 贝朗斯动力商城(BRCPOWER.COM) - 买叉车蓄电池上贝朗斯商城,价格更超值,品质有保障! | 光纤测温-荧光光纤测温系统-福州华光天锐光电科技有限公司 | 大型多片锯,圆木多片锯,方木多片锯,板材多片锯-祥富机械有限公司 | 机床主轴维修|刀塔维修|C轴维修-常州翔高精密机械有限公司 | 分光色差仪,测色仪,反透射灯箱,爱色丽分光光度仪,美能达色差仪维修_苏州欣美和仪器有限公司 | 接地电阻测试仪[厂家直销]_电缆故障测试仪[精准定位]_耐压测试仪-武汉南电至诚电力设备 | 反渗透水处理设备|工业零排放|水厂设备|软化水设备|海南净水设备--海南水处理设备厂家 | 广州市哲铭油墨涂料有限公司,水性漆生产研发基地 | 护栏打桩机-打桩机厂家-恒新重工 | 空心明胶胶囊|植物胶囊|清真胶囊|浙江绿键胶囊有限公司欢迎您! | 气力输送_输送机械_自动化配料系统_负压吸送_制造主力军江苏高达智能装备有限公司! | 定量包装机,颗粒定量包装机,粉剂定量包装机,背封颗粒包装机,定量灌装机-上海铸衡电子科技有限公司 | RFID电子标签厂家-上海尼太普电子有限公司 | 单电机制砂机,BHS制砂机,制沙机设备,制砂机价格-正升制砂机厂家 单级/双级旋片式真空泵厂家,2xz旋片真空泵-浙江台州求精真空泵有限公司 | 好物生环保网、环保论坛 - 环保人的学习交流平台 | 实木家具_实木家具定制_全屋定制_美式家具_圣蒂斯堡官网 | 净化车间_洁净厂房_净化公司_净化厂房_无尘室工程_洁净工程装修|改造|施工-深圳净化公司 | 杭州货架订做_组合货架公司_货位式货架_贯通式_重型仓储_工厂货架_货架销售厂家_杭州永诚货架有限公司 | 智成电子深圳tdk一级代理-提供TDK电容电感贴片蜂鸣器磁芯lambda电源代理经销,TDK代理商有哪些TDK一级代理商排名查询。-深圳tdk一级代理 |