SHARE
TWEET

changed

furas Aug 5th, 2018 62 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Mon Jul 23 16:52:17 2018
  4.  
  5. @author: Mouaad
  6. """
  7.  
  8. # -*- coding: utf-8 -*-
  9. """
  10. Created on Sat Jul 21 01:50:01 2018
  11.  
  12. @author: Mouaad
  13. """
  14.  
  15. from nltk.corpus import stopwords
  16. from nltk.tokenize import word_tokenize, sent_tokenize
  17. from nltk.stem.snowball import SnowballStemmer
  18. # If you get an error uncomment this line and download the necessary libraries
  19. #nltk.download()*
  20. from os import chdir
  21.  
  22. chdir("C:\\Users\\Mouaad\\Desktop\\python\\projet_pythonSRD\\Corpus")
  23.  
  24. def contenue(nomfichier):
  25.     f=open(nomfichier,'r')
  26.     content=f.read()
  27.     return content
  28.    
  29. def sum(text):
  30.     stemmer = SnowballStemmer("french")
  31.     stopWords = set(stopwords.words("french"))
  32.     words = word_tokenize(text)
  33.  
  34.     freqTable = dict()
  35.     for word in words:
  36.         word = word.lower()
  37.         if word in stopWords:
  38.             continue
  39.  
  40.         word = stemmer.stem(word)
  41.        
  42.         if word in freqTable:
  43.             freqTable[word] += 1
  44.         else:
  45.             freqTable[word] = 1
  46.  
  47.     sentences = sent_tokenize(text)
  48.     sentenceValue = dict()
  49.  
  50.     for sentence in sentences:
  51.         for word, freq in freqTable.items():
  52.             if word in sentence.lower():
  53.                 if sentence in sentenceValue:
  54.                     sentenceValue[sentence] += freq
  55.                 else:
  56.                     sentenceValue[sentence] = freq
  57.  
  58.  
  59.  
  60.     sumValues = 0
  61.     for sentence in sentenceValue:
  62.         sumValues += sentenceValue[sentence]
  63.  
  64.     # Average value of a sentence from original text
  65.     average = int(sumValues / len(sentenceValue))
  66.  
  67.  
  68.     summary = ''
  69.     for sentence in sentences:
  70.         if (sentence in sentenceValue) and (sentenceValue[sentence] > (1.3* average)):
  71.             summary += " " + sentence
  72.  
  73.     print( summary )
  74.    
  75.     return summary # <-----
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top