Python“撤消”文本换行

2024-10-02 08:23:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要获取一个文本并删除\n字符,我相信我已经完成了。下一个任务是从不应该出现的单词中删除连字符,而是将连字符保留在应该出现的复合单词中。例如,“encyclo-\npedia”到“encyclopedia”和“long-\nterm”到“long-term”。建议将其与原文进行比较。在

with open('C:\Users\Paul\Desktop\Comp_Ling_Research_1\BROWN_A1_hypenated.txt', 'rU') as myfile:
data=myfile.read().replace('\n', '')

我对该做什么有一个大概的想法,但是NLP对我来说是一个全新的概念。在


Tags: 文本withopen字符单词myfileusers建议
2条回答
import re


with open('C:\Users\Paul\BROWN_A1.txt', 'rU') as truefile:
    true_corpus = truefile.read()

true_tokens = true_corpus.split(' ')

with open('C:\Users\Paul\Desktop\Comp_Ling_Research_1\BROWN_A1_hypenated.txt', 'rU') as myfile:

my_corpus = myfile.read()

my_tokens = my_corpus.split(' ')

第一步是保留一组有效的单词,如果您的断字单词在有效单词集中,则取消断字。Ubuntu在/usr/share/dict/american english上有一个有效单词列表。过于简单的版本可能看起来像:

valid_words = set(line.strip() for line in open(valid_words_file))

output = []
for word in open(new_file).read().replace('-\n', '').replace('\n', ' ').split():
    if '-' in word and word.replace('-', '') in valid_words:
        output.append(word.replace('-', ''))
    else:
        output.append(word)

你需要处理标点符号、大写字母等等,但这就是问题所在。在

相关问题 更多 >

    热门问题