【问题标题】:How to get the WordNet synset given an offset ID?如何在给定偏移 ID 的情况下获取 WordNet 同义词集?
【发布时间】:2011-12-26 00:46:17
【问题描述】:

我有一个 WordNet 同义词集偏移量(例如 id="n#05576222")。给定这个偏移量,我如何使用 Python 获取同义词集?

【问题讨论】:

    标签: python python-2.7 nlp nltk wordnet


    【解决方案1】:

    对于 NTLK 3.2.3 或更高版本,请参阅 donners45 的回答。

    对于旧版本的 NLTK:

    NLTK 中没有内置方法,但您可以使用:

    from nltk.corpus import wordnet
    
    syns = list(wordnet.all_synsets())
    offsets_list = [(s.offset(), s) for s in syns]
    offsets_dict = dict(offsets_list)
    
    offsets_dict[14204095]
    >>> Synset('heatstroke.n.01')
    

    然后您可以腌制字典并在需要时加载它。

    对于 3.0 之前的 NLTK 版本,替换行

    offsets_list = [(s.offset(), s) for s in syns]
    

    offsets_list = [(s.offset, s) for s in syns]
    

    因为在 NLTK 3.0 之前,offset 是属性而不是方法。

    【讨论】:

    • offset 现在是一种方法。试试这个:offsets_dict = {s.offset(): s for s in wn.all_synsets()}
    • “NLTK 中没有内置方法” - 现在有!请参阅 donners45 的回答;这个已经过时了。
    【解决方案2】:

    除了使用 NLTK,另一种选择是使用来自 Open Multilingual WordNet http://compling.hss.ntu.edu.sg/omw/ 的 .tab 文件用于普林斯顿 WordNet。通常我使用下面的方法来访问 wordnet 作为字典,偏移量作为键,; 分隔字符串作为值:

    # Gets first instance of matching key given a value and a dictionary.    
    def getKey(dic, value):
      return [k for k,v.split(";") in dic.items() if v in value]
    
    # Read Open Multi WN's .tab file
    def readWNfile(wnfile, option="ss"):
      reader = codecs.open(wnfile, "r", "utf8").readlines()
      wn = {}
      for l in reader:
        if l[0] == "#": continue
        if option=="ss":
          k = l.split("\t")[0] #ss as key
          v = l.split("\t")[2][:-1] #word
        else:
          v = l.split("\t")[0] #ss as value
          k = l.split("\t")[2][:-1] #word as key
        try:
          temp = wn[k]
          wn[k] = temp + ";" + v
        except KeyError:
          wn[k] = v  
      return wn
    
    princetonWN = readWNfile('wn-data-eng.tab')
    offset = "n#05576222"
    offset = offset.split('#')[1]+'-'+ offset.split('#')[0]
    
    print princetonWN.split(";")
    print getKey('heatstroke')
    

    【讨论】:

      【解决方案3】:

      从 NLTK 3.2.3 开始,有一个公共方法可以做到这一点:

      wordnet.synset_from_pos_and_offset(pos, offset)
      

      在早期版本中您可以使用:

      wordnet._synset_from_pos_and_offset(pos, offset)
      

      这会根据其 POS 和 offest ID 返回一个同义词集。我认为这种方法仅在 NLTK 3.0 中可用,但我不确定。

      例子:

      from nltk.corpus import wordnet as wn
      wn.synset_from_pos_and_offset('n',4543158)
      >> Synset('wagon.n.01')
      

      【讨论】:

      • 这个解决方案需要一个 pos 标签,而 Suzana 的不需要。有人可以解释为什么 wn.synset_from_pos_and_offset() 需要 pos 标签吗?
      【解决方案4】:

      可以使用of2ss(),例如:

      from nltk.corpus import wordnet as wn
      syn = wn.of2ss('01580050a')
      

      将返回 Synset('necessary.a.01')

      【讨论】:

        猜你喜欢
        • 2015-09-22
        • 1970-01-01
        • 2013-03-11
        • 1970-01-01
        • 2013-02-26
        • 1970-01-01
        • 1970-01-01
        • 2017-04-18
        • 2013-10-16
        相关资源
        最近更新 更多