concordance.py :  » Database » PyWordNet » pywordnet-2.0.1 » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Database » PyWordNet 
PyWordNet » pywordnet 2.0.1 » concordance.py
# some accessing of the semantic concordance data for wordnet 1.6
# by Des Berry, berry@ais.it

import string, os
from wordnet import binarySearchFile

# Sample entries in the 'taglist' file
#   ordinary%1:18:01:: 1 br-a01:78,1;86,1;88,4
#   ordered%5:00:00:organized:01 2 br-j23:6,14;13,32;66,12
# where the general form is:
#   lemma%ss_type:lex_filenum:lex_id:head_word:head_id sense_number
[location_list]
#   location_list: filename:sent_num,word_num[;sent_num,word_num...]

ss_type = ("NOUN", "VERB", "ADJECTIVE", "ADVERB", "ADJECTIVE SATELLITE")

# given a sentence number (and the contents of a semantic concordance file)
# return a string of words as the sentence
def find_sentence(snum, msg):
  str = "<s snum=%s>" % snum
  s = string.find(msg, str)
  if s < 0:
    return "<Unknown>"
  s = s + len(str)
  sentence = ""
  tag = ""
  while 1:
    if msg[s] == '\n':
      s = s + 1
    n = string.find(msg, '<', s)
    if n < 0:
      break
    if n - s != 0:
      if tag == "w" and msg[s] != "'" and len(sentence) > 0: # word form
        sentence = sentence + " "
      sentence = sentence + msg[s:n]
    e = string.find(msg, '>', n)
    if e < 0:
      break
    tag = msg[n+1]
    if tag == "/": #check for ending sentence
      if msg[n+2] == 's':
        #end of sentence
        break
    s = e + 1
  return sentence

# given a taglist sense (one line of the tagfile) and where to find the tagfile (root)
# return a tuple of
#  symset type ('1' .. '5')
#  sense (numeric character string)
#  list of sentences (constructed from the taglist)
def tagsentence(tag, root):
  s = string.find(tag, '%')
  sentence = []
  type = tag[s+1]
  c = s
  for i in range(0,4):
    c = string.find(tag, ':', c + 1)
  c = string.find(tag, ' ', c + 1)
  sense = tag[c+1]
  c = c + 3
  while 1:
    d = string.find(tag, ' ', c) # file separator
    if d < 0:
      loclist = tag[c:]
    else:
      loclist = tag[c:d]
      c = d + 1

    e = string.find(loclist, ':')
    filename = loclist[:e]
    fh = open(root + filename, "rb")
    msg = fh.read()
    fh.close()

    while 1:
      e = e + 1
      f = string.find(loclist, ';', e)
      if f < 0:
        sent_word = loclist[e:]
      else:
        sent_word = loclist[e:f]
        e = f

      g = string.find(sent_word, ',')
      sent = sent_word[:g]

      sentence.append(find_sentence(sent, msg))

      if f < 0:
        break

    if d < 0:
      break
  return (type, sense, sentence)

# given a word to search for and where to find the files (root)
# displays the information
# This could be changed to display in different ways!
def sentences(word, root):
  cache = {}
  file = open(root + "taglist", "rb")
  key = word + "%"
  keylen = len(key)
  binarySearchFile(file, key + " ", cache, 10)
  print "Word '%s'" % word
  while 1:
    line = file.readline()
    if line[:keylen] != key:
      break
    type, sense, sentence = tagsentence(line, root + "tagfiles/")
    print ss_type[string.atoi(type) - 1], sense
    for sent in sentence:
      print sent


def _test(word, corpus, base):
  print corpus
  sentences("ordinary", base + corpus + "/")

if __name__ == '__main__':
  base = "C:/win16/dict/semcor/"
  word = "ordinary"
  _test(word, "brown1", base)
  _test(word, "brown2", base)
  _test(word, "brownv", base)
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.