如何轻松计算可读性分数或者如何为此编写函数?

问题描述 投票:0回答:2

我必须计算文本文档的可读性分数。是否有包或内置函数。互联网上的一切似乎都太复杂了。任何人都可以帮我解决这个问题或如何编写我自己的函数吗?

我已经完成了文本的预处理,计算了文档的tfidf,但我想找到文档的可读性分数或雾指数。我尝试使用其他平台上可用的代码,但它不起作用

def text_process(mess):

    nopunc = [char for char in mess if char not in string.punctuation]

    #nopunc = [char for char in mess if char not in string.punctuation]

    nopunc = ''.join(nopunc)

    text = [word for word in tokens if word not in stops]

    text = [wl.lemmatize(word) for word in mess]

    return [word for word in nopunc.split() if word.lower() not in stopwords.words('english')]

from sklearn.feature_extraction.text import TfidfVectorizer

import pandas as pd

vect = TfidfVectorizer()

tfidf_matrix = vect.fit_transform(df["comments"].head(10000))

df1 = pd.DataFrame(tfidf_matrix.toarray(),columns=vect.get_feature_names())

print(df1)      

我不知道如何获得期望的可读性分数结果。如果有人能帮助我,我将不胜感激

python machine-learning nlp text-mining
2个回答
0
投票

您可以使用

automated_readability_index()
中的
textstat
来获得您想要的分数

import textstat

text = 'your text'

score = textstat.automated_readability_index(text)

print (score)

输出:

-1.7

score
越高,您的文字就越好。

或者您也可以尝试使用

flesch_reading_ease()
中的
textstat
,我发现它对我的
nlp
相关任务(评估机器人的对话水平)很有用

有关更多信息,请参阅 textstat 的 文档。


0
投票

您可以使用API。这很简单。

import http.client

conn = http.client.HTTPSConnection("api.apyhub.com")

payload = "{\n    \"text\": \" ADD-TEXT-HERE \"\n}"

headers = {
    'apy-token': "YOUR-SECRET-APY-TOKEN", // Replace it with your secret apy token
    'Content-Type': "application/json"
    }

conn.request("POST", "/extract/text/readability-score", payload, headers)

res = conn.getresponse()
data = res.read()

print(data.decode("utf-8"))

您可以从此处

生成免费的 apy 令牌
© www.soinside.com 2019 - 2024. All rights reserved.