在J

时间:2018-04-21 19:12:43

标签: j

>text
┌───────────┬──────────┬───────────┬──────────┬──────────┬─────────┬──────────┬─────────────┬─────────────┬──────────┬───────────────┬──────────┬──────────┬────────────┬─────────────────┬──────────┬──────────┬──────────────┬─────────────┬─────────────┬────...
│speak      │conceal   │terribl    │option    │write     │book     │come      │tuesdai      │matter       │act       │conceal        │catastroph│integr    │depart      │justic           │put       │wai       │choic         │realli       │bad          │opti...
├───────────┼──────────┼───────────┼──────────┼──────────┼─────────┼──────────┼─────────────┼─────────────┼──────────┼───────────────┼──────────┼──────────┼────────────┼─────────────────┼──────────┼──────────┼──────────────┼─────────────┼─────────────┼────...
│trump      │logu      │talk       │entir     │time      │talk     │entir     │time         │discov       │someth    │frequent       │doe       │logu      │thi         │direct           │logu      │direct    │logu          │differ       │direct       │cons...
├───────────┼──────────┼───────────┼──────────┼──────────┼─────────┼──────────┼─────────────┼─────────────┼──────────┼───────────────┼──────────┼──────────┼────────────┼─────────────────┼──────────┼──────────┼──────────────┼─────────────┼─────────────┼────...
│cohen      │lawyer    │object     │taint     │team      │anoth    │unusu     │move         │lawyer       │trump     │file           │emerg     │motion    │court       │sundai           │night     │sai       │presid        │object       │extraordinari│meas...
├───────────┼──────────┼───────────┼──────────┼──────────┼─────────┼──────────┼─────────────┼─────────────┼──────────┼───────────────┼──────────┼──────────┼────────────┼─────────────────┼──────────┼──────────┼──────────────┼─────────────┼─────────────┼────...
│photo      │presid    │trump      │fire      │jame      │comei    │director  │mai          │did          │mean      │end            │comei     │time      │public      │memoir           │higher    │loyalti   │releas        │comei        │featur       │wide...
├───────────┼──────────┼───────────┼──────────┼──────────┼─────────┼──────────┼─────────────┼─────────────┼──────────┼───────────────┼──────────┼──────────┼────────────┼─────────────────┼──────────┼──────────┼──────────────┼─────────────┼─────────────┼────...
│british    │deleg     │organ      │wrote     │twitter   │russia   │syria     │allow        │access       │douma     │unfett         │access    │essenti   │russia      │syria            │cooper    │western   │diplomat      │confirm      │syria        │russ...
├───────────┼──────────┼───────────┼──────────┼──────────┼─────────┼

cleaned_text

    ┌─────┬───────┬───────┬──────┬─────┬────┬────┬───────┬──────┬───┬───────┬──────────┬──────┬──────┬──────┬───┬───┬─────┬──────┬───┬──────┬──────────┬──────┬────┬────┬────┬────────┬─────┬─────┬───────┬───────┬───────┬───────┬───┬─────┬───────┬────┬───────┬──...
    │speak│conceal│terribl│option│write│book│come│tuesdai│matter│act│conceal│catastroph│integr│depart│justic│put│wai│choic│realli│bad│option│catastroph│option│hard│call│tell│congress│thing│chang│clinton│fervent│support│disagre│sai│least│philipp│rein│longtim│tr...
    └─────┴───────┴───────┴──────┴─────┴────┴────┴───────┴──────┴───┴───────┴──────────┴──────┴──────┴──────┴───┴───┴─────┴──────┴───┴──────┴──────────┴──────┴────┴────┴────┴────────┴─────┴─────┴───────┴───────┴───────┴───────┴───┴─────┴───────┴────┴───────┴──...

“text”的每一行都是一篇新闻文章,我试图从每篇文章中的cleaning_text中找出每个词汇的数量,这样我就可以创建一个像这样的频率矩阵:

    art1 art2 art3 ...
mai 4    5    4 
sai 1    0    0
...
我正在寻找电子邮件。和E.动词计算每篇文章中每个词汇的数量,但在这种情况下我很难使用它们。

任何人都可以帮我解决这个问题吗?谢谢!

1 个答案:

答案 0 :(得分:3)

我会使用稍微不同的方法。为了简单起见,我将使用p

的示例
   p
┌─────┬─────┬─────┬─────┬─────┐
│pants│shirt│shirt│hat  │pants│
├─────┼─────┼─────┼─────┼─────┤
│shoes│shoes│socks│pants│shirt│
├─────┼─────┼─────┼─────┼─────┤
│shirt│hat  │pants│shoes│shoes│
├─────┼─────┼─────┼─────┼─────┤
│socks│pants│shirt│shirt│hat  │
├─────┼─────┼─────┼─────┼─────┤
│pants│shoes│shoes│socks│pants│
├─────┼─────┼─────┼─────┼─────┤
│shirt│shirt│hat  │pants│shoes│
└─────┴─────┴─────┴─────┴─────┘

要计算每件衣服的数量,我需要将每一行与整个词汇进行比较。我通过ravelling(,)p得到整个词汇并获得结点(~.)这确保了p中每个可能的单词都被考虑在内。

   ~.@:,p
┌─────┬─────┬───┬─────┬─────┐
│pants│shirt│hat│shoes│socks│
└─────┴─────┴───┴─────┴─────┘

现在我将转置(|:)p,以便我可以使用=/将每一行与结点进行比较,最后总计每个项目的总和。 +/@:

   +/@:(|: =/ ~.@,)p
2 2 1 0 0
1 1 0 2 1
1 1 1 2 0
1 2 1 0 1
2 0 0 2 1
1 2 1 1 0

阅读这些数字对着小块我看到第一排有2件裤子2件式的1帽0鞋和0袜子,通过检查这是正确的。第二排有1-pant 1-shirt 0-hats 2-shoes and 1-sock等...

希望这有帮助。

相关问题