Rake Tutorial

短文本关键词提取算法RAKE & TextRank及改进 最近做的一个项目是短文本关键词提取(twitter, linkedin post),这里主要用到了两个算法,一个是TextRank, 一个是RAKE,总的来说,这两个算法思路上差别很大,但对于短文本的关键词提取来说,RAKE算法效果更为明显。TextRank 介绍 TextRank 算法是一种用于文本的基于图的排序算法。其基本思想来源于谷歌的 PageRank算法, 通过把文本分割成若干组成单元(单 阅读详情

http://jasonseifer.com/2010/04/06/rake-tutorial


Rake Tutorial

in  Ruby

If you’re developing with Rails you’ve probably encountered rake once or twice. This blog post aims to walk you through where rake came from and an introduction on how to use it effectively in your Rails apps.

A Little Bit of History

Rake is the project of Jim Weirich. It’s a build tool. For a good laugh and an even more in depth history check out the "rational.rdoc" from the Rake documentation. Essentially, rake started as an idea for using Ruby inside of a Makefile. Though Jim doesn’t sound convinced from the tone in that document, it is a good idea.

What’s the need for an automated build system at all? As usual, Wikipedia has the answer:

Historically, developers used build automation to call compilers and linkers from inside a build script versus attempting to make the compiler calls from the command line. It is simple to use the command line to pass a single source module to a compiler and then to a linker to create the final deployable object. However, when attempting to compile and link many source code modules, in a particular order, using the command line process is not a reasonable solution. [sic]
As the build process grew more complex, developers began adding pre and post actions around the calls to the compilers such as a check-out from version control to the copying of deployable objects to a test location. The term “build automation” now includes managing the pre and post compile and link activities as well as the compile and link activities.

It’s about Dependencies

This may be a bit of a stretch to say but build tools are about dependencies. One file or set of files depends on another set to get compiled, linked, or other fun things before the next set can be processed. The same idea exists in rake with tasks and task dependencies. Let’s look at a simple rake task. Save the following as “Rakefile” in any directory:

  directory "tmp"
 
  file "hello.tmp" => "tmp" do
    sh "echo 'Hello' >> 'tmp/hello.tmp'"
  end

What we’re saying here is that the file named “hello.tmp” depends on the directory "tmp". When rake runs across this, it’s going to create the directory "tmp" first before running the "hello.tmp" task. When you run it, you’ll see something like the following:

  [jason@brick:~/src]$ rake hello.tmp
  (in /Users/jason/src)
  echo 'Hello' > 'tmp/hello.tmp'

If you were to look at the "hello.tmp" file you would see the phrase "Hello". What happens if you run it again? You’ll see the same output again. What’s going on? Rake is generating the file again. It’s doing this because it can’t actually find the file tmp/hello.tmp from that definition. Let’s redefine the task:

  directory "tmp"
 
  file "tmp/hello.tmp" => "tmp" do
    sh "echo 'Hello' > 'tmp/hello.tmp'"
  end

Now if you were to run it twice you would see something like this:

  [jason@brick:~/src]$ rake "tmp/hello.tmp"
  (in /Users/jason/src)
  mkdir -p tmp
  echo 'Hello' > 'tmp/hello.tmp'
  [jason@brick:~/src]$ rake "tmp/hello.tmp"
  (in /Users/jason/src)

Rake now knows that the file task has been run.

Running Other Tasks

Rake tasks can take the form of having prerequisites and can depend on another task. Let’s say I wanted to get ready in the morning. My process would be something like this:

  1. Turn off alarm clock.
  2. Groom myself.
  3. Make coffee.
  4. Walk dog.

Let’s further assume that I have OCD and have to do all of these in order. In rake I might express my morning as follows:

  task :turn_off_alarm do
    puts "Turned off alarm. Would have liked 5 more minutes, though."
  end
 
  task :groom_myself do
    puts "Brushed teeth."
    puts "Showered."
    puts "Shaved."
  end
 
  task :make_coffee do
    cups = ENV["COFFEE_CUPS"] || 2
    puts "Made #{cups} cups of coffee. Shakes are gone."
  end
 
  task :walk_dog do
    puts "Dog walked."
  end
 
  task :ready_for_the_day => [:turn_off_alarm, :groom_myself, :make_coffee, :walk_dog] do
    puts "Ready for the day!"
  end

If I were to run this as is I would type rake ready_for_the_day and I’d see the following:

  [jason@brick:~/src]$ rake ready_for_the_day
  (in /Users/jason/src)
  Turned off alarm. Would have liked 5 more minutes, though.
  Brushed teeth.
  Showered.
  Shaved.
  Made 5 cups of coffee. Shakes are gone.
  Dog walked.
  Ready for the day!

By running the ready_for_the_day task it notices that the turn_off_alarm, groom_myself, make_coffee, and walk_dog tasks are all prerequisites of theready_for_the_day task. Then it runs them all in the appropriate order. You’ll notice that we can pass something in to the make_coffee task. If we were having a really tough day we could pass in a value to the COFFEE_CUPS environment variable and be more prepared:

  [jason@brick:~/src]$ rake COFFEE_CUPS=5 make_coffee
  (in /Users/jason/src)
  Made 5 cups of coffee. Shakes are gone.

Namespaces

Rake supports the concept of namespaces which essentially lets you group together similar tasks inside of one namespace. You’d then specify the namespace when you call a task inside it. It keeps things tidy while still being quite effective. In Rails, you might notice thedb:migrate task. In that example, db is the namespace and migrate is the task. Using the above example, we might put everything in to the morning namespace:

  namespace :morning do
    task :turn_of_alarm
    ....
  end

Now if you were to run rake COFFEE_CUPS=3 morning:ready_for_the_day you would have the same output as above, only it only took 3 cups of coffee today. Score!

The Default Task

Rake has the concept of a default task. This is essentially the task that will be run if you type rake without any arguments. If we wanted our default task to be turning off the alarm from the example above, we’d do this:

  task :default => 'morning:turn_off_alarm'

Running rake now produces the following:

  [jason@brick:~/src]$ rake
  (in /Users/jason/src)
  Turned off alarm. Would have liked 5 more minutes, though.

Describing Your Tasks

You can use the desc method to describe your tasks. This is done on the line right above the task definition. It’s also what gives you that nice output when you run rake -T to get a list of tasks. Tasks are displayed in alphabetical order. We’ll define some descriptions in our Rakefile (abbreviated for brevity):

  ...
  desc "Make coffee"
  task :make_coffee do
    cups = ENV["COFFEE_CUPS"] || 2
    puts "Made #{cups} cups of coffee. Shakes are gone."
  end
  ...

Now when we run rake -T for our list of tasks we get the following output:

  [jason@brick:~/src]$ rake -T
  (in /Users/jason/src)
  rake afternoon:make_coffee      # Make afternoon coffee
  rake morning:groom_myself       # Take care of normal hygeine tasks.
  rake morning:make_coffee        # Make coffee
  rake morning:ready_for_the_day  # Get ready for the day
  rake morning:turn_off_alarm     # Turn off alarm.
  rake morning:walk_dog           # Walk the dog

You can add in a string to get tasks matching that displayed. Running rake -T af would show just the afternoon task.

Redefining Tasks

Let’s say you want to add on to an existing task. Perhaps you have another item in your grooming routine like styling your hair. You could write another task and slip it in as a dependency for groom_myself but you could also redefine groom_myself later on (shortened for brevity but you get the idea):

  namespace :morning do
	....
	task :groom_myself do
      puts "Brushed teeth."
      puts "Showered."
      puts "Shaved."
    end
    ....
  end
  ...
  namespace :morning do
    task :groom_myself do
      puts "Styled hair."
    end
  end
  [jason@brick:~/src]$ rake morning:groom_myself
  (in /Users/jason/src)
  Brushed teeth.
  Showered.
  Shaved.
  Styled hair.

Invoking Tasks

You may at some point want to invoke a task from inside another task. Let’s say, for example, you wanted to make coffee in the afternoon, too. If you need an extra upper after lunch you could do that the following way:

  namespace :afternoon do
    task :make_coffee do
      Rake::Task['morning:make_coffee'].invoke
      puts "Ready for the rest of the day!"
    end
  end

Which outputs:

  [jason@brick:~/src]$ rake afternoon:make_coffee COFFEE_CUPS=1
  (in /Users/jason/src)
  Made 1 cups of coffee. Shakes are gone.
  Ready for the rest of the day!

A real world example of this is the rcov:all task. I use this in Genius Pool for aggregate rcov data. It’s shamelessly stolen from Clayton Lengel-Zigich. Go check out that post for a good example of invoking other tasks from rake.

Refactoring

You’ll notice in the example above we’re delegating most of the work to already defined methods and tasks in the RSpec and Cucumber classes. As a general rule, try to keep your methods already defined other places and call them from rake with your specific options and use cases. Let’s say I had a Rails application that e-mailed all accounts in the system that their account was expiring in a certain number of days. Here’s one way to write it:

  namespace :accounts do
    desc "Email expiring accounts to let them know"
    task :email_expiring => :environment do
      date = ENV['from'] ? Date.parse(ENV['from']) : Date.today
      accounts = Account.find(:all, :conditions => ["expiration_date = ?", date]
      accounts.each do |account|
        Notifier.deliver_account_expiration(account)
      end
    end
  end

A better way, that would let you test it more thoroughly would be to do the following:

  namespace :accounts do
    desc "Email expiring accounts to let them know"
    task :email_expiring => :environment do
      date = ENV['from'] ? Date.parse(ENV['from']) : Date.today
      Account.notify_expiring(date)
    end
  end

This lets you unit test your notify_expiring method on the account class and make sure that it’s doing what it’s supposed to do. This is a small, made up example, but you get the idea. Here’s an example from Resque:

  desc 'Restart redis'
  task :restart do
    RedisRunner.stop
    RedisRunner.start
  end

Notice the delegation to the RedisRunner class methods? This is a great rake task

Rails

You can get access to your models, and in fact, your whole environment by making tasks dependent on the environment task. This lets you do things like run rake RAILS_ENV=staging db:migrate. Rails will autmatically pick up tasks in lib/tasks. Any files named with the .rake extension will get picked up when you do rake -T.

Scheduling Rake Tasks

You can use cron to schedule rake tasks. Let’s say you wanted to run the account email expiration task every night at 12:15 on your production server, you might have something like this:

  15 * * * * cd /data/my_app/current && /usr/bin/rake RAILS_ENV=production accounts:email_expiring

Misc

Rake.original_dir gives you the directory that the original rake task was run from.

Derivatives

  • Thor is a more class based approach to solving some of the things rake does as far as actual tasks go.
  • Capistrano is the de facto standard for deploying rails apps. Its syntax is inspired pretty heavily by Rake, but it is definitely not rake.

Further Reading

Useful Rake Examples


rake:用于快速自动关键字提取(RAKE)的Java库 快速自动关键字提取(RAKE) RAKE是一种算法,用于从文档中提取与文档内容具有高度相关性或重要性的关键字(从技术上讲是短语,但我不质疑科学文献)。 例如,文本中的前五个重要关键字: 线性约束系统在自然数集上的兼容性。 考虑了线性丢番图方程组,严格不等式和非严格不等式的相容性准则。 给出了针对所有类型系统的最小解集的组成部分的上限以及构造最小解集的构建算法。 这些标准和用于构造解决方案的最小支持集的相应算法可用于解决所有考虑的系统类型和混合类型的系统。 计算为: 关键词 关联 线性双色子方程 10.666 最小发电机组 10.333 最小支持集 8.833 上限 6.0 自然数 6.0 正在安装 该库尚未在中央存储库中,因此请从发布页面中获取JAR URL,并将其扔到您正在使用的任何依赖项管理器中。 那,或者只是下载JAR。 使用API 使用该库非常简单。 主类仅导出一个公共方法getKeywordsFromText() ,并且需要语言代码才能运行。 在RakeLanguages找到的任何常量都可以RakeLanguages地使用。 因此,例如: public c 立即下载

相关推荐

rake4j-keyword-extractor

rake4j 这是Java中的重写。 快速自动关键字提取(RAKE)算法的实现,如以下所述: 跑步 样本 正常运行 Document doc = new Document (text); RakeAnalyzer rake = new RakeAnalyzer (); rake . loadDocument(doc); rake . runWithoutOffset(); System . out . println(doc . termListToString()); 使用偏移量信息和词干 Document doc = new Document (text); RakeAnalyzer rake = new RakeAnalyzer (); rake .

rake算法学习

关键词提取算法有TF-IDF,主题模型和RAKE算法,RAKE算法在短文本关键字提取方面有不错的效果。RAKE算法学习

baidu_15113429的博客 3304

RAKE-tutorial, 一种快速自动关键词提取的python 实现.zip

RAKE-tutorial, 一种快速自动关键词提取的python 实现 RAKE快速自动关键字提取( 耙) 算法的python 实现,如下所述: ,S,Engel,D,,N,& Cowley,W 。 ( 2010 ) 从单个文档中自动提取关键字。 在M 。W 。& 。 Kogan ( eds

RAKE-tutorial 项目教程

RAKE-tutorial 项目教程 1. 项目介绍 RAKE-tutorial 是一个 Python 实现的 Rapid Automatic Keyword Extraction (RAKE) 算法项目。RAKE 算法是一种自动从文本中提取关键词的方法,广泛应用于自然语言处理(NLP)领域。该项目提供了 RAKE 算法的实现,并附带了一些示例和测试数据,帮助用户快速上手和应用该算法。 2. 项目...

gitblog_00496的博客 720

ArcMap学习笔记(三)地图数字化

地图数字化前言地图数字化实现步骤1.加载地形图2.新建要素图层3.开启编辑环境4.设置捕捉环境5.选择数字化图层6.数字化控制点7.数字化等高线8.数字化图幅边框9.保存数字化成果总结 前言 使用软件:ArcMap10.4.1中文版 如果出现侵权,请在评论留言,我会及时删除,谢谢。 地图数字化 空间数据采集(输入)是将现有的地图、外业观测成果、航空像片、遥感图片数据、文本资料等转换成GIS可以直接...

JayWang博客 1万+

逆向分析美团外卖mtgsig3.0签名算法:从抓包到算法还原实战

在移动应用开发与安全领域,API接口签名是保障数据传输完整性与身份验证的核心机制。其基本原理是通过特定的算法,将请求参数、时间戳等数据与密钥结合,生成唯一的签名串,服务器通过验证签名来识别请求的合法性。这项技术的核心价值在于防止数据篡改、重放攻击和未授权访问,是构建安全客户端-服务器通信的基石。在实际应用中,签名算法广泛应用于电商、社交、金融等各类App的接口保护场景。本文以美团外卖App的mtgsig3.0签名为具体案例,详细拆解了其生成逻辑。通过结合Frida动态调试与Jadx静态分析,文章深入探讨了如

weixin_29325515的博客 1281

RAKE-tutorial 项目使用指南

RAKE-tutorial 项目使用指南 1. 项目目录结构及介绍 RAKE-tutorial 项目的目录结构如下: RAKE-tutorial/ ├── data/ │ ├── docs/ │ │ └── fao_test/ │ └── stoplists/ ├── __init__.py ├── evaluate_rake.py ├── optimize_rake.py ├──...

gitblog_00802的博客 425

PID参数自整定实战解析:从继电反馈到智能调节的完整实现

本文深入解析PID参数自整定的实战应用,从继电反馈原理到智能调节算法的完整实现。通过详细讲解振荡波形特征提取、参数计算的工程化改进及工业场景调试技巧,帮助工程师快速掌握PID自整定技术,提升控制系统性能。特别针对嵌入式系统优化和噪声环境下的稳定策略提供了实用解决方案。

weixin_28731223的博客 466

RAKE-tutorial:快速、自动的关键词抽取利器

RAKE-tutorial:快速、自动的关键词抽取利器 项目简介 是一个基于Python实现的开源项目,旨在提供一种快速且易于使用的关键词提取工具。它采用了 Rapid Automatic Keyword Extraction (RAKE) 算法,该算法由 Rose等人在2010年提出,主要用于从非结构化文本中自动化提取关键信息。 技术解析 RAKE算法的核心思想是通过词频和共现频率的对比来确定关...

gitblog_00026的博客 546

手把手用LTspice仿真运算放大器电路:从虚短虚断到频响分析

本文通过LTspice仿真软件,手把手教你从搭建基础运算放大器电路开始,验证虚短虚断原理,并逐步深入频响分析与稳定性探究。内容涵盖反相/同相放大器、积分器仿真,导入真实运放模型进行蒙特卡洛分析,以及利用交流分析和波特图评估电路性能,帮助工程师和学生将抽象理论转化为直观、可测量的仿真结果,提升电路设计实战能力。

salt9的博客 1112

因子数据预处理-去极值和标准化

多因子数据预处理

cl2010abc的专栏 2432

Ruby on Rails Rake Tutorial

Ruby on Rails Rake Tutorial (aka. How rake turned me into an alcoholic) by gregg on Jun 11, 2007 As a Rails developer you're probably familiar with running "rake" to run your tests or maybe you'...

wxmfly 204

人工智能在高中教育中的应用现状剖析与挑战应对

人工智能在高中教育中的应用现状剖析与挑战应对

燕鹏 1346

NLP keyword extraction tutorial with RAKE and Maui

https://www.airpair.com/nlp/keyword-extraction-tutorial 1 Introduction In this tutorial you will learn how to extract keywords automatically using both Python and Java, and you will also understand it...

weixin_42079989的博客 432

OpenDataLab 大模型训练数据集下载记录

大模型 训练数据集

u013558123的博客 3485

Altium Designer(AD24)导入DDB库文件(Protel 99SE)方法

Altium Designer和Protel均为Altium公司开发的软件。本文介绍Altium Designer 24软件导入DDB库文件(Protel 99SE)方法。

fydar的博客 2012

Ruby on Rails Rake Tutorial转载

写的非常棒!提供一个链接希望对大家有帮助。 [url]http://www.railsenvy.com/2007/6/11/ruby-on-rails-rake-tutorial[/url]

melin1204的博客 141

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

rails fckeditor

fckeditor 现在版本是:0.4.3下载网址:http://rubyforge.org/projects/fckeditorp/下载安装包,到vender/plugin中,用rake   fckeditor:install安装 使用方法请参考:http://zsbfree.is-programmer.com/show/655.html   ...

zhangjian0111的专栏 156

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

visio安装包

Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。

ruby on rails tutorial 笔记(第二章)

本章主要是通过一个简单的程序来说明rails的工作过程,主要有以下一些知识点 1)、bundle exec rake db:migrate 该命令会使用新定义的User数据模型更新数据库,为了使用Gemfile中制定的Rake版本,我们会通过bundle exec来执行rake。 2)、可以通过以下命令来查看所有和数据库有关的任务 bundle exec rake -T d

Ucooling 440

老版QQ原生表情gif动态

这是老版的QQ表情,不过全是动态的的,Windows照片查看器是不会动的,可以右击用浏览器打开查看,再说一遍是老版的,只是分享给宁愿用老版也要动态的人。

Ruby on Rails Tutorial 学习笔记 --第六章 用户模型

-3.新装gem后要重启服务器 -2.开发数据库: [ruby] view plaincopy bundle exec rake db:migrate   测试数据库: [ruby] view plaincopy $ bundle exec rake db:test:pr

920

Ruby on Rails Tutorial 第二章 之 用户资源&MVC&REST

说明:用户资源包括用户数据模型和这个模型相关的Web页面。 1、用户数据模型如下: 2、使用Rails内置的脚手架生成用户资源中,执行如下所示命令: $ rails generate scaffold User name:string email:string #创建模型 $ rails destroy scaffold User #销毁模型 $ bundle exec ...

weixin_30480075的博客 163

Ruby on Rails Tutorial笔记

第2章 $ bundle exec rake db:migrate 这样能确保rake使用Gemfile指定的版本 $ bundle exec rake -T $ bundle exec rake -T db 相当于Help 转载于:https://www.cnblogs.com/scige/archive/2012/09/15/2686818.html...

weixin_30414245的博客 93

db migrate mysql,在MySQL中使用rake db:migrate命令在Rails中创建表的主键问题

My version of rails is 4.0.0, my version of mysql is Ver 14.14 Distrib 5.7.9, for Win64 (x86_64). I am operating of an older version of rails as I was getting some clashes with the mysql as per my pre...

weixin_35147832的博客 2514

Traveling Ruby实战:使用Bundler管理gem依赖的终极方法

Traveling Ruby是一个能在任何Linux发行版和macOS机器上运行的自包含Ruby二进制文件,通过Bundler管理gem依赖可以让Ruby应用的部署和分发变得更加简单高效。本文将详细介绍如何在Traveling Ruby项目中使用Bundler管理gem依赖的完整流程和最佳实践。 ## 为什么选择Bundler管理Traveling Ruby项目依赖 在开发Ruby应用时,ge

gitblog_00938的博客 994
上一篇: Ruby on Rails中的Rake教程
下一篇: 初次接触ruby线程
watkins
博客等级 码龄18年 1277粉丝 246原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值