/
TextSummary.php
172 lines (142 loc) · 6.01 KB
/
TextSummary.php
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<?php
/*
ARTIFICIAL INTELLIGENCE TERM PAPER (1st SEMESTER 2015-16)
SHREYANS MITTAL 2013A7PS032P (IMPLEMENTATION OF TEXTRANK)
HIMANSHU SINGH DHONI
K. SAI TEJA 2013A7PS034P (IMPLEMETATION OF SIMPLIFIED LESK ALGORITHM)
**********************LESK ALGORITHM IMPLEMENTATION BEGINS**********************
(IMPLEMENTED BY SAI TEJA K 2013A7PS034P)
EXTERNAL LIBRARIES USED: wordnet Dictionary
*/
require("Database.php");
class TextSummary
{
private $db;
private $text;
private $sentences=array();
private $text_array;
private $frequencies=array();
private $sentence_count=array();
private $stopwords=array("a","able","about","across","after","all","almost","also","am","among","an","and","any","are","as","at","be",
"because","been","but","by","can","cannot","could","dear","did","do",
"does","either","else","ever","every","for","from","get","got","had",
"has","have","he","her","hers","him","his","how","however","i","if",
"in","into","is","it","its","just","least","let","like","likely","may",
"me","might","most","must","my","neither","no","nor","not","of","off","often",
"on","only","or","other","our","own","rather","said","say","says","she","should",
"since","so","some","than","that","the","their","them","then","there","these",
"they","this","tis","to","too","twas","us","wants","was","we","were","what","when",
"where","which","while","who","whom","why","will","with","would","yet","you",
"your","ain't","aren't","can't","could've","couldn't","didn't","doesn't",
"don't","hasn't","he'd","he'll","he's","how'd","how'll","how's","i'd","i'll",
"i'm","i've","isn't","it's","might've","mightn't","must've","mustn't","shan't",
"she'd","she'll","she's","should've","shouldn't","that'll","that's","there's",
"they'd","they'll","they're","they've","wasn't","we'd","we'll","we're","weren't",
"what'd","what's","when'd","when'll","when's","where'd","where'll","where's","who'd",
"who'll","who's","why'd","why'll","why's","won't","would've","wouldn't","you'd","you'll","you're","you've");
public function __construct($text)
{
$this->text=$text;
/* split $content into array of substrings of $content i.e wordwise */
$wordArray = preg_split('/[^a-z]/', $text, -1, PREG_SPLIT_NO_EMPTY);
/* "stop words", filter them */
$filteredArray = array_filter($wordArray, function($x){
return !preg_match("/^(a|able|about|across|after|all|almost|also|am|among|an|and|any|are|
as|at|be|because|been|but|by|can|cannot|could|dear|did|do|does|either|else|ever|every|for
|from|get|got|had|has|have|he|her|hers|him|his|how|however|i|if|in|into|is|it|its|just|least|
let|like|likely|may|me|might|most|must|my|neither|no|nor|not|of|off|often|on|only|or|other|our
|own|rather|said|say|says|she|should|since|so|some|than|that|the|their|them|then|there|these|they
|this|tis|to|too|twas|us|wants|was|we|were|what|when|where|which|while|who|whom|why|will|with|would
|yet|you|your|ain't|aren't|can't|could've|couldn't|didn't|doesn't|don't|hasn't|he'd|he'll|he's|how'd
|how'll|how's|i'd|i'll|i'm|i've|isn't|it's|might've|mightn't|must've|mustn't|shan't|she'd|she'll|she's
|should've|shouldn't|that'll|that's|there's|they'd|they'll|they're|they've|wasn't|we'd|we'll|we're|weren't
|what'd|what's|when'd|when'll|when's|where'd|where'll|where's|who'd|who'll|who's|why'd|why'll|why's|won't
|would've|wouldn't|you'd|you'll|you're|you've)$/",$x);
});
/* get associative array of values from $filteredArray as keys and their frequency count as value */
$this->text_array = array_count_values($filteredArray);
$this->sentences=explode(".",$text);
}
public function frequency($words)
{$frequency=0;
foreach($words as $element2)
{
if(!in_array(($element2), $this->stopwords))
$frequency+=$this->text_array[$element2];
}
return $frequency;
}
public function mainP()
{
$sen=0;
$db=new Database;
foreach($this->sentences as $sentence)
{
$highest=0;
$words=array();
$words=explode(" ",$sentence);
foreach($words as $word)
{
if(!in_array(strtolower($word), $this->stopwords))
{
//Fetching the meaning definition of the senses From the WordNet
$db->query("SELECT * from words where lemma like :word");
$db->bind("word",$word);
$wordid=$db->single()->wordid;
$db->query("SELECT * from senses where wordid like :wordid");
$db->bind("wordid",$wordid);
$resulta2 = $db->resultset();
foreach($resulta2 as $row)
{
$synsetid=$row->synsetid;
$sample2[]=$synsetid;
$title = $row->wordid . " " . $row->senseid;
}
$db->query("select * from synsets where synsetid like :sample");
$db->bind("sample",$sample2[0]);
$resulta3 = $db->resultset();
unset($sample2[0]);
for($i=0;$i<(count($sample2)+1);$i++)
{
${result_A.$i}=$sample2[$i];
${lexdomains_var.$i}=$lexdomains[$i];
$db->query("select * from synsets where synsetid like :sample");
$db->bind("sample",${result_A.$i});
${result.$i} =$db->resultset();
foreach(${result.$i} as $row)
{
${pos.$i}=$row->pos;
if(${pos.$i}=="s"){
${pos.$i}="satellite adj";}
$def = $row->definition;
$def=preg_replace('/[^a-zA-Z0-9_ -]/s', '', $def);
$definition=array();
$definition=explode(" ",$def );
$frequency=$this->frequency($definition);
if($frequency>$highest)
$highest=$frequency;
}
}$sample2=array();
}
}
$this->frequencies[]=$highest;
$this->sentence_count[]=$sen;
$sen++;
}
}
public function getSummary($fraction)
{
array_multisort($this->frequencies,SORT_DESC, $this->sentence_count);
$tot=count($this->sentence_count);
$new= $fraction*$tot;
$top_sentences=array_slice($this->sentence_count, 0,$new);
sort($top_sentences);
echo "<underline>Original Text</underline><br>";
echo $this->text;
echo '<br><br> Summary:-<br><br><pre>';
foreach($top_sentences as $sentence)
echo $this->sentences[$sentence].'.';
echo '</pre>';
}
}
?>