sentence_io.py 3.22 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# Copyright 2017 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================

Ivan Bogatyy's avatar
Ivan Bogatyy committed
16
17
18
19
20
"""Utilities for reading and writing sentences in dragnn."""
import tensorflow as tf
from syntaxnet.ops import gen_parser_ops


21
22
class FormatSentenceReader(object):
  """A reader for formatted files, with optional projectivizing."""
Ivan Bogatyy's avatar
Ivan Bogatyy committed
23

24
25
26
27
28
29
30
  def __init__(self,
               filepath,
               record_format,
               batch_size=32,
               check_well_formed=False,
               projectivize=False,
               morph_to_pos=False):
Ivan Bogatyy's avatar
Ivan Bogatyy committed
31
32
33
34
35
    self._graph = tf.Graph()
    self._session = tf.Session(graph=self._graph)
    task_context_str = """
          input {
            name: 'documents'
36
            record_format: '%s'
Ivan Bogatyy's avatar
Ivan Bogatyy committed
37
38
39
            Part {
             file_pattern: '%s'
            }
40
          }""" % (record_format, filepath)
Ivan Bogatyy's avatar
Ivan Bogatyy committed
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
    if morph_to_pos:
      task_context_str += """
          Parameter {
            name: "join_category_to_pos"
            value: "true"
          }
          Parameter {
            name: "add_pos_as_attribute"
            value: "true"
          }
          Parameter {
            name: "serialize_morph_to_pos"
            value: "true"
          }
          """
    with self._graph.as_default():
      self._source, self._is_last = gen_parser_ops.document_source(
          task_context_str=task_context_str, batch_size=batch_size)
59
60
      if check_well_formed:
        self._source = gen_parser_ops.well_formed_filter(self._source)
Ivan Bogatyy's avatar
Ivan Bogatyy committed
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
      if projectivize:
        self._source = gen_parser_ops.projectivize_filter(self._source)

  def read(self):
    """Reads a single batch of sentences."""
    if self._session:
      sentences, is_last = self._session.run([self._source, self._is_last])
      if is_last:
        self._session.close()
        self._session = None
    else:
      sentences, is_last = [], True
    return sentences, is_last

  def corpus(self):
    """Reads the entire corpus, and returns in a list."""
    tf.logging.info('Reading corpus...')
    corpus = []
    while True:
      sentences, is_last = self.read()
      corpus.extend(sentences)
      if is_last:
        break
    tf.logging.info('Read %d sentences.' % len(corpus))
    return corpus
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102


class ConllSentenceReader(FormatSentenceReader):
  """A sentence reader that uses an underlying 'conll-sentence' reader."""

  def __init__(self,
               filepath,
               batch_size=32,
               projectivize=False,
               morph_to_pos=False):
    super(ConllSentenceReader, self).__init__(
        filepath,
        'conll-sentence',
        check_well_formed=True,
        batch_size=batch_size,
        projectivize=projectivize,
        morph_to_pos=morph_to_pos)