File: dict2db

package info (click to toggle)
duali 0.2.0-1.1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 132 kB
  • ctags: 140
  • sloc: python: 823; makefile: 38
file content (164 lines) | stat: -rwxr-xr-x 4,274 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#!/usr/bin/env python
#---
# $Id: dict2db,v 1.7 2003/12/09 05:11:00 elzubeir Exp $
#
# ------------
# Description:
# ------------
#
# Convert the Buckwalter data sets to 
#
# (C) Copyright 2003, Arabeyes, Mohammed Elzubeir
# -----------------
# Revision Details:    (Updated by Revision Control System)
# -----------------
#  $Date: 2003/12/09 05:11:00 $
#  $Author: elzubeir $
#  $Revision: 1.7 $
#  $Source: /home/arabeyes/cvs/projects/duali/pyduali/dict2db,v $
#
#  This program is written under the BSD License.
#---

import sys, os, string, getopt, anydbm 

scriptname = os.path.splitext(os.path.basename(sys.argv[0]))[0]
scriptversion = '$Revision: 1.7 $'

def chomp(s):
  if (s.endswith('\n')):
    return s[:-1]
  else:
    return s

def getGlossPOS(s):
  """
  Break up the glosspos into a tuple, first the pos then the gloss
  If the there is no pos then simply return an empty string and the gloss
  """
  start = s.find('<pos>')
  if (not start):
    return ('', s)
  end = s.find('</pos>', start)
  return (s[start+5:end-6].strip(), s[:start-1].strip())

def createDictDB(filename):
  "Create bsddb hash dictionary"

  w_stem = ''

  try:
    lines = open(filename, 'r').readlines()
  except IOError:
    print "Unable to read '%s'" % filename
    sys.exit(0)

  dbfile = "%sdb" % filename
  dict = anydbm.open(dbfile, 'n')

  print "\nWriting %s..." % (filename),
  for line in lines:
    line = chomp(line)
    if (line.startswith(';;')):
      dict[line[3:]] = "\t\t\t\t"
      w_stem = line[3:]
    if (line.startswith(';')):
      pass
    else:
      nsplit = len(line.split('\t'))
      if (nsplit < 4 or nsplit > 4):
        print "\nFatal error in file: %s\nline: %s" % (filename, line)
        sys.exit(1)
      (w_vanilla, w_full, w_cat, w_glossPOS) = line.split('\t')
      w_pos, w_gloss = getGlossPOS(w_glossPOS)
      if (len(w_pos)==0):
        if (w_cat.startswith('Pref-0') 
          or w_cat.startswith('Suff-0')):
          w_pos = ""
        elif (w_cat.startswith('F')):
          w_pos = "%s/FUNC_WORD" % w_full
        elif (w_cat.startswith('IV')):
          w_pos = "%s/VERB_IMPERFECT" % w_full
        elif (w_cat.startswith('PV')):
          w_pos = "%s/VERB_PERFECT" % w_full
        elif (w_cat.startswith('CV')):
          w_pos = "%s/VERB_IMPERATIVE" % w_full
        elif (w_cat.startswith('N')):
          w_pos = "%s/NOUN" % w_full # needs review here
        else:
          print "Fatal error has occurred parsing %s" \
              % filename
          print "line: %s" % line
          sys.exit(1)
      try:
        dict[w_vanilla] = "%s\t%s\t%s\t%s\t%s" % (w_full, w_cat, w_gloss, w_pos,
                                                  w_stem)
      except:
        print "\nWarning: Failed to index \'%s\' - blame MS-Windows ;)" \
              % w_gloss
  if os.uname()[0] == 'FreeBSD':
    pass
  else:
    dict.sync()

def usage():
  "Display usage options"

  print "(C) Copyright 2003, Arabeyes, Mohammed Elzubeir\n"
  print "Usage: %s [OPTIONS]" % scriptname
  print "\t[-h | --help           ]\toutputs this usage message"
  print "\t[-v | --version        ]\tprogram version"
  print "\t[-p | --path           ]\tpath to dictionary database"
  print "\r\nThis program is licensed under the BSD License\n"

def grabargs():
  "Grab command-line arguments"

  path = ''

  try:
    opts, args = getopt.getopt(sys.argv[1:], "hvp:",
                               ["help", "version", "path="],)
  except getopt.GetoptError:
    usage()
    sys.exit(0)
  for o, val in opts:    
    if o in ("-h", "--help"):
      usage()
      sys.exit(0)
    if o in ("-v", "--version"):
      print scriptversion
      sys.exit(0)
    if o in ("-p", "--path"):
      path = val
  return (path)


def main():
  "Main function"

  print "%s - %s" % (scriptname, scriptversion)
  path = grabargs()

  stems = "stems"
  prefixes = "prefixes"
  suffixes = "suffixes"

  if not path:
    if not os.path.exists(path):
      print "Path does not exist!"
      sys.exit(0)
  else:
    stems = os.path.join(path, stems)
    prefixes = os.path.join(path, prefixes)
    suffixes = os.path.join(path, suffixes)
  
  createDictDB(stems)
  createDictDB(prefixes)
  createDictDB(suffixes)

  sys.exit(0)

if __name__ == "__main__":
  main()