-
Notifications
You must be signed in to change notification settings - Fork 0
/
atom.xml
178 lines (105 loc) · 48.7 KB
/
atom.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title>Monkey Café</title>
<subtitle>Would you like a cup of coffee?</subtitle>
<link href="/atom.xml" rel="self"/>
<link href="http://www.ouyang.xin/"/>
<updated>2020-08-31T16:06:15.436Z</updated>
<id>http://www.ouyang.xin/</id>
<author>
<name>欧阳欣</name>
</author>
<generator uri="http://hexo.io/">Hexo</generator>
<entry>
<title>有趣的算法题一:位运算</title>
<link href="http://www.ouyang.xin/2020/08/31/%E6%9C%89%E8%B6%A3%E7%9A%84%E7%AE%97%E6%B3%95%E9%A2%98%E5%85%B6%E4%B8%80%EF%BC%88%E4%BD%8D%E8%BF%90%E7%AE%97%EF%BC%89/"/>
<id>http://www.ouyang.xin/2020/08/31/有趣的算法题其一(位运算)/</id>
<published>2020-08-31T15:41:49.000Z</published>
<updated>2020-08-31T16:06:15.436Z</updated>
<content type="html"><![CDATA[<blockquote><p>我本科可能学的是球类专业(气球和篮球)。 —— Roger</p></blockquote><h1 id="写在开头"><a href="#写在开头" class="headerlink" title="写在开头"></a>写在开头</h1><p>虽然曾经也有过面试别人的经历,但是作为正式的面试官却是头一回。面试了很多同学下来发现,大家在基础算法能力和数据结构上都还是挺不足的,可能和我一样都学了个篮球专业吧哈哈。俗话说以人为镜,可以正衣冠,在这方面我现在也有严重的欠缺。故借此机会,从新刷刷题,找找感觉,同时整理下自己的思路。最后,又开始记博客了,撒花~</p><h1 id="什么是位运算"><a href="#什么是位运算" class="headerlink" title="什么是位运算"></a>什么是位运算</h1><p>位运算当然就是按位运算啦。</p>]]></content>
<summary type="html">
<blockquote>
<p>我本科可能学的是球类专业(气球和篮球)。 —— Roger</p>
</blockquote>
<h1 id="写在开头"><a href="#写在开头" class="headerlink" title="写在开头"></a>写在开头
</summary>
<category term="算法,基础" scheme="http://www.ouyang.xin/categories/%E7%AE%97%E6%B3%95-%E5%9F%BA%E7%A1%80/"/>
<category term="算法" scheme="http://www.ouyang.xin/tags/%E7%AE%97%E6%B3%95/"/>
</entry>
<entry>
<title>Topic Model简单指北</title>
<link href="http://www.ouyang.xin/2019/12/15/TopicModel%E7%AE%80%E5%8D%95%E6%8C%87%E5%8C%97/"/>
<id>http://www.ouyang.xin/2019/12/15/TopicModel简单指北/</id>
<published>2019-12-15T15:44:48.000Z</published>
<updated>2019-12-25T12:53:09.636Z</updated>
<content type="html"><![CDATA[<h1 id="Topic-Model-简单指北"><a href="#Topic-Model-简单指北" class="headerlink" title="Topic Model 简单指北"></a>Topic Model 简单指北</h1><h2 id="0-前言"><a href="#0-前言" class="headerlink" title="0 前言"></a>0 前言</h2><p>说起来好久没有回顾topic model了,刚好借此机会重温一下。这里推荐久远之前看过的一个系列,<a href="http://www.52nlp.cn/category/topic-model" target="_blank" rel="noopener">想看戳我</a> 。</p><p>topic model本质是计算词与文档相似度的一种方法,在序列模型未普及的时候以至于现在,仍然是一种简单而高效的方法,但由于本人并不是topicmodel出身,故本篇也仅仅基于个人理解,仅做入门参考~</p><p>若有问题,请联系本人互怼。</p><h2 id="1-主题模型之前"><a href="#1-主题模型之前" class="headerlink" title="1 主题模型之前"></a>1 主题模型之前</h2><p>主题模型在搞啥?</p><p>文档中的topic结构,文档和topic的相似度,doclist和topic的相似度。</p><p>词-词、词-文档、文档-词的相似度。</p><h2 id="2-TFIDF"><a href="#2-TFIDF" class="headerlink" title="2 TFIDF"></a>2 TFIDF</h2><blockquote><p>我们要优化这个井盖。</p><p> ——安神</p></blockquote><p>那么首先说到关键词和相似度,就逃不开TFIDF。</p><p>TFIDF(Term Frequency/Inverse Document Frequency)简单讲就是一个词在一个语料库单个文档的重要程度。</p><p>其中TF指词频:<br>$$<br>tf_{i,j} = \frac{n_{i,j}}{\sum_k n_{k,j}}<br>$$<br>其中$n_{i,j}$ 表示词i在文档j中出现的次数,normalization是为了防止文档长度对词出现概率的影响(越长则词越容易出现)。 </p><p>光考虑词频,是不是少了点什么?常用词的词频总是很高,它们能作为关键词么?</p><p>所以IDF出现了,它指在多少文档中出现过:<br>$$<br>idf_i = log\frac{|D|}{|d, t_i \in d_j|}<br>$$</p><p>然后乘一乘,单车变摩托。直观而看,在一个文档中的某个词,如果在该文档出现的越多且其他文档出现的越少,它就更能作为该文档的一个关键词表达。</p><p>抛个问题,TFIDF选择的是特征的权重还是区分度?</p><blockquote><p> 人的指纹太复杂,包含太多的位置和几何形状,要完全重现一个人的指纹,存储和计算都是大麻烦。因此第一步总是一个特征选择的问题,我们把全人类的指纹都统计一下,看看哪几个位置能够最好的区分不同的人。显然不同的位置效果很不一样,在有的位置上,我的指纹是是什么形状,其他人也大都是这个形状,这个位置就不具有区分度,或者说不具有表征性,或者说,对分类问题来说,它的重要程度低。这样的位置我们就倾向于在识别的时候根本不看它,不考虑它。</p></blockquote><h2 id="3-矩阵分解"><a href="#3-矩阵分解" class="headerlink" title="3 矩阵分解"></a>3 矩阵分解</h2><blockquote><p>万物皆可矩阵,万物皆可分解。</p><p> ——猴子先生</p></blockquote><p>TFIDF顺利抽取出了主题词,那咱们的主题呢?讲道理,主题应该是一系列词分布,直接整主题词还是太大了(参考中文常用字6000),那么咋办,矩阵分解呗。<br>$$<br>p(Word|Doc) = \sum_{topic}p(Word|topic)*p(topic|Doc)<br>$$</p><h3 id="3-1-LSA(也叫LSI)"><a href="#3-1-LSA(也叫LSI)" class="headerlink" title="3.1 LSA(也叫LSI)"></a>3.1 LSA(也叫LSI)</h3><p>文档和词袋,天生就是一个矩阵(文档用id表示),这个时候不分解一下怎么对得起自己。</p><p>LSA(Latent Semantic Analysis)就是早期的主题模型,但是矩阵过大直接分解等于送头,所以LSA的一个版本考虑了SVD(Singular Value Decomposition 奇异值分解)。</p><h4 id="3-1-1-撕一发SVD"><a href="#3-1-1-撕一发SVD" class="headerlink" title="3.1.1 撕一发SVD"></a>3.1.1 撕一发SVD</h4><p><img src="/Users/ouyangxin02/Library/Application Support/typora-user-images/image-20191225002805830.png" alt="image-20191225002805830"></p><p><img src="/Users/ouyangxin02/Library/Application Support/typora-user-images/image-20191225003055933.png" alt="image-20191225003055933"></p><h2 id="参考资料"><a href="#参考资料" class="headerlink" title="参考资料"></a>参考资料</h2><p>等我补啊。。。</p>]]></content>
<summary type="html">
<h1 id="Topic-Model-简单指北"><a href="#Topic-Model-简单指北" class="headerlink" title="Topic Model 简单指北"></a>Topic Model 简单指北</h1><h2 id="0-前言"><a
</summary>
<category term="NLP,主题模型" scheme="http://www.ouyang.xin/tags/NLP-%E4%B8%BB%E9%A2%98%E6%A8%A1%E5%9E%8B/"/>
</entry>
<entry>
<title>双系统mbr分区grub引导启动问题解决指北</title>
<link href="http://www.ouyang.xin/2017/03/25/%E5%8F%8C%E7%B3%BB%E7%BB%9Fmbr%E5%88%86%E5%8C%BAgrub%E5%BC%95%E5%AF%BC%E5%90%AF%E5%8A%A8%E9%97%AE%E9%A2%98%E8%A7%A3%E5%86%B3%E6%8C%87%E5%8C%97/"/>
<id>http://www.ouyang.xin/2017/03/25/双系统mbr分区grub引导启动问题解决指北/</id>
<published>2017-03-25T12:15:46.000Z</published>
<updated>2018-11-04T16:28:35.302Z</updated>
<content type="html"><![CDATA[<h2 id="问题"><a href="#问题" class="headerlink" title="问题"></a>问题</h2><p>相信有很多人和我一样,喜欢用linux敲代码和工作,却抵挡不了windows的诱(you)惑(xi)而选择了双系统(买mac的壕们请无视)。<br>那么问题就来了,很多时候由于安装不当,启动时只能找到一个系统,所以在此列举一些应对措施。 </p><a id="more"></a><h2 id="安装linux启动不了windows"><a href="#安装linux启动不了windows" class="headerlink" title="安装linux启动不了windows"></a>安装linux启动不了windows</h2><p>大部分linux启动文件都使用grub或grub2。如果安装双系统后启动电脑启动不了windows,可以进入linux修复引导。<br>进入linux,使用如下命令即可更新grub自动找回启动项:<code>sudo update-grub2</code>,之后重启电脑即可。</p><h2 id="双系统误操作黑屏找不到分区"><a href="#双系统误操作黑屏找不到分区" class="headerlink" title="双系统误操作黑屏找不到分区"></a>双系统误操作黑屏找不到分区</h2><h3 id="PE启动"><a href="#PE启动" class="headerlink" title="PE启动"></a>PE启动</h3><ul><li>下载老毛桃或者大白菜</li><li>u盘启动(F12 或其他)进入dg分区管理</li><li>修复主分区mbr,即可重新引导</li><li>咳咳,一般没用</li></ul><h3 id="linux-liveCD启动"><a href="#linux-liveCD启动" class="headerlink" title="linux liveCD启动"></a>linux liveCD启动</h3><ul><li>下载liveCD linux从u盘启动进入系统</li><li><code>sudo -i</code>以下命令都要在root权限下执行</li><li><code>fdisk -l</code>查看硬盘分区,找到/和/boot分区号(如/dev/sda1 /dev/sda2)</li><li><code>mount /dev/sda1 /mnt</code> <code>mount /dev/sda2/ /mnt/boot</code>挂载这两个分区</li><li>在硬盘重建mbr分区表,执行以下命令 <code>gurb2-install --root-directory=/dev/sda</code> PS.这里路径是硬盘号,不是分区号</li><li>重启~</li></ul><h3 id="grub-rescue"><a href="#grub-rescue" class="headerlink" title="grub rescue"></a>grub rescue</h3><p>win+linux双系统下若进行重装linux,修改硬盘分区等操作时可能会出现启动电脑黑屏,提示无法找到分区,此时会进入类似shell的grub rescue界面。<br>这个模式命令只有<code>ls</code>,<code>set</code>,<code>insmod</code>,<code>root</code>,<code>prefix</code>,<code>normal</code>等可用。<br>如果linux系统还在,那么还有救,步骤如下:</p><ul><li><code>ls</code>查看硬盘信息,如(h0,msdos1)既为一个分区</li><li><code>ls (hd0,msdos1)/boot/grub/</code>或<code>ls (hd0,msdos1)/grub/</code>找寻存放grub的boot分区</li><li><code>set root=(hd0,msdos1)</code>设置该分区为root</li><li><code>set prefix=(hd0,msdos1)/boot/grub</code>设置启动项</li><li><code>insmode normal</code>进入grub菜单,如果成功,即可进入linux系统</li><li>照第一节内容更新grub,即可解决问题</li></ul><h2 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h2><p>这里只说到了MBR分区下的启动问题,当然GPT分区启动也可能会出现问题,总之安装双系统坑很多。<br>以后也许可以抽空写一下分区表和启动的区别,当然得有空。希望实验顺利~</p>]]></content>
<summary type="html">
<h2 id="问题"><a href="#问题" class="headerlink" title="问题"></a>问题</h2><p>相信有很多人和我一样,喜欢用linux敲代码和工作,却抵挡不了windows的诱(you)惑(xi)而选择了双系统(买mac的壕们请无视)。<br>那么问题就来了,很多时候由于安装不当,启动时只能找到一个系统,所以在此列举一些应对措施。 </p>
</summary>
<category term="Linux" scheme="http://www.ouyang.xin/categories/Linux/"/>
<category term="其他" scheme="http://www.ouyang.xin/categories/Linux/%E5%85%B6%E4%BB%96/"/>
<category term="Linux" scheme="http://www.ouyang.xin/tags/Linux/"/>
<category term="grub" scheme="http://www.ouyang.xin/tags/grub/"/>
</entry>
<entry>
<title>Fedora24 解决搜狗拼音输入法不能用问题</title>
<link href="http://www.ouyang.xin/2017/03/22/Fedora24-%E8%A7%A3%E5%86%B3%E6%90%9C%E7%8B%97%E6%8B%BC%E9%9F%B3%E8%BE%93%E5%85%A5%E6%B3%95%E4%B8%8D%E8%83%BD%E7%94%A8%E9%97%AE%E9%A2%98/"/>
<id>http://www.ouyang.xin/2017/03/22/Fedora24-解决搜狗拼音输入法不能用问题/</id>
<published>2017-03-22T08:11:15.000Z</published>
<updated>2018-11-04T16:28:35.302Z</updated>
<content type="html"><![CDATA[<p>自从用了Fedora, 各种问题层出不穷,可能是我的水平还不够吧,革命继续~ </p><h2 id="问题"><a href="#问题" class="headerlink" title="问题"></a>问题</h2><p>这回碰到的问题是,搜狗拼音可以加载,但是输入时只出一个小框,不能打出汉字。查了很多原因无果,最后在搜狗社区找到了解决方案。</p><h2 id="解决方案"><a href="#解决方案" class="headerlink" title="解决方案"></a>解决方案</h2><p>原链接见<a href="http://pinyin.sogou.com/bbs/forum.php?mod=viewthread&tid=2681098&extra=page%3D1" target="_blank" rel="noopener">这里</a><br>删除用户目录下<code>./config</code>的三个文件夹即可。<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">SogouPY</span><br><span class="line">SogouPY.users</span><br><span class="line">sogou-qimpanel</span><br></pre></td></tr></table></figure></p><h2 id="尾声"><a href="#尾声" class="headerlink" title="尾声"></a>尾声</h2><p>虽然解决了问题,但是还是没找到问题具体出在哪里,可能是配置文件问题,也可能是F24更新出现的依赖库不兼容问题。<br>还有一个问题就是输入法经常会当掉,等论文忙完了要好好研究一下了。</p>]]></content>
<summary type="html">
<p>自从用了Fedora, 各种问题层出不穷,可能是我的水平还不够吧,革命继续~ </p>
<h2 id="问题"><a href="#问题" class="headerlink" title="问题"></a>问题</h2><p>这回碰到的问题是,搜狗拼音可以加载,但是输入
</summary>
<category term="Linux" scheme="http://www.ouyang.xin/categories/Linux/"/>
<category term="Fedora" scheme="http://www.ouyang.xin/categories/Linux/Fedora/"/>
<category term="Linux" scheme="http://www.ouyang.xin/tags/Linux/"/>
</entry>
<entry>
<title>Effective Java: 构造器相关</title>
<link href="http://www.ouyang.xin/2017/01/18/Effective-Java-%E6%9E%84%E9%80%A0%E5%99%A8%E7%9B%B8%E5%85%B3/"/>
<id>http://www.ouyang.xin/2017/01/18/Effective-Java-构造器相关/</id>
<published>2017-01-18T05:09:46.000Z</published>
<updated>2018-11-04T16:28:35.302Z</updated>
<content type="html"><![CDATA[<p>整理自我的Wiz笔记 </p><hr><h1 id="用私有构造器或者枚举类型强化Singleton属性"><a href="#用私有构造器或者枚举类型强化Singleton属性" class="headerlink" title="用私有构造器或者枚举类型强化Singleton属性"></a>用私有构造器或者枚举类型强化Singleton属性</h1><h2 id="Singleton"><a href="#Singleton" class="headerlink" title="Singleton"></a>Singleton</h2><p>仅仅被实例化一次的类,通常被用来代表那些本质上唯一的系统组件,比如窗口管理器或者文件系统。<br>java1.5之后,有三种方式实现singleton。</p><h3 id="Singleton-with-public-final-field"><a href="#Singleton-with-public-final-field" class="headerlink" title="Singleton with public final field"></a>Singleton with public final field</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Elvis</span> </span>{</span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">final</span> Elvis INSTANCE = <span class="keyword">new</span> Elvis();</span><br><span class="line"> <span class="function"><span class="keyword">private</span> <span class="title">Elvis</span><span class="params">()</span> </span>{...}</span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">leaveTheBuilding</span><span class="params">()</span> </span>{...}</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p>调用一次private 构造器,实例化公有的静态final域Elvis.INSTANCE。<br><strong>特权客户端可以借助AccessibleObject.setAccessible,通过反射机制调用私有构造器!</strong><br><a id="more"></a></p><h3 id="Singleton-with-static-factory"><a href="#Singleton-with-static-factory" class="headerlink" title="Singleton with static factory"></a>Singleton with static factory</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Elvis</span> </span>{</span><br><span class="line"> <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> Elvis INSTANCE = <span class="keyword">new</span> Elvis();</span><br><span class="line"> <span class="function"><span class="keyword">private</span> <span class="title">Elvis</span><span class="params">()</span> </span>{...}</span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> Elvis <span class="title">getInstance</span><span class="params">()</span> </span>{<span class="keyword">return</span> INSTANCE;}</span><br><span class="line"> </span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">leaveTheBuilding</span><span class="params">()</span> </span>{...}</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p>该方法的优势在于,提供了灵活性。 </p><h3 id="Enum-Singleton"><a href="#Enum-Singleton" class="headerlink" title="Enum Singleton"></a>Enum Singleton</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">enum</span> Elvis {</span><br><span class="line"> INSTANCE;</span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">leaveTheBuilding</span><span class="params">()</span> </span>{...}</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p>该方法更加简洁,并提供了序列化机制。<strong>单元素的枚举类型已经成为实现Singleton的最佳方法!</strong> </p><h2 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h2><p>根据面神的经验,<strong>在公司中为了可读性和其他原因仍然会选择使用静态类来实现,不会选择枚举类型。</strong> </p><hr><h1 id="遇到多个构造器参数时要考虑用构建器"><a href="#遇到多个构造器参数时要考虑用构建器" class="headerlink" title="遇到多个构造器参数时要考虑用构建器"></a>遇到多个构造器参数时要考虑用构建器</h1><h2 id="重叠构造器"><a href="#重叠构造器" class="headerlink" title="重叠构造器"></a>重叠构造器</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">NutritionFacts</span> </span>{</span><br><span class="line"> <span class="keyword">private</span> <span class="keyword">final</span> <span class="keyword">int</span> servingSize;</span><br><span class="line"> <span class="keyword">private</span> <span class="keyword">final</span> <span class="keyword">int</span> servings;</span><br><span class="line"> <span class="keyword">private</span> <span class="keyword">final</span> <span class="keyword">int</span> calories;</span><br><span class="line"> <span class="keyword">private</span> <span class="keyword">final</span> <span class="keyword">int</span> fat;</span><br><span class="line"> <span class="keyword">private</span> <span class="keyword">final</span> <span class="keyword">int</span> sodium;</span><br><span class="line"> <span class="keyword">private</span> <span class="keyword">final</span> <span class="keyword">int</span> carbohydrate;</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="title">NutritionFacts</span><span class="params">(<span class="keyword">int</span> servingSize, <span class="keyword">int</span> servings)</span></span>{</span><br><span class="line"> <span class="keyword">this</span>(servingSize, servings, <span class="number">0</span>);</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="title">NutritionFacts</span><span class="params">(<span class="keyword">int</span> servingSize, <span class="keyword">int</span> servings, <span class="keyword">int</span> calories)</span></span>{</span><br><span class="line"> <span class="keyword">this</span>(servingSize, servings, calories, <span class="number">0</span>);</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="title">NutritionFacts</span><span class="params">(<span class="keyword">int</span> servingSize, <span class="keyword">int</span> servings, <span class="keyword">int</span> calories, <span class="keyword">int</span> fat)</span></span>{</span><br><span class="line"> <span class="keyword">this</span>(servingSize, servings, calories, fat, <span class="number">0</span>);</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="title">NutritionFacts</span><span class="params">(<span class="keyword">int</span> servingSize, <span class="keyword">int</span> servings, <span class="keyword">int</span> calories, <span class="keyword">int</span> fat, <span class="keyword">int</span> sodium)</span></span>{</span><br><span class="line"> <span class="keyword">this</span>(servingSize, servings, calories, fat, sodium, <span class="number">0</span>);</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="title">NutritionFacts</span><span class="params">(<span class="keyword">int</span> servingSize, <span class="keyword">int</span> servings, <span class="keyword">int</span> calories, <span class="keyword">int</span> fat, <span class="keyword">int</span> sodium, <span class="keyword">int</span> carbohydrate)</span></span>{</span><br><span class="line"> <span class="keyword">this</span>.servingSize = servingSize;</span><br><span class="line"> <span class="keyword">this</span>.servings = servings;</span><br><span class="line"> <span class="keyword">this</span>.calories = calories;</span><br><span class="line"> <span class="keyword">this</span>.fat = fat;</span><br><span class="line"> <span class="keyword">this</span>.sodium = sodium;</span><br><span class="line"> <span class="keyword">this</span>.carbohydrate = carbohydrate;</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure><h3 id="缺点"><a href="#缺点" class="headerlink" title="缺点"></a>缺点</h3><p>当有许多参数的适合,客户端代码会很难编写,并且仍然较难以阅读。 </p><h2 id="javaBeans"><a href="#javaBeans" class="headerlink" title="javaBeans"></a>javaBeans</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">NutritionFacts</span> </span>{</span><br><span class="line"> <span class="keyword">private</span> <span class="keyword">int</span> servingSize = -<span class="number">1</span>;</span><br><span class="line"> <span class="keyword">private</span> <span class="keyword">int</span> servings = -<span class="number">1</span>;</span><br><span class="line"> <span class="keyword">private</span> <span class="keyword">int</span> calories = <span class="number">0</span>;</span><br><span class="line"> <span class="keyword">private</span> <span class="keyword">int</span> fat = <span class="number">0</span>;</span><br><span class="line"> <span class="keyword">private</span> <span class="keyword">int</span> sodium = <span class="number">0</span>;</span><br><span class="line"> <span class="keyword">private</span> <span class="keyword">int</span> carbohydrate = <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="title">NutritionFacts</span><span class="params">()</span> </span>{}</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setServingSize</span><span class="params">(<span class="keyword">int</span> val)</span> </span>{ servingSize = val;}</span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setServings</span><span class="params">(<span class="keyword">int</span> val)</span> </span>{ servingSize = val;}</span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setCalories</span><span class="params">(<span class="keyword">int</span> val)</span> </span>{ servingSize = val;}</span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setFat</span><span class="params">(<span class="keyword">int</span> val)</span> </span>{ servingSize = val;}</span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setSodium</span><span class="params">(<span class="keyword">int</span> val)</span> </span>{ servingSize = val;}</span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setCarbohydrate</span><span class="params">(<span class="keyword">int</span> val)</span> </span>{ servingSize = val;}</span><br><span class="line">}</span><br></pre></td></tr></table></figure><h3 id="缺点-1"><a href="#缺点-1" class="headerlink" title="缺点"></a>缺点</h3><p>构造过程中可能<strong>处于不一致的状态</strong>,也就是说线程不安全。</p><h2 id="builder-Pattern-构建器"><a href="#builder-Pattern-构建器" class="headerlink" title="builder Pattern (构建器)"></a>builder Pattern (构建器)</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">NutritionFacts</span> </span>{</span><br><span class="line"> <span class="keyword">private</span> <span class="keyword">final</span> <span class="keyword">int</span> servingSize;</span><br><span class="line"> <span class="keyword">private</span> <span class="keyword">final</span> <span class="keyword">int</span> servings;</span><br><span class="line"> <span class="keyword">private</span> <span class="keyword">final</span> <span class="keyword">int</span> calories;</span><br><span class="line"> <span class="keyword">private</span> <span class="keyword">final</span> <span class="keyword">int</span> fat;</span><br><span class="line"> <span class="keyword">private</span> <span class="keyword">final</span> <span class="keyword">int</span> sodium;</span><br><span class="line"> <span class="keyword">private</span> <span class="keyword">final</span> <span class="keyword">int</span> carbohydrate;</span><br><span class="line"></span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">static</span> <span class="class"><span class="keyword">class</span> <span class="title">Builder</span> </span>{</span><br><span class="line"> <span class="keyword">private</span> <span class="keyword">final</span> <span class="keyword">int</span> servingSize;</span><br><span class="line"> <span class="keyword">private</span> <span class="keyword">final</span> <span class="keyword">int</span> servings;</span><br><span class="line"></span><br><span class="line"> <span class="keyword">private</span> <span class="keyword">int</span> calories = <span class="number">0</span>;</span><br><span class="line"> <span class="keyword">private</span> <span class="keyword">int</span> fat = <span class="number">0</span>;</span><br><span class="line"> <span class="keyword">private</span> <span class="keyword">int</span> carbohydrate = <span class="number">0</span>;</span><br><span class="line"> <span class="keyword">private</span> <span class="keyword">int</span> sodium = <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="title">Builder</span><span class="params">(<span class="keyword">int</span> servingSize, <span class="keyword">int</span> servings)</span> </span>{</span><br><span class="line"> <span class="keyword">this</span>.servingSize = servingSize;</span><br><span class="line"> <span class="keyword">this</span>.servings = servings;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> Builder <span class="title">calories</span><span class="params">(<span class="keyword">int</span> val)</span></span>{ calories = val; <span class="keyword">return</span> <span class="keyword">this</span>;}</span><br><span class="line"> <span class="function"><span class="keyword">public</span> Builder <span class="title">fat</span><span class="params">(<span class="keyword">int</span> val)</span></span>{ fat = val; <span class="keyword">return</span> <span class="keyword">this</span>;}</span><br><span class="line"> <span class="function"><span class="keyword">public</span> Builder <span class="title">sodium</span><span class="params">(<span class="keyword">int</span> val)</span></span>{ sodium = val; <span class="keyword">return</span> <span class="keyword">this</span>;}</span><br><span class="line"> <span class="function"><span class="keyword">public</span> Builder <span class="title">carbohydrate</span><span class="params">(<span class="keyword">int</span> val)</span></span>{ carbohydrate = val; <span class="keyword">return</span> <span class="keyword">this</span>;}</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> NutritionFacts <span class="title">build</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="keyword">return</span> <span class="keyword">new</span> NutritionFacts3(<span class="keyword">this</span>);</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">private</span> <span class="title">NutritionFacts</span><span class="params">(Builder builder)</span> </span>{</span><br><span class="line"> servingSize = builder.servingSize;</span><br><span class="line"> servings = builder.servings;</span><br><span class="line"> calories = builder.calories;</span><br><span class="line"> fat = builder.fat;</span><br><span class="line"> sodium = builder.sodium;</span><br><span class="line"> carbohydrate = builder.carbohydrate;</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p>优雅的构建!!!!<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">NutritionFacts cocaCola = <span class="keyword">new</span> NutritionFacts.Builder(<span class="number">240</span>, <span class="number">8</span>)</span><br><span class="line">.calories(<span class="number">100</span>).sodium(<span class="number">35</span>).carbohydrate(<span class="number">27</span>).build();</span><br></pre></td></tr></table></figure></p><h2 id="总结-1"><a href="#总结-1" class="headerlink" title="总结"></a>总结</h2><p>静态工厂和构造器有一个局限性,都不能很好的扩展到大量可选参数。一共有三个方法可以用于构造有<strong>大量</strong>可选构造函数的类的实例。<br>如果类的构造器或者静态工厂中具有多个参数,Builder模式就是一种不错的选择。<strong>但是在某些十分注重性能的情况下,可能就会成为问题</strong> </p><hr><h1 id="通过私有构造器强化不可实例化的能力"><a href="#通过私有构造器强化不可实例化的能力" class="headerlink" title="通过私有构造器强化不可实例化的能力"></a>通过私有构造器强化不可实例化的能力</h1><p>有时候想只写一个包含静态方法的类,又不想实例化,<strong>通过将类做成抽象类来强制该类不可被实例化是行不通的。</strong><br>这个时候,让它包含私有构造器即可!<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">UtilityClass</span> </span>{</span><br><span class="line"> <span class="function"><span class="keyword">private</span> <span class="title">UtilityClass</span><span class="params">()</span></span>{</span><br><span class="line"> <span class="keyword">throw</span> <span class="keyword">new</span> AssertionError();</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure></p><h1 id="未完待续…"><a href="#未完待续…" class="headerlink" title="未完待续…"></a>未完待续…</h1>]]></content>
<summary type="html">
<p>整理自我的Wiz笔记 </p>
<hr>
<h1 id="用私有构造器或者枚举类型强化Singleton属性"><a href="#用私有构造器或者枚举类型强化Singleton属性" class="headerlink" title="用私有构造器或者枚举类型强化Singleton属性"></a>用私有构造器或者枚举类型强化Singleton属性</h1><h2 id="Singleton"><a href="#Singleton" class="headerlink" title="Singleton"></a>Singleton</h2><p>仅仅被实例化一次的类,通常被用来代表那些本质上唯一的系统组件,比如窗口管理器或者文件系统。<br>java1.5之后,有三种方式实现singleton。</p>
<h3 id="Singleton-with-public-final-field"><a href="#Singleton-with-public-final-field" class="headerlink" title="Singleton with public final field"></a>Singleton with public final field</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Elvis</span> </span>&#123;</span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">final</span> Elvis INSTANCE = <span class="keyword">new</span> Elvis();</span><br><span class="line"> <span class="function"><span class="keyword">private</span> <span class="title">Elvis</span><span class="params">()</span> </span>&#123;...&#125;</span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">leaveTheBuilding</span><span class="params">()</span> </span>&#123;...&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>调用一次private 构造器,实例化公有的静态final域Elvis.INSTANCE。<br><strong>特权客户端可以借助AccessibleObject.setAccessible,通过反射机制调用私有构造器!</strong><br>
</summary>
<category term="读书笔记" scheme="http://www.ouyang.xin/categories/%E8%AF%BB%E4%B9%A6%E7%AC%94%E8%AE%B0/"/>
<category term="Effective Java" scheme="http://www.ouyang.xin/categories/%E8%AF%BB%E4%B9%A6%E7%AC%94%E8%AE%B0/Effective-Java/"/>
<category term="Java" scheme="http://www.ouyang.xin/tags/Java/"/>
</entry>
<entry>
<title>python 生成器小插曲</title>
<link href="http://www.ouyang.xin/2017/01/17/python-%E7%94%9F%E6%88%90%E5%99%A8%E5%B0%8F%E6%8F%92%E6%9B%B2/"/>
<id>http://www.ouyang.xin/2017/01/17/python-生成器小插曲/</id>
<published>2017-01-17T12:15:49.000Z</published>
<updated>2018-11-04T16:28:35.302Z</updated>
<content type="html"><![CDATA[<h1 id="问题"><a href="#问题" class="headerlink" title="问题"></a>问题</h1><p>对分词后的words迭代两次保存文件,但是结果程序只执行第一次的迭代,如下代码:</p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">words = pseg.cut(i)</span><br><span class="line"><span class="keyword">for</span> word,flag <span class="keyword">in</span> list(words):</span><br><span class="line"> test.write(<span class="string">'%s %s\n'</span> % (word, flag))</span><br><span class="line"><span class="keyword">for</span> word, flag <span class="keyword">in</span> list(words):</span><br><span class="line"> train.write(<span class="string">'%s %s I-P\n'</span> % (word, flag))</span><br></pre></td></tr></table></figure><p>逐步调试后发现,第二次for循环words已经为空,没有重新遍历。结巴cut生成的是generator而不是list和字典,<strong>通过for循环generator只能遍历一次</strong>!<br><a id="more"></a></p><h1 id="生成器-generator"><a href="#生成器-generator" class="headerlink" title="生成器 generator"></a>生成器 generator</h1><h2 id="优点"><a href="#优点" class="headerlink" title="优点"></a>优点</h2><p>generator是一种边计算,边循环机制的迭代器,这样可以减少大量数据下list占用的大量内存。也就是说,generator在需要时返回中间值,保存当前状态,等待下一次返回要求。 关键字yield,在程序调用generator时,函数执行到yield时会被挂起,等待下一次调用。</p><blockquote><p>PEP255: A python generator is a kind of python iterator, but of an especially powerful kind.</p></blockquote><h2 id="使用方法"><a href="#使用方法" class="headerlink" title="使用方法"></a>使用方法</h2><ul><li><p>括号直接生成 </p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">g = (x * x <span class="keyword">for</span> x <span class="keyword">in</span> range(<span class="number">10</span>))</span><br></pre></td></tr></table></figure></li><li><p>构造函数,并且把其中print改为yield,这样该函数返回的就是一个生成器 </p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">range</span><span class="params">(cnt)</span>:</span></span><br><span class="line"> n = <span class="number">0</span></span><br><span class="line"> <span class="keyword">while</span> n < cnt:</span><br><span class="line"> <span class="keyword">yield</span> n</span><br><span class="line"> n = n + <span class="number">1</span></span><br></pre></td></tr></table></figure></li></ul><h2 id="其他方法"><a href="#其他方法" class="headerlink" title="其他方法"></a>其他方法</h2><ol><li>next()或者for循环遍历generator,当next ()调用到最后一次之后,会抛出异常。</li><li>send()用来发送信息,这里会将阻塞住的yield的值换成sent()发送的值。</li><li>close()用来关闭generator</li></ol><h2 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h2><p>想让结巴生成list请用<code>jieba.lcut()</code>!!!<br>当然generator还有更有趣的用法,待日后发掘更新~</p>]]></content>
<summary type="html">
<h1 id="问题"><a href="#问题" class="headerlink" title="问题"></a>问题</h1><p>对分词后的words迭代两次保存文件,但是结果程序只执行第一次的迭代,如下代码:</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">words = pseg.cut(i)</span><br><span class="line"><span class="keyword">for</span> word,flag <span class="keyword">in</span> list(words):</span><br><span class="line"> test.write(<span class="string">'%s %s\n'</span> % (word, flag))</span><br><span class="line"><span class="keyword">for</span> word, flag <span class="keyword">in</span> list(words):</span><br><span class="line"> train.write(<span class="string">'%s %s I-P\n'</span> % (word, flag))</span><br></pre></td></tr></table></figure>
<p>逐步调试后发现,第二次for循环words已经为空,没有重新遍历。结巴cut生成的是generator而不是list和字典,<strong>通过for循环generator只能遍历一次</strong>!<br>
</summary>
<category term="python" scheme="http://www.ouyang.xin/categories/python/"/>
<category term="基础" scheme="http://www.ouyang.xin/categories/python/%E5%9F%BA%E7%A1%80/"/>
<category term="python" scheme="http://www.ouyang.xin/tags/python/"/>
</entry>
</feed>