//
// Core Program for TETDM 
// TextData.java Version 1.00
// Copyright(C) 2011-2018 TETDM CORE MEMBER ALL RIGHTS RESERVED.
// This program is provided under the license.
//


package source.TextData;

import source.*;

import java.io.*;
import java.util.*;

import java.util.regex.*;


import net.reduls.igo.Tagger;
import net.reduls.igo.Morpheme;	

public class TextData
{
	public int textID;
	
	public String originalText;		//Loaded text data	
	public String sourceFilename;	//Load text filename
	public String absolutePath;		//Absolute path of execution directory
//	public String defaultCode;
	
	public int textLength;
	
	//Segment Data
	public SegmentData segment[];
	public int segmentNumber;
    
    String segmentTag;

	//Sentence Data
	public SentenceData sentence[];		//pointer to segment[].sentence[]
	public int sentenceNumber;
    
    String sentenceTag;
	
	public int sentenceToSegment[];		//segment number of i-th sentence
	
	//Keywords Data
	public KeywordData keyword[];
	public int keywordNumber;

	public final int maxKeywordNumber = 10000;	//Maximum number of keywords, The process is NOT implemented when number is above 10000.

    //Word Data
    public int wordNumber;
	
	//Text Relation Data
//	public RelationalData segmentRelation, sentenceRelation, keywordRelationBySegment, keywordRelationBySentence;
	public RelationalData segmentRelation, sentenceRelation, keywordRelationBySegment;	
	
    public int keywordRelationLimit = 1500;
	public int segmentRelationLimit = 1000;
    public int sentenceRelationLimit = 100;


	
	public TextData segmentPartialTextData[];
	public boolean segmentPartialInitializationCheck[];
	public int maxSegmentPartialNumber = 3;
	public TextData sentencePartialTextData[];
	public boolean sentencePartialInitializationCheck[];	
	public int maxSentencePartialNumber = 3;
	
	public int originalSegmentNumbers[];
	public int originalSentenceNumbers[];

    
    public TextData partOfTextData[];
    
	
    public class Attention
    {
        protected boolean attentionKeywords[];
        protected boolean attentionSegments[];
        protected boolean attentionSentences[];
        
        Attention(int keywordNumber, int sentenceNumber, int segmentNumber)
        {
            attentionKeywords = new boolean[keywordNumber];
            attentionSegments = new boolean[segmentNumber];
            attentionSentences = new boolean[sentenceNumber];
            
            clearAllAttention();
        }

        public void clearAllAttention()
        {
            System.out.println("CLEAR ATTENTION SETS ID="+textID);
            
            clearAttentionKeywords();
            clearAttentionSegments();
            clearAttentionSentences();
        }
        
        public void clearAttentionKeywords()
        {
            for(int i=0;i<keywordNumber;i++)
                attentionKeywords[i] = false;
        }
        public void clearAttentionSegments()
        {
            for(int i=0;i<segmentNumber;i++)
                attentionSegments[i] = false;
        }
        public void clearAttentionSentences()
        {
            for(int i=0;i<sentenceNumber;i++)
                attentionSentences[i] = false;
        }
  
        public void setAttentionKeywords(boolean value[])
        {
            for(int i=0;i<keywordNumber;i++)
                attentionKeywords[i] = value[i];
        }
        
        public void setAttentionSegments(boolean value[])
        {
            for(int i=0;i<segmentNumber;i++)
                attentionSegments[i] = value[i];
        }
        
        public void setAttentionSentences(boolean value[])
        {
            for(int i=0;i<sentenceNumber;i++)
                attentionSentences[i] = value[i];
        }
        

        
        public boolean[] getAttentionKeywords()
        {
            boolean data[] = new boolean[keywordNumber];
            for(int i=0;i<keywordNumber;i++)
                data[i] = attentionKeywords[i];
            return data;
        }
        public boolean[] getAttentionSegments()
        {
            boolean data[] = new boolean[segmentNumber];
            for(int i=0;i<segmentNumber;i++)
                data[i] = attentionSegments[i];
            return data;
        }
        public boolean[] getAttentionSentences()
        {
            boolean data[] = new boolean[sentenceNumber];
            for(int i=0;i<sentenceNumber;i++)
                data[i] = attentionSentences[i];
            return data;
        }
        
    }
    
	//display for visualization
//	public int fontSize = 14; //-> move to settingData

	public class Focus
	{
		protected boolean focusKeywords[];
		protected boolean focusSegments[];
		protected boolean focusSentences[];

		protected int mainFocusKeyword, subFocusKeyword;
		protected int mainFocusSegment, subFocusSegment;
		protected int mainFocusSentence, subFocusSentence;
        
        protected int focusOption = 0;    // 1-12, 0:others
        

        
		
		Focus(int keywordNumber, int sentenceNumber, int segmentNumber)
		{
			focusKeywords = new boolean[keywordNumber];
			focusSegments = new boolean[segmentNumber];
			focusSentences = new boolean[sentenceNumber];
			
			clearAllFocus();
		}
		
		public void clearAllFocus()////
		{
            clearMainSub();
            clearFocusSets();
		}
        
        public void clearMainSub()////
        {
            mainFocusKeyword = subFocusKeyword = mainFocusSegment = subFocusSegment = mainFocusSentence = subFocusSentence = -1;
        }
        
        public void clearFocusSets()
        {
            System.out.println("CLEAR FOCUS SETS ID="+textID);
            
            clearFocusKeywords();
            clearFocusSegments();
            clearFocusSentences();
        }
        
        public void clearFocusKeywords()
        {
            for(int i=0;i<keywordNumber;i++)
                focusKeywords[i] = false;
        }
        public void clearFocusSegments()
        {
            for(int i=0;i<segmentNumber;i++)
                focusSegments[i] = false;
        }
        public void clearFocusSentences()
        {
            for(int i=0;i<sentenceNumber;i++)
                focusSentences[i] = false;
        }
        
        public boolean isSetFocusKeywords()
        {
            for(int i=0;i<keywordNumber;i++)
                if(focusKeywords[i])
                    return true;
            return false;
        }
        public boolean isSetFocusSegments()
        {
            for(int i=0;i<segmentNumber;i++)
                if(focusSegments[i])
                    return true;
            return false;
        }
        public boolean isSetFocusSentences()
        {
            for(int i=0;i<sentenceNumber;i++)
                if(focusSentences[i])
                    return true;
            return false;
        }
        
        public int getFocusStatus(int dataNumber)////****//////For detect only one focus data
        {
            if(dataNumber == segmentNumber)
            {
                if(mainFocusSegment != -1 && subFocusSegment == -1) //Main Only
                    return 1;
                if(mainFocusSegment == -1 && subFocusSegment != -1) //Sub only
                    return 2;
                if(mainFocusSegment != -1 && subFocusSegment != -1) //Main and Sub
                    return 3;
                return 4;                                           //no Main and Sub
            }
            
            if(dataNumber == sentenceNumber)
            {
                if(mainFocusSentence != -1 && subFocusSentence == -1) //Main Only
                    return 5;
                if(mainFocusSentence == -1 && subFocusSentence != -1) //Sub only
                    return 6;
                if(mainFocusSentence != -1 && subFocusSentence != -1) //Main and Sub
                    return 7;
                return 8;                                           //no Main and Sub
            }

            if(dataNumber == keywordNumber)
            {
                if(mainFocusKeyword != -1 && subFocusKeyword == -1) //Main Only
                    return 9;
                if(mainFocusKeyword == -1 && subFocusKeyword != -1) //Sub only
                    return 10;
                if(mainFocusKeyword != -1 && subFocusKeyword != -1) //Main and Sub
                    return 11;
                return 12;                                           //no Main and Sub
            }
            
            return 0;
        }
        
        

/*
        public void setMainFocusKeyword(int value)////////
        {
            String focusWords = "";
            
            mainFocusKeyword = value;
            mainFocusSegment = subFocusSegment = mainFocusSentence = subFocusSentence = -1;
            clearFocusSets();
            if(mainFocusKeyword != -1)
            {
                focusKeywords[mainFocusKeyword] = true;
                focusWords += keyword[mainFocusKeyword].word+" ";
            }
            if(subFocusKeyword != -1)
            {
                focusKeywords[subFocusKeyword] = true;
                focusWords += keyword[subFocusKeyword].word;
            }
            setFocusKeywordsToOthers("Including the keyword "+focusWords);
        }
        public void setSubFocusKeyword(int value)////////
        {
            String focusWords = "";
            
            subFocusKeyword = value;
            mainFocusSegment = subFocusSegment = mainFocusSentence = subFocusSentence = -1;
            clearFocusSets();
            focusKeywords[subFocusKeyword] = true;
            if(mainFocusKeyword != -1)
            {
                focusKeywords[mainFocusKeyword] = true;
                focusWords += keyword[mainFocusKeyword].word+" ";
            }
            if(subFocusKeyword != -1)
            {
                focusKeywords[subFocusKeyword] = true;
                focusWords += keyword[subFocusKeyword].word;
            }
            setFocusKeywordsToOthers("Including the keyword "+focusWords);
        }
        public void setMainFocusSegment(int value)////////
        {
            String focusWords = "";
            
            mainFocusSegment = value;
            mainFocusKeyword = subFocusKeyword = mainFocusSentence = subFocusSentence = -1;
            clearFocusSets();
            if(mainFocusSegment != -1)
            {
                focusSegments[mainFocusSegment] = true;
                focusWords += (mainFocusSegment+1)+" ";
            }
            if(subFocusSegment != -1)
            {
                focusSegments[subFocusSegment] = true;
                focusWords += "No."+(subFocusSegment+1);
            }
            setFocusSegmentsToOthers("Focusing the Segment No."+focusWords);
        }
        public void setSubFocusSegment(int value)////////
        {
            String focusWords = "";
            
            subFocusSegment = value;
            mainFocusKeyword = subFocusKeyword = mainFocusSentence = subFocusSentence = -1;
            clearFocusSets();
            if(mainFocusSegment != -1)
            {
                focusSegments[mainFocusSegment] = true;
                focusWords += (mainFocusSegment+1)+" ";
            }
            if(subFocusSegment != -1)
            {
                focusSegments[subFocusSegment] = true;
                focusWords += "No."+(subFocusSegment+1);
            }
            setFocusSegmentsToOthers("Focusing the Segment No."+focusWords);
        }
        public void setMainFocusSentence(int value)////////
        {
            String focusWords = "";
            
            mainFocusSentence = value;
            mainFocusKeyword = subFocusKeyword = mainFocusSegment = subFocusSegment = -1;
            clearFocusSets();
            if(mainFocusSentence != -1)
            {
                focusSentences[mainFocusSentence] = true;
                focusWords += (mainFocusSentence+1)+" ";
            }
            if(subFocusSentence != -1)
            {
                focusSentences[subFocusSentence] = true;
                focusWords += "No."+(subFocusSentence+1);
            }
            setFocusSentencesToOThers("Including the Sentence No."+focusWords);
        }
        public void setSubFocusSentence(int value)////////
        {
            String focusWords = "";
            
            subFocusSentence = value;
            mainFocusKeyword = subFocusKeyword = mainFocusSegment = subFocusSegment = -1;
            clearFocusSets();
            if(mainFocusSentence != -1)
            {
                focusSentences[mainFocusSentence] = true;
                focusWords += (mainFocusSentence+1)+" ";
            }
            if(subFocusSentence != -1)
            {
                focusSentences[subFocusSentence] = true;
                focusWords += "No."+(subFocusSentence+1);
            }
            setFocusSentencesToOThers("Including the Sentence No."+focusWords);
        }
       */
        public void setMainFocusKeyword(int toolType, int toolID, int value, String japaneseCondition, String englishCondition, boolean only, boolean revision)////****////
        {
            if(only == true)
                subFocusKeyword = -1;
            
            mainFocusKeyword = value;
            mainFocusSegment = subFocusSegment = mainFocusSentence = subFocusSentence = -1;
            clearFocusSets();
            if(mainFocusKeyword != -1)
                focusKeywords[mainFocusKeyword] = true;

            if(subFocusKeyword != -1)
                focusKeywords[subFocusKeyword] = true;

            setFocusKeywordsToOthers(toolType, toolID, japaneseCondition, englishCondition,revision);
        }
        public void setSubFocusKeyword(int toolType, int toolID, int value, String japaneseCondition, String englishCondition, boolean only, boolean revision)////****////
        {
            if(only == true)
                mainFocusKeyword = -1;
            
            subFocusKeyword = value;
            mainFocusSegment = subFocusSegment = mainFocusSentence = subFocusSentence = -1;
            clearFocusSets();
            focusKeywords[subFocusKeyword] = true;
            if(mainFocusKeyword != -1)
                focusKeywords[mainFocusKeyword] = true;

            if(subFocusKeyword != -1)
                focusKeywords[subFocusKeyword] = true;

            setFocusKeywordsToOthers(toolType, toolID, japaneseCondition, englishCondition,revision);
        }
        public void setMainFocusSegment(int toolType, int toolID, int value, String japaneseCondition, String englishCondition, boolean only, boolean revision)////****////
        {
            if(only == true)
                subFocusSegment = -1;
            
            mainFocusSegment = value;
            mainFocusKeyword = subFocusKeyword = mainFocusSentence = subFocusSentence = -1;
            clearFocusSets();
            if(mainFocusSegment != -1)
                focusSegments[mainFocusSegment] = true;
            
            if(subFocusSegment != -1)
                focusSegments[subFocusSegment] = true;

            setFocusSegmentsToOthers(toolType, toolID, japaneseCondition, englishCondition,revision);
        }
        public void setSubFocusSegment(int toolType, int toolID, int value, String japaneseCondition, String englishCondition, boolean only, boolean revision)////****////
        {
            if(only == true)
                mainFocusSegment = -1;
            
            subFocusSegment = value;
            mainFocusKeyword = subFocusKeyword = mainFocusSentence = subFocusSentence = -1;
            clearFocusSets();
            if(mainFocusSegment != -1)
                focusSegments[mainFocusSegment] = true;

            if(subFocusSegment != -1)
                focusSegments[subFocusSegment] = true;

            setFocusSegmentsToOthers(toolType, toolID, japaneseCondition, englishCondition,revision);
        }
        public void setMainFocusSentence(int toolType, int toolID, int value, String japaneseCondition, String englishCondition, boolean only, boolean revision)////****////
        {
            if(only == true)
                subFocusSentence = -1;
            
            mainFocusSentence = value;
            mainFocusKeyword = subFocusKeyword = mainFocusSegment = subFocusSegment = -1;
            clearFocusSets();
            if(mainFocusSentence != -1)
                focusSentences[mainFocusSentence] = true;

            if(subFocusSentence != -1)
                focusSentences[subFocusSentence] = true;

            setFocusSentencesToOThers(toolType, toolID, japaneseCondition, englishCondition,revision);
        }
        public void setSubFocusSentence(int toolType, int toolID, int value, String japaneseCondition, String englishCondition, boolean only, boolean revision)////****////
        {
            if(only == true)
                mainFocusSentence = -1;
            
            subFocusSentence = value;
            mainFocusKeyword = subFocusKeyword = mainFocusSegment = subFocusSegment = -1;
            clearFocusSets();
            if(mainFocusSentence != -1)
                focusSentences[mainFocusSentence] = true;

            if(subFocusSentence != -1)
                focusSentences[subFocusSentence] = true;

            setFocusSentencesToOThers(toolType, toolID, japaneseCondition, englishCondition,revision);
        }

        public int getMainFocusKeyword()
        {
            return mainFocusKeyword;
        }
        public int getSubFocusKeyword()
        {
            return subFocusKeyword;
        }
        public int getMainFocusSegment()
        {
            return mainFocusSegment;
        }
        public int getSubFocusSegment()
        {
            return subFocusSegment;
        }
        public int getMainFocusSentence()
        {
            return mainFocusSentence;
        }
        public int getSubFocusSentence()
        {
            return subFocusSentence;
        }
        
//////////////////////////////////////////////
        
        int countFocusSegments()
        {
            int count=0;
            for(int i=0;i<segmentNumber;i++)
                if(focusSegments[i])
                    count++;
            return count;
        }
        
        
        //create overload methods that can receive meaning of shrink condition
        /*
        public void setFocusKeywords(boolean value[])
        {
            clearAllFocus();
            
            //Keywords
            for(int i=0;i<keywordNumber;i++)
                focusKeywords[i] = value[i];
            
            int first = -1;
            for(int i=0;i<keywordNumber;i++)
                if(focusKeywords[i])
                {
                    first = i;
                    break;
                }
            
            System.out.println("SET FOCUS KEYWORDS ID="+textID);
            
            if(first != -1)
                setFocusKeywordsToOthers("Including keywords such as "+keyword[first].word);
            else
                setFocusKeywordsToOthers("keywords: NO matched");
        }*/
        public void setFocusKeywords(int toolType, int toolID, boolean value[], String japaneseCondition, String englishCondition, boolean revision)////****////
        {
            clearAllFocus();
            
            //Keywords
            for(int i=0;i<keywordNumber;i++)
                focusKeywords[i] = value[i];

            System.out.println("SET FOCUS KEYWORDS ID="+textID);
            
            setFocusKeywordsToOthers(toolType, toolID, japaneseCondition, englishCondition, revision);
        }
        
        public void setFocusKeywordsLocal(boolean value[])
        {
            clearAllFocus();
            
            //Keywords
            for(int i=0;i<keywordNumber;i++)
                focusKeywords[i] = value[i];
            
            System.out.println("SET FOCUS KEYWORDS LOCAL ID="+textID);
            
            setFocusKeywordsToOthersLocal();
        }
        
        /*
        public void setFocusSegments(boolean value[])
        {
            clearAllFocus();

            //Segments
            for(int i=0;i<segmentNumber;i++)
                focusSegments[i] = value[i];
            
            int first = -1;
            for(int i=0;i<segmentNumber;i++)
                if(focusSegments[i])
                {
                    first = i;
                    break;
                }
            
            System.out.println("SET FOCUS SEGMENTS ID="+textID);
            
            if(first != -1)
                setFocusSegmentsToOthers("Including segments such as No."+(first+1));
            else
                setFocusSegmentsToOthers("segments: NO matched");
        }*/
        
        public void setFocusSegmentsLocal(boolean value[])
        {
            clearAllFocus();
            
            //Segments
            for(int i=0;i<segmentNumber;i++)
                focusSegments[i] = value[i];
            
            System.out.println("SET FOCUS SEGMENTS LOCAL ID="+textID);
            
            setFocusSegmentsToOthersLocal();
        }
        
        
        public void setFocusSegments(int toolType, int toolID, boolean value[], String japaneseCondition, String englishCondition, boolean revision)
        {
            clearAllFocus();
            
            //Segments
            for(int i=0;i<segmentNumber;i++)
                focusSegments[i] = value[i];
          
            System.out.println("SET FOCUS SEGMENTS ID="+textID);
            
            setFocusSegmentsToOthers(toolType, toolID, japaneseCondition,englishCondition, revision);
        }
        
        public void setInvertionFocusSegments()
        {
            clearMainSub();
            for(int i=0;i<sentenceNumber;i++)
                focusSentences[i] = false;
            for(int i=0;i<keywordNumber;i++)
                focusKeywords[i] = false;
            
            //Segments
            for(int i=0;i<segmentNumber;i++)
                focusSegments[i] = !focusSegments[i];
            
            System.out.println("SET INVERTION FOCUS SEGMENTS ID="+textID);
            
            setInvertionFocusSegmentsToOthers();
        }
        /*
        public void setFocusSentences(boolean value[])
        {
            clearAllFocus();
            
            //Sentences
            for(int i=0;i<sentenceNumber;i++)
                focusSentences[i] = value[i];
            
            int first = -1;
            for(int i=0;i<sentenceNumber;i++)
                if(focusSentences[i])
                {
                    first = i;
                    break;
                }
            
            System.out.println("SET FOCUS SENTENCES ID="+textID);
            
            if(first != -1)
                setFocusSentencesToOThers("Including sentences such as No."+(first+1));
            else
                setFocusSentencesToOThers("sentences: NO matched");
        }*/
        public void setFocusSentences(int toolType, int toolID, boolean value[], String japaneseCondition, String englishCondition, boolean revision)////****////
        {
            clearAllFocus();
            
            //Sentences
            for(int i=0;i<sentenceNumber;i++)
                focusSentences[i] = value[i];
           
            System.out.println("SET FOCUS SENTENCES ID="+textID);
            
            setFocusSentencesToOThers(toolType, toolID, japaneseCondition,englishCondition, revision);
        }
        
        public void setFocusSentencesLocal(boolean value[])
        {
            clearAllFocus();
            
            //Sentences
            for(int i=0;i<sentenceNumber;i++)
                focusSentences[i] = value[i];
            
            System.out.println("SET FOCUS SENTENCES LOCAL ID="+textID);
            
            setFocusSentencesToOthersLocal();
        }
        
        private boolean[] createOriginalFocusSegments()
        {
            boolean globalFocusSegments[] = new boolean[control.text.segmentNumber];
            for(int i=0;i<control.text.segmentNumber;i++)
                globalFocusSegments[i] = false;
            
            for(int i=0;i<segmentNumber;i++)
                globalFocusSegments[originalSegmentNumbers[i]] = focusSegments[i];
            
            return globalFocusSegments;
        }
        
        private void setFocusKeywordsToOthers(int toolType, int toolID, String japaneseConditionWords, String englishConditionWords, boolean revision)////****////
        {
            String example = "";
            
            //Segments and Sentences
            for(int i=0;i<keywordNumber;i++)
                if(focusKeywords[i])
                {
                    for(int j=0;j<keyword[i].segmentFrequency;j++)
                        focusSegments[keyword[i].appearingSegment[j]] = true;
                    
                    for(int j=0;j<keyword[i].sentenceFrequency;j++)
                        focusSentences[keyword[i].appearingSentence[j]] = true;
                }
            
            if(textID != 0)
                control.text.focus.setFocusSegments(toolType, toolID, createOriginalFocusSegments(), "","",false);
            
            System.out.println("KEYWORDS TO OTHERS ID="+textID);
            
            for(int i=0,count=0;i<keywordNumber && count<10;i++)
                if(focusKeywords[i])
                {
                    example += keyword[i].word+" ";
                    count++;
                }
            if(revision == true)
                control.focus.reviseCondition(toolType, toolID, 3, japaneseConditionWords, englishConditionWords, example, countFocusSegments());
        }
        
        private void setFocusKeywordsToOthersLocal()////****////
        {
            //Segments and Sentences
            for(int i=0;i<keywordNumber;i++)
                if(focusKeywords[i])
                {
                    for(int j=0;j<keyword[i].segmentFrequency;j++)
                        focusSegments[keyword[i].appearingSegment[j]] = true;
                    
                    for(int j=0;j<keyword[i].sentenceFrequency;j++)
                        focusSentences[keyword[i].appearingSentence[j]] = true;
                }
            
            System.out.println("KEYWORDS TO OTHERS LOCAL ID="+textID);
        }
        
        private void setFocusSentencesToOthersLocal()////****////
        {
            //Segments
            for(int i=0;i<sentenceNumber;i++)
                if(focusSentences[i])
                    focusSegments[sentence[i].positionOfSegment] = true;
            
            //Keywords
            for(int i=0;i<sentenceNumber;i++)
                if(focusSentences[i])
                    for(int j=0;j<sentence[i].keywordNumber;j++)
                        focusKeywords[sentence[i].keywordIDList[j]] = true;
            
            System.out.println("SENTECES TO OTHERS LOCAL ID="+textID);
        }

        private void setFocusSegmentsToOthersLocal()
        {
            //Sentences
            for(int i=0;i<segmentNumber;i++)
                if(focusSegments[i])
                    for(int j=0;j<segment[i].sentenceNumber;j++)
                        focusSentences[segment[i].positionOfFirstSentence+j] = true;
            
            //Keywords
            for(int i=0;i<sentenceNumber;i++)
                if(focusSentences[i])
                    for(int j=0;j<sentence[i].keywordNumber;j++)
                        focusKeywords[sentence[i].keywordIDList[j]] = true;
            
            System.out.println("SEGMENTS TO OTHERS LOCAL ID="+textID);
        }
        
        private void setFocusSegmentsToOthers(int toolType, int toolID, String japaneseConditionWords, String englishConditionWords, boolean revision)////****////
        {
            String example = "";
            
            //Sentences
            for(int i=0;i<segmentNumber;i++)
                if(focusSegments[i])
                    for(int j=0;j<segment[i].sentenceNumber;j++)
                        focusSentences[segment[i].positionOfFirstSentence+j] = true;
            
            //Keywords
            for(int i=0;i<sentenceNumber;i++)
                if(focusSentences[i])
                    for(int j=0;j<sentence[i].keywordNumber;j++)
                        focusKeywords[sentence[i].keywordIDList[j]] = true;
            
            if(textID != 0)
                control.text.focus.setFocusSegments(toolType, toolID, createOriginalFocusSegments(), "","",false);
            
            System.out.println("SEGMENTS TO OTHERS ID="+textID);
            
            for(int i=0,count=0;i<control.text.segmentNumber && count<10;i++)
                if(control.text.focus.focusSegments[i])
                {
                    example += "["+(i+1)+"]";
                    count++;
                }
            

            
            if(revision == true)
                control.focus.reviseCondition(toolType, toolID, 1, japaneseConditionWords, englishConditionWords, example, countFocusSegments());
        }
        
        private void setInvertionFocusSegmentsToOthers()////****////
        {
            String example = "";
            
            //Sentences
            for(int i=0;i<segmentNumber;i++)
                if(focusSegments[i])
                    for(int j=0;j<segment[i].sentenceNumber;j++)
                        focusSentences[segment[i].positionOfFirstSentence+j] = true;
            
            //Keywords
            for(int i=0;i<sentenceNumber;i++)
                if(focusSentences[i])
                    for(int j=0;j<sentence[i].keywordNumber;j++)
                        focusKeywords[sentence[i].keywordIDList[j]] = true;
            
            if(textID != 0)
                control.text.focus.setFocusSegments(-1, -1, createOriginalFocusSegments(), "","",false);
            
            System.out.println("SEGMENTS TO OTHERS ID="+textID);
            
            control.focus.reviseInvertionCondition(countFocusSegments());
        }
        
        private void setFocusSentencesToOThers(int toolType, int toolID, String japaneseConditionWords, String englishConditionWords, boolean revision)////****////
        {
            String example = "";
            
            //Segments
            for(int i=0;i<sentenceNumber;i++)
                if(focusSentences[i])
                    focusSegments[sentence[i].positionOfSegment] = true;
            
            //Keywords
            for(int i=0;i<sentenceNumber;i++)
                if(focusSentences[i])
                    for(int j=0;j<sentence[i].keywordNumber;j++)
                        focusKeywords[sentence[i].keywordIDList[j]] = true;
            
            if(textID != 0)
                control.text.focus.setFocusSegments(toolType, toolID, createOriginalFocusSegments(), "","",false);
            
            System.out.println("SENTECES TO OTHERS ID="+textID);
            
            for(int i=0,count=0;i<control.text.sentenceNumber && count < 10;i++)
                if(control.text.focus.focusSentences[i])
                {
                    example += "("+(i+1)+")";
                    count++;
                }
            


            if(revision == true)
                control.focus.reviseCondition(toolType, toolID, 2, japaneseConditionWords, englishConditionWords, example, countFocusSegments());
        }
        
//////////////////////////////////////////////
        
        public boolean[] getFocusKeywords()
        {
            boolean data[] = new boolean[keywordNumber];
            for(int i=0;i<keywordNumber;i++)
                data[i] = focusKeywords[i];
            return data;
        }
        public boolean[] getFocusSegments()
        {
            boolean data[] = new boolean[segmentNumber];
            for(int i=0;i<segmentNumber;i++)
                data[i] = focusSegments[i];
            return data;
        }
        public boolean[] getFocusSentences()
        {
            boolean data[] = new boolean[sentenceNumber];
            for(int i=0;i<sentenceNumber;i++)
                data[i] = focusSentences[i];
            return data;
        }
        
        
        
        
        

		
		
		
		
		
	}

	public Focus focus;
    public Attention attention;
	
	public Control control;
	
	public SettingData settingData;
	
	public Connection connection;	
	
	public TextData(){}

	public TextData(int ID, String sourcefile, String originalText, SettingData sp1, String absolute, Control cont)
	{
		textID = ID;
		sourceFilename = sourcefile;
//		defaultCode = sp1.defaultCode;
		settingData = sp1;
		control = cont;//		this.cont = cont;	
		absolutePath = absolute;
        segmentTag = sp1.segment_tag;
        sentenceTag = sp1.kind_touns[0];
        
        partOfTextData = new TextData[7];
        partOfTextData[0] = this;
		
		connection = new Connection(this);
		
		this.originalText = originalText;//FILEIO.TextFileAllReadCode(new File(sourcefile));//Prepare the original text
		
		//Keywords Data
		keyword = new KeywordData[maxKeywordNumber];
		
		for(int i=0;i<maxKeywordNumber;i++)
			keyword[i] = new KeywordData();
		keywordNumber = 0;
		
		//Segment Data
		segmentNumber = FILEIO.CountTags(originalText, sp1.segment_tag);
		segment = new SegmentData[segmentNumber];
		String cut_text[] = new String[segmentNumber];
		FILEIO.CutText(originalText, cut_text, sp1.segment_tag, sp1.leftTags);
		for(int i=0;i<segmentNumber;i++)
			segment[i] = new SegmentData(i, cut_text[i], i);
		
		//Load words information from the result of ChaSen
		readChasenFile(sp1);	//Read word data from a cha-file	//count sentenceNumber
   

        //original Number mainly used for partial data
        originalSegmentNumbers = new int[segmentNumber];
        originalSentenceNumbers = new int[sentenceNumber];
        for(int i=0;i<segmentNumber;i++)
            originalSegmentNumbers[i] = i;
        for(int i=0;i<sentenceNumber;i++)
            originalSentenceNumbers[i] = i;
        
		initializeData(0);
		
//		if(sp1.mode == 3)
			initializePartialText();
		
		/*
		boolean active[] = new boolean[keywordNumber];
		for(int i=0;i<keywordNumber;i++)
			if(keyword[i].segmentFrequency < 2)
				active[i] = false;
			else
				active[i] = true;
		*/
		//		keywordRelationBySegment.optimizeLink(keywordRelationBySegment.cos, 0.5, active);
		
		/*		MakeCluster mk = new MakeCluster(keywordRelationBySegment);   CLUSTERING */
		
		//		boolean active[] = new boolean[segmentNumber];		
		
		//		segr.optimizeLink(segr.cos, 0.5, active);
		//		MakeCluster mk = new MakeCluster(segr);
		

	}	
	
	public TextData(int ID)
	{
		textID = ID;
	}
	
	public void initializePartialText()
	{
		//For partialData
		segmentPartialTextData = new TextData[maxSegmentPartialNumber];
		segmentPartialInitializationCheck = new boolean[maxSegmentPartialNumber];
		for(int i=0;i<maxSegmentPartialNumber;i++)
		{
			segmentPartialInitializationCheck[i] = false;
			segmentPartialTextData[i] = new TextData(1 + 2*i);
			segmentPartialTextData[i].createSegmentPartialData(this, this.focus.focusSegments, 1);
            
            partOfTextData[1 + 2*i] = segmentPartialTextData[i];
		}
		sentencePartialTextData = new TextData[maxSentencePartialNumber];
		sentencePartialInitializationCheck = new boolean[maxSentencePartialNumber];
		for(int i=0;i<maxSentencePartialNumber;i++)
		{
			sentencePartialInitializationCheck[i] = false;
			sentencePartialTextData[i] = new TextData(2 + 2*i);
			sentencePartialTextData[i].createSentencePartialData(this, this.focus.focusSentences, 1);
			
            partOfTextData[2 + 2*i] = sentencePartialTextData[i];
		}		
	}
	
	public String getFontName()
	{
		return settingData.fontName;
	}
    
    public void initializeData(int option)  //option=0 : originalText
	{		
		System.out.println("SEGMENT NUMBER = "+segmentNumber);
		System.out.println("SENTENCE NUMBER = "+sentenceNumber);
		System.out.println("KEYWORD NUMBER = "+keywordNumber);		
		

        
		//Copy sentence, Calculate: sentenceToSegment[]
		sentenceToSegment = new int[sentenceNumber];
		sentence = new SentenceData[sentenceNumber];
		
		for(int i=0,s=0;i<segmentNumber;i++)
			for(int j=0;j<segment[i].sentenceNumber;j++)
			{
				sentence[s]	= segment[i].sentence[j];
				sentenceToSegment[s++] = i;
			}
		
		//move line.separator from top of sentences to last
//		String br = System.getProperty("line.separator");
//		for(int i=1;i<sentenceNumber;i++)
//			if(sentence[i].word[0].equals(br))
//			{
//				sentence[i-1].sentenceText += br;
//				sentence[i].sentenceText = sentence[i].sentenceText.substring(1,sentence[i].sentenceText.length());
//				}


		for(int i=0;i<keywordNumber;i++)
		{
			keyword[i].frequencyAsSubject = 0;
			keyword[i].segmentFrequencyAsSubject = 0;
			keyword[i].lastAppearingSegment = -1;
		}
        
        for(int i=0;i<segmentNumber;i++)
            for(int j=0;j<segment[i].sentenceNumber;j++)
                for(int k=0;k<segment[i].sentence[j].wordNumber;k++)
                    if(segment[i].sentence[j].subjectCheck[k] && segment[i].sentence[j].wordIDList[k] >= 0)
                    {
                        keyword[segment[i].sentence[j].wordIDList[k]].frequencyAsSubject++;
                        if(i != keyword[segment[i].sentence[j].wordIDList[k]].lastAppearingSegment)
                        {
                            keyword[segment[i].sentence[j].wordIDList[k]].segmentFrequencyAsSubject++;
                            keyword[segment[i].sentence[j].wordIDList[k]].lastAppearingSegment = i;
                        }
                    }
		
		//Count length
		textLength = 0;
		for(int i=0;i<sentenceNumber;i++)
		{
			sentence[i].sentenceLength = sentence[i].sentenceText.trim().replaceAll(System.getProperty("line.separator"),"").length();
			textLength += sentence[i].sentenceLength;
		}
		for(int i=0;i<segmentNumber;i++)
		{
			segment[i].segmentLength = 0;
			for(int j=0;j<segment[i].sentenceNumber;j++)
				segment[i].segmentLength += segment[i].sentence[j].sentenceLength;
		}
		
		//Count word Number
		wordNumber=0;
		for(int i=0;i<sentenceNumber;i++)
		    wordNumber+=sentence[i].wordNumber;
		
		for(int i=0;i<segmentNumber;i++)
		{
			segment[i].wordNumber = 0;
			for(int j=0;j<segment[i].sentenceNumber;j++)
				segment[i].wordNumber += segment[i].sentence[j].wordNumber;
		}		
		
		//Calculate keyword appearance data: appearingSentenceTable[], appearingSentence[], sentenceFrequency, seg_times[], seg_sentimes[], appearingSegmentTable[], appearingSegment[], segmentFrequency 
		createKeywordAppearance();
		
		//Calculate segment[].kind_num, sentence[].kind_num
		createKindNumbers();
		
		//PreProcess 1, Calculate cos similarity between segments, sentences, keywords by segment frequencies, and keywords by sentences frequencies

        System.out.println("START RELATION CALCULATION");        
		if(option == 0)
			createTextRelation();
		
		focus = new Focus(keywordNumber,sentenceNumber,segmentNumber);
        attention = new Attention(keywordNumber,sentenceNumber,segmentNumber);    
	}
	/*
	public void setSegmentPartialTextData()
	{
		segmentPartialTextData[0].createSegmentPartialData(this);
	}
	
	public void setSentencePartialTextData()
	{
		sentencePartialTextData[0].createSentencePartialData(this);
	}		
	*/
	//newly created
	public void setSegmentPartialTextData(int number)
	{
		if(number >= 0 && number < maxSegmentPartialNumber)		
			segmentPartialTextData[number].createSegmentPartialData(this, this.focus.focusSegments, 0);
	}
	//newly created
	public void setSentencePartialTextData(int number)
	{
		if(number >= 0 && number < maxSentencePartialNumber)		
			sentencePartialTextData[number].createSentencePartialData(this, this.focus.focusSentences, 0);
	}	


//	public TextData(String sourcefile, String chafile, SettingData sp1, String absolute, Control cont)
	//Executed when the shrink button pushed
    /*
    public void createSegmentPartialData(TextData text, int option) //text: originalText
	{
        System.out.println("Creating partial Segment "+text.textID+" : "+textID);
        
		sourceFilename = text.sourceFilename;
//		defaultCode = text.defaultCode;
		settingData = text.settingData;
		control = text.control;
		absolutePath = text.absolutePath;
        segmentTag = text.segmentTag;
        sentenceTag = text.sentenceTag;
		
		connection = text.connection;

		
		StringWriter sw = new StringWriter();			//originalText
		BufferedWriter bw = new BufferedWriter(sw);	
		
		try{
			for(int i=0;i<text.segmentNumber;i++)
				if(text.focus.focusSegments[i])
				{
					bw.write(text.segment[i].segmentText);
					bw.newLine();
				}
			bw.flush();
			bw.close();
		}
		catch(Exception e){
			System.out.println("writing ERROR in Creating Partial TextData3");
		}		
		
		originalText = sw.toString();		
		

		//Keywords Data
		keywordNumber = text.keywordNumber;		
		keyword = new KeywordData[keywordNumber];
		
		for(int i=0;i<keywordNumber;i++)
			keyword[i] = new KeywordData(text, i);

		int count = 0;
		for(int i=0;i<text.segmentNumber;i++)
			if(text.focus.focusSegments[i])
				for(int j=0;j<keywordNumber;j++)
				{
					keyword[j].frequency += text.keyword[j].frequencyInSegment[i];

				}

		//Segment Data		
		count = 0;
		for(int i=0;i<text.segmentNumber;i++)
			if(text.focus.focusSegments[i])
				count++;
		
		originalSegmentNumbers = new int[count];		
		segmentNumber = count;		
		segment = new SegmentData[segmentNumber];
		
		count = 0;
		int count2 = 0;
		for(int i=0;i<text.segmentNumber;i++)
			if(text.focus.focusSegments[i])
			{
//                System.out.println("i====="+i);
				originalSegmentNumbers[count] = i;
				segment[count] = new SegmentData(i, text.segment[i].segmentText, count);
				segment[count].positionOfFirstSentence = count2;
				count2 += text.segment[i].sentenceNumber;
				count++;				
			}
		
		//Sentence Data
		sentenceNumber = count2;
		originalSentenceNumbers = new int[count2];
		
		count = 0;
		count2 = 0;
		for(int i=0;i<text.segmentNumber;i++)
			if(text.focus.focusSegments[i])
			{
				segment[count].initializeSentence(text, i);
				
				for(int j=0;j<text.segment[i].sentenceNumber;j++)
					originalSentenceNumbers[count2+j] = text.segment[i].positionOfFirstSentence+j;
				
				count2 += text.segment[i].sentenceNumber;				
				count++;
			}		

		initializeData(option);
		
//		count=0;
//		for(int i=0;i<segmentNumber;i++)
//			for(int j=0;j<segment[i].sentenceNumber;j++)
//				System.out.println("ORIGINAL "+originalSegmentNumbers[i]+" "+originalSentenceNumbers[count++]);
	}	
*/
    public void createSegmentPartialData(TextData text, boolean focusSegments[], int option) //text: originalText
    {
        System.out.println("Creating partial Segment "+text.textID+" : "+textID);
        
        sourceFilename = text.sourceFilename;
        //        defaultCode = text.defaultCode;
        settingData = text.settingData;
        control = text.control;
        absolutePath = text.absolutePath;
        segmentTag = text.segmentTag;
        sentenceTag = text.sentenceTag;
        
        connection = text.connection;
        
        
        StringWriter sw = new StringWriter();            //originalText
        BufferedWriter bw = new BufferedWriter(sw);
        
        try{
            for(int i=0;i<text.segmentNumber;i++)
                if(focusSegments[i])
                {
                    bw.write(text.segment[i].segmentText);
                    bw.newLine();
                }
            bw.flush();
            bw.close();
        }
        catch(Exception e){
            System.out.println("writing ERROR in Creating Partial TextData3");
        }
        
        originalText = sw.toString();
        
        
        //Keywords Data
        keywordNumber = text.keywordNumber;
        keyword = new KeywordData[keywordNumber];
        
        for(int i=0;i<keywordNumber;i++)
            keyword[i] = new KeywordData(text, i);
        
        int count = 0;
        for(int i=0;i<text.segmentNumber;i++)
            if(focusSegments[i])
                for(int j=0;j<keywordNumber;j++)
                {
                    keyword[j].frequency += text.keyword[j].frequencyInSegment[i];
                    
                }
        
        //Segment Data
        count = 0;
        for(int i=0;i<text.segmentNumber;i++)
            if(focusSegments[i])
                count++;
        
        originalSegmentNumbers = new int[count];
        segmentNumber = count;
        segment = new SegmentData[segmentNumber];
        
        count = 0;
        int count2 = 0;
        for(int i=0;i<text.segmentNumber;i++)
            if(focusSegments[i])
            {
                //                System.out.println("i====="+i);
                originalSegmentNumbers[count] = i;
                segment[count] = new SegmentData(i, text.segment[i].segmentText, count);
                segment[count].positionOfFirstSentence = count2;
                count2 += text.segment[i].sentenceNumber;
                count++;
            }
        
        //Sentence Data
        sentenceNumber = count2;
        originalSentenceNumbers = new int[count2];
        
        count = 0;
        count2 = 0;
        for(int i=0;i<text.segmentNumber;i++)
            if(focusSegments[i])
            {
                segment[count].initializeSentence(text, i);
                
                for(int j=0;j<text.segment[i].sentenceNumber;j++)
                    originalSentenceNumbers[count2+j] = text.segment[i].positionOfFirstSentence+j;
                
                count2 += text.segment[i].sentenceNumber;
                count++;
            }
        
        initializeData(option);
        
        //        count=0;
        //        for(int i=0;i<segmentNumber;i++)
        //            for(int j=0;j<segment[i].sentenceNumber;j++)
        //                System.out.println("ORIGINAL "+originalSegmentNumbers[i]+" "+originalSentenceNumbers[count++]);
    }
    
	//Executed when the shrink button pushed        //CURRENTLY NOT USED    should be re-calculate segment value such as posiNega
	public void createSentencePartialData(TextData text, boolean focusSentences[], int option)
	{
		sourceFilename = text.sourceFilename;
//		defaultCode = text.defaultCode;
		settingData = text.settingData;
		control = text.control;
		absolutePath = text.absolutePath;
        segmentTag = text.segmentTag;
        sentenceTag = text.sentenceTag;
        
		connection = text.connection;

		
		StringWriter sw = new StringWriter();			//originalText
		BufferedWriter bw = new BufferedWriter(sw);	
		
		try{
			for(int i=0;i<text.sentenceNumber;i++)
				if(focusSentences[i])
				{
					bw.write(text.sentence[i].sentenceText);
//					bw.newLine();
				}
			bw.flush();
		}
		catch(Exception e){
			System.out.println("writing ERROR in Creating Partial TextData1");
		}		
		
		originalText = sw.toString();		

		
		//Keywords Data
		keywordNumber = text.keywordNumber;		
		keyword = new KeywordData[keywordNumber];
		
		for(int i=0;i<keywordNumber;i++)
			keyword[i] = new KeywordData(text, i);
		
		for(int i=0;i<keywordNumber;i++)
			keyword[i].frequency = text.keyword[i].frequency;
		
		for(int i=0;i<text.sentenceNumber;i++)
			if(focusSentences[i] == false)
				for(int j=0;j<text.sentence[i].keywordNumber;j++)
				{
					keyword[text.sentence[i].keywordIDList[j]].frequency--;
					
				}
		

		//Segment Data		// should be re-calculate int posiNega;
		segmentNumber = text.segmentNumber;
		segment = new SegmentData[segmentNumber];
		
		originalSegmentNumbers = new int[text.segmentNumber];
		for(int i=0;i<text.segmentNumber;i++)
			originalSegmentNumbers[i] = i;
		
		int count = 0;
		int count2 = 0;
		for(int i=0;i<text.segmentNumber;i++)
		{
			String segmentText;
			try{
				count = count2;
				for(int j=0;j<text.segment[i].sentenceNumber;j++)
					if(focusSentences[text.segment[i].positionOfFirstSentence + j])
					{
						bw.write(text.sentence[count2].sentenceText);
						//					bw.newLine();
						count2++;
					}
				bw.flush();
			}
			catch(Exception e){
				System.out.println("writing ERROR in Creating Partial TextData2");
			}		
			
			segmentText = sw.toString();
			
			segment[i] = new SegmentData(i, segmentText, i);
			segment[i].positionOfFirstSentence = count;			
		}
		
		//Sentence Data		
		sentenceNumber = count2;
		originalSentenceNumbers = new int[count2];
		
		count2=0;
		for(int i=0;i<text.sentenceNumber;i++)
			if(focusSentences[i])
				originalSentenceNumbers[count2++] = i;

		for(int i=0;i<segmentNumber;i++)
			segment[i].initializePartialSentence(text, i);
		
		initializeData(option);
		
//		count=0;
//		for(int i=0;i<segmentNumber;i++)
//			for(int j=0;j<segment[i].sentenceNumber;j++)
//				System.out.println("ORIGINAL "+originalSegmentNumbers[i]+" "+originalSentenceNumbers[count++]);
	}	
	
	
//---------------------------	
	
    public String getSegmentTag()
    {
        return segmentTag;
    }
    
    public String getSentenceTag()
    {
        return sentenceTag;
    }
    
	public void fileLoad()
	{
		control.textDataLoadingAgain();//////////
		control.resetSelectpanelData(this);//////////
	}
	
	//option, 0:save only, 1: save and preprocess, 2: save, preprocess, and recalculation
	public void fileSave(String alltext, int option)
	{		
		File savefile = new File(sourceFilename);
		
		try
		{
            BufferedWriter bw1;
//            if(settingData.morpheme.equals("Igo"))
                bw1 = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(savefile), "SJIS"));
  //          else
    //            bw1 = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(savefile), defaultCode));
			
			bw1.write(alltext);	
			bw1.close();			
			System.out.println("Save text to the file: "+savefile.getName());
		}
		catch(Exception e){
			System.out.println("File Writing ERROR Text data");
		}
		
		if(option > 0)
			control.textDataReset(alltext);//////////
		if(option > 1)
			control.resetSelectpanelData();//////////
		
        EXECUTE.filecopy(sourceFilename,absolutePath+"REVISEDTEXT");
        System.out.println("Save to the file "+absolutePath+"REVISEDTEXT");		
	}
	
	public void fileSave(String subtexts[], int option)
	{
		File savefile = new File(sourceFilename);
        String alltext = "";
		
		try
		{
            StringWriter sw = new StringWriter();			//For words
            BufferedWriter bw = new BufferedWriter(sw);
            
            BufferedWriter bw1;
//            if(settingData.morpheme.equals("Igo"))
                bw1 = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(savefile), "SJIS"));
  //          else
    //            bw1 = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(savefile), defaultCode));
			
			for(int i=0;i<subtexts.length;i++)
			{
                bw.write(subtexts[i]);
				bw1.write(subtexts[i]);
			}
            bw.flush();
            bw.close();
			bw1.close();
			System.out.println("Save text to the file: "+savefile.getName());
			
            alltext = sw.toString();
		}
		catch(Exception e){
			System.out.println("File Writing ERROR Text data");
		}

		if(option > 0)
			control.textDataReset(alltext);//////////
		if(option > 1)		
			control.resetSelectpanelData();//////////
		
        EXECUTE.filecopy(sourceFilename,absolutePath+"REVISEDTEXT");
        System.out.println("Save to the file "+absolutePath+"REVISEDTEXT");		
	}		
	
	
	/*
	public void fileSaveOnly(String alltext)
	{
		File savefile = new File(sourceFilename);
		
		try
		{
            BufferedWriter bw1;
            if(settingData.morpheme.equals("Igo"))
                bw1 = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(savefile), "SJIS"));
            else
                bw1 = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(savefile), defaultCode));
			
			bw1.write(alltext);
			bw1.close();
			System.out.println("Save text to the file: "+savefile.getName());
		}
		catch(Exception e){
			System.out.println("File Writing ERROR Text data");
		}
		
        EXECUTE.filecopy(sourceFilename,absolutePath+"REVISEDTEXT");
        System.out.println("Save to the file "+absolutePath+"REVISEDTEXT");		
	}
	
	public void fileSaveOnly(String subtexts[])
	{
		File savefile = new File(sourceFilename);
		
		try
		{
            BufferedWriter bw1;
            if(settingData.morpheme.equals("Igo"))
                bw1 = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(savefile), "SJIS"));
            else
                bw1 = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(savefile), defaultCode));
            
			for(int i=0;i<subtexts.length;i++)
			{
				bw1.write(subtexts[i]);
			}
			bw1.close();
			System.out.println("Save text to the file: "+savefile.getName());
		}
		catch(Exception e){
			System.out.println("File Writing ERROR Text data");
		}
		
        EXECUTE.filecopy(sourceFilename,absolutePath+"REVISEDTEXT");
        System.out.println("Save to the file "+absolutePath+"REVISEDTEXT");		
	}
    
	public void fileSave(String alltext)
	{
		File savefile = new File(sourceFilename);
		
		try
		{
            BufferedWriter bw1;
            if(settingData.morpheme.equals("Igo"))
                bw1 = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(savefile), "SJIS"));
            else
                bw1 = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(savefile), defaultCode));
                
			bw1.write(alltext);	
			bw1.close();			
			System.out.println("Save text to the file: "+savefile.getName());
		}
		catch(Exception e){
			System.out.println("File Writing ERROR Text data");
		}
		control.textDataLoadingWithoutCopy();//////////
		control.resetSelectpanelData();//////////
		
        EXECUTE.filecopy(sourceFilename,absolutePath+"REVISEDTEXT");
        System.out.println("Save to the file "+absolutePath+"REVISEDTEXT");		
	}
	
	public void fileSave(String subtexts[])
	{
		File savefile = new File(sourceFilename);
		
		try
		{
            BufferedWriter bw1;
            if(settingData.morpheme.equals("Igo"))
                bw1 = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(savefile), "SJIS"));
            else
                bw1 = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(savefile), defaultCode));

			for(int i=0;i<subtexts.length;i++)
			{	
				bw1.write(subtexts[i]);
			}	
			bw1.close();
			System.out.println("Save text to the file: "+savefile.getName());			
		}
		catch(Exception e){
			System.out.println("File Writing ERROR Text data");
		}
		control.textDataLoadingWithoutCopy();//////////
		control.resetSelectpanelData();//////////
		
        EXECUTE.filecopy(sourceFilename,absolutePath+"REVISEDTEXT");
        System.out.println("Save to the file "+absolutePath+"REVISEDTEXT");		
	}	
*/

	public void createKeywordAppearance()
	{
		int id;
		
		for(int i=0;i<keywordNumber;i++)
			keyword[i].initializeTables(segmentNumber, sentenceNumber);
		
		for(int i=0, k=0;i<segmentNumber;i++)
			for(int j=0;j<segment[i].sentenceNumber;j++,k++)
				for(int n=0;n<segment[i].sentence[j].keywordNumber;n++)
				{
					id = segment[i].sentence[j].keywordIDList[n];

					if(keyword[id].lastAppearingSegment != i && keyword[id].frequency > 0)
						// Avoid from counting twice in a segment, Noise may have id but frequency must be zero					
					{
						keyword[id].appearingSegmentTable[i] = 1;
						keyword[id].lastAppearingSegment = keyword[id].appearingSegment[keyword[id].segmentFrequency++] = i;
					}
					
					if(keyword[id].lastAppearingSentence != k && keyword[id].frequency > 0)
						// Avoid from counting twice in a sentence, Noise may have id but frequency must be zero					
					{
						keyword[id].appearingSentenceTable[k] = 1;
						keyword[id].lastAppearingSentence = keyword[id].appearingSentence[keyword[id].sentenceFrequency++] = k;
						
						keyword[id].sentenceFrequencyInSegment[i]++;
					}
					keyword[id].frequencyInSegment[i]++;
				}
	}
	
	public void createKeywordAppearanceSegmentOnly()
	{
		int id;
		
		for(int i=0;i<keywordNumber;i++)
			keyword[i].initializeSegmentTable(segmentNumber);
		
		for(int i=0, k=0;i<segmentNumber;i++)
			for(int j=0;j<segment[i].sentenceNumber;j++,k++)
				for(int n=0;n<segment[i].sentence[j].keywordNumber;n++)
				{
					id = segment[i].sentence[j].keywordIDList[n];
					
					if(keyword[id].lastAppearingSegment != i && keyword[id].frequency > 0)
						// Avoid from counting twice in a segment, Noise may have id but frequency must be zero					
					{
						keyword[id].appearingSegmentTable[i] = 1;
						keyword[id].lastAppearingSegment = keyword[id].appearingSegment[keyword[id].segmentFrequency++] = i;
					}
					
					if(keyword[id].lastAppearingSentence != k && keyword[id].frequency > 0)
						// Avoid from counting twice in a sentence, Noise may have id but frequency must be zero					
					{
						keyword[id].sentenceFrequencyInSegment[i]++;
					}
					keyword[id].frequencyInSegment[i]++;
				}
	}

	public void createKindNumbers()
	{
		createKindNumbersForSegment();
		createKindNumbersForSentence();		
	}
	
	public void createKindNumbersForSegment()
	{
		int kindNumberList[] = new int[segmentNumber];		
		for(int i=0;i<segmentNumber;i++)
		{
			kindNumberList[i] = 0;
			
			for(int j=0;j<keywordNumber;j++)
				for(int k=0;k<segmentNumber && keyword[j].appearingSegment[k] != -1;k++)
					kindNumberList[keyword[j].appearingSegment[k]]++;
			
			segment[i].kindKeywordNumber = kindNumberList[i];
		}
	}
	
	public void createKindNumbersForSentence()
	{
		int kindNumberList[] = new int[sentenceNumber];		
		for(int i=0;i<sentenceNumber;i++)
		{
			kindNumberList[i] = 0;
			
			for(int j=0;j<keywordNumber;j++)
				for(int k=0;k<sentenceNumber && keyword[j].appearingSentence[k] != -1;k++)
					kindNumberList[keyword[j].appearingSentence[k]]++;
			
			sentence[i].kindKeywordNumber = kindNumberList[i];
		}
	}	

    //---------------------------
    
    
    public int[] getCommonKeywords(boolean segments[])
    {
        int commonKeywords[] = new int[keywordNumber];
        
        for(int i=0;i<keywordNumber;i++)
            commonKeywords[i] = 0;
        
        for(int i=0;i<keywordNumber;i++)
            for(int j=0;j<segmentNumber;j++)
            {
                if(segments[j] == false)
                    continue;
                
                if(keyword[i].frequencyInSegment[j] == 0)
                {
                    commonKeywords[i] = 0;
                    break;
                }
                else
                    commonKeywords[i] += keyword[i].frequencyInSegment[j];
            }
        
        return commonKeywords;
    }
    
//---------------------------
	
	public String nameList[];
	int frequencyList[];
	int coFrequencyList[][];	
	
	public void createTextRelation()
	{
        System.out.println("Start SegmentRelation");        
		createSegmentRelation();
        System.out.println("Start SentenceRelation");
		createSentenceRelation();
        System.out.println("Start KeywordsRelation");         
		createKeywordsRelation();
	}
	
	public void createSegmentRelation()
	{
		int limit = segmentRelationLimit;
		
		if(segmentNumber > limit)
			System.out.println("NOT CREATED SEGMENT RELATION segmentNumber/max =:"+segmentNumber +"/"+limit);
		else
		{
			Pattern pattern = Pattern.compile("([A-Za-z0-9_]+.txt)");		//TEXT FILE NAME
			
			segmentRelation = new RelationalData(segmentNumber);
			initializeLists(segmentNumber);
			for(int i=0;i<segmentNumber;i++)
				if(segment[i].sentenceNumber > 0)				
				{
					Matcher matcher = pattern.matcher(segment[i].sentence[0].sentenceText);
					if(matcher.find())
						nameList[i] = matcher.group(1);
					else
						nameList[i] = "SEG"+(segment[i].segmentID+1);					
				}
				else
					nameList[i] = "SEG"+(segment[i].segmentID+1);
			
			for(int i=0;i<segmentNumber;i++)
				frequencyList[i] = segment[i].kindKeywordNumber;
			for(int i=0;i<segmentNumber;i++)
				for(int j=0;j<=i;j++)
				{
					coFrequencyList[i][j] = coFrequencyList[j][i] = 0;
					for(int k=0;k<keywordNumber;k++)
						coFrequencyList[i][j] = coFrequencyList[j][i] += (keyword[k].appearingSegmentTable[i] * keyword[k].appearingSegmentTable[j]);
				}
			calcurateValues(segmentRelation);
		}		
	}
	
	public void createSentenceRelation()
	{
		int limit = sentenceRelationLimit;
		
		if(sentenceNumber > limit)
			System.out.println("NOT CREATED SENTENCE RELATION sentenceNumber/max =:"+sentenceNumber +"/"+limit);
		else
		{
			sentenceRelation = new RelationalData(sentenceNumber);
			initializeLists(sentenceNumber);
			for(int i=0;i<sentenceNumber;i++)
				nameList[i] = "SEN"+(sentence[i].sentenceID+1);
			for(int i=0;i<sentenceNumber;i++)
				frequencyList[i] = sentence[i].kindKeywordNumber;
			for(int i=0;i<sentenceNumber;i++)
				for(int j=0;j<=i;j++)
				{
					coFrequencyList[i][j] = coFrequencyList[j][i] = 0;
					for(int k=0;k<keywordNumber;k++)
						coFrequencyList[i][j] = coFrequencyList[j][i] += (keyword[k].appearingSentenceTable[i] * keyword[k].appearingSentenceTable[j]);
				}
			calcurateValues(sentenceRelation);
		}		
	}
	
	public void createKeywordsRelation()
	{
		int limit = keywordRelationLimit;
        
		if(keywordNumber > limit)
			System.out.println("NOT CREATED KEYWORD RELATION keywordNumber/max =:"+keywordNumber +"/"+limit);
		else
		{
            //keyword relation by segment
			keywordRelationBySegment = new RelationalData(keywordNumber);
			initializeLists(keywordNumber);
			for(int i=0;i<keywordNumber;i++)
				nameList[i] = keyword[i].word;
			for(int i=0;i<keywordNumber;i++)
				frequencyList[i] = keyword[i].segmentFrequency;
			for(int i=0;i<keywordNumber;i++)
				for(int j=0;j<=i;j++)
				{
					coFrequencyList[i][j] = coFrequencyList[j][i] = 0;
					for(int k=0;k<segmentNumber;k++)
						coFrequencyList[i][j] = coFrequencyList[j][i] += (keyword[i].appearingSegmentTable[k] * keyword[j].appearingSegmentTable[k]);
				}
			calcurateValues(keywordRelationBySegment);
			
            //keyword relation by sentence
            /*
			keywordRelationBySentence = new RelationalData(keywordNumber);
			initializeLists(keywordNumber);
			for(int i=0;i<keywordNumber;i++)
				nameList[i] = keyword[i].word;
			for(int i=0;i<keywordNumber;i++)
				frequencyList[i] = keyword[i].sentenceFrequency;
			for(int i=0;i<keywordNumber;i++)
				for(int j=0;j<=i;j++)
				{
					coFrequencyList[i][j] = coFrequencyList[j][i] = 0;
					for(int k=0;k<sentenceNumber;k++)
						coFrequencyList[i][j] = coFrequencyList[j][i] += (keyword[i].appearingSentenceTable[k] * keyword[j].appearingSentenceTable[k]);
				}
			calcurateValues(keywordRelationBySentence);
             */
		}
	}
	
	void initializeLists(int num)
	{
		nameList = new String[num];
		frequencyList = new int[num];
		coFrequencyList = new int[num][num];
	}
	
	void calcurateValues(RelationalData rd)
	{
		rd.setName(nameList);
		rd.setFrequency(frequencyList);
		rd.setCoFrequency(coFrequencyList);
		rd.createCondValue();
		rd.createCosValue();
	}


	
//---------------------------	
	

	//Save load data to buffer and copies after all
	public void readChasenFile(SettingData sp1)
	{

		String ss, ssNext;
        String ssOriginal;
		String tempword, gomi1, endformword, hinshi, tempword2;
		int notr = 0;		//Flag for eliminating new-lines without words at the end of a segment
		
	


		
		String keywords = "";
		
		String japaneseWords[];
		japaneseWords = FILEIO.TextFileAllReadCodeArray(new File(absolutePath + "source" + File.separator + "TextData" + File.separator + "Japanese.txt"));		

		try
		{
			int countw1 = 0;	//Number of keywords in a sentence
			int countw2 = 0;	//Number of all words in a sentence
			int counts = 0;		//Number of lines
			int total_counts = 0;	//Number of all lines in original text
			int seg_n = 0;		//Number of segment that is currently loaded

			int countSub = 999;		//For search subjects(Nominative)

			boolean checked = false;	//For checking subjects
            
			String DataInJapanese[];
			String filename = absolutePath + "source" + File.separator + "japaneseText"+ File.separator  + "JapaneseMakecha.txt";
			File jpname = new File(filename);
			DataInJapanese = FILEIO.TextFileAllReadCodeArray(jpname);

		
			BufferedReader br1;
			
			////
			if(sp1.isEnglish)
			{
				System.out.println("[ENGLISH MODE]");								
				System.out.println("[ENGLISH STEMMING]");
                
                //AUTO CHECK VERB IN settingData
                
                {
                    int check=0;
                    for(check=0;check<sp1.verbs;check++)
                        if(sp1.kind_verbs[check].equals(sp1.kind_speech[6]))
                            break;
                    if(check == sp1.verbs)  //add verb
                    {
                        String temp_verbs[] = new String[0];
                        if(sp1.verbs > 0)
                        {
                            temp_verbs = new String[sp1.verbs];
                            for(int i=0;i<sp1.verbs;i++)
                                temp_verbs[i] = sp1.kind_verbs[i];
                        }
                        sp1.kind_verbs = new String[sp1.verbs+1];
                        if(sp1.verbs > 0)
                        {
                            for(int i=0;i<sp1.verbs;i++)
                                sp1.kind_verbs[i] = temp_verbs[i];
                        }
                        sp1.kind_verbs[sp1.verbs++] = sp1.kind_speech[6];
                        
                    }
                }

                //AUTO CHECK PERIOD IN settingData
                
                {
                    int check=0;
                    for(check=0;check<sp1.touns;check++)
                        if(sp1.kind_touns[check].equals("."))
                            break;
                    if(check == sp1.touns)  //add verb
                    {
                        String temp_touns[] = new String[0];
                        if(sp1.touns > 0)
                        {
                            temp_touns = new String[sp1.touns];
                            for(int i=0;i<sp1.touns;i++)
                                temp_touns[i] = sp1.kind_touns[i];
                        }
                        sp1.kind_touns = new String[sp1.touns+1];
                        if(sp1.touns > 0)
                        {
                            for(int i=0;i<sp1.touns;i++)
                                sp1.kind_touns[i] = temp_touns[i];
                        }
                        sp1.kind_touns[sp1.touns++] = ".";
                        
                    }
                }
                
                //AUTO CHECK END
				
				Stemmer s = new Stemmer();
				
				StringWriter swStem = new StringWriter();			//For words
				BufferedWriter bwStem = new BufferedWriter(swStem);
				
				char[] w = new char[501];
			
				{
					StringReader sr = new StringReader(originalText);				
					BufferedReader brStem = new BufferedReader(sr);					
						
					try
					{ 
						while(true)
						{
							int ch = brStem.read();
							if (Character.isLetter((char) ch))
							{
								int j = 0;
								while(true)
								{  
									ch = Character.toLowerCase((char) ch);
									w[j] = (char) ch;
									if (j < 500) 
										j++;
									ch = brStem.read();
									if (!Character.isLetter((char) ch))
									{
										/* to test add(char ch) */
										for (int c = 0; c < j; c++) s.add(w[c]);
									
										/* or, to test add(char[] w, int j) */
										/* s.add(w, j); */
									
										s.stem();
										{  
											String u;
										
											/* and now, to test toString() : */
											u = s.toString();
										
											/* to test getResultBuffer(), getResultLength() : */
											/* u = new String(s.getResultBuffer(), 0, s.getResultLength()); */
										
//											if(u != null)
												bwStem.write(u);
								//				System.out.print(u);											
										}
										break;
									}
								}
							}
							if (ch < 0) 
								break;
					//		System.out.print((char)ch); //KIGOU
							bwStem.write((char)ch);							
						}
						
						bwStem.flush();
						bwStem.close();
					}
					catch (IOException e){  
						System.out.println("error reading english.txt");
					}			
				}
											
				StringWriter sw = new StringWriter();			//For words
				BufferedWriter bw = new BufferedWriter(sw);				
				
				try
				{
					StringReader srStem = new StringReader(swStem.toString());
					BufferedReader brStem = new BufferedReader(srStem);
                    
                    StringReader srOriginal = new StringReader(originalText);
                    BufferedReader brOriginal = new BufferedReader(srOriginal);
					
		//			System.out.println("WORDS= "+swStem.toString());
					
					ss = "";
                    ssOriginal = "";
					int count = 0, count2 = 0;
					while((ss = brStem.readLine()) != null)
					{
						String oneline[] = ss.split(" "); //SPACE
                        
                        ssOriginal = brOriginal.readLine();
						String onelineOriginal[] = ssOriginal.split(" "); //SPACE
						
						for(int i=0;i<oneline.length;i++)
						{
							String word = oneline[i];
                            String wordOriginal = onelineOriginal[i];
                            
							if(!word.equals(""))
							{
								char c = word.charAt(word.length()-1);
								
								if(c == ',')
									word = word.substring(0, word.length()-1);
								
								if(c == '.' || c == '!' || c == '?')//End of Sentence
								{
									word = word.substring(0, word.length()-1);
                                    wordOriginal = wordOriginal.substring(0, wordOriginal.length()-1);
									
					/**/				System.out.println(wordOriginal+"	"+DataInJapanese[4]+",*,*,*,*,*,"+word+",*,*");
									bw.write(wordOriginal+"	"+DataInJapanese[4]+",*,*,*,*,*,"+word+",*,*");
									bw.write(System.getProperty("line.separator"));										
									
					/**/				System.out.println(DataInJapanese[0]+"	"+DataInJapanese[1]+",*,*,*,*,*,"+DataInJapanese[0]+",*,*");
									bw.write(DataInJapanese[0]+"	"+DataInJapanese[1]+",*,*,*,*,*,"+DataInJapanese[0]+",*,*");
									bw.write(System.getProperty("line.separator"));									
								}								
								else if(word.equals(sp1.segment_tag))//End of Segment
								{
					/**/				System.out.println(sp1.segment_tag+"	"+DataInJapanese[4]+",*,*,*,*,*,"+sp1.segment_tag+",*,*");
									bw.write(sp1.segment_tag+"	"+DataInJapanese[4]+",*,*,*,*,*,"+sp1.segment_tag+",*,*");
									bw.write(System.getProperty("line.separator"));	
								}
								else//Each Word
								{
					/**/				System.out.println(wordOriginal+"	"+DataInJapanese[4]+",*,*,*,*,*,"+word+",*,*");
									bw.write(wordOriginal+"	"+DataInJapanese[4]+",*,*,*,*,*,"+word+",*,*");
									bw.write(System.getProperty("line.separator"));							
								}
							}				
						}
					}
					
					bw.write("EOS");    //last of the English document
					bw.write(System.getProperty("line.separator"));
					bw.flush();
					bw.close();
				}
				catch(Exception e){
					System.out.println("File Reading ERROR in TextData ENGLISH PROCESS");
				}
				
				StringReader sr = new StringReader(sw.toString());
				br1 = new BufferedReader(sr);
				
			}
			else if(sp1.isCsv) //CSV FILE PROCESS
			{
				StringWriter sw = new StringWriter();			//For words
				BufferedWriter bw = new BufferedWriter(sw);				
				
				try
				{
					boolean endOfSegment = false;
					
					StringReader sr = new StringReader(originalText);
					BufferedReader br = new BufferedReader(sr);
					
					while((ss = br.readLine()) != null)
					{
						System.out.println(ss);
						
						String oneline[] = ss.split(","); //Comma
						
						for(int i=0;i<oneline.length;i++)
						{
							String word = oneline[i];
							if(!word.equals(""))
							{
								if(word.equals(sp1.segment_tag))//End of Segment
								{
						//			System.out.println(word+"	"+DataInJapanese[4]+",*,*,*,*,*,"+word+",*,*");
									bw.write(word+"	"+DataInJapanese[4]+",*,*,*,*,*,"+word+",*,*");
									bw.write(System.getProperty("line.separator"));							

									endOfSegment = true;
								}
								else//Each Word
								{
					//				System.out.println(word+"	"+DataInJapanese[4]+",*,*,*,*,*,"+word+",*,*");
									bw.write(word+"	"+DataInJapanese[4]+",*,*,*,*,*,"+word+",*,*");
									bw.write(System.getProperty("line.separator"));							
								}
							}				
						}
						
						if(!endOfSegment)//End of Sentence
						{
					//		System.out.println(DataInJapanese[0]+"	"+DataInJapanese[1]+",*,*,*,*,*,"+DataInJapanese[0]+",*,*");
							bw.write(DataInJapanese[0]+"	"+DataInJapanese[1]+",*,*,*,*,*,"+DataInJapanese[0]+",*,*");
							bw.write(System.getProperty("line.separator"));
						}
						endOfSegment = false;
					}
					
					
					bw.write("EOS");			//last of the CSV document
					bw.write(System.getProperty("line.separator"));	//need
					bw.flush();
					bw.close();

				}
				catch(Exception e){
					System.out.println("writing ERROR in TextData CSV PROCESS");
				}
				
				StringReader sr = new StringReader(sw.toString());
				br1 = new BufferedReader(sr);				
				
			}
			else // GENERAL PROCESS
			{

				StringWriter sw = new StringWriter();			//For words
				BufferedWriter bw = new BufferedWriter(sw);	
			
				try{		
				
					final String dicDir = control.sp1.dictionaryPath+File.separator+"ipadic";
					final Tagger tagger = new Tagger(dicDir);
				
					int lastLength, lastStart, length;
					lastLength = 0;
					lastStart = 0;
					
					for(Morpheme m : tagger.parse(originalText))
					{
						for(int i=0;i<(m.start - lastStart) - lastLength;i++)	//If invisible words are included
						{
                            String invisibleWord = originalText.substring(lastStart+lastLength+i, lastStart+lastLength+i+1);
                            
                            if(invisibleWord.equals(System.getProperty("line.separator")) || invisibleWord.charAt(0) == 10)
                                bw.write("EOS");
                            else
                                bw.write(DataInJapanese[5]+"\t"+"SPACE/TAB,*,*,*,*,*,X,*,*");
                            
							bw.write(System.getProperty("line.separator"));
						}
						lastLength = m.surface.length();
						lastStart = m.start;
											
						
//						System.out.println(m.surface+"	"+m.start); //TAB
						
								//			System.out.println(m.surface+"	"+m.feature); //TAB
						bw.write(m.surface+"	"+m.feature);	//TAB
						bw.write(System.getProperty("line.separator"));
							
					}

					bw.write("EOS");			   // last of the Japanese document
					bw.write(System.getProperty("line.separator"));	//need
					bw.flush();
					bw.close();
				}
				catch(Exception e){
					System.out.println("writing ERROR in TextData GENERAL PROCESS");
				}			
			
				StringReader sr = new StringReader(sw.toString());
				br1 = new BufferedReader(sr);


			}
			
			
			StringWriter sw1 = new StringWriter();			//For words
			BufferedWriter bw1 = new BufferedWriter(sw1);
			StringWriter sw2 = new StringWriter();			//For word IDs
			BufferedWriter bw2 = new BufferedWriter(sw2);
			StringWriter sw3 = new StringWriter();			//For keywords
			BufferedWriter bw3 = new BufferedWriter(sw3);
			StringWriter sw4 = new StringWriter();			//For end-form of words
			BufferedWriter bw4 = new BufferedWriter(sw4);
			
			StringWriter sw5 = new StringWriter();			//For subject words
			BufferedWriter bw5 = new BufferedWriter(sw5);			

			ss = br1.readLine();
			

			
			for(;ss != null; ss = ssNext)
			{
	//			System.out.println("SS= "+ss);

				ssNext = br1.readLine();
				
				if(ss.equals(""))
					continue;
			
				String oneline[] = ss.split("	"); //TAB
				tempword = oneline[0];
                
				if(tempword.equals("")) // for Windows
					continue;				

				
    //            System.out.println(tempword);//
      //         System.out.println("counts = "+counts+" total_counts= "+total_counts);//
				
                
				if(tempword.equals("EOS"))
				{

					bw1.newLine();
					bw1.write(" ");

					bw4.newLine();
					bw4.write(" ");
					bw2.write("-8 ");
					bw5.write("-8 ");					

					countw2++;
					continue;
				}
                
//                if(igo)
                {
                    String oneline9[] = oneline[1].split(",");//Igo/////////////
					
//					if(oneline9.length == 7)	//kigo-kaigyo , meishi-michigo
//						continue;
					
                    endformword = oneline9[6];
					if(endformword.equals("*"))//unknown words for IGO
						endformword = tempword;
					
                    hinshi = oneline9[0];//+"-"+oneline9[1]+"-"+oneline9[2]+"-"+oneline9[3];
                    for(int i=0;i<3;i++)
                        if(oneline9[i+1].equals("*"))
                            break;
                        else
                            hinshi = hinshi+"-"+oneline9[i+1];
                }
  //              else
    //            {
      //              endformword = oneline[2];
        //            hinshi = oneline[3];
          //      }
/*
				gomi1 = oneline[1];

 */

				//SEGMENT DIVISION
				if(tempword.equals(sp1.segment_tag))	//The end of the segment [sunaribarafuto]
				{
                    String oneline2[] = ssNext.split("	"); //TAB
                    tempword2 = oneline2[0];
 
                    
					//Process at the end of a sentence//Do not count as sentence that is only new-line
					if(countw2 > 0 && notr == 1)
					{
                        counts++;
                        bw1.write(" @@");
						bw2.write(" @");
						bw3.write(countw1+" ");
						bw4.write(" @@");
						bw5.write(" @");
	//					System.out.println("SEN:"+counts+" "+countw1+"/"+countw2);//  //
						countw1 = countw2 = 0;
						notr = 0;
						countSub = 999; //NEW
					}
                        
					//Process at the end of a segment
					segment[seg_n].sentenceNumber = counts;
					segment[seg_n].positionOfFirstSentence = total_counts;
					total_counts += counts;
					bw1.flush();
					bw2.flush();
					bw3.flush();
					bw4.flush();
					bw5.flush();
					segment[seg_n].initializeSentence(sw1.toString(), sw2.toString(), sw3.toString(), sw4.toString(), sw5.toString(), sp1.isEnglish);
					sw1.getBuffer().delete(0,sw1.getBuffer().length());
					sw2.getBuffer().delete(0,sw2.getBuffer().length());
					sw3.getBuffer().delete(0,sw3.getBuffer().length());
					sw4.getBuffer().delete(0,sw4.getBuffer().length());
					sw5.getBuffer().delete(0,sw5.getBuffer().length());
					seg_n++;
		//			System.out.println("SEG-"+seg_n+" sen:"+counts);//
					counts = 0;
					
                    if(sp1.leftTags == false)
                        continue;  // IF YOU WANT TO ADD TAG-WORD INTO TEXT DATA, NOT CONTINUE;  AND YOU ALSO CHANGE CutText method in FILEIO.java
				}
                
                
                //UNKNOWN word
                if(hinshi.equals(sp1.unknown))	
                {
                    bw1.write(tempword+" ");
					bw4.write(endformword+" ");
					bw2.write("-1 ");
					bw5.write("0 ");
					countw2++;
					notr = 1;
						
					if(countSub > 1)
						countSub = 0;			//Subject candidate
                        
                    continue;
				}
					


//System.out.println(tempword);//
//System.out.println(hinshi);//

				/*
				if(oneline[1].equals(""))	//For ChaSen bug { Symbol opening parenthesis //It's OK if morpheme was IGO
				{
					bw1.write(tempword+" ");
					bw4.write(endformword+" ");
					bw2.write("-2 ");
					bw5.write("0 ");					
					countw2++;
					notr = 1;
					continue;
				}
				 */

				bw1.write(tempword+" ");	//All other words
				bw4.write(endformword+" ");	//All other words
				notr = 1;
				countw2++;

				int k=0;	//For ID

	//			System.out.println("countw2= "+countw2+" notr= "+notr);
				
				// End of a sentence
				for(int i=0;i<sp1.touns;i++)
					if(tempword.equals(sp1.kind_touns[i]))
					{
						String oneline2[] = ssNext.split("	"); //TAB
						tempword2 = oneline2[0];
						
						if(tempword2.equals("EOS"))
						{
							bw1.newLine();
							bw1.write(" ");
							
							bw4.newLine();
							bw4.write(" ");
							bw2.write("-8 ");
							bw5.write("-8 ");					
							
							countw2++;
							ssNext = br1.readLine();
						}
						
						counts++;
						bw1.write(" @@");
						bw2.write("-3 @");
						bw3.write(countw1+" ");
						bw4.write(" @@");
						bw5.write("-3 @");
						
	//					System.out.println("SEN:"+counts+" "+countw1+"/"+countw2);// ///////
						countw1 = countw2 = 0;
						notr = 0;
						k=1;
						countSub = 9999; //NEW
//						System.out.println(tempword+"countS = "+countSub);						
					}
				
				if(countSub == 9999)
				{
					countSub++;
					continue;
				}

				countSub++;//ALL WORDS except ends of segments, sentences, and {

				checked = false;
				

                
				//Particles for finding subject
				for(int i=0;i<sp1.objects;i++)
					if(tempword.equals(sp1.kind_objects[i]) && hinshi.indexOf(sp1.kind_objects_speech[i]) == 0)
					{
						bw5.write(countSub+" ");
//						System.out.println("bw5="+countSub+" ");//FOR SUBJECT
						checked = true;						
						break;
					}

//				if(tempword.equals("is") || tempword.equals("are") || tempword.equals("am")  || tempword.equals("was") || tempword.equals("were") || tempword.equals("be"))
				if(tempword.equals("is") || tempword.equals("ar") || tempword.equals("am")  || tempword.equals("wa") || tempword.equals("were") || tempword.equals("be"))	//after stemming			
				{
					bw5.write(countSub+" ");
					//						System.out.println("bw5="+countSub+" ");//FOR SUBJECT
					checked = true;						
				}				

				//FOR FINDING SUBJECTS - Japanese-NOUN, English-VERB
                
                if(sp1.isEnglish) // english - verb
                {
                    if(hinshi.indexOf(japaneseWords[1]) == 0)
                    {
                        if(countSub > 1)
                            countSub = 0;
                    }
                }
                else    //japanese - noun
                {
                    if(hinshi.indexOf(japaneseWords[0]) == 0)
                    {
                        if(countSub > 1)
                            countSub = 0;
                    }
                }
                
                /*
				for(int i=0;i<japaneseWords.length;i++)
					if(hinshi.indexOf(japaneseWords[i]) == 0)
					{
						if(countSub > 1)
							countSub = 0;			//Subject candidate	
						
//						System.out.println(tempword+"countS = "+countSub);
					}
				*/
				
				//Process for nonNoise words: not excluded
				{
					int j;
					for(j=0;j<sp1.nonNoises;j++)
						if(endformword.equals(sp1.kind_nonNoises[j]))
							break;
					
					if(j == sp1.nonNoises) // if a word is not one of a non stop words
					{
				
				
						//Process for noise-1: Elimination of stop words
						{
							int i;
							for(i=0;i<sp1.noises;i++)
								if(endformword.equals(sp1.kind_noises[i]))
									break;
                            
                            int i2;
                            for(i2=0;i2<sp1.tempNoiseNumber;i2++)
                                if(endformword.equals(sp1.tempNoises[i2]))
                                    break;
                            
                            
							if(i < sp1.noises || i2 < sp1.tempNoiseNumber)
							{
								bw2.write("-4 ");
								if(!checked)
									bw5.write("-1 ");						
								continue;
							}
						}


                
						//Process for noise-2: Elimination of HIRAGANA, one letter except for periods(k==0)
						if(endformword.length() == 1 && k == 0)//For One character words
							if(sp1.excludeOne)
							{
								bw2.write("-5 ");
								if(!checked)
									bw5.write("-1 ");						
								continue;
							}

						char code = tempword.charAt(0);
						if(code>=0x3041 && code<=0x3093)	//For HIRAGANA words
							if(sp1.excludeHiragana)					
							{
								bw2.write("-6 ");
								if(!checked)
									bw5.write("-1 ");						
								continue;
							}

						if(code>=0x30a1 && code<=0x30f3)	//For KATAKANA words
							if(sp1.excludeKatakana)					
							{
								bw2.write("-6 ");
								if(!checked)
									bw5.write("-1 ");						
								continue;
							}
                
						if(tempword.getBytes("UTF-8").length == tempword.length()) //For HANKAKU words
							if(sp1.excludeHankaku)
							{
								bw2.write("-6 ");
								if(!checked)
									bw5.write("-1 ");
								continue;
							}
						
					}
				}
						
						
                
				//Keywords without conjugation
				for(int i=0;i<sp1.nouns;i++)
					if(hinshi.indexOf(sp1.kind_nouns[i]) == 0)
					{
						//Entry for Keyword list
						countw1++;
						int j;
						for(j=0;j<keywordNumber;j++)
							if(endformword.equals(keyword[j].word))
								break;
						if(j == keywordNumber)
						{
							keyword[keywordNumber].word = endformword;
							keywordNumber++;
							keywords += (tempword+" ");
						}

						keyword[j].frequency++;	//Count frequency


						bw2.write(j+" ");	

						if(!checked)
							bw5.write("-1 ");						
						k=1;
                        


						if(hinshi.indexOf(sp1.kind_speech[0]) ==0)
							keyword[j].partOfSpeech = 1;//NOUN
						else if(hinshi.indexOf(sp1.kind_speech[1]) ==0)
							keyword[j].partOfSpeech = 4;//CONJUNCTION
						else if(hinshi.indexOf(sp1.kind_speech[2]) ==0)
							keyword[j].partOfSpeech = 5;//ADVERB
						else if(hinshi.indexOf(sp1.kind_speech[3]) ==0)
							keyword[j].partOfSpeech = 9;//pre-noun adjectival						
						else if(hinshi.indexOf(sp1.kind_speech[4]) ==0)
							keyword[j].partOfSpeech = 6;//INTERJECTION
						else if(hinshi.indexOf(sp1.kind_speech[5]) ==0)
							keyword[j].partOfSpeech = 7;//PARTICLE
						
					}

				//Keywords with conjugation
				for(int i=0;i<sp1.verbs;i++)
					if(hinshi.indexOf(sp1.kind_verbs[i]) == 0)
					{
						//Entry for keyword list
						countw1++;
						int j;
						for(j=0;j<keywordNumber;j++)
							if(endformword.equals(keyword[j].word))
								break;
						if(j == keywordNumber)
						{
							keyword[keywordNumber].word = endformword;
							keywordNumber++;
							keywords += (" "+tempword);
						}
						keyword[j].frequency++;	//Count frequency

						bw2.write(j+" ");
						if(!checked)
							bw5.write("-1 ");						
						k=1;

						if(hinshi.indexOf(sp1.kind_speech[6]) ==0)
							keyword[j].partOfSpeech = 2;//VERB
						else if(hinshi.indexOf(sp1.kind_speech[7]) ==0)
							keyword[j].partOfSpeech = 3;//ADJECTIVE
						else if(hinshi.indexOf(sp1.kind_speech[8]) ==0)
							keyword[j].partOfSpeech = 8;//AUXILIARY VERB
					}
				if(k == 0)
				{
					bw2.write("-7 ");
					if(!checked)
						bw5.write("-1 ");					
				}

			}

			br1.close();

			if(countw2 > 1 && notr == 1)	//When text does not end with a period
											//Process at the end of sentences//Do not count as sentence which has only new-line
			{
				counts++;
				bw3.write(countw1+" ");
//				System.out.println("[SEN]:"+counts+" "+countw1+"/"+countw2);// //
			}

			//AT THE END OF A SEGMENT

			segment[seg_n].sentenceNumber = counts;
			segment[seg_n].positionOfFirstSentence = total_counts;
			total_counts += counts;
			bw1.flush();
			bw2.flush();
			bw3.flush();
			bw4.flush();
			bw5.flush();			
			segment[seg_n].initializeSentence(sw1.toString(), sw2.toString(), sw3.toString(), sw4.toString(), sw5.toString(), sp1.isEnglish);
			sw1.getBuffer().delete(0,sw1.getBuffer().length());
			sw2.getBuffer().delete(0,sw2.getBuffer().length());
			sw3.getBuffer().delete(0,sw3.getBuffer().length());
			sw4.getBuffer().delete(0,sw4.getBuffer().length());
			sw5.getBuffer().delete(0,sw5.getBuffer().length());			
			seg_n++;
			//System.out.println("[SEG]-"+seg_n+" sen:"+counts);//
			
			bw1.close();
			bw2.close();
			bw3.close();
			bw4.close();
			bw5.close();			
			

			sentenceNumber = total_counts;
		}
		catch(Exception e){
			System.out.println("File Reading ERROR chasen data in TextData.java");
		}
	}
}

