Как вставить pos-теги в фрейм данных, расположенных в отдельных столбцах в python? - PullRequest
0 голосов
/ 20 апреля 2019

Я пометил мой текст ввода с помощью TextBlob и экспортировал его в текстовый файл. Это дает мне три информации как POS, Parse Chunker и Deep Parsing. Выходные данные этого тегирования имеют следующий формат: Техника: Обычный / NNP / B-NP / O и / CC / I-NP / O. Я хочу, чтобы это было организовано в кадре данных в отдельных столбцах для каждого.

Это код, который я использую.

 import pandas as pd
 import csv
 from textblob import TextBlob
 with open('report1to8_1.txt', 'r') as myfile:
    report=myfile.read().replace('\n', '')
 out = TextBlob(report).parse()
 tagS = 'taggedop.txt'
 f = open('taggedop.txt', 'w')
 f.write(str(out))
 df = pd.DataFrame(columns=['Words', 'POS', 'Parse chunker','Deep 
 Parsing'])
 df = pd.read_csv('taggedop.txt', sep=' ',error_bad_lines=False, 
 quoting=csv.QUOTE_NONE)   

Мой ожидаемый результат - иметь такой фрейм данных: enter image description here Тем не менее, в настоящее время я получаю это: enter image description here

Пожалуйста, помогите !!

1 Ответ

1 голос
/ 20 апреля 2019

Попробуй это. В этом примере вы переводите данные в правильный формат, чтобы вы могли создать фрейм данных. Вам необходимо создать список, содержащий списки ваших данных. Эти данные должны быть единообразно организованы. Затем вы можете создать свой фрейм данных. Прокомментируйте, если вам нужна дополнительная помощь

from textblob import TextBlob as blob
import pandas as pd
from string import punctuation

def remove_punctuation(text):
    return ''.join(c for c in text if c not in punctuation)

data = []

text = '''
He an thing rapid these after going drawn or. 
Timed she his law the spoil round defer. 
In surprise concerns informed betrayed he learning is ye. 
Ignorant formerly so ye blessing. He as spoke avoid given downs money on we. 
Of properly carriage shutters ye as wandered up repeated moreover. 
Inquietude attachment if ye an solicitude to. 
Remaining so continued concealed as knowledge happiness. 
Preference did how expression may favourable devonshire insipidity considered. 
An length design regret an hardly barton mr figure.
Those an equal point no years do. Depend warmth fat but her but played. 
Shy and subjects wondered trifling pleasant. 
Prudent cordial comfort do no on colonel as assured chicken. 
Smart mrs day which begin. Snug do sold mr it if such. 
Terminated uncommonly at at estimating. 
Man behaviour met moonlight extremity acuteness direction. '''

text = remove_punctuation(text)
text = text.replace('\n', '')

text = blob(text).parse()
text = text.split(' ')

for tagged_word in text:

    t_word = tagged_word.split('/')
    data.append([t_word[0], t_word[1], t_word[2], t_word[3]])

df = pd.DataFrame(data, columns = ['Words', 'POS', 'Parse Chunker', 'Deep Parsing'] )

Результат

Out[18]: 
          Words   POS Parse Chunker Deep Parsing
0            He   PRP          B-NP            O
1            an    DT          I-NP            O
2         thing    NN          I-NP            O
3         rapid    JJ        B-ADJP            O
4         these    DT             O            O
5         after    IN          B-PP        B-PNP
6         going   VBG          B-VP        I-PNP
7         drawn   VBN          I-VP        I-PNP
8            or    CC             O            O
9         Timed   NNP          B-NP            O
10          she   PRP          I-NP            O
11          his  PRP$          I-NP            O
12          law    NN          I-NP            O
13          the    DT             O            O
14        spoil    VB          B-VP            O
15        round    NN          B-NP            O
16        defer    VB          B-VP            O
17           In    IN          B-PP        B-PNP
18     surprise    NN          B-NP        I-PNP
19     concerns   NNS          I-NP        I-PNP
20     informed   VBN          B-VP        I-PNP
21     betrayed   VBN          I-VP        I-PNP
22           he   PRP          B-NP        I-PNP
23     learning   VBG          B-VP        I-PNP
24           is   VBZ          I-VP            O
25           ye   PRP          B-NP            O
26     Ignorant   NNP          I-NP            O
27     formerly    RB          I-NP            O
28           so    RB          I-NP            O
29           ye   PRP          I-NP            O
..          ...   ...           ...          ...
105          no    DT             O            O
106          on    IN          B-PP        B-PNP
107     colonel    NN          B-NP        I-PNP
108          as    IN          B-PP        B-PNP
109     assured   VBN          B-VP        I-PNP
110     chicken    NN          B-NP        I-PNP
111       Smart   NNP          I-NP        I-PNP
112         mrs   NNS          I-NP        I-PNP
113         day    NN          I-NP        I-PNP
114       which   WDT             O            O
115       begin    VB          B-VP            O
116        Snug   NNP          B-NP            O
117          do   VBP          B-VP            O
118        sold   VBN          I-VP            O
119          mr    NN          B-NP            O
120          it   PRP          I-NP            O
121          if    IN          B-PP        B-PNP
122        such    JJ          B-NP        I-PNP
123  Terminated   NNP          I-NP        I-PNP
124  uncommonly    RB        B-ADVP            O
125          at    IN          B-PP        B-PNP
126          at    IN          I-PP        I-PNP
127  estimating   VBG          B-VP        I-PNP
128         Man    NN          B-NP        I-PNP
129   behaviour    NN          I-NP        I-PNP
130         met   VBD          B-VP            O
131   moonlight    NN          B-NP            O
132   extremity    NN          I-NP            O
133   acuteness    NN          I-NP            O
134   direction    NN          I-NP            O

[135 rows x 4 columns]
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...