Function Repository Resource:

KatakanaQ

Source Notebook

Test if a string is composed of katakana characters

Contributed by: Richard Hennigan (Wolfram Research)

ResourceFunction["KatakanaQ"][string]

yields True if all the characters in string are katakana characters, and yields False otherwise.

Details and Options

Katakana (片仮名, カタカナ) is a Japanese syllabary, one component of the Japanese writing system, along with hiragana, kanji and, in some cases, rōmaji (Latin script).
ResourceFunction["KatakanaQ"][string] by default gives False if string contains any space or punctuation characters.
ResourceFunction["KatakanaQ"] has the following options:
IgnorePunctuationFalsewhether to ignore PunctuationCharacter in the string
"IgnoreWhitespace"Falsewhether to ignore WhitespaceCharacter in the string
ResourceFunction["KatakanaQ"] automatically threads over lists.

Examples

Basic Examples (2) 

Test whether a character is katakana:

In[1]:=
ResourceFunction["KatakanaQ"]["ア"]
Out[1]=
In[2]:=
ResourceFunction["KatakanaQ"]["a"]
Out[2]=
In[3]:=
ResourceFunction["KatakanaQ"]["あ"]
Out[3]=

Test if a string contains only katakana characters:

In[4]:=
ResourceFunction["KatakanaQ"]["カタカナ"]
Out[4]=
In[5]:=
ResourceFunction["KatakanaQ"]["片仮名"]
Out[5]=
In[6]:=
ResourceFunction["KatakanaQ"]["katakana"]
Out[6]=
In[7]:=
ResourceFunction["KatakanaQ"]["かたかな"]
Out[7]=

Scope (4) 

KatakanaQ yields False when given spaces:

In[8]:=
ResourceFunction["KatakanaQ"]["ウルフラム ラングウィッジ"]
Out[8]=

Use the katakana middle dot as a valid separator:

In[9]:=
ResourceFunction["FromCharacterName"]["KatakanaMiddleDot"]
Out[9]=
In[10]:=
ResourceFunction["KatakanaQ"][%]
Out[10]=
In[11]:=
ResourceFunction["KatakanaQ"]["ウルフラム・ラングウィッジ"]
Out[11]=

KatakanaQ yields False when given punctuation characters:

In[12]:=
ResourceFunction["KatakanaQ"]["ウルフラムラングウィッジ!"]
Out[12]=

KatakanaQ threads over lists:

In[13]:=
ResourceFunction["KatakanaQ"][{"あ", "ア", "チェケラ", "二"}]
Out[13]=

Options (4) 

IgnorePunctuation (2) 

By default, the presence of characters that match PunctuationCharacter will cause KatakanaQ to yield False:

In[14]:=
ResourceFunction["KatakanaQ"]["チェケラ!"]
Out[14]=

Ignore punctuation:

In[15]:=
ResourceFunction["KatakanaQ"]["チェケラ!", IgnorePunctuation -> True]
Out[15]=

IgnoreWhitespace (2) 

By default, the presence of characters that match WhitespaceCharacter will cause KatakanaQ to yield False:

In[16]:=
ResourceFunction["KatakanaQ"]["ウルフラム ラングウィッジ"]
Out[16]=

Ignore whitespace characters:

In[17]:=
ResourceFunction["KatakanaQ"]["ウルフラム ラングウィッジ", "IgnoreWhitespace" -> True]
Out[17]=

Properties and Relations (4) 

An empty string will yield True:

In[18]:=
ResourceFunction["KatakanaQ"][""]
Out[18]=

Get the full list of katakana characters:

In[19]:=
Select[FromCharacterCode /@ Range[0, 65535], ResourceFunction[
 "KatakanaQ"]]
Out[19]=

Verify the output of Alphabet["Katakana"]:

In[20]:=
ResourceFunction["KatakanaQ"][Alphabet["Katakana"]]
Out[20]=

Test if a character name corresponds to a katakana character:

In[21]:=
katakanaNameQ = ResourceFunction["KatakanaQ"]@*ResourceFunction["FromCharacterName"]
Out[21]=
In[22]:=
katakanaNameQ["KatakanaLetterA"]
Out[22]=
In[23]:=
katakanaNameQ["HiraganaLetterA"]
Out[23]=

Possible Issues (1) 

The set of characters matched by KatakanaQ is larger than Alphabet["Katakana"] since it includes additional characters (e.g. diacritics):

In[24]:=
Select[FromCharacterCode /@ Range[0, 65535], ResourceFunction[
 "KatakanaQ"]]
Out[24]=
In[25]:=
Complement[%, Alphabet["Katakana"]]
Out[25]=
In[26]:=
 CharacterName /@ %
Out[26]=

Neat Examples (2) 

Test if a string will fully Transliterate into katakana:

In[27]:=
ResourceFunction["KatakanaQ"][Transliterate[#, "Katakana"], "IgnoreWhitespace" -> True] & /@ {
  "watashi no neko ha watashi ni nihongo wo oshiete imasu",
  "KatakanaQ[\"katakana\"]",
  "hello world",
  "1234"
  }
Out[27]=

SpeechSynthesize a piece of text, but use a Japanese voice for katakana characters:

In[28]:=
text = "Wolfram Language in katakana is ウルフラム・ラングウィッジ. My favorite ResourceFunction is ビイルドセイ. Please チェケラッチョ!"
Out[28]=
In[29]:=
voice = First[VoiceStyleData[#Language == "Japanese" &]]
Out[29]=
In[30]:=
Replace[StringSplit[text, h : (Longest[__?(
ResourceFunction["KatakanaQ"])] ~~ PunctuationCharacter) :> SpeechSynthesize[h, voice]], s_String :> SpeechSynthesize[s], {1}]
Out[30]=
In[31]:=
AudioPlay[AudioJoin[%]]
Out[31]=

Requirements

Wolfram Language 11.3 (March 2018) or above

Version History

  • 1.0.0 – 04 March 2019

Related Resources

License Information