บทความนี้อธิบายถึงวิธีการของ Java ในการแปลงตัวละครจีนให้เป็นพินอินจีน แบ่งปันสำหรับการอ้างอิงของคุณดังนี้:
ฉันเดินไปรอบ ๆ อินเทอร์เน็ตและเห็นอุปกรณ์ที่น่าสนใจมากที่เรียกว่า Pinyin4j มันสามารถแปลงตัวละครจีนเป็นพินอินจีน การใช้คำพูดของเขาและรวมกับ Lucene และภาษาจีนคุณสามารถสร้างฟังก์ชั่นคล้ายกับ Google เพื่อป้อนพินอินภาษาจีนสำหรับการค้นหาข้อความแบบเต็ม รหัสที่ใช้งานมีดังนี้
แพ็คเกจ pinyin4j; นำเข้า net.sourceforge.pinyin4j.pinyinhelper; นำเข้าสุทธิ. sourceforge.pinyin4j.format.hanyupinyincasetype; นำเข้าสุทธิ net.sourceforge.pinyin4j.format.hanyupinyinvchartype; นำเข้า net.sourceforge.pinyin4j.format.exception.badhanyupinyinoutputformatcombination; โมฆะ); System.out.println (เอาต์พุต); } catch (badhanyupinyinoutputformatcombination e) {// todo บล็อก catch block ที่สร้างขึ้นอัตโนมัติ e.printstacktrace (); }} /*** @parm inputcn อินพุตสตริงภาษาจีน* @parm seg delimiter เมื่อเอาท์พุท pinyin จีน** hanyupinyinoutputformat ให้โหมดเอาต์พุตหลายโหมด* hanyupinyincaseType: กำหนดว่าผลลัพธ์ของอินพุตเป็นตัวพิมพ์เล็กหรือไม่ เน้นด้วย _tone_number: หมายถึงโทนเช่น YE1 1-4 ซึ่งหมายถึง 1-4 เสียง* โดยไม่ต้อง _tone: สัญลักษณ์โทนเสียงไม่แสดง hanyupinyinvchartype: การเข้ารหัสแบบพินอินแบบใด inputcn.tochararray (); ถ้า (seg == null) seg = ""; รูปแบบ hanyupinyinoutputformat = ใหม่ hanyupinyinoutputformat (); format.setCasetype (hanyupinyincasetype.lowercase); format.settonetype (hanyupinyintonetype.without_tone); format.setVchartype (hanyupinyinvchartype.with_v); string output = ""; สตริง [] temp = สตริงใหม่ [10]; สำหรับ (int i = 0; i <inputarray.length; i ++) {temp = pinyinhelper.tohanyupinyinstringarray (inputarray [i], รูปแบบ); // หากตัวละครภาษาจีนที่ป้อนเป็นตัวละครโพลีโฟนิกการออกเสียงที่แตกต่างกันจะถูกวางลงในอุณหภูมิ [] ตามลำดับ หากไม่ใช่อักขระ polyphonic เฉพาะค่าในอุณหภูมิ [0] สำหรับ (int j = 0; j <temp.length; j ++) {output += temp [j] +seg; }} return output; -ฉันหวังว่าบทความนี้จะเป็นประโยชน์กับการเขียนโปรแกรม Java ของทุกคน