标签: textacy

在 Textacy 中计算单个单词的 TD-IDF

我正在尝试使用Textacy计算标准语料库中单个单词的 TF-IDF 分数,但我对收到的结果有点不清楚。

我期待一个代表词在语料库中的频率的浮点数。那么为什么我会收到 7 个结果的列表 (?)?

“acculer”实际上是一个法语单词,因此期望从英语语料库中得到 0 的结果。

word = 'acculer'
vectorizer = textacy.Vectorizer(tf_type='linear', apply_idf=True, idf_type='smooth')
tf_idf = vectorizer.fit_transform(word)
logger.info("tf_idf:")
logger.info(tfidf)
Run Code Online (Sandbox Code Playgroud)

输出

tf_idf:
(0, 0)  2.386294361119891
(1, 1)  1.9808292530117262
(2, 1)  1.9808292530117262
(3, 5)  2.386294361119891
(4, 3)  2.386294361119891
(5, 2)  2.386294361119891
(6, 4)  2.386294361119891
Run Code Online (Sandbox Code Playgroud)

问题的第二部分是如何将我自己的语料库提供给 Textacy 中的 TF-IDF 函数,尤其是。一种不同的语言?

编辑

正如@Vishal 所提到的,我已经使用这一行记录了输出:

logger.info(vectorizer.vocabulary_terms)
Run Code Online (Sandbox Code Playgroud)

似乎提供的单词acculer已被拆分为字符。

{'a': 0, 'c': 1, 'u': 5, 'l': 3, 'e': 2, 'r': 4}
Run Code Online (Sandbox Code Playgroud)

(1)如何针对语料库获取这个词的TF-IDF,而不是每个字符?

(2) 如何提供自己的语料库并将其作为参数指向?

(3)TF-IDF可以在句子层面使用吗?即:这句话的术语相对于语料库的相对频率是多少。

python nlp machine-learning spacy textacy

5
推荐指数
1
解决办法
1070
查看次数

我的问题是关于“模块‘textacy’没有属性‘Doc’”

找不到模块 'textacy' 没有属性 'Doc' 我试图从 spacy 中提取动词短语,但没有这样的库。请帮助我如何使用spacy提取动词短语或形容词短语。我想做完整的浅解析。

def extract_named_nouns(row_series):
    """Combine nouns and non-numerical entities. 

    Keyword arguments:
    row_series -- a Pandas Series object

    """
    ents = set()
    idxs = set()
    # remove duplicates and merge two lists together
    for noun_tuple in row_series['nouns']:
        for named_ents_tuple in row_series['named_ents']:
            if noun_tuple[1] == named_ents_tuple[1]: 
                idxs.add(noun_tuple[1])
                ents.add(named_ents_tuple)
        if noun_tuple[1] not in idxs:
            ents.add(noun_tuple)

    return sorted(list(ents), key=lambda x: x[1])

def add_named_nouns(df):
    """Create new column in data frame with nouns and named ents.

    Keyword arguments:
    df -- a …
Run Code Online (Sandbox Code Playgroud)

spacy textacy

3
推荐指数
1
解决办法
2191
查看次数

更有效地实现 Textacy / spacy 'subject_verb_object_triples'

我正在尝试从数据集上的 textacy 实现“extract.subject_verb_object_triples”函数。然而,我编写的代码非常慢并且占用大量内存。有没有更高效的实现方式?

import spacy
import textacy

def extract_SVO(text):

    nlp = spacy.load('en_core_web_sm')
    doc = nlp(text)
    tuples = textacy.extract.subject_verb_object_triples(doc)
    tuples_to_list = list(tuples)
    if tuples_to_list != []:
        tuples_list.append(tuples_to_list)

tuples_list = []          
sp500news['title'].apply(extract_SVO)
print(tuples_list)
Run Code Online (Sandbox Code Playgroud)

样本数据 (sp500news)

    date_publish  \
0       2013-05-14 17:17:05   
1       2014-05-09 20:15:57   
4       2018-07-19 10:29:54   
6       2012-04-17 21:02:54   
8       2012-12-12 20:17:56   
9       2018-11-08 10:51:49   
11      2013-08-25 07:13:31   
12      2015-01-09 00:54:17   

 title  
0       Italy will not dismantle Montis labour reform  minister                            
1       Exclusive US agency FinCEN rejected veterans in bid to hire …
Run Code Online (Sandbox Code Playgroud)

python nlp pandas spacy textacy

3
推荐指数
1
解决办法
2345
查看次数

Jupyter Notebook的Textacy:如何抑制多个错误警告?

我正在使用Textacy(在Spacy之上)来处理许多文本片段。

具体来说,我使用Textacy的可读性评分。因为我有很多短文本,所以我得到一条警告,我需要抑制它,否则它会损坏我的笔记本电脑。

我的代码:

def readability(x):
    d = nlp(x);
    t = textacy.TextStats(d);
    return t.readability_stats;

df["readability"] = df.message.apply(readability)
Run Code Online (Sandbox Code Playgroud)

对于我的数据框的每个条目,我都会收到此警告(这是可以预期的):

2017-09-23 19:44:23,283:警告:对于n <sents <30的SMOG分数可能不可靠

我该如何抑制呢?我在文档或网络上都找不到任何提示。

在此先感谢您对正确方向的任何提示。

python nlp spacy jupyter-notebook textacy

1
推荐指数
1
解决办法
627
查看次数