The code below reads a specific text file, analyzes it with a morpheme, It's a code that counts the number of morphemes in a document in a large order.
If the get_tags function is interrupted within the main function, no new output_file is generated. Can you tell me where and what kind of error it is...?
//
When running the import sys #module, the terminal imports 'sys' to use 'sys.argv' to get a print from the user
From konlpy.tag import Okt #import the Korean morpheme analyzer 'konlpy' to separate/extract nouns
from collections import Counter
#A function that calculates frequency after separating and extracting nouns from text.
defget_tags(text, ntags = 50): #text is the text to use for analysis,
# ntags is the number of nouns to output the result among the separated nouns (if no input is entered, output only the top 50 noun results)
splitter = Okt() #When the function is executed, first create an okt object
nouns = splitter.nouns(text) #Afterwards, use the noons method of the okt object to separate and extract nouns from text
Count = Counter(nouns) #Counter object (pre-object for frequency calculation) is generated and assigned to the reference variable count.
return_list = [] #List for storing frequency by noun
""" The 'most_common' method of the 'counter' object is entered with an integer, and among the nouns in the object,
Returns objects stored by the number of integers entered in order from nouns with high frequency""""
for n, c in count.most_common(ntags):
temp = {'tag':n, 'count': c}
return_list.append(temp)
return return_list
#Main function
def main(argv):
if len(argv) !=4:
print ('python [module name][text file name.txt][result file name.txt][number of words])
return
open_file_name = argv[1]
output_file_name = argv[2]
noun_count = argv[3]
open_text_file = open(open_file_name, 'r') #rReadModewWwriteModeaNew ContentAdditionalMode
text = open_text_file.read()
tags = get_tags(text, noun_count)
open_text_file.close()
output_file = open(output_file_name, 'w')
for tag in tags:
noun = tag['tag']
count = tag['count']
output_file.write('{} {}\n'.format(noun, count))
output_file.close()
if __name__ == '__main__':
main(sys.argv)
You can use pycharm or vs code to set a breakpoint in debug mode and run it.
The tags are empty, so you're most likely not to turn around the for door, so first, set a breakpoint on the line with output_file.write
and run it in debug mode.
If it's spinning because of a break point on the line, I think I'll check the file name or execution path.
What I want to say is, it's convenient to learn how to debug in debug mode.
If you want to debug simply,
def main(argv):
if len(argv) !=4:
print ('python [module name][text file name.txt][result file name.txt][number of words])
return
open_file_name = argv[1]
output_file_name = argv[2]
noun_count = argv[3]
Modify argv[1]
, argv[2]
and argv[3]
to the appropriate "input file name", "output file name", and "noun count" respectively to run debugging. For example,
def main(argv):
if len(argv) !=4:
print ('python [module name][text file name.txt][result file name.txt][number of words])
return
open_file_name = "data.txt" # argv[1]
output_file_name = "out.txt" # argv[2]
noun_count = "10" # argv[3]
© 2025 OneMinuteCode. All rights reserved.