summaryrefslogtreecommitdiff
path: root/sa-extract/cdat.pyx
blob: 57d3ad63605ef5ec09a043957f1dba61b9a95384 (plain)
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# cdat.pyx
# Defines "data arrays" that can be directly written to/read from disk in binary format
# In particular, the array itself is written/read directly as a glob of binary data
# Adam Lopez <alopez@cs.umd.edu>

import sys
import gzip
import log
import cintlist

from libc.stdio cimport FILE, fopen, fread, fwrite, fclose
from libc.stdlib cimport malloc, realloc, free
from libc.string cimport memset, strcpy, strlen

cdef class DataArray:

  def __init__(self, filename=None, from_binary=False, use_sent_id=False):
    self.word2id = {"END_OF_FILE":0, "END_OF_LINE":1}
    self.id2word = ["END_OF_FILE", "END_OF_LINE"]
    self.data = cintlist.CIntList(1000,1000)
    self.sent_id = cintlist.CIntList(1000,1000)
    self.sent_index = cintlist.CIntList(1000,1000)
    self.use_sent_id = use_sent_id

    if filename is not None:
      if from_binary:
        self.read_binary(filename)
      else:
        self.read_text(filename)


  def __len__(self):
    return len(self.data)


  def getSentId(self, i):
    return self.sent_id.arr[i]


  def getSent(self, i):
    cdef int j, start, stop
    sent = []
    start = self.sent_index.arr[i]
    stop = self.sent_index.arr[i+1]
    for i from start <= i < stop:
      sent.append(self.id2word[self.data.arr[i]])
    return sent


  def getSentPos(self, loc):
    return loc - self.sent_index.arr[self.sent_id.arr[loc]]


  def get_id(self, word):
    if not word in self.word2id:
      self.word2id[word] = len(self.id2word)
      self.id2word.append(word)
    return self.word2id[word]


  def get_word(self, id):
    return self.id2word[id]


  def write_text(self, filename):
    f = open(filename, "w")
    for w_id in self.data:
      if w_id > 1:
        f.write("%s " % self.get_word(w_id))
      if w_id == 1:
        f.write("\n")
    f.close()


  def read_text(self, filename):
    cdef int word_count

    if filename[-2:] == "gz":
      file = gzip.GzipFile(filename)
    else:
      file = open(filename)
    word_count = 0
    for line_num, line in enumerate(file):
      self.sent_index.append(word_count)
      for word in line.split():
        self.data.append(self.get_id(word))
        if self.use_sent_id:
          self.sent_id.append(line_num)
        word_count = word_count + 1
      self.data.append(1)
      if self.use_sent_id:
        self.sent_id.append(line_num)
      word_count = word_count + 1
    self.data.append(0)
    self.sent_index.append(word_count)

  def read_binary(self, filename):
    cdef FILE* f
    cdef bytes bfilename = filename
    cdef char* cfilename = bfilename
    f = fopen(cfilename, "r")
    self.read_handle(f)
    fclose(f)


  cdef void read_handle(self, FILE* f):
    cdef int num_words
    cdef int word_len
    cdef char* c_word
    cdef bytes py_word
    self.data.read_handle(f)
    self.sent_index.read_handle(f)
    self.sent_id.read_handle(f)
    fread(&(num_words), sizeof(int), 1, f)
    for i in xrange(num_words):
      fread(&(word_len), sizeof(int), 1, f)
      c_word = <char*> malloc (word_len * sizeof(char))
      fread(c_word, sizeof(char), word_len, f)
      py_word = c_word
      free(c_word)
      self.word2id[py_word] = len(self.id2word)
      self.id2word.append(py_word)
    if len(self.sent_id) == 0:
      self.use_sent_id = False
    else:
      self.use_sent_id = True


  cdef void write_handle(self, FILE* f):
    cdef int word_len
    cdef int num_words
    cdef char* c_word

    self.data.write_handle(f)
    self.sent_index.write_handle(f)
    self.sent_id.write_handle(f)
    num_words = len(self.id2word) - 2
    fwrite(&(num_words), sizeof(int), 1, f)
    for word in self.id2word[2:]:
      c_word = word
      word_len = strlen(c_word) + 1
      fwrite(&(word_len), sizeof(int), 1, f)
      fwrite(c_word, sizeof(char), word_len, f)


  def write_binary(self, filename):
    cdef FILE* f
    cdef bytes bfilename = filename
    cdef char* cfilename = bfilename
    f = fopen(cfilename, "w")
    self.write_handle(f)
    fclose(f)


  def write_enhanced_handle(self, f):
    for i in self.data:
      f.write("%d " %i)
    f.write("\n")
    for i in self.sent_index:
      f.write("%d " %i)
    f.write("\n")
    for i in self.sent_id:
      f.write("%d " %i)
    f.write("\n")
    for word in self.id2word:
      f.write("%s %d " % (word, self.word2id[word]))
    f.write("\n")



  def write_enhanced(self, filename):
    f = open(filename, "w")
    self.write_enhanced_handle(self, f)
    f.close()