#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# This file is from ICU (with some small modifications, to avoid CJK dictionary break)
#
# Copyright (C) 2002-2016, International Business Machines Corporation 
# and others. All Rights Reserved.
#
# file:  word.txt
#
# ICU Word Break Rules
#      See Unicode Standard Annex #29.
#      These rules are based on UAX #29 Revision 29 for Unicode Version 9.0
#
# Note:  Updates to word.txt will usually need to be merged into
#        word_POSIX.txt also.

##############################################################################
#
#  Character class definitions from TR 29
#
##############################################################################

!!chain;
!!quoted_literals_only;


#
#  Character Class Definitions.
#

$CR                 = [\p{Word_Break = CR}];
$LF                 = [\p{Word_Break = LF}];
$Newline            = [\p{Word_Break = Newline}];
$Extend             = [\p{Word_Break = Extend}];
$Regional_Indicator = [\p{Word_Break = Regional_Indicator}];
$Format             = [\p{Word_Break = Format}];
$Katakana           = [\p{Word_Break = Katakana}];
$Hebrew_Letter      = [\p{Word_Break = Hebrew_Letter}];
$ALetter            = [\p{Word_Break = ALetter}];
$Single_Quote       = [\p{Word_Break = Single_Quote}];
$Double_Quote       = [\p{Word_Break = Double_Quote}];
$MidNumLet          = [\p{Word_Break = MidNumLet}];
$MidLetter          = [\p{Word_Break = MidLetter}];
$MidNum             = [\p{Word_Break = MidNum}];
$Numeric            = [\p{Word_Break = Numeric}[[:Decomposition_Type=Wide:]&[:General_Category=Decimal_Number:]]];
$ExtendNumLet       = [\p{Word_Break = ExtendNumLet}];

$Han                = [:Han:];
$Hiragana           = [:Hiragana:];


#   Dictionary character set, for triggering language-based break engines. Currently
#   limited to LineBreak=Complex_Context. Note that this set only works in Unicode
#   5.0 or later as the definition of Complex_Context was corrected to include all
#   characters requiring dictionary break.

$Control        = [\p{Grapheme_Cluster_Break = Control}]; 
$HangulSyllable = [\uac00-\ud7a3];
$ComplexContext = [:LineBreak = Complex_Context:];
$KanaKanji      = [$Han $Hiragana $Katakana];
$dictionaryCJK  = [$Han $Hiragana $HangulSyllable];
$dictionary     = [$ComplexContext];

# leave CJK scripts out of ALetterPlus
$ALetterPlus  = [$ALetter-$dictionaryCJK [$ComplexContext-$Extend-$Control]];


#
#  Rules 4    Ignore Format and Extend characters, 
#             except when they appear at the beginning of a region of text.
#
# TODO: check if handling of katakana in dictionary makes rules incorrect/void
$KatakanaEx           = $Katakana           ($Extend |  $Format)*;
$Hebrew_LetterEx      = $Hebrew_Letter      ($Extend |  $Format)*;
$ALetterEx            = $ALetterPlus        ($Extend |  $Format)*;
$Single_QuoteEx       = $Single_Quote       ($Extend |  $Format)*;
$Double_QuoteEx       = $Double_Quote       ($Extend |  $Format)*;
$MidNumLetEx          = $MidNumLet          ($Extend |  $Format)*;
$MidLetterEx          = $MidLetter          ($Extend |  $Format)*;
$MidNumEx             = $MidNum             ($Extend |  $Format)*;
$NumericEx            = $Numeric            ($Extend |  $Format)*;
$ExtendNumLetEx       = $ExtendNumLet       ($Extend |  $Format)*;
$Regional_IndicatorEx = $Regional_Indicator ($Extend |  $Format)*;

$Ideographic    = [\p{Ideographic}];
$HiraganaEx     = $Hiragana     ($Extend |  $Format)*;
$IdeographicEx  = $Ideographic  ($Extend |  $Format)*;

## -------------------------------------------------

!!forward;


# Rule 3 - CR x LF
#
$CR $LF;

# Rule 4 - ignore Format and Extend characters, except when they appear at the beginning
#          of a region of Text.   The rule here comes into play when the start of text
#          begins with a group of Format chars, or with a "word" consisting of a single
#          char that is not in any of the listed word break categories followed by
#          format char(s), or is not a CJK dictionary character.
[^$CR $LF $Newline]? ($Extend |  $Format)+;

$NumericEx {100};
$ALetterEx {200};
$HangulSyllable {200};
$Hebrew_LetterEx{200};
$KatakanaEx {300};       # note:  these status values override those from rule 5
$HiraganaEx {300};       #        by virtue of being numerically larger.
$IdeographicEx {400};    #

#
# rule 5
#    Do not break between most letters.
#
($ALetterEx | $Hebrew_LetterEx)  ($ALetterEx | $Hebrew_LetterEx) {200};

# rule 6 and 7
($ALetterEx | $Hebrew_LetterEx) ($MidLetterEx | $MidNumLetEx | $Single_QuoteEx) ($ALetterEx | $Hebrew_LetterEx) {200};

# rule 7a
$Hebrew_LetterEx $Single_QuoteEx {200};

# rule 7b and 7c
$Hebrew_LetterEx $Double_QuoteEx $Hebrew_LetterEx {200};

# rule 8

$NumericEx $NumericEx {100};

# rule 9

($ALetterEx | $Hebrew_LetterEx) $NumericEx {200};

# rule 10

$NumericEx ($ALetterEx | $Hebrew_LetterEx) {200};

# rule 11 and 12 

$NumericEx ($MidNumEx | $MidNumLetEx | $Single_QuoteEx) $NumericEx {100};

# rule 13
$KatakanaEx  $KatakanaEx {300};

# rule 13a/b

$ALetterEx       $ExtendNumLetEx {200};    #  (13a)
$Hebrew_LetterEx $ExtendNumLetEx {200};    #  (13a)
$NumericEx       $ExtendNumLetEx {100};    #  (13a)
$KatakanaEx      $ExtendNumLetEx {300};    #  (13a)
$ExtendNumLetEx  $ExtendNumLetEx {200};    #  (13a)

$ExtendNumLetEx  $ALetterEx      {200};    #  (13b)
$ExtendNumLetEx  $Hebrew_Letter  {200};    #  (13b)
$ExtendNumLetEx  $NumericEx      {100};    #  (13b)
$ExtendNumLetEx  $KatakanaEx     {300};    #  (13b)

# rules 15 - 17
#    Pairs of Regional Indicators stay together.
#    With rule chaining disabled by ^, this rule will match exactly two of them.
#    No other rule begins with a Regional_Indicator, so chaining cannot extend the match.
#
$Regional_IndicatorEx $Regional_IndicatorEx;

# special handling for CJK characters: chain for later dictionary segmentation
$HangulSyllable $HangulSyllable {200};

# Rule 999
#     Match a single code point if no other rule applies.
.;