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
|
#!/usr/bin/python
# -*- coding: iso-8859-1 -*-
#
# LanguageTool -- A Rule-Based Style and Grammar Checker
# Copyright (C) 2002,2003,2004 Daniel Naber <daniel.naber@t-online.de>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
import sys
import re
import Entities
import SentenceSplitter
class SentenceSplitterEval:
def __init__(self):
return
def findSentence(self, real_boundary, bnc_sentences):
sent = None
sent_disp = None
l = 0
i = 0
for s in bnc_sentences:
l = l + len(s)
if l == real_boundary:
sent = s
next_sent_start = ""
try:
next_sent_start = bnc_sentences[i+1][0:20]
except IndexError:
pass
sent_disp = "%s###%s..." % (s, next_sent_start)
break
i = i + 1
return sent, sent_disp
def run(self, bnc_string):
self.s = SentenceSplitter.SentenceSplitter()
# manual testing:
#bnc_string = "<s n=0000>This a test. Sentence.</s> <s n=1111>Another one.</s>"
#bnc_string = "<s n=0000>This a Sentence</s> <s n=1111>Another one.</s>"
bnc_paras = re.compile("<p>(.*?)</p>", re.DOTALL).findall(bnc_string)
bnc_paras_str = str.join(' ', bnc_paras)
bnc_sentences = re.compile("<s\s.*?>(.*?)</s>", re.DOTALL).findall(bnc_paras_str)
bnc_boundaries = []
l = 0
i = 0
for s in bnc_sentences:
s = bnc_sentences[i]
s = Entities.Entities.cleanEntities(s)
s = re.compile("<.*?>").sub("", s)
s = s.strip()
if not s.endswith(" "):
# TODO: is this fair?
s = s + " "
bnc_sentences[i] = s
l = l + len(s)
bnc_boundaries.append(l)
i = i + 1
###print bnc_sentences
bnc_sentences_str = str.join('', bnc_sentences)
#print bnc_sentences_str
detected_sentences = self.s.split(bnc_sentences_str)
###print detected_sentences
detected_boundaries = []
l = 0
for s in detected_sentences:
l = l + len(s)
detected_boundaries.append(l)
sent_count = 0
# recall = how many of the sentence boundaries have been detected?
recall_count = 0
for real_boundary in bnc_boundaries:
if real_boundary in detected_boundaries:
recall_count = recall_count + 1
#print "Found: '%s'" % s
else:
pass
(s, s_disp) = self.findSentence(real_boundary, bnc_sentences)
print "Not found: '%s'" % s_disp
sent_count = sent_count + 1
recall = 0
if len(bnc_boundaries) > 0:
recall = float(recall_count) / float(len(bnc_boundaries))
# precision = how many of detected boundaries are real sentence boundaries?
precision_count = 0
for detected_boundary in detected_boundaries:
if detected_boundary in bnc_boundaries:
precision_count = precision_count + 1
precision = 0
if len(detected_boundaries) > 0:
precision = float(precision_count) / float(len(detected_boundaries))
print "Real sentences = %d" % sent_count
print "Recall = %.3f" % recall
print "Precision = %.3f" % precision
return
if __name__ == "__main__":
prg = SentenceSplitterEval()
if len(sys.argv) <= 1:
print "Usage: ./SentenceSplitterEval.py <bnc_sampler_files>"
else:
for filename in sys.argv[1:]:
print filename
f = open(filename)
bnc_string = f.read()
f.close()
prg.run(bnc_string)
|