-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsentence_scoring.py
70 lines (52 loc) · 2.04 KB
/
sentence_scoring.py
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
#!/usr/bin/python
import sys
import operator
import math
import nltk
from nltk.tokenize import sent_tokenize, word_tokenize
from util.preprocessor import Preprocessor
from util.word_counter import WordCounter
class SentenceScoreCalculator:
def __init__(self, p, HEADING):
self.sCount = p.sCount
self.pCount = p.pCount
self.hWords = word_tokenize(HEADING)
self.counter = WordCounter(p.sCount).count(p.processed)
self.vocabulary = self.counter.wordDict.keys()
self.paragraphStructure = p.paragraphStructure
self.sentenceWords = p.processed
def score(self, index):
words = self.sentenceWords[index]
return self.surfaceScore(index, words) + self.headingScore(index, words)
def surfaceScore(self, index, words):
return (
self.positionScore(index, words) +
self.lengthScore(index, words) +
self.paragraphScore(index, words)
)
def paragraphScore(self, index, words):
return 1 - ( self.paragraphStructure[index] / self.pCount )
def headingScore(self, index, words):
if len(self.hWords) == 0:
return 0
wordsInHeading = len(filter(lambda w: w in self.hWords, words))
totalWords = ( math.log( len(words) ) + math.log( len(self.hWords) ) )
return float(wordsInHeading) / totalWords if totalWords > 0 else 0
def positionScore(self, index, words):
return 1 - ( index / self.sCount )
def lengthScore(self, index, words):
return len(words) / len(self.vocabulary)
def generateSummary(PATH, SIZE, HEADING):
p = Preprocessor(PATH, 1).parse()
scorer = SentenceScoreCalculator(p, HEADING)
summary = sorted(
sorted(
range(p.sCount), key=lambda s: scorer.score(s), reverse=True)[0:SIZE])
return map(lambda s: p.sentences[s], summary)
if __name__ == "__main__":
PATH = sys.argv[1]
SIZE = int(sys.argv[2]) if len(sys.argv) > 2 else 3
HEADING = sys.argv[3] if len(sys.argv) > 3 else ""
# Print summary
for s in generateSummary(PATH, SIZE, HEADING):
print s