#!/usr/bin/python
#-*-coding:utf-8-*-
import math
"""
@author:xyl
This is an example for text summary,
which will be an base algorithm for news recommendation
"""
"""
sentence: the sentence to be summary
keyWords: top key words of the article or sentece
"""
def dbs(sentence, keyWords):
sentence = sentence.lower()
wordArr = sentence.split()#this split the sentence by space,which can be changed
words = []
for word in wordArr:
if word != " ":
words.append(word)
if len(words) == 0:
return 0
count = 1
for word in words:
if keyWords.hasKey(word):#check whether the word is a keyword
count += 1
summ = 0
firstWord = []#first key word
secondWord = []#second key word
for index in xrange(len(words)):
wordIndex = keyWords.getWordIndex(words[index])#get the word's position in senetence or article
if wordIndex > -1:
score = keyWords.getScore(wordIndex) #get the word's weight,which can be computed by tf、tf*idf
if firstWord == []:
firstWord = [(index,score)]
else:
secondWord = firstWord
firstWord = [(index,score)]
summ += (firstWord[0][1]*secondWord[0][1])/math.pow((firstWord[0][0]-secondWord[0][0]),2) #update the sum score
formula = float((1/count*(count+1)*summ))
return formula
def sbs(sentence,keyWords):
sentence = sententce.lower()
wordArr = sentence.split()
words = []
for word in wordArr:
if word != " ":
words.append(word)
if len(words) == 0:
return 0
summ = 0
for index in xrange(len(words)):
wordIndex = keyWords.getWordIndex(words[index])
if wordIndex > -1:
score = keyWords.getScore(wordIndex)
summ += score
formula = float((1/len(words))*summ)