Click here for details: https://pypi.python.org/pypi/PrettyTable I installed the following with pip. This is convenient when the length of each element of list is different and it is difficult to see even if you try to output it. This time code: https://github.com/KodairaTomonori/Qiita/blob/master/module/prettyprint/make_table.py
pip.
$ pip search prettytable
prettytable-extras - An extension to the excellent prettytable Python library
This time, I will explain using mecab
, which analyzes morphological elements.
When you put a sentence, mecab returns the morphological analysis result as a character string.
This can also be easily installed with pip.
mecab_test.
$ python
Python 3.5.0
>>> import MeCab
>>> MeCab.Tagger().parse('The body is made of a sword')
'body\t noun,General,*,*,*,*,body,Body,Body\n is\t particle,Particle,*,*,*,*,Is,C,Wow\n sword\t noun,General,*,*,*,*,sword,Ken,Ken\at n\t particle,Case particles,General,*,*,*,so,De,De\n done\t verb,Independence,*,*,One step,Continuous form,Can do,Deキ,Deキ\n\t particle,Connection particle,*,*,*,*,hand,Te,Te\n\t verb,非Independence,*,*,One step,Uninflected word,Is,Il,Il\nEOS\n'
>>> print(MeCab.Tagger().parse('The body is made of a sword') )
Body noun,General,*,*,*,*,body,Body,Body
Is a particle,Particle,*,*,*,*,Is,C,Wow
Sword noun,General,*,*,*,*,sword,Ken,Ken
Particles,Case particles,General,*,*,*,so,De,De
Verb,Independence,*,*,One step,Continuous form,Can do,Deki,Deki
Particles,Connection particle,*,*,*,*,hand,Te,Te
Verb,Non-independent,*,*,One step,Uninflected word,Is,Il,Il
EOS
It will be output like this, but if you just print it, it will be difficult to see because there is nothing behind it
So I'd like to use pretty table
to make this easier to see.
make_prettytable.py
import MeCab
import prettytable
def make_prettytable(others, header=[]):
if not header: header = range(len(list(others)[0]) )
if len(header) != len(others[0]):
print('incorrect length!')
return
table = prettytable.PrettyTable(header)
for other in others: table.add_row(other)
return table
def make_mecab_info_table(sentence, output_info=[], \
mecab_parser=MeCab.Tagger.parse):
others = list(map(lambda x: x.split(','), \
mecab_parser(sentence).replace('\t', ',').split('\n') ) )[:-2]
return make_prettytable(others, output_info)
if __name__ == '__main__':
mecab_output = 'Surface type,Part of speech,Part of speech細分類1,Part of speech細分類2,Part of speech細分類3,Inflected form,Utilization type,Prototype,reading,pronunciation'
parser = MeCab.Tagger().parse
header = mecab_output.split(',')
print(make_mecab_info_table(input(), header, parser) )
It became easier to see like this.
make_prettytable
contains the contents in the first argument (ʻothers) and the information in the upper row in the second argument (
header).
header assigns a number with
range if it does not receive an argument. ʻOthers
should include a list like[[1,2, ...], [2,3, ...] ...]
.
In prettytable.PrettyTable
, put the upper row ( header
) and make the base of the table.
In the following for
statement, ʻothers is turned and
.add_row` is added line by line.
After that, you can easily see the table just by printing.
make_mecab_info_table just handles characters normally.
It is convenient when the lengths of the elements of list are different, because it is difficult to see even if you try to output and check.
Recommended Posts