Jump to content

Python Module 'readability' in Visual Studio

Go to solution Solved by WereCatf,
7 minutes ago, Kagami Tsukino said:

i just install from anaconda promp "pip install readability-lxml" it should be the Jul 3, 2020 version

Um, readability and readability-lxml are two different modules. From your code, it looks like you're trying to use readability, not readability-lxml.

I having issue with my module 'readability' has no attribute 'getmeasures'. I can't run terminal as it just open back visual studio. I don't understand want going on

one person say "bad news is you're treatnig it as a method already results = readability.getmeasures(text, lang='en')" and i still have no idea what this mean.

 

 

when type the .txt file it just given an exception thrown:

Spoiler

image.thumb.png.f33c373cf12664eb084bc83113d29a09.png

 

the copy detail is 

Spoiler


  Message=module 'readability' has no attribute 'getmeasures'
  Source=C:\Users\Silvia\source\repos\President_Speech\President_Speech\President_Speech.py
  StackTrace:
  File "C:\Users\Silvia\source\repos\President_Speech\President_Speech\President_Speech.py", line 97, in <module>
    main()

from PIL import Image
from wordcloud import WordCloud, STOPWORDS, ImageColorGenerator
import matplotlib.pyplot as plt
import readability

#Function to get readability score
def readability_score(filename):
    with open(filename) as f:
        text=f.read()
    results = readability.getmeasures(text, lang='en')
    print(results['readability grades']['FleschReadingEase'])

#Function to generate word cloud
def word_cloud(myStr):
    wordcloud = WordCloud(max_font_size=50, max_words=100, background_color="white").generate(myStr)
    plt.figure()
    plt.imshow(wordcloud, interpolation="bilinear")
    plt.axis("off")
    plt.show()

#Function for word stats
def word_stats(filename):
    with open(filename) as f:
        words=f.read().split()
    new_words=[] #List containing all the words
    for word in words:
        word=word.replace(',','')
        nword=word.strip('.')
        nword = word.strip('\"')
        new_words.append(nword)
    myDict={} #Dictionary to store the frequency of each word
    for nword in new_words:
        myDict[nword]=new_words.count(nword)
    
    sorted_dict={k: v for k, v in sorted(myDict.items(), key=lambda item: item[1],reverse=True)}
    mystr='' #String to store words so it can be used to generate a word cloud
    for key in sorted_dict.keys():
        if sorted_dict[key]>2:
          mystr+=key+" "
        print(key,sorted_dict[key])
    #Finding number of sentences
    with open(filename) as f:
        data = f.read()
        total_sentences = data.count('.')
        total_spaces=data.count(' ')
    print("There are total of "+str(len(new_words))+" words and "+str(total_sentences)+" sentences.")
    #Finding average letters per word
    sum=0
    for i in new_words:
        sum+=len(i)
    #Finding average words per sentence
    with open(filename) as f:
        data=f.read().split('.')
    sum1=0
    for sentence in data:
        spaces=sentence.count(' ')
        sum1+=(spaces-1)
    print("There are about " + str(round(sum / len(new_words), 4)) + " letters per word and "+str(round(sum1/total_sentences,4))+" words per sentence.")
    print("There are "+str(total_spaces)+" non empty characters.")

    #Generating word_cloud
    word_cloud(mystr)

#Function to get longest words
def longest_words(filename):
    with open(filename, 'r') as f:
        words = f.read().split()
    new_words = []  # List containing all the longest words
    for word in words:
        word = word.replace(',', '')
        nword = word.strip('.')
        nword=word.strip('\"')
        new_words.append(nword)
    words_sorted=list(sorted(new_words,key=len))
    print('longest words!')
    for i in range(len(words_sorted)-1,len(words_sorted)-11,-1):
        print(words_sorted[i]+": "+str(len(words_sorted[i]))+" letters")

#Main Function
def main():
    #List containing all the filenames entered by user
    files=[]
    # filename = open('obama.txt')
    filename=input(("Enter name of files one at a time. when ready to compile, enter 'done': "))
    if filename!='done':
        readability_score(filename)
    while filename!='done':
        files.append(filename)
        filename = input(("Enter name of files one at a time. when ready to compile, enter 'done': "))
        if filename != 'done':
          readability_score(filename)
    for file in files:
        print(file+" analysis :")
        longest_words(file)
        word_stats(file)

main()

 

Link to comment
https://linustechtips.com/topic/1266687-python-module-readability-in-visual-studio/
Share on other sites

Link to post
Share on other sites

10 minutes ago, Kagami Tsukino said:

I having issue with my module 'readability' has no attribute 'getmeasures'

You are probably using a VERY old version of the readability-module, like all the way from 2014!

Hand, n. A singular instrument worn at the end of the human arm and commonly thrust into somebody’s pocket.

Link to post
Share on other sites

21 minutes ago, WereCatf said:

You are probably using a VERY old version of the readability-module, like all the way from 2014!

i just install from anaconda promp "pip install readability-lxml" it should be the Jul 3, 2020 version

Link to post
Share on other sites

7 minutes ago, Kagami Tsukino said:

i just install from anaconda promp "pip install readability-lxml" it should be the Jul 3, 2020 version

Um, readability and readability-lxml are two different modules. From your code, it looks like you're trying to use readability, not readability-lxml.

Hand, n. A singular instrument worn at the end of the human arm and commonly thrust into somebody’s pocket.

Link to post
Share on other sites

2 minutes ago, WereCatf said:

Um, readability and readability-lxml are two different modules. From your code, it looks like you're trying to use readability, not readability-lxml.

Ahh ok Thanks that fix it had to install

"pip install https://github.com/andreasvc/readability/tarball/master" instead and it work now.
Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×