鹅厂面试题,英语单词拼写检查算法?

时间:2024-12-16 21:42 人气:0 编辑:招聘街

一、鹅厂面试题,英语单词拼写检查算法?

又到安利Python的时间, 最终代码不超过30行(优化前),加上优化也不过40行。

第一步. 构造Trie(用dict登记结点信息和维持子结点集合):

-- 思路:对词典中的每个单词,逐词逐字母拓展Trie,单词完结处的结点用None标识。

def make_trie(words):
    trie = {}
    for word in words:
        t = trie
        for c in word:
            if c not in t: t[c] = {}
            t = t[c]
        t[None] = None
    return trie

第二步. 容错查找(容错数为tol):

-- 思路:实质上是对Trie的深度优先搜索,每一步加深时就消耗目标词的一个字母。当搜索到达某个结点时,分为不消耗容错数和消耗容错数的情形,继续搜索直到目标词为空。搜索过程中,用path记录搜索路径,该路径即为一个词典中存在的词,作为纠错的参考。

-- 最终结果即为诸多搜索停止位置的结点路径的并集。

def check_fuzzy(trie, word, path='', tol=1):
    if word == '':
        return {path} if None in trie else set()
    else:
        p0 = set()
        if word[0] in trie:
            p0 = check_fuzzy(trie[word[0]], word[1:], path+word[0], tol)
        p1 = set()
        if tol > 0:
            for k in trie:
                if k is not None and k != word[0]:
                    p1.update(check_fuzzy(trie[k], word[1:], path+k, tol-1))
        return p0 | p1

简单测试代码 ------

构造Trie:

words = ['hello', 'hela', 'dome']
t = make_trie(words)

In [11]: t
Out[11]: 
{'d': {'o': {'m': {'e': {'$': {}}}}},
 'h': {'e': {'l': {'a': {'$': {}}, 'l': {'o': {'$': {}}}}}}}

容错查找:

In [50]: check_fuzzy(t, 'hellu', tol=0)
Out[50]: {}

In [51]: check_fuzzy(t, 'hellu', tol=1)
Out[51]: {'hello'}

In [52]: check_fuzzy(t, 'healu', tol=1)
Out[52]: {}

In [53]: check_fuzzy(t, 'healu', tol=2)
Out[53]: {'hello'}

似乎靠谱~

---------------------------分--割--线--------------------------------------

以上是基于Trie的approach,另外的approach可以参看@黄振童鞋推荐Peter Norvig即P神的How to Write a Spelling Corrector

虽然我已有意无意模仿P神的代码风格,但每次看到P神的源码还是立马跪...

话说word[1:]这种表达方式其实是有渊源的,相信有的童鞋对(cdr word)早已烂熟于心...(呵呵

------------------------分-----割-----线-----二--------------------------------------

回归正题.....有童鞋说可不可以增加新的容错条件,比如增删字母,我大致对v2方法作了点拓展,得到下面的v3版本。

拓展的关键在于递归的终止,即每一次递归调用必须对参数进行有效缩减,要么是参数word,要么是参数tol~

def check_fuzzy(trie, word, path='', tol=1):
    if tol < 0:
        return set()
    elif word == '':
        results = set()
        if None in trie:
            results.add(path)
        # 增加词尾字母
        for k in trie:
            if k is not None:
                results |= check_fuzzy(trie[k], '', path+k, tol-1)
        return results
    else:
        results = set()
        # 首字母匹配
        if word[0] in trie:
            results |= check_fuzzy(trie[word[0]], word[1:], path + word[0], tol)
        # 分情形继续搜索(相当于保留待探索的回溯分支)
        for k in trie:
            if k is not None and k != word[0]:
                # 用可能正确的字母置换首字母
                results |= check_fuzzy(trie[k], word[1:], path+k, tol-1)
                # 插入可能正确的字母作为首字母
                results |= check_fuzzy(trie[k], word, path+k, tol-1)
        # 跳过余词首字母
        results |= check_fuzzy(trie, word[1:], path, tol-1)
        # 交换原词头两个字母
        if len(word) > 1:
            results |= check_fuzzy(trie, word[1]+word[0]+word[2:], path, tol-1)
        return results

好像还是没有过30行……注释不算(

本答案的算法只在追求极致简洁的表达,概括问题的大致思路。至于实际应用的话可能需要很多Adaption和Tuning,包括基于统计和学习得到一些词语校正的bias。我猜测这些拓展都可以反映到Trie的结点构造上面,比如在结点处附加一个概率值,通过这个概率值来影响搜索倾向;也可能反映到更多的搜索分支的控制参数上面,比如增加一些更有脑洞的搜索分支。(更细节的问题这里就不深入了逃

----------------------------------分-割-线-三----------------------------------------

童鞋们可能会关心时间和空间复杂度的问题,因为上述这种优(cu)雅(bao)的写法会导致产生的集合对象呈指数级增加,集合的合并操作时间也指数级增加,还使得gc不堪重负。而且,我们并不希望搜索算法一下就把所有结果枚举出来(消耗的时间亦太昂贵),有可能我们只需要搜索结果的集合中前三个结果,如果不满意再搜索三个,诸如此类...

那肿么办呢?................是时候祭出yield小魔杖了゚ ∀゚)ノ

下述版本姑且称之为lazy,看上去和v3很像(其实它俩在语义上是几乎等同的

def check_lazy(trie, word, path='', tol=1):
    if tol < 0:
        pass
    elif word == '':
        if None in trie:
            yield path
        # 增加词尾字母
        for k in trie:
            if k is not None:
                yield from check_lazy(trie[k], '', path + k, tol - 1)
    else:
        if word[0] in trie:
            # 首字母匹配成功
            yield from check_lazy(trie[word[0]], word[1:], path+word[0], tol)
        # 分情形继续搜索(相当于保留待探索的回溯分支)
        for k in trie:
            if k is not None and k != word[0]:
                # 用可能正确的字母置换首字母
                yield from check_lazy(trie[k], word[1:], path+k, tol-1)
                # 插入可能正确的字母作为首字母
                yield from check_lazy(trie[k], word, path+k, tol-1)
        # 跳过余词首字母
        yield from check_lazy(trie, word[1:], path, tol-1)
        # 交换原词头两个字母
        if len(word) > 1:
            yield from check_lazy(trie, word[1]+word[0]+word[2:], path, tol-1)

不借助任何容器对象,我们近乎声明式地使用递归子序列拼接成了一个序列。

[新手注释] yield是什么意思呢?就是程序暂停在这里了,返回给你一个结果,然后当你调用next的时候,它从暂停的位置继续走,直到有下个结果然后再暂停。要理解yield,你得先理解yield... Nonono,你得先理解iter函数和next函数,然后再深入理解for循环,具体内容童鞋们可以看官方文档。而yield from x即相当于for y in x: yield y。

给刚认识yield的童鞋一个小科普,顺便回忆一下组合数C(n,m)的定义即

C(n, m) = C(n-1, m-1) + C(n-1, m)

如果我们把C视为根据n和m确定的集合,加号视为并集,利用下面这个generator我们可以懒惰地逐步获取所有组合元素:

def combinations(seq, m):
    if m > len(seq):
        raise ValueError('Cannot choose more than sequence has.')
    elif m == 0:
        yield ()
    elif m == len(seq):
        yield tuple(seq)
    else:
        for p in combinations(seq[1:], m-1):
            yield (seq[0],) + p
        yield from combinations(seq[1:], m)

for combi in combinations('abcde', 2): 
    print(combi)

可以看到,generator结构精准地反映了集合运算的特征,而且蕴含了对元素进行映射的逻辑,可读性非常强。

OK,代码到此为止。利用next函数,我们可以懒惰地获取查找结果。

In [54]: words = ['hell', 'hello', 'hela', 'helmut', 'dome']

In [55]: t = make_trie(words)

In [57]: c = check_lazy(t, 'hell')

In [58]: next(c)
Out[58]: 'hell'

In [59]: next(c)
Out[59]: 'hello'

In [60]: next(c)
Out[60]: 'hela'

话说回来,lazy的一个问题在于我们不能提前预测并剔除重复的元素。你可以采用一个小利器decorator,修饰一个generator,保证结果不重复。

from functools import wraps

def uniq(func):
    @wraps(func)
    def _func(*a, **kw): 
        seen = set()
        it = func(*a, **kw)
        while 1: 
            x = next(it) 
            if x not in seen:
                yield x
                seen.add(x) 
    return _func

这个url打开的文件包含常用英语词汇,可以用来测试代码:

In [10]: import urllib

In [11]: f = urllib.request.urlopen("https://raw.githubusercontent.com/eneko/data-repository/master/data/words.txt")

# 去除换行符
In [12]: t = make_trie(line.decode().strip() for line in f.readlines())

In [13]: f.close()

----------------------分-割-线-四-----------------------------

最后的最后,Python中递归是很昂贵的,但是递归的优势在于描述问题。为了追求极致性能,我们可以把递归转成迭代,把去除重复的逻辑直接代入进来,于是有了这个v4版本:

from collections import deque

def check_iter(trie, word, tol=1):
    seen = set()
    q = deque([(trie, word, '', tol)])
    while q:
        trie, word, path, tol = q.popleft()
        if word == '':
            if None in trie:
                if path not in seen:
                    seen.add(path)
                    yield path
            if tol > 0:
                for k in trie:
                    if k is not None:
                        q.appendleft((trie[k], '', path+k, tol-1))
        else:
            if word[0] in trie:
                q.appendleft((trie[word[0]], word[1:], path+word[0], tol))
            if tol > 0:
                for k in trie.keys():
                    if k is not None and k != word[0]:
                        q.append((trie[k], word[1:], path+k, tol-1))
                        q.append((trie[k], word, path+k, tol-1))
                q.append((trie, word[1:], path, tol-1))
                if len(word) > 1:
                    q.append((trie, word[1]+word[0]+word[2:], path, tol-1)) 

可以看到,转为迭代方式后我们仍然可以最大程度保留递归风格的程序形状,但也提供了更强的灵活性(对于递归,相当于我们只能用栈来实现这个q)。基于这种迭代程序的结构,如果你有词频数据,可以用该数据维持一个最优堆q,甚至可以是根据上下文自动调整词频的动态堆,维持高频词汇在堆顶,为词语修正节省不少性能。这里就不深入了。

【可选的一步】我们在对单词进行纠正的时候往往倾向于认为首字母是无误的,利用这个现象可以减轻不少搜索压力,花费的时间可以少数倍。

def check_head_fixed(trie, word, tol=1):
    for p in check_lazy(trie[word[0]], word[1:], tol=tol):
        yield word[0] + p

最终我们简单地benchmark一下:

In [18]: list(check_head_fixed(trie, 'misella', tol=2))
Out[18]:
['micellar',
 'malella',
 'mesilla',
 'morella',
 'mysell',
 'micelle',
 'milla',
 'misally',
 'mistell',
 'miserly']

In [19]: %timeit list(check_head_fixed(trie, 'misella', tol=2))
1.52 ms ± 2.84 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

在Win10的i7上可以在两毫秒左右返回所有结果,可以说令人满意。

二、蜜蜂每天检查可以不

蜜蜂是我们生活中非常重要的昆虫之一,它们不仅能够采集花蜜,制作蜂蜜,还可以帮助花朵传粉,促进植物的繁殖。蜜蜂社会高度有组织,分工明确,每只蜜蜂承担着特定的任务,其中之一就是每天的检查工作。

为什么蜜蜂每天要进行检查?

蜜蜂每天进行检查有着重要的意义和作用。首先,蜜蜂需要确保蜂巢内的环境和资源充足,以供整个蜂巢内的成员生存和繁衍。蜜蜂会检查蜂巢内的温度、湿度等环境因素是否适宜,还会检查蜂巢中的食物供应是否充足。其次,蜜蜂需要确保蜂巢的安全和免受病害侵扰。

蜜蜂每天的检查内容

蜜蜂每天的检查内容主要包括以下几个方面:

  1. 巢穴检查:蜜蜂会检查巢穴的结构是否完好,并修复可能存在的损坏。巢穴的结构稳固和完整性对整个蜂巢的运行至关重要。
  2. 食物供应检查:蜜蜂会检查花蜜储存量是否充足,并将花蜜逐渐转化为蜂蜜。如果食物供应不足,蜜蜂会及时寻找新的花朵采集花蜜。
  3. 病虫害检查:蜜蜂会检查蜂巢内是否出现病虫害,并采取相应的措施进行防治,以保证整个蜂群的健康。
  4. 雄蜂、雌蜂检查:蜜蜂会检查蜂巢内是否需要繁殖新的蜜蜂,并调控雄蜂和雌蜂的比例,以保持蜂巢内的平衡。
  5. 蜂蜜质量检查:蜜蜂会检查蜂蜜的质量,并确保蜂巢内的蜂蜜干燥且符合标准。

蜜蜂检查的重要性

蜜蜂每天的检查工作对整个蜂巢的运行至关重要。首先,巢穴检查可以保证整个蜂巢的稳固,防止蜂巢的损坏和坍塌,从而保护蜜蜂的生命安全。其次,食物供应检查可以确保蜂巢内的成员有足够的食物供应,避免食物短缺导致蜜蜂饥饿而死亡。同时,病虫害检查可以及时发现和控制病虫害,保持整个蜂群的健康和繁衍。

此外,蜜蜂每天的检查还有助于维持蜂巢内的生态平衡和社会秩序。通过调控雄蜂和雌蜂的比例,蜜蜂可以控制蜂巢内的种群数量和结构,以适应不同的环境条件。而蜂蜜质量检查则可以保证蜂巢内的蜂蜜的营养价值和品质,为蜜蜂提供充足的能量和营养。

结语

蜜蜂每天进行检查,是它们生活中不可或缺的一部分。这项工作保证了蜂巢内的秩序和稳定,保障了整个蜂群的生存和繁衍。正是蜜蜂每天的默默努力,才有了我们日常食用的美味蜂蜜。值得我们对它们的辛勤付出表示敬意,并加强保护和关注蜜蜂的生存环境,共同守护着这个与人类息息相关的生态系统。

三、空调不制冷怎么检查?

1.首先要检查空调是否正常运转,包括室内机和室外机,尤其是要检查室外机的压缩机,只要有其中一个没有正常运转,那么就得请师傅来维修一下。

2.然后要检查空调的过滤网,如果空调正常启动,但是却散不出冷气,很有可能是过滤网灰尘太多,最好用那种专门的过滤网清洗剂喷一下清洗。

3.再检查一下制冷剂是不是不足,也就是所谓的氟利昂,如果不足了的话就要加氟利昂,同时检查冷循环系统是不是出现了漏气现象,如果出现了漏气现象的就得补漏。

4.如果没有上述问题,空调也能正常运转,那么要检查空调风扇运转情况,假如风扇的风量非常小,那么可能是风扇电机老旧,要换新的风扇电机。

5.如果空调的通风管安装的比较长,可能会导致将室内热量传到室外的效果比较差,从而出现制冷不是很明显的情况,最好找个好一点的位置重新安装,争取将通风管最短化。

6.如果空调运行正常,而且其他一切也都完好,但是房间里感觉不到冷气,只有凑到空调吹风口上才有冷气,很可能是因为空调的马力太小,而房间的空间太大导致,这时最好换一个马力较大的空调。

四、怎么检查空调不制冷?

空调不制冷的检查方法有:

检查散热器是否需要清洗。空调需要换热器进行室内外的热量转移,当换热器上覆盖大量灰尘、蛛网等垃圾时,就无法进行有效散热,导致空调制冷效果差,需要对内机和外机的换热器进行清洗。

检查电压是否稳定。用电高峰期供电电压会不稳定,达不到空调运转所需的正常电压,会导致空调不制冷或效果差,高温期间大量空调同时运转更容易出现这种现象。这种问题个人是无法解决的,可以选用变频空调

五、做胃检查空腹不?

胃部检查需要禁食。目前常用的胃检查方法包括碳13呼气试验、碳14呼气试验、上消化道钡餐、胃镜和胃功能检查。所有这些检测方法都需要在禁食禁水的条件下进行,否则一方面会影响检测的准确性,干扰检测结果,出现假阳性。另一方面,它会影响胃粘膜的观察和病理变化,还会导致胃内容物在恶心呕吐的情况下回流到食道,导致窒息或肺部感染。

六、机动车检查合格标志可以不贴不?

3月1日起,3项试点的新措施已经开始执行。其中一项就是纸质机动车检验标志电子化,跟着元贝驾考小编一起来看看吧!

一:机动车检验标志电子化有哪些城市?

试点城市:北京、天津、上海、重庆、哈尔滨、南京、杭州、宁波、济南、株洲、深圳、海口、成都、贵阳、玉溪、乌鲁木齐等16个城市。

领取方法:驾驶人可以通过互联网交通安全综合服务平台或登录“交管12123”手机APP,已经领取检验标志电子凭证的机动车,无需在窗口粘贴纸质标志。

二、特殊机动车如何办理?

办理注册、转移、变更登记的机动车:驾驶人在办理完机动车登记发放纸质检验标志之后。系统会自动生成电子凭证,驾驶人可通过上述的领取方法进行领取、查看。

六年内免检的机动车:机动车车主可直接通过上述的领取方法进行领取、查看。如需要纸质标志凭证,可以自行前往车管所领取或邮寄。

线上检验的机动车:机动车检验合格之后,在领取纸质标志的同时,机动车车主可以在网上查看、下载检验标志电子凭证。如果在检验有效期内的机动车,可直接通过上述的领取方法进行领取、查看。

检验标志纸质凭证遗失或摧毁的机动车:不需要办理补领检验标志,可直接通过上述的领取方法进行领取、查看。

三、检验标志电子凭证遇检测如何出示?

检验标志电子凭证出示方法:在线出示、离线出示、打印出示。

在线出示:机动车车主直接登录“交管12123”手机APP,在线查看出示机动车检验标志电子凭证。

离线出示:提前登录互联网交通安全综合服务平台或登录“交管12123”手机APP,将检验标志电子凭证保存到手机中,需要时出示。

打印出示:保存下的电子凭证可直接打印出,使用时出示即可。

元贝小编在这里提醒所有驾驶人,机动车的检验标志电子凭证与纸质凭证具有同等效力,遇检查时主动出示。

七、mahout面试题?

之前看了Mahout官方示例 20news 的调用实现;于是想根据示例的流程实现其他例子。网上看到了一个关于天气适不适合打羽毛球的例子。

训练数据:

Day Outlook Temperature Humidity Wind PlayTennis

D1 Sunny Hot High Weak No

D2 Sunny Hot High Strong No

D3 Overcast Hot High Weak Yes

D4 Rain Mild High Weak Yes

D5 Rain Cool Normal Weak Yes

D6 Rain Cool Normal Strong No

D7 Overcast Cool Normal Strong Yes

D8 Sunny Mild High Weak No

D9 Sunny Cool Normal Weak Yes

D10 Rain Mild Normal Weak Yes

D11 Sunny Mild Normal Strong Yes

D12 Overcast Mild High Strong Yes

D13 Overcast Hot Normal Weak Yes

D14 Rain Mild High Strong No

检测数据:

sunny,hot,high,weak

结果:

Yes=》 0.007039

No=》 0.027418

于是使用Java代码调用Mahout的工具类实现分类。

基本思想:

1. 构造分类数据。

2. 使用Mahout工具类进行训练,得到训练模型。

3。将要检测数据转换成vector数据。

4. 分类器对vector数据进行分类。

接下来贴下我的代码实现=》

1. 构造分类数据:

在hdfs主要创建一个文件夹路径 /zhoujainfeng/playtennis/input 并将分类文件夹 no 和 yes 的数据传到hdfs上面。

数据文件格式,如D1文件内容: Sunny Hot High Weak

2. 使用Mahout工具类进行训练,得到训练模型。

3。将要检测数据转换成vector数据。

4. 分类器对vector数据进行分类。

这三步,代码我就一次全贴出来;主要是两个类 PlayTennis1 和 BayesCheckData = =》

package myTesting.bayes;

import org.apache.hadoop.conf.Configuration;

import org.apache.hadoop.fs.FileSystem;

import org.apache.hadoop.fs.Path;

import org.apache.hadoop.util.ToolRunner;

import org.apache.mahout.classifier.naivebayes.training.TrainNaiveBayesJob;

import org.apache.mahout.text.SequenceFilesFromDirectory;

import org.apache.mahout.vectorizer.SparseVectorsFromSequenceFiles;

public class PlayTennis1 {

private static final String WORK_DIR = "hdfs://192.168.9.72:9000/zhoujianfeng/playtennis";

/*

* 测试代码

*/

public static void main(String[] args) {

//将训练数据转换成 vector数据

makeTrainVector();

//产生训练模型

makeModel(false);

//测试检测数据

BayesCheckData.printResult();

}

public static void makeCheckVector(){

//将测试数据转换成序列化文件

try {

Configuration conf = new Configuration();

conf.addResource(new Path("/usr/local/hadoop/conf/core-site.xml"));

String input = WORK_DIR+Path.SEPARATOR+"testinput";

String output = WORK_DIR+Path.SEPARATOR+"tennis-test-seq";

Path in = new Path(input);

Path out = new Path(output);

FileSystem fs = FileSystem.get(conf);

if(fs.exists(in)){

if(fs.exists(out)){

//boolean参数是,是否递归删除的意思

fs.delete(out, true);

}

SequenceFilesFromDirectory sffd = new SequenceFilesFromDirectory();

String[] params = new String[]{"-i",input,"-o",output,"-ow"};

ToolRunner.run(sffd, params);

}

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

System.out.println("文件序列化失败!");

System.exit(1);

}

//将序列化文件转换成向量文件

try {

Configuration conf = new Configuration();

conf.addResource(new Path("/usr/local/hadoop/conf/core-site.xml"));

String input = WORK_DIR+Path.SEPARATOR+"tennis-test-seq";

String output = WORK_DIR+Path.SEPARATOR+"tennis-test-vectors";

Path in = new Path(input);

Path out = new Path(output);

FileSystem fs = FileSystem.get(conf);

if(fs.exists(in)){

if(fs.exists(out)){

//boolean参数是,是否递归删除的意思

fs.delete(out, true);

}

SparseVectorsFromSequenceFiles svfsf = new SparseVectorsFromSequenceFiles();

String[] params = new String[]{"-i",input,"-o",output,"-lnorm","-nv","-wt","tfidf"};

ToolRunner.run(svfsf, params);

}

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

System.out.println("序列化文件转换成向量失败!");

System.out.println(2);

}

}

public static void makeTrainVector(){

//将测试数据转换成序列化文件

try {

Configuration conf = new Configuration();

conf.addResource(new Path("/usr/local/hadoop/conf/core-site.xml"));

String input = WORK_DIR+Path.SEPARATOR+"input";

String output = WORK_DIR+Path.SEPARATOR+"tennis-seq";

Path in = new Path(input);

Path out = new Path(output);

FileSystem fs = FileSystem.get(conf);

if(fs.exists(in)){

if(fs.exists(out)){

//boolean参数是,是否递归删除的意思

fs.delete(out, true);

}

SequenceFilesFromDirectory sffd = new SequenceFilesFromDirectory();

String[] params = new String[]{"-i",input,"-o",output,"-ow"};

ToolRunner.run(sffd, params);

}

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

System.out.println("文件序列化失败!");

System.exit(1);

}

//将序列化文件转换成向量文件

try {

Configuration conf = new Configuration();

conf.addResource(new Path("/usr/local/hadoop/conf/core-site.xml"));

String input = WORK_DIR+Path.SEPARATOR+"tennis-seq";

String output = WORK_DIR+Path.SEPARATOR+"tennis-vectors";

Path in = new Path(input);

Path out = new Path(output);

FileSystem fs = FileSystem.get(conf);

if(fs.exists(in)){

if(fs.exists(out)){

//boolean参数是,是否递归删除的意思

fs.delete(out, true);

}

SparseVectorsFromSequenceFiles svfsf = new SparseVectorsFromSequenceFiles();

String[] params = new String[]{"-i",input,"-o",output,"-lnorm","-nv","-wt","tfidf"};

ToolRunner.run(svfsf, params);

}

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

System.out.println("序列化文件转换成向量失败!");

System.out.println(2);

}

}

public static void makeModel(boolean completelyNB){

try {

Configuration conf = new Configuration();

conf.addResource(new Path("/usr/local/hadoop/conf/core-site.xml"));

String input = WORK_DIR+Path.SEPARATOR+"tennis-vectors"+Path.SEPARATOR+"tfidf-vectors";

String model = WORK_DIR+Path.SEPARATOR+"model";

String labelindex = WORK_DIR+Path.SEPARATOR+"labelindex";

Path in = new Path(input);

Path out = new Path(model);

Path label = new Path(labelindex);

FileSystem fs = FileSystem.get(conf);

if(fs.exists(in)){

if(fs.exists(out)){

//boolean参数是,是否递归删除的意思

fs.delete(out, true);

}

if(fs.exists(label)){

//boolean参数是,是否递归删除的意思

fs.delete(label, true);

}

TrainNaiveBayesJob tnbj = new TrainNaiveBayesJob();

String[] params =null;

if(completelyNB){

params = new String[]{"-i",input,"-el","-o",model,"-li",labelindex,"-ow","-c"};

}else{

params = new String[]{"-i",input,"-el","-o",model,"-li",labelindex,"-ow"};

}

ToolRunner.run(tnbj, params);

}

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

System.out.println("生成训练模型失败!");

System.exit(3);

}

}

}

package myTesting.bayes;

import java.io.IOException;

import java.util.HashMap;

import java.util.Map;

import org.apache.commons.lang.StringUtils;

import org.apache.hadoop.conf.Configuration;

import org.apache.hadoop.fs.Path;

import org.apache.hadoop.fs.PathFilter;

import org.apache.hadoop.io.IntWritable;

import org.apache.hadoop.io.LongWritable;

import org.apache.hadoop.io.Text;

import org.apache.mahout.classifier.naivebayes.BayesUtils;

import org.apache.mahout.classifier.naivebayes.NaiveBayesModel;

import org.apache.mahout.classifier.naivebayes.StandardNaiveBayesClassifier;

import org.apache.mahout.common.Pair;

import org.apache.mahout.common.iterator.sequencefile.PathType;

import org.apache.mahout.common.iterator.sequencefile.SequenceFileDirIterable;

import org.apache.mahout.math.RandomAccessSparseVector;

import org.apache.mahout.math.Vector;

import org.apache.mahout.math.Vector.Element;

import org.apache.mahout.vectorizer.TFIDF;

import com.google.common.collect.ConcurrentHashMultiset;

import com.google.common.collect.Multiset;

public class BayesCheckData {

private static StandardNaiveBayesClassifier classifier;

private static Map<String, Integer> dictionary;

private static Map<Integer, Long> documentFrequency;

private static Map<Integer, String> labelIndex;

public void init(Configuration conf){

try {

String modelPath = "/zhoujianfeng/playtennis/model";

String dictionaryPath = "/zhoujianfeng/playtennis/tennis-vectors/dictionary.file-0";

String documentFrequencyPath = "/zhoujianfeng/playtennis/tennis-vectors/df-count";

String labelIndexPath = "/zhoujianfeng/playtennis/labelindex";

dictionary = readDictionnary(conf, new Path(dictionaryPath));

documentFrequency = readDocumentFrequency(conf, new Path(documentFrequencyPath));

labelIndex = BayesUtils.readLabelIndex(conf, new Path(labelIndexPath));

NaiveBayesModel model = NaiveBayesModel.materialize(new Path(modelPath), conf);

classifier = new StandardNaiveBayesClassifier(model);

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

System.out.println("检测数据构造成vectors初始化时报错。。。。");

System.exit(4);

}

}

/**

* 加载字典文件,Key: TermValue; Value:TermID

* @param conf

* @param dictionnaryDir

* @return

*/

private static Map<String, Integer> readDictionnary(Configuration conf, Path dictionnaryDir) {

Map<String, Integer> dictionnary = new HashMap<String, Integer>();

PathFilter filter = new PathFilter() {

@Override

public boolean accept(Path path) {

String name = path.getName();

return name.startsWith("dictionary.file");

}

};

for (Pair<Text, IntWritable> pair : new SequenceFileDirIterable<Text, IntWritable>(dictionnaryDir, PathType.LIST, filter, conf)) {

dictionnary.put(pair.getFirst().toString(), pair.getSecond().get());

}

return dictionnary;

}

/**

* 加载df-count目录下TermDoc频率文件,Key: TermID; Value:DocFreq

* @param conf

* @param dictionnaryDir

* @return

*/

private static Map<Integer, Long> readDocumentFrequency(Configuration conf, Path documentFrequencyDir) {

Map<Integer, Long> documentFrequency = new HashMap<Integer, Long>();

PathFilter filter = new PathFilter() {

@Override

public boolean accept(Path path) {

return path.getName().startsWith("part-r");

}

};

for (Pair<IntWritable, LongWritable> pair : new SequenceFileDirIterable<IntWritable, LongWritable>(documentFrequencyDir, PathType.LIST, filter, conf)) {

documentFrequency.put(pair.getFirst().get(), pair.getSecond().get());

}

return documentFrequency;

}

public static String getCheckResult(){

Configuration conf = new Configuration();

conf.addResource(new Path("/usr/local/hadoop/conf/core-site.xml"));

String classify = "NaN";

BayesCheckData cdv = new BayesCheckData();

cdv.init(conf);

System.out.println("init done...............");

Vector vector = new RandomAccessSparseVector(10000);

TFIDF tfidf = new TFIDF();

//sunny,hot,high,weak

Multiset<String> words = ConcurrentHashMultiset.create();

words.add("sunny",1);

words.add("hot",1);

words.add("high",1);

words.add("weak",1);

int documentCount = documentFrequency.get(-1).intValue(); // key=-1时表示总文档数

for (Multiset.Entry<String> entry : words.entrySet()) {

String word = entry.getElement();

int count = entry.getCount();

Integer wordId = dictionary.get(word); // 需要从dictionary.file-0文件(tf-vector)下得到wordID,

if (StringUtils.isEmpty(wordId.toString())){

continue;

}

if (documentFrequency.get(wordId) == null){

continue;

}

Long freq = documentFrequency.get(wordId);

double tfIdfValue = tfidf.calculate(count, freq.intValue(), 1, documentCount);

vector.setQuick(wordId, tfIdfValue);

}

// 利用贝叶斯算法开始分类,并提取得分最好的分类label

Vector resultVector = classifier.classifyFull(vector);

double bestScore = -Double.MAX_VALUE;

int bestCategoryId = -1;

for(Element element: resultVector.all()) {

int categoryId = element.index();

double score = element.get();

System.out.println("categoryId:"+categoryId+" score:"+score);

if (score > bestScore) {

bestScore = score;

bestCategoryId = categoryId;

}

}

classify = labelIndex.get(bestCategoryId)+"(categoryId="+bestCategoryId+")";

return classify;

}

public static void printResult(){

System.out.println("检测所属类别是:"+getCheckResult());

}

}

八、webgis面试题?

1. 请介绍一下WebGIS的概念和作用,以及在实际应用中的优势和挑战。

WebGIS是一种基于Web技术的地理信息系统,通过将地理数据和功能以可视化的方式呈现在Web浏览器中,实现地理空间数据的共享和分析。它可以用于地图浏览、空间查询、地理分析等多种应用场景。WebGIS的优势包括易于访问、跨平台、实时更新、可定制性强等,但也面临着数据安全性、性能优化、用户体验等挑战。

2. 请谈谈您在WebGIS开发方面的经验和技能。

我在WebGIS开发方面有丰富的经验和技能。我熟悉常用的WebGIS开发框架和工具,如ArcGIS API for JavaScript、Leaflet、OpenLayers等。我能够使用HTML、CSS和JavaScript等前端技术进行地图展示和交互设计,并能够使用后端技术如Python、Java等进行地理数据处理和分析。我还具备数据库管理和地理空间数据建模的能力,能够设计和优化WebGIS系统的架构。

3. 请描述一下您在以往项目中使用WebGIS解决的具体问题和取得的成果。

在以往的项目中,我使用WebGIS解决了许多具体问题并取得了显著的成果。例如,在一次城市规划项目中,我开发了一个基于WebGIS的交通流量分析系统,帮助规划师们评估不同交通方案的效果。另外,在一次环境监测项目中,我使用WebGIS技术实现了实时的空气质量监测和预警系统,提供了准确的空气质量数据和可视化的分析结果,帮助政府和公众做出相应的决策。

4. 请谈谈您对WebGIS未来发展的看法和期望。

我认为WebGIS在未来会继续发展壮大。随着云计算、大数据和人工智能等技术的不断进步,WebGIS将能够处理更大规模的地理数据、提供更丰富的地理分析功能,并与其他领域的技术进行深度融合。我期望未来的WebGIS能够更加智能化、个性化,为用户提供更好的地理信息服务,助力各行各业的决策和发展。

九、freertos面试题?

这块您需要了解下stm32等单片机的基本编程和简单的硬件设计,最好能够了解模电和数电相关的知识更好,还有能够会做操作系统,简单的有ucos,freeRTOS等等。最好能够使用PCB画图软件以及keil4等软件。希望对您能够有用。

十、paas面试题?

1.负责区域大客户/行业客户管理系统销售拓展工作,并完成销售流程;

2.维护关键客户关系,与客户决策者保持良好的沟通;

3.管理并带领团队完成完成年度销售任务。

相关资讯
热门频道

Copyright © 2024 招聘街 滇ICP备2024020316号-38