简体   繁体   English

在ActiveRecord中寻找常用词/短语

[英]Seeking Common Words/Phrases in ActiveRecord

Rails with PG in production but SQLite3 on development. 在生产中使用PG进行Rails开发,在开发中使用SQLite3。 App is a survey with several questions. 应用程式是一项有几个问题的问卷调查。 I have a Model for each question response. 每个问题的回答都有一个模型。 I have a query that pulls the responses for each question individually. 我有一个查询,可分别提取每个问题的答案。

I want to be able to pick out common words and/or phrases from that ActiveRecord result to build something like a "word cloud". 我希望能够从该ActiveRecord结果中挑选出常见的单词和/或短语来构建类似“单词云”的内容。 Is there a gem that would be useful to do this? 有没有宝石可以做到这一点? Otherwise is there a code example/tutorial that runs through this. 否则,将有一个贯穿此的代码示例/教程。 I'm not certain of what to search for to find an answer, but searching for "word cloud" just seems to pop up front end design hacks. 我不确定要寻找答案的搜索内容,但是搜索“词云”似乎会弹出前端设计技巧。 I'm more concerned with getting the data. 我更关心获取数据。

You can offload the word counting logic to PSQL: 您可以将字计数逻辑卸载到PSQL:
This query should return each word in some_column for some_table alongside its number of occurrences. 此查询应返回some_column中的some_table每个单词以及其出现的次数。

SELECT word, count(*) AS word_count
FROM ( 
  SELECT regexp_split_to_table(some_column, '\s') as word
  FROM some_table
) t
GROUP BY word

This can be executed via ActiveRecord like this: 这可以通过ActiveRecord执行,如下所示:

result = ActiveRecord::Base.connection.exec_query(query)

You can combine that with magic_cloud gem( https://github.com/zverok/magic_cloud ) to generate the words cloud as an image, something like this: 您可以将其与magic_cloud gem( https://github.com/zverok/magic_cloud )结合使用以生成单词cloud作为图像,如下所示:

words = result.map {|k, v| [v['word'], v['word_count'] ]} #untested
cloud = MagicCloud::Cloud.new(words, rotate: :free, scale: :log)

Or alternatively, return the words as JSON and process it with whichever JS visualization library you like. 或者,将单词返回为JSON并使用您喜欢的任何JS可视化库进行处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM