作者:明明如月學長, CSDN 博客專家,螞蟻集團高級 Java 工程師,《性能優(yōu)化方法論》作者、《解鎖大廠思維:剖析《阿里巴巴Java開發(fā)手冊》》、《再學經(jīng)典:《EffectiveJava》獨家解析》專欄作者。
熱門文章推薦:
- (1)《人工智能時代,軟件工程師們將會被取代?》
- (2)《如何寫出高質(zhì)量的文章:從戰(zhàn)略到戰(zhàn)術》
- (3)《我的技術學習方法論》
- (4)《什么? 你還沒用過 Cursor? 智能 AI 代碼生成工具 Cursor 安裝和使用介紹》
- (5)《我的性能方法論》
一、背景
今天有個朋友在技術群里請教,“Java有啥比較好用的語言檢測的工具嗎,只要檢測出非英文就行,目前試了language detector或者字符檢測效果都不理想”
可以看到該同學耗費了很多時間。技術群里展開了大討論,有些朋友說用是否包含英語字母來判斷,該同學說德語、意大利語容易誤判。
那單純靠字符誤判率較高,還有什么更好地方法嗎?
人工智能時代,遇事不決, GPT…
接下來重點展示如何通過 New Bing 幫助我們更快、更好地解決問題。
二、過程
2.1 準備提示詞
我們可以嘗試用中文提問,如果中文得不到很好的答案可以,將我們的需求描述使用 New Bing 或者其他翻譯軟件轉(zhuǎn)為英文:
請用 Java語言編寫了一個程序來判斷一段文本是否為英語,除英語以外的任何語言都會返回 false。
If you write a program in the Java language that determines whether a piece of text is English, any language other than English returns false.
2.2 提問
2.2.1 如何實現(xiàn)
Bing 給了我們?nèi)缦碌拇鸢福?br>
There are different ways to write a program in Java that determines whether a piece of text is English. One possible way is to use a library called Apache Tika that can detect the language of a given text. Here is an example of how to use it:
// import the library
import org.apache.tika.language.LanguageIdentifier;
// create a string with some text
String text = "This is an example of English text";
// create a language identifier object
LanguageIdentifier identifier = new LanguageIdentifier(text);
// get the language code of the text
String language = identifier.getLanguage();
// check if the language code is "en" for English
if (language.equals("en")) {
System.out.println("The text is English");
} else {
System.out.println("The text is not English");
}
This program will print “The text is English” as the output. If you change the text to another language, such as 中文, it will print “The text is not English”.
根據(jù)我們的經(jīng)驗 apache 的庫一般質(zhì)量都相當高,如果這個回答是靠譜的應該可以解決我們的問題。
2.2.2 尋找依賴
代碼有了,我們也知道了哪個庫的哪個類,我們直接讓 New Bing 幫我找到 maven 依賴。
如果您想在Maven項目中使用 org.apache.tika.language.LanguageIdentifier
類,您需要添加 tika-langdetect 模塊的依賴到您的 pom.xml 文件中。
這個模塊提供了語言檢測的功能,可以根據(jù)文本內(nèi)容來判斷語言。以下是 tika-langdetect 模塊的Maven依賴:
<dependency>
<groupId>org.apache.tika</groupId>
<artifactId>tika-langdetect</artifactId>
<version>2.7.0</version>
</dependency>
New Bing 的“了解詳情”部分給出了 maven 倉庫的鏈接,我們可以點進去核實, New Bing 確實給了我們 maven 倉庫的最新版:
但是經(jīng)過驗證,發(fā)現(xiàn)有兩個問題:
(1) New Bing 給出的 maven 依賴,缺少 type 因為 2.7.0 版本為 pom 類型
(2)雖然版本最新,但是這個版本并不存在這個類
我們問 New Bing 這個版本里壓根就沒有 org.apache.tika.language.LanguageIdentifier
這個類,讓它給我們其他版本:
no org.apache.tika.language.LanguageIdentifier in tika-langdetect 2.7.0, can you give me another versioin?
根據(jù)搜索結果,org.apache.tika.language.LanguageIdentifier
類已經(jīng)在 Tika 2.0.0 版本中被移除,取而代之的是org.apache.tika.language.detect.LanguageDetector
接口和其實現(xiàn)類。如果您想繼續(xù)使用 LanguageIdentifier
類,您需要使用Tika 1.x 版本的 tika-langdetect 模塊,如以下的Maven依賴:
<dependency>
<groupId>org.apache.tika</groupId>
<artifactId>tika-langdetect</artifactId>
<version>1.28</version>
</dependency>
這是目前最新的Tika 1.x版本的 tika-langdetect 模塊。如果您想使用 Tika 2.x版本的 tika-langdetect模塊,您需要修改您的代碼,使用 LanguageDetector
接口和其實現(xiàn)類,如 OptimaizeLangDetector
或 CommonsLang3LangDetector
。
2.2.3 尋找優(yōu)化方法
既然最初的代碼示例中的類已經(jīng)廢棄,我們提問如何使用 OptimaizeLangDetector
實現(xiàn)一樣的功能
How do you use OptimaizeLangDetector to implement the same functionality?
根據(jù)搜索結果,OptimaizeLangDetector
是Tika 2.x版本中提供的一個語言檢測的實現(xiàn)類,它使用了一個開源的語言檢測庫。要使用 OptimaizeLangDetector
來實現(xiàn)和 LanguageIdentifier
類相同的功能,您可以參考以下的示例代碼:
// import the library
import org.apache.tika.language.detect.LanguageDetector;
import org.apache.tika.language.detect.LanguageResult;
import org.apache.tika.langdetect.OptimaizeLangDetector;
// create a string with some text
String text = "This is an example of English text";
// create a language detector object
LanguageDetector detector = new OptimaizeLangDetector().loadModels();
// add the text to the detector
detector.addText(text);
// get the language result of the text
LanguageResult result = detector.detect();
// get the language code of the text
String language = result.getLanguage();
// check if the language code is "en" for English
if (language.equals("en")) {
System.out.println("The text is English");
} else {
System.out.println("The text is not English");
}
這個程序也會打印 “The text is English” 作為輸出。如果您改變文本為其他語言,如中文,它會打印"The text is not English"。
但是我們發(fā)現(xiàn)給的代碼還不夠完整,我希望它直接給我放到 isEnglish 方法里,讓它繼續(xù)優(yōu)化:
i want you put this codes in a function like public static boolean isEnglish(String text)
如果您想把這些代碼放在一個函數(shù)中,如 public static boolean isEnglish(String text)
,您可以參考以下的示例代碼:
// import the library
import org.apache.tika.language.detect.LanguageDetector;
import org.apache.tika.language.detect.LanguageResult;
import org.apache.tika.langdetect.OptimaizeLangDetector;
// define a function that takes a string as input and returns a boolean value
public static boolean isEnglish(String text) {
// create a language detector object
LanguageDetector detector = new OptimaizeLangDetector().loadModels();
// add the text to the detector
detector.addText(text);
// get the language result of the text
LanguageResult result = detector.detect();
// get the language code of the text
String language = result.getLanguage();
// check if the language code is "en" for English and return true or false accordingly
return language.equals("en");
}
// test the function with some examples
System.out.println(isEnglish("This is an example of English text")); // true
System.out.println(isEnglish("這是一段中文文本的例子")); // false
System.out.println(isEnglish("Ceci est un exemple de texte fran?ais")); // false
發(fā)現(xiàn) New Bing 已經(jīng)幫我們寫好了,甚至把一些用例都寫好了,點贊??!
如果想讓它再加一些其他語言在測試代碼中或者有其他要求,也可以繼續(xù)告訴它,讓它修改。
我們將代碼放在 IDE 里面運行,發(fā)現(xiàn)也符合預期。
當然,如果上述方案還不滿意,你也可以嘗試問 New Bing 有沒有可以實現(xiàn)類似功能的其他類庫等。
三、啟發(fā)
人工智能時代,利用好 AI 工具,編寫代碼、解決問題的效率將得到倍速提升。
現(xiàn)在人工智能飛速發(fā)展,能力也在不斷提升,但是回答的準確度還依賴于我們是否可以更準確和具體的表達意圖。
我們對 New Bing 和 ChatGPT 提問時,當我們得不到想要的答案時,可以考慮換一種說法。如果你認為描述很準確還是不能得到理想的答案,可以嘗試將提問翻譯成英文再提問,可能效果更好。
此外,人工智能給的答案可以當做一個重要的參考,這些答案也可能會存在一些錯誤,需要我們自己去核實。
總之,人工智能時代,我們學習和解決問題的方式需要改變,遇事不決可以優(yōu)先請教 AI 。文章來源:http://www.zghlxwxcb.cn/news/detail-408291.html
創(chuàng)作不易,如果本文對你有幫助,歡迎點贊、收藏加關注,你的支持和鼓勵,是我創(chuàng)作的最大動力。
文章來源地址http://www.zghlxwxcb.cn/news/detail-408291.html
到了這里,關于New Bing 編程提效實踐 - 語言識別功能的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!