How to get the WordNet synset given an offset ID?

前端 未结 4 2035
天命终不由人
天命终不由人 2020-12-13 10:51

I have a WordNet synset offset (for example id=\"n#05576222\"). Given this offset, how can I get the synset using Python?

4条回答
  •  萌比男神i
    2020-12-13 11:28

    Other than using NLTK, another option would be to use the .tab file from the Open Multilingual WordNet http://compling.hss.ntu.edu.sg/omw/ for the Princeton WordNet. Normally i used the recipe below to access wordnet as a dictionary with offset as the key and ; delimited strings as a values:

    # 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')
    

提交回复
热议问题