String Scala中的单词计数示例?
我看到很多Scala教程中都有一些例子,比如重新遍历或解决数学问题。在我的日常编程生活中,我感觉我大部分的编码时间都花在了诸如字符串操作、数据库查询和日期操作之类的日常任务上。有人想给出以下perl脚本的Scala版本的示例吗String Scala中的单词计数示例?,string,scala,associative-array,String,Scala,Associative Array,我看到很多Scala教程中都有一些例子,比如重新遍历或解决数学问题。在我的日常编程生活中,我感觉我大部分的编码时间都花在了诸如字符串操作、数据库查询和日期操作之类的日常任务上。有人想给出以下perl脚本的Scala版本的示例吗 #!/usr/bin/perl use strict; #opens a file with on each line one word and counts the number of occurrences # of each word, case insensit
#!/usr/bin/perl
use strict;
#opens a file with on each line one word and counts the number of occurrences
# of each word, case insensitive
print "Enter the name of your file, ie myfile.txt:\n";
my $val = <STDIN>;
chomp ($val);
open (HNDL, "$val") || die "wrong filename";
my %count = ();
while ($val = <HNDL>)
{
chomp($val);
$count{lc $val}++;
}
close (HNDL);
print "Number of instances found of:\n";
foreach my $word (sort keys %count) {
print "$word\t: " . $count{$word} . " \n";
}
#/usr/bin/perl
严格使用;
#打开一个文件,每行有一个单词,并统计出现的次数
#每个单词的名称,不区分大小写
打印“输入文件名,即myfile.txt:\n”;
我的$val=;
chomp($val);
打开(HNDL,“$val”)| |死“错误文件名”;
我的%count=();
而($val=)
{
chomp($val);
$count{lc$val}++;
}
关闭(HNDL);
打印“找到的实例数:\n”;
foreach my$word(排序键%count){
打印“$word\t:”.$count{$word}.\n”;
}
总之:
- 请求文件名
- 读取文件(每行包含1个单词)
- 取消线端(cr、lf或crlf)
- 将单词小写
- 字的增量计数
- 按字母顺序打印每个单词及其计数
TIA像这样的简单字数可以写如下:
import io.Source
import java.io.FileNotFoundException
object WC {
def main(args: Array[String]) {
println("Enter the name of your file, ie myfile.txt:")
val fileName = readLine
val words = try {
Source.fromFile(fileName).getLines.toSeq.map(_.toLowerCase.trim)
} catch {
case e: FileNotFoundException =>
sys.error("No file named %s found".format(fileName))
}
val counts = words.groupBy(identity).mapValues(_.size)
println("Number of instances found of:")
for((word, count) <- counts) println("%s\t%d".format(word, count))
}
}
导入io.Source
导入java.io.FileNotFoundException
对象WC{
def main(参数:数组[字符串]){
println(“输入文件名,即myfile.txt:”)
val fileName=readLine
val words=尝试{
Source.fromFile(fileName).getLines.toSeq.map(u.toLowerCase.trim)
}抓住{
案例e:FileNotFoundException=>
sys.error(“未找到名为%s的文件”。格式(文件名))
}
val counts=words.groupBy(identity).mapValues(uz.size)
println(“找到的实例数:”)
对于((单词,计数)这样一个简单的单词计数可以写如下:
import io.Source
import java.io.FileNotFoundException
object WC {
def main(args: Array[String]) {
println("Enter the name of your file, ie myfile.txt:")
val fileName = readLine
val words = try {
Source.fromFile(fileName).getLines.toSeq.map(_.toLowerCase.trim)
} catch {
case e: FileNotFoundException =>
sys.error("No file named %s found".format(fileName))
}
val counts = words.groupBy(identity).mapValues(_.size)
println("Number of instances found of:")
for((word, count) <- counts) println("%s\t%d".format(word, count))
}
}
导入io.Source
导入java.io.FileNotFoundException
对象WC{
def main(参数:数组[字符串]){
println(“输入文件名,即myfile.txt:”)
val fileName=readLine
val words=尝试{
Source.fromFile(fileName).getLines.toSeq.map(u.toLowerCase.trim)
}抓住{
案例e:FileNotFoundException=>
sys.error(“未找到名为%s的文件”。格式(文件名))
}
val counts=words.groupBy(identity).mapValues(uz.size)
println(“找到的实例数:”)
对于((word,count)如果您想要简洁/紧凑,您可以在2.10中:
// Opens a file with one word on each line and counts
// the number of occurrences of each word (case-insensitive)
object WordCount extends App {
println("Enter the name of your file, e.g. myfile.txt: ")
val lines = util.Try{ io.Source.fromFile(readLine).getLines().toSeq } getOrElse
{ sys.error("Wrong filename.") }
println("Number of instances found of:")
lines.map(_.trim.toLowerCase).toSeq.groupBy(identity).toSeq.
map{ case (w,ws) => s"$w\t: ${ws.size}" }.sorted.foreach(println)
}
如果您想要简洁/紧凑,您可以在2.10中:
// Opens a file with one word on each line and counts
// the number of occurrences of each word (case-insensitive)
object WordCount extends App {
println("Enter the name of your file, e.g. myfile.txt: ")
val lines = util.Try{ io.Source.fromFile(readLine).getLines().toSeq } getOrElse
{ sys.error("Wrong filename.") }
println("Number of instances found of:")
lines.map(_.trim.toLowerCase).toSeq.groupBy(identity).toSeq.
map{ case (w,ws) => s"$w\t: ${ws.size}" }.sorted.foreach(println)
}
印刷品:
this,3
is,3
three,1
line,3
2,1
one,1
印刷品:
this,3
is,3
three,1
line,3
2,1
one,1
你试过搜索吗?我在“scala字数”谷歌搜索结果的第一页找到了几个例子。包含文本处理工具。你试过搜索吗?我在“scala字数”的第一页找到了几个例子谷歌搜索结果。包含文本处理工具。谢谢drexin,这正是我想看到的。所以scala也不太详细。它不仅不详细,而且没有加密!谢谢drexin,这正是我想看到的。所以scala也不太详细。它不仅不详细,而且没有加密!哇,rex,这确实很紧凑。比perl原版(虽然它不是为了紧凑而编写的)。也进行了排序。谢谢。哇,rex,这确实是紧凑的。比perl原版(虽然它不是为了紧凑而编写的)。也进行了排序。谢谢。