技术标签: output input dictionary encoding windows each
一个1-gram实现,网上有个类似的python的,由于要交作业,写了个perl的,娱乐而已。
备份一下。无版权,需要自取。
#!/usr/bin/perl -w
# Attention please!
# This program should only be executed in UNIX-like platform.
# If run in windows, some unexpected problem will appear.
# In most cases, windows text editor will add in some special control byte in your text file.
# They are sheltered by windows and you almost cannot find them.
# What is worse, this is a chinese segmentation program, an unwanted byte will cause disaster!
#===============================================================================
# Introduction:
# The algorithm used is 1-gram.
# Simply, isn't it? But I don't think so...
#
# Basicly, three extra files is needed.
# 1. The one used for create a dictinary. Default: 199801q.txt
# 2. The input file. Default: input.txt
# 3. The output file. Default: output.txt
# * Program will create a temp file called "config" to speed up itself.
#===============================================================================
# Author. yinxs
# Mail. [email protected]
# Welcome to exchage ideas.
#===============================================================================
use Encode;
use utf8;
binmode( STDIN, ':encoding(utf8)' );
binmode( STDOUT, ':encoding(utf8)' );
binmode( STDERR, ':encoding(utf8)' );
# some global variants.
# If using OOP, we could discard them.
my %trainedDict;
my %dictMap;
my %wordMap;
sub biSplit {
my @words;
my $str = $_[0];
my $len = length($str);
foreach ( 1 .. $len-1 ) {
push( @words, substr( $str, 0, $_ ) );
push( @words, substr( $str, $_, $len - $_ ) );
}
@words;
}
sub maxPrb {
my $sentence = $_[0];
my $len = length($sentence);
if ( $len <= 1 ) {
return &getPrb($sentence);
}
my @words = &biSplit($sentence);
my $maxThisPrb = 0;
my $maxThisPair;
my $i = 0;
for ( $i = 0 ; $i < scalar @words - 1 ; $i += 2 ) {
my $thisPrb = 0;
if ( length( $words[$i] ) > 0 and length( $words[ $i + 1 ] ) > 0 ) {
my $p1 = 0;
my $p2 = 0;
if ( exists( $dictMap{ $words[$i] } ) ) {
$p1 = $dictMap{ $words[$i] };
}
else {
$p1 = maxPrb( $words[$i] );
}
if ( exists( $dictMap{ $words[ $i + 1 ] } ) ) {
$p2 = $dictMap{ $words[ $i + 1 ] };
}
else {
$p2 = maxPrb( $words[ $i + 1 ] );
}
$thisPrb = $p1 * $p2;
}
if ( $thisPrb > $maxThisPrb ) {
$maxThisPrb = $thisPrb;
$maxThisPair = $words[$i]."\t".$words[$i+1];
}
}
$joinPrb = &getPrb($sentence);
if ( ( $joinPrb > $maxThisPrb ) and &isBelong($sentence) ) {
$dictMap{$sentence} = $joinPrb;
$wordMap{$sentence} = $sentence;
return $joinPrb;
}
else {
$dictMap{$sentence} = $maxThisPrb;
$wordMap{$sentence} = $maxThisPair;
return $maxThisPrb;
}
}
sub display {
my $output = "output.txt";
open( OUTPUT, "> $output" ) or die "cannot open output.";
binmode( OUTPUT, ":encoding(utf8)" );
while(my ($key,$value) = each(%dictMap)) {
print OUTPUT "$key $value\n";
}
print OUTPUT "\n";
while(my ($key,$value) = each(%wordMap)) {
print OUTPUT "$key is divided into:\t";
print OUTPUT $value."\n";
}
close(OUTPUT);
}
sub dictInit {
my $dictName = $_[0];
my $realWord;
# If there is a config file already, using it.
# It will simplify the dictionary initialization.
if(open( CONFIG, "< config" )) {
print "Read config file successfully.\n";
while(<CONFIG>) {
chomp;
my @dicting = split("\t");
$trainedDict{$dicting[0]} = $dicting[1];
}
close(CONFIG);
return;
}
open( CONFIG, "> config" ) or die "cannot open config file.";
open( DICTFD, "< $dictName" ) or die "can't open $dictName.";
binmode( CONFIG, ":encoding(utf8)" );
binmode( DICTFD, ":encoding(utf8)" );
while (<DICTFD>) {
chomp;
my @wordSlice = split(" ");
@wordSlice = @wordSlice[1 .. $#wordSlice];
foreach (@wordSlice) {
# I discarded the POS of the word,
# this is just a simple demo of segmentation.
my @mixedSlice = split("/");
$realWord = $mixedSlice[0];
if ( exists( $trainedDict{$realWord} ) ) {
$trainedDict{$realWord}++;
}
else {
$trainedDict{$realWord} = 1;
}
}
}
close(DICTFD);
# Writting all content of trainedMap into config file.
# The next time I would not open and read the naive file.
while(my ($key,$value) = each(%trainedDict)) {
print CONFIG "$key\t$value\n";
}
close(CONFIG);
}
sub isBelong {
exists( $trainedDict{ $_[0] } );
}
sub getCnt {
if ( exists( $trainedDict{ $_[0] } ) ) {
$trainedDict{ $_[0] };
}
else {
0.5; # Using for smoothing.
}
}
sub getPrb {
&getCnt( $_[0] ) / ( scalar keys %trainedDict );
}
sub input {
my $input = "input.txt";
open( INPUT, "< $input" ) or die "cannot open input.";
binmode( INPUT, ":encoding(utf8)" );
while (<INPUT>) {
chomp;
&maxPrb($_);
}
close(INPUT);
}
sub main {
&dictInit("199801q.txt");
&input;
&display;
}
# main function start
&main;
前言:软件测试常见面试题到这里就结束了哈,后续·可能会出一个关于自动化测试的,看你们想看什么方面的文字吧,废话不多说直接开始正文。十九、持续集成19.1 jenkins+ant+ jmeter+svn接口自动化测试?jenkins+ant+ jmeter+svn环境搭建原来这个环境是我这边搭建的,主要是几个步骤,第一 Jenkins安装、第二,ant安装、第三, jmeter安装、第四, jmeter与ant连接第五,Sn安装、第六,任务的构...
做Java开发一般都使用MyEclipse或者Eclipse,MyEclipse是一个极耗内存的大家伙,建议机器至少2G内存,不然会很卡,为减低其内存消耗可以做如下简单设置(Eclipse想要提高速度也可按这样设置)。 1、去除不需要加载的模块 Windows->Preferences->General->Startup and Shutdown 此时右侧显示的是Eclipse启动
停更了好久了啊,哈哈哈。最近参与了新项目事情比较多。新的项目组需要做appium app自动化测试,最近呢也在学习这些东西,后面呢分享一些学习心得体会吧。首先先介绍一下什么是android sdk:其实呢就是安卓专属的软件开发工具包,这也是我后期在自动化测试过程中必须要安装的。安装Android sdk 前呢必须得配置好jdk环境,至于jdk如何配置这里就不多讲了,接下来呢下载android sdk的包下载地址http://tools.android-studio.org/index.php/sdk
亚马逊产品点赞也算是产品优化推广的一部分,可能没有直接刷单测评那么明显,但是对于增加销量也是有一定的作用,因为评论首页如果都是好评的话,那么转化率也会变得更高。评论点赞的具体方法是利用多账号进行点击,可以在评论列表后几页的好评下方点击helpful,点击的人越多,越有可能会排在首页去。但是利用多账号进行点击时也要注意ip问题,如果关联了,就会被封号,所做的一切行为也会被清空!所以可以使用亚马逊鲲鹏系统进行多账号管理操作!亚马逊鲲鹏系统支持绑定代理ip及防指纹技术,可以让每个账号都拥有独立的运行环
(大部分内容来源于网络,有本人整理总结+个人见解所得)概念:DMA(direct memoryaccess直接内存访问),是一种避开CPU,可以直接对外设和内存进行访问的技术,可以实现外设与内存、内存之间、外设之间数据的访问和搬移。目的:不需要CPU的干预而直接服务外设,这样CPU就可以去处理别的事务,从而提高系统的效率。(附录举例补充)构成:·一般而言,DMA控制器将包括一条
1.树莓派学习网站https://shumeipai.nxez.com2.树莓派系统镜像下载网站https://shumeipai.nxez.com/download#os这里有很多树莓派用到的工具和多个版本的镜像下载地址。3.烧录镜像软件是Win32DiskImager.exe4.树莓派下设置中文显示和输入法ssh中输入以下命令:sudo apt-get install ...
part one: 一篇博客的介绍:Pytorch的load方法和load_state_dict方法只能较为固定的读入参数文件,他们要求读入的state_dict的key和Model.state_dict()的key对应相等。而我们在进行迁移学习的过程中也许只需要使用某个预训练网络的一部分,把多个网络拼和成一个网络,或者为了得到中间层的输出而分离预训练模型中的Sequential 等等,...
阿里移动安全 · 2015/12/01 10:41Author:[email protected]0x00 序2015年7月以色列移动信息安全公司Zimperium在Android Stagefright框架中发现了多个整数溢出和下溢,不正确整数溢出检查等漏洞,可导致任意代码执行等问题。攻击者通过发送包含特制媒体文件的MMS或WEB页来触发该漏洞。由于stagefright不只是用来播放媒体文件的...
前一段时间就在研究declare-styleable,这是给自定义控件添加自定义属性用的。 今天我就遇到了这个需求,于是写个自定义view玩玩。 这里首先我们给出declare-styleable的代码<?xml version="1.0" encoding="UTF-8"?><resources> <declare-styleable name="RoundProgressBar">
一、 脚本如下#!/bin/bashto=$1subject=$2content=$3echo -e "$content" | mail -s "$subject" "$to"二、测试发送邮件失败[[email protected]_server alertscripts]# ./sendmail.sh '******@***.com' 'test Bash 1' 'Zabbix Bas...
用application 比较合适 大家都能看到监听器是 servlet 的一种特例,对整个web 环境的监听ServletContext是一个全局的储存信息的空间,服务器开始,其就存在,服务器关闭,其才释放。为了节省空间,提高效率,ServletContext中,要放必须的、重要的、所有用户需要共享的,线程又是安全的一些信息。如,做一个购物类的网站,要从数据库中提取物品信息,如果用se...
搭建工具:NetBeans,xDebug,wamp 1. 选择最合适的xdebug版本,在测试xdebug版本改网址,将本地的phpinfo()中的内容ctrl+a、ctrl+v,根据提示,选择合适的版本。根据提示,将下载的dll放到php的ext文件下; 2. 配置php中的php.ini文件,wamp中在php,apache中均有,都一些更改;