Reranker Framework (ReFr)
Reranking framework for structure prediction and discriminative language modeling
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
pyutil.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 #-----------------------------------------------------------------------
3 # Copyright 2012, Google Inc.
4 # All rights reserved.
5 #
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions are
8 # met:
9 #
10 # * Redistributions of source code must retain the above copyright
11 # notice, this list of conditions and the following disclaimer.
12 # * Redistributions in binary form must reproduce the above
13 # copyright notice, this list of conditions and the following disclaimer
14 # in the documentation and/or other materials provided with the
15 # distribution.
16 # * Neither the name of Google Inc. nor the names of its
17 # contributors may be used to endorse or promote products derived from
18 # this software without specific prior written permission.
19 #
20 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 # -----------------------------------------------------------------------------
32 # File: khutil
33 # Version ID: $Id: khutil.py,v 1.8 2007/11/16 21:35:40 khall Exp $
34 # various utility functions
35 #
36 #-----------------------------------------------------------------------
37 import subprocess,os,sys,math
38 DEBUG = 7
39 
40 def checkFile(fileName):
41  if (not os.path.isfile(fileName)):
42  sys.stderr.write('ERROR: file not found: ' + fileName + '\n')
43  sys.exit(10)
44  return
45 
46 def runCommand(sysCommand):
47  printDebug(7, "Executing command: " + sysCommand + "\n")
48  return os.system(sysCommand)
49 
50 def readCommand(sysCommand, outList):
51  printDebug(7, "Executing command: " + sysCommand + "\n")
52  syspipe = os.popen(sysCommand)
53  for sysLine in syspipe:
54  outList.append(sysLine .strip())
55  return (syspipe.close() == None)
56 
57 def writeCommand(sysCommand, toWrite):
58  printDebug(7, "Writing " + toWrite + "\n to: " + sysCommand + "\n")
59  newfsmpipe = os.popen(sysCommand, 'w')
60  newfsmpipe.write(toWrite.strip() + "\n")
61  newfsmpipe.close()
62 
63 def rwCommand(sysCommand, toWrite, outList):
64  printDebug(7, "Reading output from writing " + toWrite + "\n to: " + sysCommand + "\n")
65  (pin, pout) = os.popen2(sysCommand)
66  pin.write(toWrite.strip() + "\n")
67  pin.close()
68  for sysLine in pout:
69  outList.append(sysLine.strip())
70  pout.close()
71  return
72 
73 def printDebug(debLevel, errStr):
74  if (DEBUG >= debLevel):
75  sys.stderr.write(errStr)
76  return
77 
78 def printInfo(errStr):
79  sys.stderr.write("INFO: " + errStr + "\n")
80 
81 def printWarn(errStr):
82  sys.stderr.write("WARN: " + errStr + "\n")
83 
84 def printError(errorCode, errStr):
85  sys.stderr.write("ERROR: " + errStr + "\n")
86  sys.exit(errorCode)
87 
88 def logSum(x, y):
89  if (x < (y - math.log(1e200))):
90  return y
91  if (y < (x - math.log(1e200))):
92  return x
93  logdiff = x - y
94  # make sure the difference is not too large
95  if (x > y and logdiff > x):
96  return x
97  if (y > x and logdiff > y):
98  return y
99  #otherwise return the actual sum
100  return y + math.log(1.0 + math.exp(logdiff))
101 
102 # A class which let's us start up a command, send requests and read replies
103 # via stdio.
104 class CommandIO:
105  def __init__(self, sysCommand):
106  printDebug(7, "Starting up process: " + sysCommand)
107  self.evalp_ = subprocess.Popen(sysCommand, shell=True, bufsize=0,
108  stdin=subprocess.PIPE, stdout=subprocess.PIPE,
109  close_fds=True)
110  if self.evalp_.poll():
111  printError(100, "Command " + sysCommand + " did not start up correctly.")
112 
113  def sendreceive(self, toWrite):
114  self.evalp_.stdin.write(toWrite.strip() + "\n")
115  return self.evalp_.stdout.readline().strip()
def runCommand
Definition: pyutil.py:46
def printWarn
Definition: pyutil.py:81
def checkFile
Definition: pyutil.py:40
def rwCommand
Definition: pyutil.py:63
def writeCommand
Definition: pyutil.py:57
def printDebug
Definition: pyutil.py:73
def readCommand
Definition: pyutil.py:50
def printError
Definition: pyutil.py:84
def printInfo
Definition: pyutil.py:78
def logSum
Definition: pyutil.py:88