a, as, e, es, o, os로 끝나는 포르투갈어 단어를 구분하는 프로그램이 있습니다. 몇 가지 목록을 만들고 파일을 반복하고 파일의 단어를 엔딩에 따라 다른 목록에 할당합니다. 패턴과 일치하지 않는 단어는 "기타"라는 목록에 할당됩니다. 이제 마지막 두 글자를 기준으로 나머지 모든 단어를 분리하고 싶습니다. 예를 들어 'em'으로 끝나는 단어는 'em'이라는 목록에 할당되고 'ul'으로 끝나는 단어는 'ul'이라는 목록에 할당됩니다. 그러나 확인한 결과 470 개의 다른 엔딩이 있기 때문에 엄청난 코드로 끝날 것입니다! 따라서 470 개의 목록을 수동으로 만들어야합니다. 누구든지 내가 어떻게 자동으로 할 수 있는지 알고 있습니까? 아니면 문제에 대한 다른 해결책이 있습니까? 지금까지 내 코드는 다음과 같습니다. 미리 감사드립니다 !!
from nltk.tokenize import sent_tokenize,wordpunct_tokenize
import re
import os
import io
import sys
from pathlib import Path
while True:
try:
file_to_open =Path(input("Please, insert your file path: "))
with open(file_to_open,'r', encoding="utf-8") as f:
words = f.read().lower()
break
except FileNotFoundError:
print("\nFile not found. Better try again")
except IsADirectoryError:
print("\nIncorrect Directory path.Try again")
other=[]
e=[]
o=[]
a=[]
for y in words:
if y[-1:] == 'a'or y[-2:]=='as':
a.append(y)
elif y[-1:] == 'o' or y[-2:] =='os' :
o.append(y)
elif y[-1:] == 'e'or y[-2:]=='es':
e.append(y)
else:
other.append(y)
otherendings=[]
for t in other:
endings=t[-2:]
otherendings.append(endings)
print(len(otherendings))
print(set(otherendings)) #470
키가 단어로 끝나는 사전 만들기 :
word_dict = {}
for word in words:
ending = word[-2:]
try:
word_dict[ending].append(word)
except:
word_dict[ending] = [word]
단어를 반복하면 키가 두 글자로 구성된 문자열이되는 사전이 생기고 각 키에는이 두 글자로 끝나는 단어 목록이 포함됩니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다