【问题标题】:Where did I go wrong with retrieving POS proportions through analysis with spaCy?通过 spaCy 分析检索 POS 比例时我哪里出错了?
【发布时间】:2020-10-05 11:36:57
【问题描述】:

所以,最近,我非常想获得一些关于我喜欢的书籍词汇量的统计数据。没有什么先进的——就像,一个句子的平均长度是多少?用了多少形容词,占总词数的比例?

问题是 ---spaCy,这是我选择的纯粹使用简单的工具,它似乎表明:我分析的每一个文本似乎都有 5.88235% 的形容词与总单词数相比。正好 2.94118% 的助剂。正好 14.7059% 的名词。每个句子正好 34 个单词 (!)。

现在,我很确定我在使用的代码中犯了一些错误——只是,我看不到它!

这是一个示例代码。我按章节(共 34 章)对本书进行了划分,并打印了一些统计数据表。这些数字看起来是合法的——但是这些比例,我分析的每篇文章中的每一章都不可能有完全相同的结构。

headers = ['chapter', 'num_sents', 'num_words', 'num_verbs', 'prop_verbs', 'num_adjs', \
           'prop_adjs', 'num_adv', 'prop_adv', 'num_aux', 'prop_aux', 'num_intj', \
           'prop_intj', 'num_non', 'prop_non', 'num_pron', 'prop_pron', 'num_propn', \
           'prop_propn', 'words/sentences']
stats = list()
count_chpt = 1
for chapter in kf_spc:
  num_sents = 0
  num_words = 0
  num_verbs = 0
  num_adjs = 0
  num_adv = 0
  num_aux = 0
  num_intj = 0
  num_non = 0
  num_pron = 0
  num_propn = 0
  for sentence in chapter.sents:
    num_sents += 1
    for token in sent:
      num_words += 1
      if token.pos_ == 'VERB':
        num_verbs += 1
      elif token.pos_ == 'ADJ':
        num_adjs += 1
      elif token.pos_ == 'ADV':
        num_adv += 1
      elif token.pos_ == 'AUX':
        num_aux += 1
      elif token.pos_ == 'INTJ':
        num_intj += 1
      elif token.pos_ == 'NOUN':
        num_non += 1
      elif token.pos_ == 'PRON':
        num_pron += 1
      elif token.pos_ == 'PROPN':
        num_propn += 1
  stats.append([count_chpt, num_sents, num_words, num_verbs, num_verbs/num_words, \
                num_adjs, num_adjs/num_words, num_adv, num_adv/num_words, \
                num_aux, num_aux/num_words, num_intj, num_intj/num_words, num_non, \
                num_non/num_words, num_pron, num_pron/num_words, num_propn, \
                num_propn/num_words, num_words/num_sents])
  count_chpt += 1

print(tabulate(stats, headers=headers))

输出:

  chapter    num_sents    num_words    num_verbs    prop_verbs    num_adjs    prop_adjs    num_adv    prop_adv    num_aux    prop_aux    num_intj    prop_intj    num_non    prop_non    num_pron    prop_pron    num_propn    prop_propn    words/sentences
---------  -----------  -----------  -----------  ------------  ----------  -----------  ---------  ----------  ---------  ----------  ----------  -----------  ---------  ----------  ----------  -----------  -----------  ------------  -----------------
        1          251         8534         1255      0.147059         251    0.0294118        502   0.0588235        251   0.0294118           0            0       1255    0.147059         502    0.0588235          753     0.0882353                 34
        2          155         5270          775      0.147059         155    0.0294118        310   0.0588235        155   0.0294118           0            0        775    0.147059         310    0.0588235          465     0.0882353                 34
        3          235         7990         1175      0.147059         235    0.0294118        470   0.0588235        235   0.0294118           0            0       1175    0.147059         470    0.0588235          705     0.0882353                 34
        4          226         7684         1130      0.147059         226    0.0294118        452   0.0588235        226   0.0294118           0            0       1130    0.147059         452    0.0588235          678     0.0882353                 34
        5           80         2720          400      0.147059          80    0.0294118        160   0.0588235         80   0.0294118           0            0        400    0.147059         160    0.0588235          240     0.0882353                 34
        6          276         9384         1380      0.147059         276    0.0294118        552   0.0588235        276   0.0294118           0            0       1380    0.147059         552    0.0588235          828     0.0882353                 34
        7          276         9384         1380      0.147059         276    0.0294118        552   0.0588235        276   0.0294118           0            0       1380    0.147059         552    0.0588235          828     0.0882353                 34
        8          412        14008         2060      0.147059         412    0.0294118        824   0.0588235        412   0.0294118           0            0       2060    0.147059         824    0.0588235         1236     0.0882353                 34
        9          123         4182          615      0.147059         123    0.0294118        246   0.0588235        123   0.0294118           0            0        615    0.147059         246    0.0588235          369     0.0882353                 34
       10          475        16150         2375      0.147059         475    0.0294118        950   0.0588235        475   0.0294118           0            0       2375    0.147059         950    0.0588235         1425     0.0882353                 34
       11           80         2720          400      0.147059          80    0.0294118        160   0.0588235         80   0.0294118           0            0        400    0.147059         160    0.0588235          240     0.0882353                 34
       12          169         5746          845      0.147059         169    0.0294118        338   0.0588235        169   0.0294118           0            0        845    0.147059         338    0.0588235          507     0.0882353                 34
       13          358        12172         1790      0.147059         358    0.0294118        716   0.0588235        358   0.0294118           0            0       1790    0.147059         716    0.0588235         1074     0.0882353                 34
       14          415        14110         2075      0.147059         415    0.0294118        830   0.0588235        415   0.0294118           0            0       2075    0.147059         830    0.0588235         1245     0.0882353                 34
       15          146         4964          730      0.147059         146    0.0294118        292   0.0588235        146   0.0294118           0            0        730    0.147059         292    0.0588235          438     0.0882353                 34
       16          275         9350         1375      0.147059         275    0.0294118        550   0.0588235        275   0.0294118           0            0       1375    0.147059         550    0.0588235          825     0.0882353                 34
       17          332        11288         1660      0.147059         332    0.0294118        664   0.0588235        332   0.0294118           0            0       1660    0.147059         664    0.0588235          996     0.0882353                 34
       18          172         5848          860      0.147059         172    0.0294118        344   0.0588235        172   0.0294118           0            0        860    0.147059         344    0.0588235          516     0.0882353                 34
       19          268         9112         1340      0.147059         268    0.0294118        536   0.0588235        268   0.0294118           0            0       1340    0.147059         536    0.0588235          804     0.0882353                 34
       20          740        25160         3700      0.147059         740    0.0294118       1480   0.0588235        740   0.0294118           0            0       3700    0.147059        1480    0.0588235         2220     0.0882353                 34
       21          301        10234         1505      0.147059         301    0.0294118        602   0.0588235        301   0.0294118           0            0       1505    0.147059         602    0.0588235          903     0.0882353                 34
       22          172         5848          860      0.147059         172    0.0294118        344   0.0588235        172   0.0294118           0            0        860    0.147059         344    0.0588235          516     0.0882353                 34
       23          223         7582         1115      0.147059         223    0.0294118        446   0.0588235        223   0.0294118           0            0       1115    0.147059         446    0.0588235          669     0.0882353                 34
       24          791        26894         3955      0.147059         791    0.0294118       1582   0.0588235        791   0.0294118           0            0       3955    0.147059        1582    0.0588235         2373     0.0882353                 34
       25          181         6154          905      0.147059         181    0.0294118        362   0.0588235        181   0.0294118           0            0        905    0.147059         362    0.0588235          543     0.0882353                 34
       26          357        12138         1785      0.147059         357    0.0294118        714   0.0588235        357   0.0294118           0            0       1785    0.147059         714    0.0588235         1071     0.0882353                 34
       27          253         8602         1265      0.147059         253    0.0294118        506   0.0588235        253   0.0294118           0            0       1265    0.147059         506    0.0588235          759     0.0882353                 34
       28           82         2788          410      0.147059          82    0.0294118        164   0.0588235         82   0.0294118           0            0        410    0.147059         164    0.0588235          246     0.0882353                 34
       29          213         7242         1065      0.147059         213    0.0294118        426   0.0588235        213   0.0294118           0            0       1065    0.147059         426    0.0588235          639     0.0882353                 34
       30          416        14144         2080      0.147059         416    0.0294118        832   0.0588235        416   0.0294118           0            0       2080    0.147059         832    0.0588235         1248     0.0882353                 34
       31          280         9520         1400      0.147059         280    0.0294118        560   0.0588235        280   0.0294118           0            0       1400    0.147059         560    0.0588235          840     0.0882353                 34
       32          306        10404         1530      0.147059         306    0.0294118        612   0.0588235        306   0.0294118           0            0       1530    0.147059         612    0.0588235          918     0.0882353                 34
       33          322        10948         1610      0.147059         322    0.0294118        644   0.0588235        322   0.0294118           0            0       1610    0.147059         644    0.0588235          966     0.0882353                 34
       34          312        10608         1560      0.147059         312    0.0294118        624   0.0588235        312   0.0294118           0            0       1560    0.147059         624    0.0588235          936     0.0882353                 34

我正在使用大型英语语料库。我看不出哪里出错了……

【问题讨论】:

    标签: python nlp spacy


    【解决方案1】:

    您正在反复迭代同一个句子。注意sentencesent

    for sentence in chapter.sents:
        num_sents += 1
        for token in sent:
    

    作为一个额外的提示,而不是广泛的 if/else,使用来自这样集合的 Counter 对象:

    poscount = Counter()
    for word in sent:
        poscount[word.pos_] += 1
    

    【讨论】:

    • 谢谢。写完这篇文章后不久,我在一个简短的文档中运行了循环,打印了每个单独的句子——真是个愚蠢的错误 。只是证明我应该在开始阅读完整书籍之前更多地在较小的文档上测试代码。另外,非常感谢您提供有关使用 Counter 的提示——您是救生员!!!
    • 谢谢,很高兴能帮上忙。如果你觉得我的回答有帮助,你应该点击它左边的向上箭头,然后勾选接受它。
    • 我确实尝试过这样做 --- 但事实证明我的声誉还不够高,无法投票给帖子(您需要至少 15 的声誉才能投票)。长期潜伏在SO上,但我还没有真正在这里写任何东西。
    • 啊,好的。不过,您应该仍然可以接受复选标记。
    猜你喜欢
    • 1970-01-01
    • 2019-09-02
    • 1970-01-01
    • 1970-01-01
    • 2016-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多