[?k?uk?us tu: D](扣扣斯兔地)最后的d 為字母D的發(fā)音2D 指的是 二維(two dimension)cocos2d是一個(gè)開源框架,用于構(gòu)建2D游戲、演示程序和其他圖形界面交互應(yīng)用等。
Cocos2d是開發(fā)2D游戲的一款強(qiáng)大的游戲引擎,它提供了豐富的功能和靈活的工具,使開發(fā)者能夠輕松地創(chuàng)造出令人驚嘆的游戲作品。然而,對(duì)于新手開發(fā)者來說,Cocos2d的文檔可能會(huì)讓人感到有些困惑。在本篇博客中,我們將深入研究Cocos2d文檔,幫助您更好地理解和使用這個(gè)強(qiáng)大的引擎。
Cocos2d的文檔是以官方網(wǎng)站為主要平臺(tái)進(jìn)行發(fā)布和更新。您可以通過訪問官方網(wǎng)站來獲取最新的文檔版本。文檔主要包含以下幾個(gè)部分:
了解文檔的結(jié)構(gòu)對(duì)于快速找到需要的信息非常重要。同時(shí),Cocos2d文檔還提供了搜索功能,您可以通過關(guān)鍵字搜索來查找特定的內(nèi)容。
入門指南是Cocos2d文檔的第一部分,它為新手開發(fā)者提供了一個(gè)系統(tǒng)的學(xué)習(xí)路徑。在入門指南中,您將學(xué)習(xí)到以下內(nèi)容:
通過入門指南,您將建立起對(duì)Cocos2d的基本理解,為后續(xù)的開發(fā)工作打下堅(jiān)實(shí)的基礎(chǔ)。
API參考是Cocos2d文檔的核心部分,它提供了關(guān)于Cocos2d引擎各個(gè)模塊和類的詳細(xì)說明。在API參考中,您可以找到以下內(nèi)容:
API參考是Cocos2d文檔中最重要的部分之一,它提供了豐富的信息和實(shí)例,幫助您深入理解和熟練使用Cocos2d的各個(gè)功能。
教程和示例是Cocos2d文檔中另一個(gè)非常有價(jià)值的部分。通過教程和示例,您可以學(xué)習(xí)到如何實(shí)現(xiàn)特定的功能或者解決常見的問題。Cocos2d文檔中的教程和示例包括但不限于以下內(nèi)容:
通過教程和示例,您可以通過實(shí)際的代碼和案例加深對(duì)Cocos2d的理解,提高自己的開發(fā)能力。
Cocos2d擁有一個(gè)強(qiáng)大的社區(qū)支持,您可以通過訪問官方網(wǎng)站的社區(qū)支持頁(yè)面來獲取更多的資源和幫助。社區(qū)支持包括以下內(nèi)容:
通過社區(qū)支持,您可以擴(kuò)展自己的知識(shí)和技能,解決開發(fā)過程中遇到的問題,還可以結(jié)識(shí)到來自世界各地的其他Cocos2d開發(fā)者。
Cocos2d文檔是學(xué)習(xí)和使用Cocos2d引擎的重要工具。通過深入理解Cocos2d文檔的結(jié)構(gòu)和內(nèi)容,您將更好地掌握這個(gè)強(qiáng)大的游戲引擎,實(shí)現(xiàn)您的創(chuàng)意和夢(mèng)想。當(dāng)您遇到問題或者需要更多幫助時(shí),不要忘記利用Cocos2d社區(qū)支持的資源,與其他開發(fā)者一起成長(zhǎng)和進(jìn)步。祝愿您在Cocos2d的開發(fā)之路上取得巨大的成功!
歡迎來到本篇博客,今天我們將聚焦于Cocos2d文檔。無論您是初學(xué)者還是有經(jīng)驗(yàn)的開發(fā)者,文檔是學(xué)習(xí)和使用Cocos2d游戲引擎的重要資源。本文將引導(dǎo)您了解Cocos2d文檔的重要性以及如何利用文檔來提升您的開發(fā)效率。
Cocos2d是一個(gè)功能強(qiáng)大且廣受歡迎的游戲開發(fā)框架,但它也是一個(gè)復(fù)雜的工具。文檔為使用Cocos2d的開發(fā)者提供了必要的指導(dǎo)和資源,以便更好地理解框架的各個(gè)方面。文檔包含了關(guān)于Cocos2d的詳細(xì)說明、示例代碼、API參考等重要信息。
使用文檔可以幫助您快速入門并提高開發(fā)效率。無論您是在探索Cocos2d的基本概念還是處理高級(jí)特性,文檔都是您的良好指南。
使用Cocos2d文檔需要一些技巧,下面是一些利用文檔的實(shí)用建議:
以下是一些技巧,可幫助您更有效地利用Cocos2d文檔:
Cocos2d文檔對(duì)于開發(fā)Cocos2d游戲的人們來說是一個(gè)寶貴的資源。它提供了全面的指導(dǎo)、示例代碼和API參考,幫助您更好地理解和應(yīng)用Cocos2d的各個(gè)方面。通過閱讀官方文檔、查看示例代碼、參考API文檔以及參與社區(qū),您可以更快地掌握Cocos2d的技巧和技術(shù),從而提高開發(fā)效率和質(zhì)量。
因此,無論您是剛起步還是有一定經(jīng)驗(yàn)的Cocos2d開發(fā)者,花費(fèi)時(shí)間和精力去借助Cocos2d文檔進(jìn)行學(xué)習(xí)是非常值得的。希望這篇博客能夠幫助您更好地利用Cocos2d文檔,并取得開發(fā)上的成功。
作為游戲開發(fā)領(lǐng)域兩大知名引擎,Unity和Cocos2d-X都擁有自己的一席之地。對(duì)于初學(xué)者來說,選擇哪個(gè)引擎進(jìn)行學(xué)習(xí)和開發(fā)是一個(gè)重要的抉擇。本文將從新手角度出發(fā),探討Unity與Cocos2d-X的優(yōu)劣勢(shì)以及哪個(gè)更適合新手游戲開發(fā)者入門學(xué)習(xí)。
Unity:
Cocos2d-X:
了解了Unity和Cocos2d-X各自的特點(diǎn)后,新手開發(fā)者可以根據(jù)自身需求和興趣選擇合適的引擎進(jìn)行學(xué)習(xí)和開發(fā)。若是希望快速上手、跨平臺(tái)部署游戲,那么Unity可能更適合;若是對(duì)2D游戲開發(fā)感興趣、并且喜歡深入學(xué)習(xí)底層原理,那么Cocos2d-X可能更適合。
學(xué)習(xí)cocos2d-x需要學(xué)習(xí)計(jì)算機(jī)語(yǔ)言,cocos2d有多個(gè)語(yǔ)言的版本,建議用C++因?yàn)榭缙脚_(tái)比較容易些,javaC#object-c的版本都有,html5的也有,但是有局限性。計(jì)算機(jī)語(yǔ)言(computerlanguage)指用于人與計(jì)算機(jī)之間通訊的語(yǔ)言,是人與計(jì)算機(jī)之間傳遞信息的媒介。計(jì)算機(jī)系統(tǒng)最大特征是指令通過一種語(yǔ)言傳達(dá)給機(jī)器,為了使電子計(jì)算機(jī)進(jìn)行各種工作,就需要有一套用以編寫計(jì)算機(jī)程序的數(shù)字、字符和語(yǔ)法規(guī)劃,由這些字符和語(yǔ)法規(guī)則組成計(jì)算機(jī)各種指令(或各種語(yǔ)句),這些就是計(jì)算機(jī)能接受的語(yǔ)言。計(jì)算機(jī)語(yǔ)言的概念比通用的編程語(yǔ)言要更廣泛,例如,HTML是置標(biāo)語(yǔ)言,也是計(jì)算機(jī)語(yǔ)言,但并不是編程語(yǔ)言。
游戲引擎是游戲開發(fā)中至關(guān)重要的工具之一,它提供了開發(fā)人員創(chuàng)建游戲所需的基本框架和功能。在眾多游戲引擎中,Unity與Cocos2d 等引擎?zhèn)涫荛_發(fā)者青睞。
Unity 是一款功能強(qiáng)大的跨平臺(tái)游戲引擎,提供了豐富的開發(fā)工具和資源,使開發(fā)者能夠輕松創(chuàng)建高質(zhì)量的游戲作品。其靈活性和易用性使其成為業(yè)內(nèi)領(lǐng)先的游戲開發(fā)引擎之一。
Cocos2d 是另一款備受好評(píng)的游戲引擎,特別適合2D游戲的開發(fā)。其簡(jiǎn)單易學(xué)的特點(diǎn)吸引了許多開發(fā)者選擇使用它來開發(fā)手機(jī)游戲和小型游戲項(xiàng)目。
決定選擇哪款游戲引擎取決于您的項(xiàng)目需求和個(gè)人偏好。如果您的項(xiàng)目需要制作復(fù)雜、高質(zhì)量的3D游戲,Unity可能更適合您;如果您偏愛二維游戲開發(fā),Cocos2d將是不錯(cuò)的選擇。
無論您選擇哪款游戲引擎,都可以通過學(xué)習(xí)相應(yīng)的文檔和教程來提升開發(fā)技能,創(chuàng)作出引人入勝的游戲作品。希望您能根據(jù)個(gè)人需求,選擇一款適合自己的游戲引擎,開發(fā)出令人驚艷的游戲作品!
1. 什么是plist文件格式?這是一種人類可讀的串行化對(duì)象文件,由蘋果公司發(fā)明,最早用于NeXTSTEP系統(tǒng)。詳情看這里: Plist 。cocos2d-x 從 cocos2d-iphone 發(fā)展而來,因此在引擎中大量使用了這種文件格式。
2. 如何編輯plist文件?在 OS X 系統(tǒng)上,XCode 就可以直接打開和編輯plist文件。而在Windows上,我還沒有找到可用的plist編輯軟件。當(dāng)然,plist是基于XML的純文本格式,隨便找個(gè)文本編輯器就可以編輯了。
3. cocos2d-x在哪些地方使用了plist格式?圖像紋理定義文件 將多個(gè)紋理拼在一張大圖上,使用 CCSpriteFrameCache 可以載入這類plist文件; 這里有一個(gè)圖像紋理定義文件的范例: [cocos2d-x]\samples\Cpp\TestCpp\Resources\animations\grossini_family.plist。Label紋理定義文件 作用與圖像紋理定義文件類似,只不過處理的是自己,面向 CCLabelAtlas ; 這里有一個(gè)Label紋理定義文件的范例: [cocos2d-x]\samples\Cpp\TestCpp\Resources\fonts\tuffy_bold_italic-charmap.plist 。幀動(dòng)畫定義 定義一個(gè)或多個(gè)動(dòng)畫中,使用哪些紋理,使用 CCAnimationCache 可以載入這類plist文件; 這里有一個(gè)幀動(dòng)畫定義文件的范例: [cocos2d-x]\samples\Cpp\TestCpp\Resources\animations\animations.plist 。
4. 生成plist文件的工具對(duì)于紋理定義文件來說,它的作用是如何在大圖中找到碎圖的坐標(biāo)。因此很多拼合碎圖的軟件可以在拼合碎圖的同時(shí)生成plist文件。TexturePacker 是所有平臺(tái)上最好用的工具了;Zwoptex 是MAC Only的軟件,我不太喜歡用;SpritePacker 是Windows Only的軟件,功能尚可。
5. 圖像紋理定義文件格式說明cocos2d-x中的紋理定義格式,是以Zwoptex生成的格式為標(biāo)準(zhǔn)的。Zwoptex生成的格式,有4種主要不同的版本:format值為0,代表Flash版本;format值為1,Zwoptex 0.4b以前支持;format值為2,Zwoptex 1.0以后支持,與format1的區(qū)別在于支持旋轉(zhuǎn);format值為3,屬性名稱進(jìn)行了大幅修改,Zwoptes1.0.2之后支持。這3種格式的plist文件,cocos2d-x都能支持,具體的解析代碼在CCSpriteFrameCache::addSpriteFramesWithDictionary 。TexturePacker生成的for cocos2d plist格式與Zwoptex生成的format為2的格式相同。5.1 format為0的plist文件這里貼一個(gè)比較完整plist文件,為了方便描述,其中僅包含一個(gè)frame。
pygame在PC上寫小游戲非常方便,可能幾十行代碼就可以實(shí)現(xiàn)一個(gè)簡(jiǎn)單的小游戲,使用pygame編寫貪吃蛇、俄羅斯放開、連連看等小游戲很簡(jiǎn)單,有Python基礎(chǔ),可能半天就可以完成了!
Cocos2d是一個(gè)面向移動(dòng)設(shè)備的開源游戲框架,它是基于OpenGL ES 2.0的,由Cocos2d-iPhone和Cocos2d-X兩個(gè)分支組成。相對(duì)于其他游戲引擎,Cocos2d具有輕量級(jí)、易學(xué)易用、跨平臺(tái)等優(yōu)點(diǎn),因此很受開發(fā)者的歡迎。
對(duì)于有編程經(jīng)驗(yàn)或有一定編程基礎(chǔ)的開發(fā)者來說,學(xué)習(xí)Cocos2d并不是非常困難。但是對(duì)于完全沒有編程經(jīng)驗(yàn)或編程基礎(chǔ)的開發(fā)者來說,學(xué)習(xí)Cocos2d可能會(huì)比較困難,需要花費(fèi)更長(zhǎng)的時(shí)間和精力。同時(shí),需要學(xué)習(xí)的內(nèi)容也比較多,包括Cocos2d的架構(gòu)、語(yǔ)法、API等等。
不過,自學(xué)Cocos2d是可以學(xué)好的,只需要有耐心、勤奮和堅(jiān)持不懈的學(xué)習(xí)精神。建議初學(xué)者可以通過查看Cocos2d官方文檔、視頻教程、相關(guān)書籍等多種方式來學(xué)習(xí)。另外,加入Cocos2d社區(qū)可以獲得更多的幫助和支持,可以向經(jīng)驗(yàn)豐富的開發(fā)者請(qǐng)教問題,也可以了解最新的技術(shù)和開發(fā)動(dòng)態(tài)。
之前看了Mahout官方示例 20news 的調(diào)用實(shí)現(xiàn);于是想根據(jù)示例的流程實(shí)現(xiàn)其他例子。網(wǎng)上看到了一個(gè)關(guān)于天氣適不適合打羽毛球的例子。
訓(xùn)練數(shù)據(jù):
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
檢測(cè)數(shù)據(jù):
sunny,hot,high,weak
結(jié)果:
Yes=》 0.007039
No=》 0.027418
于是使用Java代碼調(diào)用Mahout的工具類實(shí)現(xiàn)分類。
基本思想:
1. 構(gòu)造分類數(shù)據(jù)。
2. 使用Mahout工具類進(jìn)行訓(xùn)練,得到訓(xùn)練模型。
3。將要檢測(cè)數(shù)據(jù)轉(zhuǎn)換成vector數(shù)據(jù)。
4. 分類器對(duì)vector數(shù)據(jù)進(jìn)行分類。
接下來貼下我的代碼實(shí)現(xiàn)=》
1. 構(gòu)造分類數(shù)據(jù):
在hdfs主要?jiǎng)?chuàng)建一個(gè)文件夾路徑 /zhoujainfeng/playtennis/input 并將分類文件夾 no 和 yes 的數(shù)據(jù)傳到hdfs上面。
數(shù)據(jù)文件格式,如D1文件內(nèi)容: Sunny Hot High Weak
2. 使用Mahout工具類進(jìn)行訓(xùn)練,得到訓(xùn)練模型。
3。將要檢測(cè)數(shù)據(jù)轉(zhuǎn)換成vector數(shù)據(jù)。
4. 分類器對(duì)vector數(shù)據(jù)進(jìn)行分類。
這三步,代碼我就一次全貼出來;主要是兩個(gè)類 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";
/*
* 測(cè)試代碼
*/
public static void main(String[] args) {
//將訓(xùn)練數(shù)據(jù)轉(zhuǎn)換成 vector數(shù)據(jù)
makeTrainVector();
//產(chǎn)生訓(xùn)練模型
makeModel(false);
//測(cè)試檢測(cè)數(shù)據(jù)
BayesCheckData.printResult();
}
public static void makeCheckVector(){
//將測(cè)試數(shù)據(jù)轉(zhuǎn)換成序列化文件
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參數(shù)是,是否遞歸刪除的意思
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);
}
//將序列化文件轉(zhuǎn)換成向量文件
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參數(shù)是,是否遞歸刪除的意思
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("序列化文件轉(zhuǎn)換成向量失??!");
System.out.println(2);
}
}
public static void makeTrainVector(){
//將測(cè)試數(shù)據(jù)轉(zhuǎn)換成序列化文件
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參數(shù)是,是否遞歸刪除的意思
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);
}
//將序列化文件轉(zhuǎn)換成向量文件
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參數(shù)是,是否遞歸刪除的意思
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("序列化文件轉(zhuǎn)換成向量失?。?#34;);
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參數(shù)是,是否遞歸刪除的意思
fs.delete(out, true);
}
if(fs.exists(label)){
//boolean參數(shù)是,是否遞歸刪除的意思
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("生成訓(xùn)練模型失??!");
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("檢測(cè)數(shù)據(jù)構(gòu)造成vectors初始化時(shí)報(bào)錯(cuò)。。。。");
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時(shí)表示總文檔數(shù)
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("檢測(cè)所屬類別是:"+getCheckResult());
}
}