2016-11-04 12 views
2

タブ区切りのテキストファイルから一意の単語リストを抽出する方法は?このようなタブ区切りファイルを考えると

$ head train.txt 
The Fulton County Grand Jury said Friday an investigation of Atlanta's recent primary election produced `` no evidence '' that any irregularities took place . AT NP-TL NN-TL JJ-TL NN-TL VBD NR AT NN IN NP$ JJ NN NN VBD `` AT NN '' CS DTI NNS VBD NN . 
The jury further said in term-end presentments that the City Executive Committee , which had over-all charge of the election , `` deserves the praise and thanks of the City of Atlanta '' for the manner in which the election was conducted . AT NN RBR VBD IN NN NNS CS AT NN-TL JJ-TL NN-TL , WDT HVD JJ NN IN AT NN , `` VBZ AT NN CC NNS IN AT NN-TL IN-TL NP-TL '' IN AT NN IN WDT AT NN BEDZ VBN . 
The September-October term jury had been charged by Fulton Superior Court Judge Durwood Pye to investigate reports of possible `` irregularities '' in the hard-fought primary which was won by Mayor-nominate Ivan Allen Jr. . AT NP NN NN HVD BEN VBN IN NP-TL JJ-TL NN-TL NN-TL NP NP TO VB NNS IN JJ `` NNS '' IN AT JJ NN WDT BEDZ VBN IN NN-TL NP NP NP . 
`` Only a relative handful of such reports was received '' , the jury said , `` considering the widespread interest in the election , the number of voters and the size of this city '' . `` RB AT JJ NN IN JJ NNS BEDZ VBN '' , AT NN VBD , `` IN AT JJ NN IN AT NN , AT NN IN NNS CC AT NN IN DT NN '' . 
The jury said it did find that many of Georgia's registration and election laws `` are outmoded or inadequate and often ambiguous '' . AT NN VBD PPS DOD VB CS AP IN NP$ NN CC NN NNS `` BER JJ CC JJ CC RB JJ '' . 
It recommended that Fulton legislators act `` to have these laws studied and revised to the end of modernizing and improving them '' . PPS VBD CS NP NNS VB `` TO HV DTS NNS VBN CC VBN IN AT NN IN VBG CC VBG PPO '' . 
The grand jury commented on a number of other topics , among them the Atlanta and Fulton County purchasing departments which it said `` are well operated and follow generally accepted practices which inure to the best interest of both governments '' . AT JJ NN VBD IN AT NN IN AP NNS , IN PPO AT NP CC NP-TL NN-TL VBG NNS WDT PPS VBD `` BER QL VBN CC VB RB VBN NNS WDT VB IN AT JJT NN IN ABX NNS '' . 
Merger proposed NN-HL VBN-HL 
However , the jury said it believes `` these two offices should be combined to achieve greater efficiency and reduce the cost of administration '' . WRB , AT NN VBD PPS VBZ `` DTS CD NNS MD BE VBN TO VB JJR NN CC VB AT NN IN NN '' . 
The City Purchasing Department , the jury said , `` is lacking in experienced clerical personnel as a result of city personnel policies '' . AT NN-TL VBG-TL NN-TL , AT NN VBD , `` BEZ VBG IN VBN JJ NNS CS AT NN IN NN NNS NNS '' . 

(タブで区切られた)最初の列だけが重要であると私は、最初の列と出力から(句読点を含む)の単語のユニークなリストを抽出したいと思いますファイルに。すなわち、単語はスペースで区切られているものとする。:

$ head train.txt | cut -f1 
The Fulton County Grand Jury said Friday an investigation of Atlanta's recent primary election produced `` no evidence '' that any irregularities took place . 
The jury further said in term-end presentments that the City Executive Committee , which had over-all charge of the election , `` deserves the praise and thanks of the City of Atlanta '' for the manner in which the election was conducted . 
The September-October term jury had been charged by Fulton Superior Court Judge Durwood Pye to investigate reports of possible `` irregularities '' in the hard-fought primary which was won by Mayor-nominate Ivan Allen Jr. . 
`` Only a relative handful of such reports was received '' , the jury said , `` considering the widespread interest in the election , the number of voters and the size of this city '' . 
The jury said it did find that many of Georgia's registration and election laws `` are outmoded or inadequate and often ambiguous '' . 
It recommended that Fulton legislators act `` to have these laws studied and revised to the end of modernizing and improving them '' . 
The grand jury commented on a number of other topics , among them the Atlanta and Fulton County purchasing departments which it said `` are well operated and follow generally accepted practices which inure to the best interest of both governments '' . 
Merger proposed 
However , the jury said it believes `` these two offices should be combined to achieve greater efficiency and reduce the cost of administration '' . 
The City Purchasing Department , the jury said , `` is lacking in experienced clerical personnel as a result of city personnel policies '' . 

$ head train.txt | cut -f2 
AT NP-TL NN-TL JJ-TL NN-TL VBD NR AT NN IN NP$ JJ NN NN VBD `` AT NN '' CS DTI NNS VBD NN . 
AT NN RBR VBD IN NN NNS CS AT NN-TL JJ-TL NN-TL , WDT HVD JJ NN IN AT NN , `` VBZ AT NN CC NNS IN AT NN-TL IN-TL NP-TL '' IN AT NN IN WDT AT NN BEDZ VBN . 
AT NP NN NN HVD BEN VBN IN NP-TL JJ-TL NN-TL NN-TL NP NP TO VB NNS IN JJ `` NNS '' IN AT JJ NN WDT BEDZ VBN IN NN-TL NP NP NP . 
`` RB AT JJ NN IN JJ NNS BEDZ VBN '' , AT NN VBD , `` IN AT JJ NN IN AT NN , AT NN IN NNS CC AT NN IN DT NN '' . 
AT NN VBD PPS DOD VB CS AP IN NP$ NN CC NN NNS `` BER JJ CC JJ CC RB JJ '' . 
PPS VBD CS NP NNS VB `` TO HV DTS NNS VBN CC VBN IN AT NN IN VBG CC VBG PPO '' . 
AT JJ NN VBD IN AT NN IN AP NNS , IN PPO AT NP CC NP-TL NN-TL VBG NNS WDT PPS VBD `` BER QL VBN CC VB RB VBN NNS WDT VB IN AT JJT NN IN ABX NNS '' . 
NN-HL VBN-HL 
WRB , AT NN VBD PPS VBZ `` DTS CD NNS MD BE VBN TO VB JJR NN CC VB AT NN IN NN '' . 
AT NN-TL VBG-TL NN-TL , AT NN VBD , `` BEZ VBG IN VBN JJ NNS CS AT NN IN NN NNS NNS '' . 

私はこれを行うことができます:

$ python 
>>> fout = open('word.dict', 'w')                                  
>>> fout.write('\n'.join(list(set(zip(*[line.split('\t')[0].lower().split() for line in open('train.txt')])[0])))) 
>>> exit() 
$ head word.dict 
trenton 
brevet 
secondly 
fig. 
magnetic 
doubts 
monte 
elisabeth 
four 
facilities 

をしかし、方法は、シェル/ bashで同じ単語のリストを抽出ありますか?

+1

「最初の列」の意味が混乱しています。すべての言葉が欲しいですか?あるいは、改行の最初の言葉ですか?それとも私が理解していない何か? –

答えて

3

はこれを試してみてください:

cut -f1 file | tr -s '[:space:]' '\n' | tr '[:upper:]' '[:lower:]' | sort -u 
  • cut -f1はそれぞれ、第一タブ区切りの列

  • tr -s '[:space:]' '\n'が効果的に単語のリストを作成し、改行と空白の各実行を置き換える抽出しますそれ自身の行に

  • tr '[:upper:]' '[:lower:]'は、すべて小文字に変換されます。

  • sort -u結果リストを重複除外(-u)に並べ替えます。タブは、あなたの投稿をサンプル入力のどこにいるか、これはテストされていないが、あなたが欲しいものを行う必要がありますので、

+0

小文字がありません=) – alvas

+1

実際、 'sort -u -f'はやや優れていますが、その方法では、どれが一意に選ばれるのかわかりません。ありがとう@ mklement0 – alvas

+1

@alvas:良い点。小文字の必要条件はPythonコードに埋め込まれていますが、私はあなたも散文でそれを追加することをお勧めします。 – mklement0

3

私が言うことはできません。

awk '{sub(/\t.*/,""); for (i=1; i<=NF; i++) if (!seen[tolower($i)]++) print $i}' file 

をしたり、小文字ですべての出力をしたい場合:

awk '{sub(/\t.*/,""); $0=tolower($0); for (i=1; i<=NF; i++) if (!seen[$i]++) print $i}' file 
関連する問題