From: hikarupsp Date: Sat, 18 May 2013 04:14:01 +0000 (+0900) Subject: 単語抽出の向上 X-Git-Url: http://git.osdn.net/view?p=chnosproject%2FCHNOSProject.git;a=commitdiff_plain;h=02aa45935c78925c364a331d74f49c60723a180b 単語抽出の向上 --- diff --git a/CHNOSProject/AI003/AI003.xcodeproj/project.pbxproj b/CHNOSProject/AI003/AI003.xcodeproj/project.pbxproj index 7aa4a97..2072c82 100644 --- a/CHNOSProject/AI003/AI003.xcodeproj/project.pbxproj +++ b/CHNOSProject/AI003/AI003.xcodeproj/project.pbxproj @@ -7,6 +7,8 @@ objects = { /* Begin PBXBuildFile section */ + 17289436174406B600071834 /* AITestData_en.txt in CopyFiles */ = {isa = PBXBuildFile; fileRef = 172894331744069100071834 /* AITestData_en.txt */; }; + 17289437174406BB00071834 /* AITestData_ja.txt in CopyFiles */ = {isa = PBXBuildFile; fileRef = 172894351744069800071834 /* AITestData_ja.txt */; }; 173BB42616C6600100126A00 /* config.txt in CopyFiles */ = {isa = PBXBuildFile; fileRef = 173BB42316C65FBB00126A00 /* config.txt */; }; 173BB42816C6618500126A00 /* words.txt in CopyFiles */ = {isa = PBXBuildFile; fileRef = 173BB42716C660E000126A00 /* words.txt */; }; 175A87A516B5827800F5166E /* main.c in Sources */ = {isa = PBXBuildFile; fileRef = 175A87A416B5827800F5166E /* main.c */; }; @@ -31,6 +33,8 @@ dstPath = ""; dstSubfolderSpec = 16; files = ( + 17289437174406BB00071834 /* AITestData_ja.txt in CopyFiles */, + 17289436174406B600071834 /* AITestData_en.txt in CopyFiles */, 173BB42816C6618500126A00 /* words.txt in CopyFiles */, 173BB42616C6600100126A00 /* config.txt in CopyFiles */, ); @@ -49,6 +53,8 @@ /* End PBXCopyFilesBuildPhase section */ /* Begin PBXFileReference section */ + 172894331744069100071834 /* AITestData_en.txt */ = {isa = PBXFileReference; lastKnownFileType = text; path = AITestData_en.txt; sourceTree = ""; }; + 172894351744069800071834 /* AITestData_ja.txt */ = {isa = PBXFileReference; lastKnownFileType = text; path = AITestData_ja.txt; sourceTree = ""; }; 173BB42316C65FBB00126A00 /* config.txt */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; lineEnding = 2; path = config.txt; sourceTree = ""; }; 173BB42716C660E000126A00 /* words.txt */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; lineEnding = 2; path = words.txt; sourceTree = ""; }; 175A87A016B5827800F5166E /* AI003 */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = AI003; sourceTree = BUILT_PRODUCTS_DIR; }; @@ -97,6 +103,8 @@ 175A87AF16B5833500F5166E /* core.h */, 175A87A416B5827800F5166E /* main.c */, 173BB42716C660E000126A00 /* words.txt */, + 172894331744069100071834 /* AITestData_en.txt */, + 172894351744069800071834 /* AITestData_ja.txt */, ); path = AI003; sourceTree = ""; @@ -211,7 +219,7 @@ "$(BUILD_DIR)/$(CONFIGURATION)$(EFFECTIVE_PLATFORM_NAME)/usr/local/include", "$(INSTALL_ROOT)/usr/local/include", ); - MACOSX_DEPLOYMENT_TARGET = 10.7; + MACOSX_DEPLOYMENT_TARGET = 10.6; ONLY_ACTIVE_ARCH = YES; SDKROOT = macosx; }; @@ -235,7 +243,7 @@ GCC_WARN_UNINITIALIZED_AUTOS = YES; GCC_WARN_UNUSED_VARIABLE = YES; "HEADER_SEARCH_PATHS[arch=*]" = "$(INSTALL_ROOT)/usr/local/include"; - MACOSX_DEPLOYMENT_TARGET = 10.7; + MACOSX_DEPLOYMENT_TARGET = 10.6; SDKROOT = macosx; }; name = Release; diff --git a/CHNOSProject/AI003/AI003.xcodeproj/project.xcworkspace/xcuserdata/hikaru.xcuserdatad/UserInterfaceState.xcuserstate b/CHNOSProject/AI003/AI003.xcodeproj/project.xcworkspace/xcuserdata/hikaru.xcuserdatad/UserInterfaceState.xcuserstate index c40eec4..ce205d4 100644 Binary files a/CHNOSProject/AI003/AI003.xcodeproj/project.xcworkspace/xcuserdata/hikaru.xcuserdatad/UserInterfaceState.xcuserstate and b/CHNOSProject/AI003/AI003.xcodeproj/project.xcworkspace/xcuserdata/hikaru.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/CHNOSProject/AI003/AI003.xcodeproj/xcuserdata/hikaru.xcuserdatad/xcdebugger/Breakpoints.xcbkptlist b/CHNOSProject/AI003/AI003.xcodeproj/xcuserdata/hikaru.xcuserdatad/xcdebugger/Breakpoints.xcbkptlist index 56110ea..1840e46 100644 --- a/CHNOSProject/AI003/AI003.xcodeproj/xcuserdata/hikaru.xcuserdatad/xcdebugger/Breakpoints.xcbkptlist +++ b/CHNOSProject/AI003/AI003.xcodeproj/xcuserdata/hikaru.xcuserdatad/xcdebugger/Breakpoints.xcbkptlist @@ -86,11 +86,11 @@ ignoreCount = "0" continueAfterRunningActions = "No" filePath = "AI003/main.c" - timestampString = "384873562.138536" + timestampString = "390425099.955042" startingColumnNumber = "9223372036854775807" endingColumnNumber = "9223372036854775807" - startingLineNumber = "187" - endingLineNumber = "187" + startingLineNumber = "272" + endingLineNumber = "272" landmarkName = "AI_Memory_AddRootWordData()" landmarkType = "7"> @@ -136,11 +136,11 @@ ignoreCount = "0" continueAfterRunningActions = "No" filePath = "AI003/main.c" - timestampString = "383409448.815474" + timestampString = "390336991.21306" startingColumnNumber = "9223372036854775807" endingColumnNumber = "9223372036854775807" - startingLineNumber = "46" - endingLineNumber = "46" + startingLineNumber = "58" + endingLineNumber = "58" landmarkName = "main()" landmarkType = "7"> @@ -157,5 +157,109 @@ landmarkName = "CHNLIB_CString_GetCountOfContain()" landmarkType = "7"> + + + + + + + + + + + + + + + + diff --git a/CHNOSProject/AI003/AI003.xcodeproj/xcuserdata/hikaru.xcuserdatad/xcschemes/xcschememanagement.plist b/CHNOSProject/AI003/AI003.xcodeproj/xcuserdata/hikaru.xcuserdatad/xcschemes/xcschememanagement.plist index f22844d..57e1cd1 100644 --- a/CHNOSProject/AI003/AI003.xcodeproj/xcuserdata/hikaru.xcuserdatad/xcschemes/xcschememanagement.plist +++ b/CHNOSProject/AI003/AI003.xcodeproj/xcuserdata/hikaru.xcuserdatad/xcschemes/xcschememanagement.plist @@ -7,7 +7,7 @@ AI003.xcscheme orderHint - 1 + 0 SuppressBuildableAutocreation @@ -22,6 +22,11 @@ primary + 1728943B17440B5700071834 + + primary + + 175A879F16B5827800F5166E primary diff --git a/CHNOSProject/AI003/AI003/AITestData_en.txt b/CHNOSProject/AI003/AI003/AITestData_en.txt new file mode 100644 index 0000000..8e26446 --- /dev/null +++ b/CHNOSProject/AI003/AI003/AITestData_en.txt @@ -0,0 +1,17 @@ +Artificial intelligence, From Wikipedia, the free encyclopedia. +Last modified on 14 February 2013 at 15:08. + +Artificial Intelligence (AI) is the intelligence of machines and robots and the branch of computer science that aims to create it. +AI textbooks define the field as "the study and design of intelligent agents" where an intelligent agent is a system that perceives its environment and takes actions that maximize its chances of success. +John McCarthy, who coined the term in 1955, defines it as "the science and engineering of making intelligent machines." +AI research is highly technical and specialised, deeply divided into subfields that often fail to communicate with each other. +Some of the division is due to social and cultural factors: subfields have grown up around particular institutions and the work of individual researchers. +AI research is also divided by several technical issues. + There are subfields which are focused on the solution of specific problems, on one of several possible approaches, on the use of widely differing tools and towards the accomplishment of particular applications. +The central problems of AI include such traits as reasoning, knowledge, planning, learning, communication, perception and the ability to move and manipulate objects. +General intelligence (or "strong AI") is still among the field's long term goals. +Currently popular approaches include statistical methods, computational intelligence and traditional symbolic AI. +There are an enormous number of tools used in AI, including versions of search and mathematical optimization, logic, methods based on probability and economics, and many others. +The field was founded on the claim that a central property of humans, intelligence (the sapience of Homo sapiens) can be so precisely described that it can be emulated by a machine. +This raises philosophical issues about the nature of the mind and the ethics of creating artificial beings, issues which have been addressed by myth, fiction and philosophy since antiquity. +Artificial intelligence has been the subject of optimism, but has also suffered setbacks Today, it has become an essential part of the technology industry, providing the heavy lifting for many of the most difficult problems in computer science. \ No newline at end of file diff --git a/CHNOSProject/AI003/AI003/AITestData_ja.txt b/CHNOSProject/AI003/AI003/AITestData_ja.txt new file mode 100644 index 0000000..73c14a7 --- /dev/null +++ b/CHNOSProject/AI003/AI003/AITestData_ja.txt @@ -0,0 +1,34 @@ +Wikipedia「人工知能」 +最終更新 2012å¹´12月25日 (火) 04:33(UTC) + +人工知能(じんこうちのう、英: Artificial Intelligence, AI)は、コンピュータに人間と同様の知能を実現させようという試み、あるいはそのための一連の基礎技術をさす。 +「人工知能」という名前は1956年にダートマス会議でジョン・マッカーシーにより命名された。 +現在では、記号処理を用いた知能の記述を主体とする情報処理や研究でのアプローチという意味あいでも使われている。 +日常語としての「人工知能」という呼び名は非常に曖昧なものになっており、多少気の利いた家庭用電気機械器具の制御システムやゲームソフトの思考ルーチンなどがこう呼ばれることもある。 +プログラミング言語LISPによる「Eliza」というカウンセラーを模倣したプログラムがしばしば引き合いに出されるが、計算機に人間の専門家の役割をさせようという「エキスパートシステム」と呼ばれる研究・情報処理システムの実現は、人間が暗黙に持つ常識の記述が問題となり、実用への利用が困難視されている現状がある。 +人工的な知能の実現へのアプローチとしては、「ファジィ理論 (fuzzy theory)」や「ニューラルネットワーク」などのようなアプローチも知られているが、従来の人工知能(後述のGOFAI)との差は記述の記号的明示性にあると言えよう。 +近年では「サポートベクターマシン (SVM)」が注目を集めた。 +また、自らの経験を元に学習を行う強化学習という手法もある。 +日本には人工知能学会があり、オンラインで機関誌も読める。 +AIはふたつの学派に大別される。 +ひとつは従来からのAIであり、もうひとつは計算知能(Computational Intelligence、CI)である。 +従来からのAIは、現在では機械学習と呼ばれている手法を使い、フォーマリズムと統計分析を特徴としている。 +これは、記号的AI、論理的AI、正統派AI、古き良きAI(Good Old Fashioned Artificial Intelligence、GOFAI)などと呼ばれる。 +その手法としては、以下のようなものがある。 +エキスパートシステム:推論機能を適用することで結論を得る。 +エキスパートシステムは大量の既知情報を処理し、それらに基づいた結論を提供することができる。 +Microsoft Office に登場するペーパークリップの Clippy はひとつの例である。 +ユーザが文字列を打ち込むと、Clippy はそこに一定の特徴を認識し、それに沿った提案をする。 +事例ベース推論(CBR):その事例に類似した過去の事例をベースにし、部分修正を加え試行を行い、その結果とその事例を事例ベースに記憶する。 +ベイジアン・ネットワーク +ふるまいに基づくAI:AIシステムを一から構築していく手法 +計算知能は開発や学習を繰り返すことを基本としている(例えば、パラメータ調整、コネクショニズムのシステム)。 +学習は経験に基づく手法であり、非記号的AI、美しくない(scruffy)AI、ソフトコンピューティングと関係している。 +その手法としては、以下のものがある。 +ニューラルネットワーク:非常に強力なパターン認識力を持つシステム。 +コネクショニズムとほぼ同義。 +ファジィ制御:不確かな状況での推論手法であり、最近の制御システムでは広く採用されている。 +進化的計算:生物学からインスパイアされた手法であり、ある問題の最適解を進化や突然変異の概念を適用して求める。 +この手法は遺伝的アルゴリズムと群知能に分類される。 +これらを統合した知的システムを作る試みもなされている。 +ACT-Rでは、エキスパートの推論ルールを、統計的学習を元にニューラルネットワークや生成規則を通して生成する。 \ No newline at end of file diff --git a/CHNOSProject/AI003/AI003/core.h b/CHNOSProject/AI003/AI003/core.h index cf6513f..1773a87 100755 --- a/CHNOSProject/AI003/AI003/core.h +++ b/CHNOSProject/AI003/AI003/core.h @@ -22,9 +22,11 @@ struct AI_WORKING_SET { //@main.c extern AI_WorkingSet WorkingSet; +void AI_Think_LearnWordFromInputString(CHNLIB_String *input); CHNLIB_UIPArray *AI_Think_SlideLookUpWordByHistory(CHNLIB_String *input); -int AI_Think_CandidateWordList_Filter00(CHNLIB_UIPArray *candidatewordlist); -void AI_Memory_AddRootWordData(CHNLIB_String *tag); +int AI_Think_CandidateWordList_Filter00(CHNLIB_UIPArray **candidatewordlist); +int AI_Think_CandidateWordList_Filter01(CHNLIB_UIPArray **candidatewordlist, int length); +int AI_Memory_AddRootWordData(CHNLIB_String *tag); int AI_Memory_AddRootWordData_IsDuplicated(const void *listtag, const void *newtag); void AI_System_InitializeSystemWorkingSet(void); void AI_System_LoadMemory(const char configfilename[]); diff --git a/CHNOSProject/AI003/AI003/main.c b/CHNOSProject/AI003/AI003/main.c index 1f3f6b4..05fbd0d 100755 --- a/CHNOSProject/AI003/AI003/main.c +++ b/CHNOSProject/AI003/AI003/main.c @@ -24,8 +24,9 @@ int main(int argc, const char * argv[]) //エントリポイント int i, i_max; CHNLIB_String *input, *temp; - CHNLIB_UIPArray *separated; + CHNLIB_UIPArray *separated, *sorted; int passthink; + FILE *readfp; CHNLIB_Environment_SetCurrentWorkingDirectory(argv[0]); @@ -34,10 +35,21 @@ int main(int argc, const char * argv[]) AI_System_InitializeSystemWorkingSet(); AI_System_LoadMemory(AI_CONFIG_FILE_NAME); + + readfp = NULL; for(;;){ passthink = False; - input = CHNLIB_ReadLine(stdin); + if(readfp == NULL){ + input = CHNLIB_ReadLine(stdin); + } else{ + input = CHNLIB_ReadLine(readfp); + if(input == NULL){ + readfp = NULL; + } else{ + puts(CHNLIB_String_GetReferencePointerOfCString(input)); + } + } if(CHNLIB_String_CompareStringWithCString(input, "::")){ //システムコマンド解釈 @@ -52,28 +64,32 @@ int main(int argc, const char * argv[]) } else if(CHNLIB_String_CompareStringWithCString(temp, "exit")){ break; } else if(CHNLIB_String_CompareStringWithCString(temp, "wordlist")){ - i_max = CHNLIB_UIPArray_GetNumberOfDatas(WorkingSet.RootWordList); + sorted = CHNLIB_UIPArray_SortInDescendingOrderByData32(WorkingSet.RootWordList); + i_max = CHNLIB_UIPArray_GetNumberOfDatas(sorted); for(i = 0; i < i_max; i++){ - printf("word%3d:%s\n", i, CHNLIB_String_GetReferencePointerOfCString(CHNLIB_UIPArray_GetPointerByIndex(WorkingSet.RootWordList, i))); + printf("word%3d:%3d:%s\n", i, CHNLIB_UIPArray_GetData32ByIndex(sorted, i), CHNLIB_String_GetReferencePointerOfCString(CHNLIB_UIPArray_GetPointerByIndex(sorted, i))); + } + CHNLIB_UIPArray_FreeOnlyArray(&sorted); + } else if(CHNLIB_String_CompareStringWithCString(temp, "readfile")){ + //::readfile:filename + readfp = fopen(CHNLIB_String_GetReferencePointerOfCString(CHNLIB_UIPArray_GetPointerByIndex(separated, 4)), "rb"); + if(readfp == NULL){ + puts("File open error."); } } else{ passthink = False; } } + //WorkingSet.SystemWordList0を使っているので、解放しないように注意 CHNLIB_UIPArray_FreeSelectedAll(&separated); } if(!passthink){ //AIへの入力 - separated = CHNLIB_UIPArray_Initialize(); - separated = AI_Think_SlideLookUpWordByHistory(input); - i_max = CHNLIB_UIPArray_GetNumberOfDatas(separated); - printf("Index(Decimal),CountOfContain(Decimal), String\n"); - for(i = 0; i < i_max; i++){ - printf("%d,%d,%s\n", i, CHNLIB_UIPArray_GetData32ByIndex(separated, i), CHNLIB_String_GetReferencePointerOfCString(CHNLIB_UIPArray_GetPointerByIndex(separated, i))); - } - CHNLIB_UIPArray_FreeAll(&separated); + //::readfile:AITestData_ja.txt + AI_Think_LearnWordFromInputString(input); + CHNLIB_UIPArray_AppendLast(&WorkingSet.InputHistory, CHNLIB_UIPArray_GetNumberOfDatas(WorkingSet.InputHistory), input); } } @@ -81,6 +97,39 @@ int main(int argc, const char * argv[]) return 0; } +void AI_Think_LearnWordFromInputString(CHNLIB_String *input) +{ + //入力文字列から単語を抽出して記憶する。 + CHNLIB_UIPArray *candidateWordList; + int i, i_max; + int tagIndex, maxExistingWordIndex; + + candidateWordList = CHNLIB_UIPArray_Initialize(); + candidateWordList = AI_Think_SlideLookUpWordByHistory(input); + i_max = CHNLIB_UIPArray_GetNumberOfDatas(candidateWordList); + maxExistingWordIndex = CHNLIB_UIPArray_GetNumberOfDatas(WorkingSet.RootWordList) - 1; + + printf("Index(Decimal),CountOfContain(Decimal), String\n"); + for(i = 0; i < i_max; i++){ + printf("%d,%d,%s\n", i, CHNLIB_UIPArray_GetData32ByIndex(candidateWordList, i), CHNLIB_String_GetReferencePointerOfCString(CHNLIB_UIPArray_GetPointerByIndex(candidateWordList, i))); + + //単語リストに登録。 + tagIndex = AI_Memory_AddRootWordData(CHNLIB_UIPArray_GetPointerByIndex(candidateWordList, i)); + + CHNLIB_UIPArray_SetData32ByIndex(WorkingSet.RootWordList, tagIndex, CHNLIB_UIPArray_GetData32ByIndex(WorkingSet.RootWordList, tagIndex) + CHNLIB_UIPArray_GetData32ByIndex(candidateWordList, i)); + + if(tagIndex <= maxExistingWordIndex){ + //既存のタグなので、解放されるようにdata32をFalseにしておく。 + CHNLIB_UIPArray_SetData32ByIndex(candidateWordList, i, False); + } + //新規追加されたタグについては0以上の値つまりTrueが設定されているはずなので、解放されない。 + } + //data32 == Falseのタグ、つまり既存のタグと同じだったもののみ解放する。 + CHNLIB_UIPArray_FreeSelectedAll(&candidateWordList); + + return; +} + CHNLIB_UIPArray *AI_Think_SlideLookUpWordByHistory(CHNLIB_String *input) { //[UTF-8] @@ -144,47 +193,84 @@ CHNLIB_UIPArray *AI_Think_SlideLookUpWordByHistory(CHNLIB_String *input) CHNLIB_UIPArray_SetData32ByIndex(candidatewordlist, k, j); } + //重複抽出フィルタリング + AI_Think_CandidateWordList_Filter01(&candidatewordlist, 1); + AI_Think_CandidateWordList_Filter00(&candidatewordlist); + return candidatewordlist; } -int AI_Think_CandidateWordList_Filter00(CHNLIB_UIPArray *candidatewordlist) +int AI_Think_CandidateWordList_Filter00(CHNLIB_UIPArray **candidatewordlist) { //[Not implemented] //[UTF-8] //AI_Think_SlideLookUpWordByHistoryで返されたArray内の候補単語に対して、フィルターをかける。 + //00:長い単語に含まれており、かつ出現頻度が長い単語と等しい単語を削除 int i, i_max; CHNLIB_String *nowstr, *basestr; - int nowstrlen, basestrlen; + int basestrCoC; //CountOfContainInHistoryStrings - i_max = CHNLIB_UIPArray_GetNumberOfDatas(candidatewordlist); - basestr = CHNLIB_UIPArray_GetPointerByIndex(WorkingSet.InputHistory, 0); - //basestrlen = + i_max = CHNLIB_UIPArray_GetNumberOfDatas(*candidatewordlist); + basestr = CHNLIB_UIPArray_GetPointerByIndex(*candidatewordlist, 0); + basestrCoC = CHNLIB_UIPArray_GetData32ByIndex(*candidatewordlist, 0); for(i = 1; i < i_max; i++){ - nowstr = CHNLIB_UIPArray_GetPointerByIndex(WorkingSet.InputHistory, i); + nowstr = CHNLIB_UIPArray_GetPointerByIndex(*candidatewordlist, i); + if(CHNLIB_UTF8_GetCountOfContain(CHNLIB_String_GetReferencePointerOfCString(basestr), CHNLIB_String_GetReferencePointerOfCString(nowstr)) > 0){ + //nowstrはbasestrに含まれている + if(basestrCoC == CHNLIB_UIPArray_GetData32ByIndex(*candidatewordlist, i)){ + //かつ出現頻度が等しいので不要な単語 + //出現頻度を0にして、後で削除する。 + CHNLIB_UIPArray_SetData32ByIndex(*candidatewordlist, i, 0); + } + } + if(CHNLIB_UIPArray_GetData32ByIndex(*candidatewordlist, i) != 0){ + //単語は削除されなかった、つまり異なる単語なので、basestrを更新 + basestr = nowstr; + basestrCoC = CHNLIB_UIPArray_GetData32ByIndex(*candidatewordlist, i); + } } + + for(i = 1; i < i_max; i++){ + if(CHNLIB_UIPArray_GetData32ByIndex(*candidatewordlist, i) == 0){ + //削除対象 + CHNLIB_UIPArray_RemoveByIndex(candidatewordlist, i); + i--; + i_max--; + } + } + return 0; } -void AI_Memory_AddRootWordData(CHNLIB_String *tag) +int AI_Think_CandidateWordList_Filter01(CHNLIB_UIPArray **candidatewordlist, int length) { - //WorkingSet.RootWordListに文字列を追加する。 - //重複がある場合は警告を出し、追加しない。 + //[Not implemented] + //[UTF-8] + //AI_Think_SlideLookUpWordByHistoryで返されたArray内の候補単語に対して、フィルターをかける。 + //01:length以下の文字数の単語を削除 + int i, i_max; - i_max = CHNLIB_UIPArray_GetNumberOfDatas(WorkingSet.RootWordList); + i_max = CHNLIB_UIPArray_GetNumberOfDatas(*candidatewordlist); for(i = 0; i < i_max; i++){ - if(CHNLIB_String_CompareString_Strict(CHNLIB_UIPArray_GetPointerByIndex(WorkingSet.RootWordList, i), tag)){ - CHNLIB_ReportError("Word[%s] has already existed.", CHNLIB_DEBUG_ARGUMENTS, CHNLIB_String_GetReferencePointerOfCString(tag)); - return; + if(CHNLIB_UTF8_GetStringLengthByCharacter(CHNLIB_String_GetReferencePointerOfCString(CHNLIB_UIPArray_GetPointerByIndex(*candidatewordlist, i))) <= length){ + CHNLIB_UIPArray_RemoveByIndex(candidatewordlist, i); + i--; + i_max--; } } - CHNLIB_UIPArray_AppendLast(&WorkingSet.RootWordList, i_max, tag); - - CHNLIB_UIPArray_AppendLast_ProtectFromDuplication(&WorkingSet.RootWordList, CHNLIB_UIPArray_GetNumberOfDatas(WorkingSet.RootWordList), tag, &AI_Memory_AddRootWordData_IsDuplicated); + return 0; +} + +int AI_Memory_AddRootWordData(CHNLIB_String *tag) +{ + //WorkingSet.RootWordListに文字列を追加する。 + //重複がある場合は警告を出し、追加しない。 + //戻り値は追加されたまたは重複したタグのIndex. - return; + return CHNLIB_UIPArray_AppendLast_ProtectFromDuplication(&WorkingSet.RootWordList, 0, tag, &AI_Memory_AddRootWordData_IsDuplicated); } int AI_Memory_AddRootWordData_IsDuplicated(const void *listtag, const void *newtag) diff --git a/CHNOSProject/AI003/AI003/words.txt b/CHNOSProject/AI003/AI003/words.txt index 373dbbe..e69de29 100644 --- a/CHNOSProject/AI003/AI003/words.txt +++ b/CHNOSProject/AI003/AI003/words.txt @@ -1,4 +0,0 @@ -E -Fg -コンピューター -コンピュータ \ No newline at end of file diff --git a/CHNOSProject/CHNOSProject.xcodeproj/project.xcworkspace/xcuserdata/hikaru.xcuserdatad/UserInterfaceState.xcuserstate b/CHNOSProject/CHNOSProject.xcodeproj/project.xcworkspace/xcuserdata/hikaru.xcuserdatad/UserInterfaceState.xcuserstate index 88695c4..98a7b67 100644 Binary files a/CHNOSProject/CHNOSProject.xcodeproj/project.xcworkspace/xcuserdata/hikaru.xcuserdatad/UserInterfaceState.xcuserstate and b/CHNOSProject/CHNOSProject.xcodeproj/project.xcworkspace/xcuserdata/hikaru.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/CHNOSProject/chn/chn.xcodeproj/xcuserdata/hikaru.xcuserdatad/xcschemes/xcschememanagement.plist b/CHNOSProject/chn/chn.xcodeproj/xcuserdata/hikaru.xcuserdatad/xcschemes/xcschememanagement.plist index 9cc691f..bb04dd0 100644 --- a/CHNOSProject/chn/chn.xcodeproj/xcuserdata/hikaru.xcuserdatad/xcschemes/xcschememanagement.plist +++ b/CHNOSProject/chn/chn.xcodeproj/xcuserdata/hikaru.xcuserdatad/xcschemes/xcschememanagement.plist @@ -7,7 +7,7 @@ chn.xcscheme orderHint - 2 + 1 SuppressBuildableAutocreation diff --git a/CHNOSProject/chn/chnlib.h b/CHNOSProject/chn/chnlib.h index 5dc7f3a..f9a2e6b 100755 --- a/CHNOSProject/chn/chnlib.h +++ b/CHNOSProject/chn/chnlib.h @@ -76,21 +76,33 @@ void CHNLIB_System_FreeMemory(void *p, const char filename[], int line, const ch //@chnlib00.c typedef struct CHNLIB_UIPARRAY CHNLIB_UIPArray; struct CHNLIB_UIP_ARRAY; +//----配列の初期化 CHNLIB_UIPArray *CHNLIB_UIPArray_Initialize(void); +//----データの追加 int CHNLIB_UIPArray_AppendLast(CHNLIB_UIPArray **array, uint data32, void *pointer); int CHNLIB_UIPArray_AppendLast_ProtectFromDuplication(CHNLIB_UIPArray **array, uint data32, void *pointer, int (*IsDuplicated)(const void *listtag, const void *newtag)); +//----データの削除 int CHNLIB_UIPArray_RemoveByIndex(CHNLIB_UIPArray **array, int index); +//----配列の解放 void CHNLIB_UIPArray_FreeOnlyArray(CHNLIB_UIPArray **array); void CHNLIB_UIPArray_FreeOnlyAllPointer(CHNLIB_UIPArray *array); void CHNLIB_UIPArray_FreeOnlySelectedPointer(CHNLIB_UIPArray *array); void CHNLIB_UIPArray_FreeAll(CHNLIB_UIPArray **array); void CHNLIB_UIPArray_FreeSelectedAll(CHNLIB_UIPArray **array); +//----配列要素数取得 int CHNLIB_UIPArray_GetNumberOfDatas(const CHNLIB_UIPArray *array); +//----Data32取得・設定 uint CHNLIB_UIPArray_GetData32ByIndex(const CHNLIB_UIPArray *array, int index); uint CHNLIB_UIPArray_SetData32ByIndex(CHNLIB_UIPArray *array, int index, uint data32); +//----Pointer取得・設定 void *CHNLIB_UIPArray_GetPointerByIndex(const CHNLIB_UIPArray *array, int index); -int CHNLIB_UIPArray_GetIndexByData32(const CHNLIB_UIPArray *array, uint data32); void *CHNLIB_UIPArray_GetPointerByData32(const CHNLIB_UIPArray *array, uint data32); +//----Index取得 +int CHNLIB_UIPArray_GetIndexByData32(const CHNLIB_UIPArray *array, uint data32); +//----配列のコピー +CHNLIB_UIPArray *CHNLIB_UIPArray_CopyArray(const CHNLIB_UIPArray *source); +//----配列のソート +CHNLIB_UIPArray *CHNLIB_UIPArray_SortInDescendingOrderByData32(const CHNLIB_UIPArray *array); //@chnlib01.c typedef struct CHNLIB_STRING CHNLIB_String; diff --git a/CHNOSProject/chn/chnlib00.c b/CHNOSProject/chn/chnlib00.c index d3d0fa6..7f3990a 100755 --- a/CHNOSProject/chn/chnlib00.c +++ b/CHNOSProject/chn/chnlib00.c @@ -57,6 +57,7 @@ CHNLIB_UIPArray *CHNLIB_UIPArray_Initialize(void) int CHNLIB_UIPArray_AppendLast(CHNLIB_UIPArray **array, uint data32, void *pointer) { + //戻り値:追加されたタグのIndex //Arrayの末尾にデータを追加する。 int index; @@ -77,22 +78,20 @@ int CHNLIB_UIPArray_AppendLast(CHNLIB_UIPArray **array, uint data32, void *point int CHNLIB_UIPArray_AppendLast_ProtectFromDuplication(CHNLIB_UIPArray **array, uint data32, void *pointer, int (*IsDuplicated)(const void *listtag, const void *newtag)) { - //戻り値:IsAdded(True/False) + //戻り値:追加されたタグ、もしくは重複する既存のタグのIndex //int (*IsDuplicated)(void *listtag, void *newtag)は、重複を確認する関数へのポインタ。 - //listtagにarrayのすべてのpointerを与え、すべてがFalseだった場合追加してFalseを返す。 - //Trueが返った時点で、追加をせずTrueを返す。 + //listtagにarrayのすべてのpointerを与え、すべてがFalseだった場合追加する。 + //Trueが返った時点で、追加をしないで既存のタグに対するIndexを返す。 int i, i_max; i_max = CHNLIB_UIPArray_GetNumberOfDatas(*array); for(i = 0; i < i_max; i++){ if(IsDuplicated(CHNLIB_UIPArray_GetPointerByIndex(*array, i), pointer)){ - return False; + return i; } } - CHNLIB_UIPArray_AppendLast(array, data32, pointer); - - return True; + return CHNLIB_UIPArray_AppendLast(array, data32, pointer); } int CHNLIB_UIPArray_RemoveByIndex(CHNLIB_UIPArray **array, int index) @@ -207,7 +206,7 @@ void CHNLIB_UIPArray_FreeAll(CHNLIB_UIPArray **array) void CHNLIB_UIPArray_FreeSelectedAll(CHNLIB_UIPArray **array) { - //Array及びdata32==TrueのPointerをFree. + //Array及びdata32==FalseのPointerをFree. CHNLIB_UIPArray_FreeOnlySelectedPointer(*array); CHNLIB_UIPArray_FreeOnlyArray(array); return; @@ -276,6 +275,12 @@ void *CHNLIB_UIPArray_GetPointerByIndex(const CHNLIB_UIPArray *array, int index) return array->tag[index].pointer; } +void *CHNLIB_UIPArray_GetPointerByData32(const CHNLIB_UIPArray *array, uint data32) +{ + //指定されたdata32を持つindexのデータのPointerを返す。 + return CHNLIB_UIPArray_GetPointerByIndex(array, CHNLIB_UIPArray_GetIndexByData32(array, data32)); +} + int CHNLIB_UIPArray_GetIndexByData32(const CHNLIB_UIPArray *array, uint data32) { //指定されたdata32を持つ最小のIndexを返す。 @@ -290,10 +295,61 @@ int CHNLIB_UIPArray_GetIndexByData32(const CHNLIB_UIPArray *array, uint data32) return CHNLIB_UIPArray_INDEX_NOTFOUND; } -void *CHNLIB_UIPArray_GetPointerByData32(const CHNLIB_UIPArray *array, uint data32) +CHNLIB_UIPArray *CHNLIB_UIPArray_CopyArray(const CHNLIB_UIPArray *source) { - //指定されたdata32を持つindexのデータのPointerを返す。 - return CHNLIB_UIPArray_GetPointerByIndex(array, CHNLIB_UIPArray_GetIndexByData32(array, data32)); + //sourceと等価な配列を新たに生成して返す。 + CHNLIB_UIPArray *destination; + int i, i_max; + + destination = CHNLIB_UIPArray_Initialize(); + i_max = CHNLIB_UIPArray_GetNumberOfDatas(source); + + for(i = 0; i < i_max; i++){ + CHNLIB_UIPArray_AppendLast(&destination, CHNLIB_UIPArray_GetData32ByIndex(source, i), CHNLIB_UIPArray_GetPointerByIndex(source, i)); + } + + return destination; +} + +CHNLIB_UIPArray *CHNLIB_UIPArray_SortInDescendingOrderByData32(const CHNLIB_UIPArray *array) +{ + //タグをData32の値で降順(大きい方から小さい方)になるように整列した新たなArrayを返す。 + CHNLIB_UIPArray *sorted, *temp; + int i, i_max; + int candidateIndex, candidateData32, currentData32; + + sorted = CHNLIB_UIPArray_Initialize(); + temp = CHNLIB_UIPArray_CopyArray(array); + i_max = CHNLIB_UIPArray_GetNumberOfDatas(temp); + + for(i = 0; i < i_max; i++){ + //tempのData32に1を足す(0を追加済みとマークするため)。 + CHNLIB_UIPArray_SetData32ByIndex(temp, i, CHNLIB_UIPArray_GetData32ByIndex(temp, i) + 1); + } + + for(;;){ + candidateData32 = 0; + for(i = 0; i < i_max; i++){ + currentData32 = CHNLIB_UIPArray_GetData32ByIndex(temp, i); + if(candidateData32 < currentData32){ + candidateData32 = currentData32; + candidateIndex = i; + } + } + if(candidateData32 == 0){ + break; + } + for(i = candidateIndex; i < i_max; i++){ + currentData32 = CHNLIB_UIPArray_GetData32ByIndex(temp, i); + if(candidateData32 == currentData32){ + //-1を忘れないように + CHNLIB_UIPArray_AppendLast(&sorted, currentData32 - 1, CHNLIB_UIPArray_GetPointerByIndex(temp, i)); + CHNLIB_UIPArray_SetData32ByIndex(temp, i, 0); + } + } + } + + return sorted; } // diff --git a/CHNOSProject/chn/chnlib02.c b/CHNOSProject/chn/chnlib02.c index 5a3e48c..2671932 100644 --- a/CHNOSProject/chn/chnlib02.c +++ b/CHNOSProject/chn/chnlib02.c @@ -125,6 +125,7 @@ int CHNLIB_UIPArray_GetSeparatedUTF8Character(CHNLIB_UIPArray **separated, const CHNLIB_String *CHNLIB_ReadLine(FILE *fp) { + //改行文字を削除して、読み込んだ文字列一行を返す。 char s[CHNLIB_MAX_STRING_LENGTH]; if(fgets(s, sizeof(s), fp) == NULL){