LoginSignup
1
0

More than 1 year has passed since last update.

文字計数処理:ちょっとした処理の変化が、データにどう変化を与えるか。

Last updated at Posted at 2022-04-24

PDFファイルの処理を行った。

AUTOSAR 21-11 (9) Specification of Adaptive Platform Core, 新人にお勧めの基本動作(2), AUTOSAR(60)

の続きを8ファイル一括で処理しようとした。

そのために、過去にscriptで一括処理した自己記事を参考にした。

単語帳作成 dockerで(文字コード対応)量子計算機 arXiv掲載 西森 秀稔 論文(shell, awk) プログラムちょい替え(10)

なぜか複合語になってしまう。

英語の複合語と単語の計算

sedのスクリプトで処理している。

今回追加した処理をする前の処理と後の処理の例である。
setがファイルが大きくなるのは、空白を挿入した数である。
何個の単語を分割したかがわかる。
wcファイルが小さくなるのは、一つの語を2つに分割しても、分割したうちの一つがすでに別の単語として存在していれば、文字数が減る可能性が十分にある。

例えば、
controlflowが32個あれば、controlとflowの間に空白が1文字増える。
setファイルで文字数が32文字増える。
そして、wcファイルでは
control,32
flow,32
であれば、文字数は4文字増えるはずである。
しかし、すでにflowが単語で存在していれば、
4文字減るかもしれない。数字の桁上がりの有無で、増減の具合が違う。
setファイルは必ず文字数が以上になるが、
wcファイルは文字数が増えるか減るかは、関係する語がすでに抽出済みかどうかに依存する。

処理前 処理後 ファイル名
60554 60554 0 AUTOSAR_EXP_AdaptivePlatformInterfacesGuidelines.pdf.gst
60758 60668 90 AUTOSAR_EXP_AdaptivePlatformInterfacesGuidelines.pdf.set
40887 40887 0 AUTOSAR_EXP_AdaptivePlatformInterfacesGuidelines.pdf.smt
40887 40887 0 AUTOSAR_EXP_AdaptivePlatformInterfacesGuidelines.pdf.txt
11362 11472 -110 AUTOSAR_EXP_AdaptivePlatformInterfacesGuidelines.pdf.wc
265710 265710 0 AUTOSAR_EXP_PlatformDesign.pdf.gst
265941 265895 46 AUTOSAR_EXP_PlatformDesign.pdf.set
155796 155796 0 AUTOSAR_EXP_PlatformDesign.pdf.smt
155796 155796 0 AUTOSAR_EXP_PlatformDesign.pdf.txt
27241 27543 -302 AUTOSAR_EXP_PlatformDesign.pdf.wc
90811 90811 0 AUTOSAR_RS_General.pdf.gst
90934 90902 32 AUTOSAR_RS_General.pdf.set
39978 39978 0 AUTOSAR_RS_General.pdf.smt
39978 39978 0 AUTOSAR_RS_General.pdf.txt
9134 9158 -24 AUTOSAR_RS_General.pdf.wc
105622 105622 0 AUTOSAR_RS_SecurityManagement.pdf.gst
105657 105637 20 AUTOSAR_RS_SecurityManagement.pdf.set
61437 61437 0 AUTOSAR_RS_SecurityManagement.pdf.smt
61437 61437 0 AUTOSAR_RS_SecurityManagement.pdf.txt
15920 16008 -88 AUTOSAR_RS_SecurityManagement.pdf.wc
904397 904397 0 AUTOSAR_SWS_AdaptivePlatformCore.pdf.gst
907220 907181 39 AUTOSAR_SWS_AdaptivePlatformCore.pdf.set
348512 348512 0 AUTOSAR_SWS_AdaptivePlatformCore.pdf.smt
348512 348512 0 AUTOSAR_SWS_AdaptivePlatformCore.pdf.txt
19813 19951 -138 AUTOSAR_SWS_AdaptivePlatformCore.pdf.wc
573305 573305 0 AUTOSAR_TR_AdaptiveMethodology.pdf.gst
573855 573746 109 AUTOSAR_TR_AdaptiveMethodology.pdf.set
264337 264337 0 AUTOSAR_TR_AdaptiveMethodology.pdf.smt
264337 264337 0 AUTOSAR_TR_AdaptiveMethodology.pdf.txt
17029 17332 -303 AUTOSAR_TR_AdaptiveMethodology.pdf.wc
103743 103743 0 AUTOSAR_TR_AdaptivePlatformReleaseOverview.pdf.gst
103933 103836 97 AUTOSAR_TR_AdaptivePlatformReleaseOverview.pdf.set
48144 48144 0 AUTOSAR_TR_AdaptivePlatformReleaseOverview.pdf.smt
48144 48144 0 AUTOSAR_TR_AdaptivePlatformReleaseOverview.pdf.txt
12199 12281 -82 AUTOSAR_TR_AdaptivePlatformReleaseOverview.pdf.wc
1034940 1034940 0 AUTOSAR_TR_AdaptivePlatformSystemTests.pdf.gst
1037035 1036950 85 AUTOSAR_TR_AdaptivePlatformSystemTests.pdf.set
448357 448357 0 AUTOSAR_TR_AdaptivePlatformSystemTests.pdf.smt
448357 448357 0 AUTOSAR_TR_AdaptivePlatformSystemTests.pdf.txt
23025 23175 -150 AUTOSAR_TR_AdaptivePlatformSystemTests.pdf.wc

setとwcの関係を一覧にし、setの増加の少ない順に並べなおした。
喃語新たに処理したかの計数を出しておらず、この2つだけで創刊計数を求めてもあまり価値がないかもしれない。
出ている数だけから創刊関係を求める危険性がわかるかもしれない。
何を図っていないと、関係性を出してもいいかどうかの判断が大事。

set wc
20 -88
32 -24
39 -138
46 -302
85 -150
90 -110
97 -82
109 -303

Autosar文書の単語帳作成上の文字計数処理での、
ちょっとした処理の変化が、データにどう変化を与えるかを記録する。

bash
../ptwc.sh
Convert the PDF: AUTOSAR_EXP_AdaptivePlatformInterfacesGuidelines.pdf
Convert the PDF: AUTOSAR_EXP_PlatformDesign.pdf
Convert the PDF: AUTOSAR_RS_General.pdf
Convert the PDF: AUTOSAR_RS_SecurityManagement.pdf
Convert the PDF: AUTOSAR_SWS_AdaptivePlatformCore.pdf
Convert the PDF: AUTOSAR_TR_AdaptiveMethodology.pdf
Convert the PDF: AUTOSAR_TR_AdaptivePlatformReleaseOverview.pdf
Convert the PDF: AUTOSAR_TR_AdaptivePlatformSystemTests.pdf

複合語としては分割しなかった語

backend
background
bandwidth
callback
metamodel
output
overhead
prototype
reset
shutdown
timeout
upstream
wakeup
widespread
without

分割した語

guide line
control flow

autosar.sedに追加した行は次の通り。
ただし、追加する前に、アルファベットの逆順に並べて、整列してから処理する。
短い文字列を先に処理し、長い文字列がうまく処理できないのを防ぐためである。

autosa.sed
s/platformreleaseoverview/platform release over view/g
s/taskdefinition/task definition/g
s/briefdescription/brief description/g
s/interdependencies/inter dependencies/g
s/applicationlevel/application level/g
s/diagnosticconditioninterface/diagnostic condition interface/g
s/diagnosticdtc/diagnostic dtc/g
s/diagnosticindicatorinterface/diagnostic indicator interface/g
s/diagnosticmonitorinterface/diagnostic monitor interface/g
s/diagnosticoperationcycleinterface/diagnostic operation cycle interface/g
s/eventinterface/event interface/g
s/executablegroups/executable groups/g
s/identifierinterface/identifier interface/g
s/informationinterface/information interface/g
s/levelinterface/level interface/g
s/timeoutduration/timeoutduration/g
s/scalelinearand/scale linear and/g
s/supportdata/support data/g
s/emplaceerror/emplace error/g
s/emplacevalue/emplace value/g
s/handlerprototype/handler prototype/g
s/constexpriterator/constexpr iterator/g
s/contiguousiterator/contiguous iterator/g
s/randomaccessiterator/random access iterator/g
s/valueorerror/value or error/g
s/countermeasures/counter measures/g
s/countermeasure/counter measure/g
s/controlflowintegrity/control flow integrity/g
s/protectorall/protector all/g
s/protectorstrong/protector strong/g
s/swarchitecturaldecision/sw architectural decision/g
s/cppcoreguidelines/cpp core guide lines/g
s/instancespecifer/instance specifer/g
s/invalidpackagemanifest/invalid package manifest/g
s/diagnosticaddress/diagnostic address/g
s/executionenvironment/execution environment/g
s/lackresources/lack resources/g
s/arepackagestates/are package states/g
s/deploymentrelated/deployment related/g
s/faileddependency/failed dependency/g
s/onrecoverablefailure/on recoverable failure/g
s/packagemanagem/package managem/g
s/pdatesession/pdate session/g
s/processcommunication/process communication/g
s/processswpackage/process sw package/g
s/serviceoriented/service oriented/g
s/platforminterfacesguidelines/platform interfaces guide lines/g
s/statemanagement/state management/g
s/communicationgroup/communication group/g
s/communicationgroupclient/communication group client/g
s/discardpendingchanges/discardpendingchanges/g

script

他で使っていたスクリプトをだいぶ変更した。

失敗の記録:文字列処理スクリプトの場合

変更点1:pdftotextだけの処理だった。tw, awk, sedの処理をいれた。

行を追加したら、エラーが出た。エラーを取るのに一番簡単な方法が削ることだったから。原因追求より対策がわかっていれば、原因は追求せずに対策を取る。

変更点2:今回対象とするpdfファイルは空白文字を含んでいない。ファイル名に空白文字が入っている場合を想定した処理を削った。

ptwc.sh
#!/bin/bash
# https://news.mynavi.jp/article/bashonwindows-17/
# https://qiita.com/kaizen_nagoya/items/319672853519990cee42

 for File in $(ls *.pdf) ; do
        echo "Convert the PDF:" ${File}
        pdftotext -q ${File} ../text/${File}.txt 
	tr 'A-Z' 'a-z' < ../text/${File}.txt > ../text/${File}.smt
	awk -f ../gs.awk ../text/${File}.smt > ../text/${File}.gst
	sed -f ../autosar.sed ../text/${File}.gst > ../text/${File}.set
	awk -f ../wc.awk ../text/${File}.set > ../text/${File}.wc
 done

前回使ったのに少し手を加えた。

gs.awk
#!/bin/sh
{
	$0 = tolower($0)    # 
	gsub(/[^a-z_ \t]/, " ", $0)  #
        print $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20, $21, $22, $23, $24, $25, $26, $27, $28, $29, $30
}

wc.awkは変更なし。

wc.awk
# Print list of word frequencies
# https://researchmap.jp/blogs/blog_entries/view/78451/c0ebdc81d4af21931f12f1993584b633
# https://qiita.com/kaizen_nagoya/items/319672853519990cee42

{
    $0 = tolower($0)    # 
    gsub(/[^a-z_ \t]/, " ", $0)  #
    for (i = 1; i <= NF; i++)
        freq[$i]++
}

END {
    for (word in freq)
        printf "%s\t%d\n", word, freq[word]
}

その他

今回の処理で、わからない点が二つ残っている。
一つは、処理上の課題で、

pdatesession-> update session なぜUが抜けた。

一つは、命名規則の課題で、
kdatabasenameis 変数名 _で繋いでいないのはなぜか。
kはkey。k_database_name_isならわかりやすい。

<この記事は個人の過去の経験に基づく個人の感想です。現在所属する組織、業務とは関係がありません。>

文書履歴(document history)

ver. 0.01 初稿  20220412
ver. 0.02 ありがとう追記 20230504

最後までおよみいただきありがとうございました。

いいね 💚、フォローをお願いします。

Thank you very much for reading to the last sentence.

Please press the like icon 💚 and follow me for your happy life.

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0