代码之家  ›  专栏  ›  技术社区  ›  Kevin Choi

在J中创建频率矩阵(计算数组中的项目数)

j
  •  1
  • Kevin Choi  · 技术社区  · 6 年前
    >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”的每一行都是一篇新闻文章,我试图从每篇文章中的cleaned\u text中计算出每个vocab的数量,以便创建如下频率矩阵:

        art1 art2 art3 ...
    mai 4    5    4 
    sai 1    0    0
    ...
    

    我正在寻找e.和e.动词来计算每篇文章中每个单字的数量,但在这种情况下我很难使用它们。

    有人能帮我解决这个问题吗???非常感谢。

    1 回复  |  直到 6 年前
        1
  •  3
  •   bob    6 年前

    我会使用稍微不同的方法。为了保持简单,我将使用以下示例 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│
    └─────┴─────┴─────┴─────┴─────┘
    

    为了计算每件衣服的数量,我需要将每一行与整个词汇表进行比较。我通过阅读掌握了全部词汇( , )p和获取nub( ~. )这确保了p中所有可能的单词都被考虑在内。

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

    现在我将转置( |: )p,以便I可以使用 =/ 最后,将每个项目的总和相加。 +/@:

       +/@:(|: =/ ~.@,)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
    

    根据这些数字,我看到第一排有两条裤子、两件衬衫、一顶帽子、0双鞋子和0双袜子,通过检查,这是正确的。第二排有长裤、衬衫、帽子、鞋子和袜子等等。。。

    希望这有帮助。