Package jazzparser :: Package parsers :: Package tagrank :: Module parser
[hide private]
[frames] | no frames]

Source Code for Module jazzparser.parsers.tagrank.parser

 1  """Simple extension of a CKY parser to propagate probabilities  
 2  through a derivation from the tag probabilities. 
 3   
 4  This is not really a probabilistic parser, it's just a basic CKY  
 5  parser with some extra bits to pass probabilities up through a  
 6  derivation from the tags. It's designed to be used with the C&C  
 7  tagger, so that the results get ranked according to the probabilities  
 8  of the tags they came from. 
 9   
10  """ 
11  """ 
12  ============================== License ======================================== 
13   Copyright (C) 2008, 2010-12 University of Edinburgh, Mark Granroth-Wilding 
14    
15   This file is part of The Jazz Parser. 
16    
17   The Jazz Parser is free software: you can redistribute it and/or modify 
18   it under the terms of the GNU General Public License as published by 
19   the Free Software Foundation, either version 3 of the License, or 
20   (at your option) any later version. 
21    
22   The Jazz Parser is distributed in the hope that it will be useful, 
23   but WITHOUT ANY WARRANTY; without even the implied warranty of 
24   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
25   GNU General Public License for more details. 
26    
27   You should have received a copy of the GNU General Public License 
28   along with The Jazz Parser.  If not, see <http://www.gnu.org/licenses/>. 
29   
30  ============================ End license ====================================== 
31   
32  """ 
33  __author__ = "Mark Granroth-Wilding <mark.granroth-wilding@ed.ac.uk>"  
34   
35  from jazzparser.parsers.cky.parser import CkyParser 
36  from jazzparser.parsers.pcfg.tools import ProbabilisticResultListTool, ProbabilityTool, ProbabilisticChartTool 
37  from jazzparser.parsers.cky.tools import ChartTool 
38   
39  from .chart import TagRankChart 
40   
41  import sys, re 
42  import logging 
43   
44  from jazzparser import settings 
45   
46  # Get the logger from the logging system 
47  logger = logging.getLogger("main_logger") 
48   
49 -class TagRankParser(CkyParser):
50 shell_tools = [ 51 ChartTool(), 52 # The PCFG parser provides some handy probability tools 53 ProbabilisticResultListTool(), 54 ProbabilityTool(), 55 ProbabilisticChartTool(), 56 ] 57
58 - def _create_chart(self, *args, **kwargs):
59 self.chart = TagRankChart(self.grammar, *args, **kwargs) 60 return self.chart
61
62 - def _add_signs(self, offset):
63 vals = super(TagRankParser, self)._add_signs(offset) or [] 64 for (start,end,signtup) in vals: 65 if signtup[0] is None: 66 continue 67 # Add the probabilities as an attribute to the signs 68 cat,tag,prob = signtup 69 cat.probability = prob 70 return vals
71
72 - def parse(self, *args, **kwargs):
73 """ 74 Performs a full parse and returns the results ranked by 75 a product of their tag probabilities. 76 77 """ 78 parses = super(TagRankParser, self).parse(*args, **kwargs) 79 # If parses were found, use the ranked version of them 80 if len(self.chart.parses) > 0: 81 return self.chart.ranked_parses 82 else: 83 # No parses, but the backoff could have given something 84 return parses
85