세종코퍼스 txt 파일에서 어절 수 찾아내기
이렇게 생긴 세종코퍼스 txt 파일로부터 코퍼스의 어절 수를 추출해 내는 기능을 만들어 보려다가 헤맸었다.
약간의 혼란 끝에 드디어 어떻게든 성공했다.
생각해 보니까 그냥 txt 파일의 맨 밑 줄부터 탐색해서 여는꺾쇠('<')로 시작하지 않는 줄을 만나면 그 줄의 어절 번호를 추출해 내면 되는 거였다.
최종 코드는 이렇게 만들어졌다.
잘하는 분들이 보기엔 엉망이겠지만 일단 돌아가니까 만족이다.
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
31
32
33
34
35
36
37
38
39
40
41
|
import re
import sys
sys.stdout = open('stdout.txt', 'w')
def counter(x):
f = open(x, "r", encoding="utf-16-le")
data = f.readlines()
for i in range(len(data)-1, -1, -1):
if data[i][0] == '<':
continue
else:
last_line = i
break
number_of_lines = re.findall("-\d+", data[last_line])
# print(number_of_lines)
# ['-00065929']
number_of_lines = number_of_lines[0]
# '-00065929'
number_of_lines = number_of_lines[4:len(number_of_lines)]
# '65929'
number_of_lines = int(number_of_lines)
# print(number_of_lines)
# 65929
return number_of_lines
filenames = open("list.txt", "r", encoding='utf-8')
filenames = filenames.readlines()
# ['BTAA0001.txt\n', 'BTAA0002.txt\n', 'BTAA0003.txt\n',
# 'BTAA0004.txt\n', ... , 'BTJO0447.txt']
sum = 0
for i in range(len(filenames)):
print('this file is: FILE #' + str(i+1))
name_with_extra_n = filenames[i]
if i == len(filenames) - 1:
goodname = name_with_extra_n
else:
goodname = name_with_extra_n[0:len(name_with_extra_n)-1]
result = counter(goodname)
print(result)
a = result
sum = sum + a
print('Total # of EOJEOLs:' + str(sum))
sys.stdout.close()
|
cs |
결과물 파일 stdout.txt는 이런 모습이다.
몇 개를 열어서 확인해 봤는데 잘 된 거 같다.
결론적으로 지금 내가 갖고 있는 세종말뭉치 현대문어 형태분석 코퍼스의 어절 수는 총 9,966,708개이다.
나중에 '곧이곧대로'와 다른 단어들 간의 연어값을 계산할 때 이 전체 어절 개수의 값이 필요할 것이다.
코퍼스로 연어(collocation) 분석해 보기 (1)
문득 '곧이곧대로'라는 단어가 아주 특정한 몇 개의 단어 또는 구문과 주로 함께 쓰이는 거 같다...
blog.naver.com
2편 - https://blog.naver.com/ks1127zzang/222826100700
세종코퍼스의 어절 수 추출하기 - 연어 분석 (2)
세종코퍼스 txt 파일의 어절 개수를 추출하는 아주 간단한 기능을 구현해 보았다. 추출한다기보다는 그냥 ...
blog.naver.com
- print() 함수의 결과를 txt 파일로 출력하는 법: https://codetorial.net/tips_and_examples/save_print_output.html
20. 파이썬 출력 결과 저장하기 - Codetorial
예제 import sys f = open('stdout2.txt', 'w') for i in range(10): if i % 2: print(i, file=f) else: print(i) f.close() 특정 출력 결과만 파일에 저장하고 싶다면 예제와 같이 print() 함수에 file 인자를 지정합니다. 아래와 같
codetorial.net