:)这对你来说可能是一个很长的问题,我明白,但相信我,这并不长。我无法确定为什么在处理完此文本后,无法阅读和编辑。我尝试在python中使用ord()函数来检查文本是否包含除ASCII字符之外的任何Unicode字符(非ASCII字符)。我发现了很多。
我有一种强烈的感觉,这可能是由于原始文本本身(输入)。
输入文件:只需复制粘贴到文件中”
acle5v1.txt
“
下面这段代码的目的是检查大写字符,并将其转换为小写字符,同时删除所有标点符号,以便对这些单词进行进一步的对齐处理。
#include<iostrea>
#include<fstream>
#include<ctype.h>
#include<cstring>
using namespace std;
ifstream fin2("acle5v1.txt");
ofstream fin3("acle5v1_op.txt");
ofstream fin4("chkcharadded.txt");
ofstream fin5("chkcharntadded.txt");
ofstream fin6("chkprintchar.txt");
ofstream fin7("chknonasci.txt");
ofstream fin8("nonprinchar.txt");
int main()
{
char ch,ch1;
fin2.seekg(0);
fin3.seekp(0);
int flag = 0;
while(!fin2.eof())
{
ch1=ch;
fin2.get(ch);
if (isprint(ch))
flag = 1;
if(flag)
{
fin6<<"Printable character:\t"<<ch<<"\t"<<(int)ch<<endl;
flag = 0;
}
else
{
fin8<<"Non printable character caught:\t"<<ch<<"\t"<<int(ch)<<endl;
}
if( isalnum(ch) || ch == '@' || ch == ' ' )
{
fin4<<"char added: "<<ch<<"\tits ascii value: "<<int(ch)<<endl;
if(isupper(ch))
{
fin3<<(char)tolower(ch);
}
else
{
fin3<<ch;
}
}
else if( ( ch=='\t' || ch=='.' || ch==',' || ch=='#' || ch=='?' || ch=='!' || ch=='"' || ch != ';' || ch != ':') && ch1 != ' ' )
{
fin3<<' ';
}
else if( (ch=='\t' || ch=='.' || ch==',' || ch=='#' || ch=='?' || ch=='!' || ch=='"' || ch != ';' || ch != ':') && ch1 == ' ' )
{
}
else if( !(int(ch)>=0 && int(ch)<=127) )
{
fin5<<"Char of ascii within range not added: "<<ch<<"\tits ascii value: "<<int(ch)<<endl;
}
else
{
fin7<<"Non ascii character caught(could be a -ve value also)\t"<<ch<<int(ch)<<endl;
}
}
return 0;
}
我有一个
类似的
上面用python编写的代码给了我一个otput,它同样不可读也不可编辑。
python中的代码如下:
import sys
input_file=sys.argv[1]
output_file=sys.argv[2]
list1=[]
f=open(input_file)
for line in f:
line=line.strip()
line=line.replace('.','')
line=line.replace(',','')
line=line.replace('#','')
line=line.replace('?','')
line=line.replace('!','')
line=line.replace('"','')
line=line.replace('।','')
line=line.replace('|','')
line = line.lower()
list1.append(line)
f.close()
f1=open(output_file,'w')
f1.write(' '.join(list1))
f1.close()
文件在运行时接收IP和OP。AS:
python punc_remover.py acle5v1.txt acle5v1_op.txt
此文件的输出位于“acle5v1_op.txt”中。
现在,在处理完这个特定的输出文件之后,需要进一步处理。这个特定的文件“aclee5v1_op.txt”是一个不可读的和不可编辑的文件,我无法使用它进行进一步的处理。我需要这个在NLP中进行单词对齐。我尝试用以下程序读取此输出
#include<iostream>
#include<fstream>
using namespace std;
ifstream fin1("acle5v1_op.txt");
ofstream fout1("chckread_acle5v1_op.txt");
ofstream fout2("chcknotread_acle5v1_op.txt");
int main()
{
char ch;
int flag = 0;
long int r = 0; long int nr = 0;
while(!(fin1))
{
fin1.get(ch);
if(ch)
{
flag = 1;
}
if(flag)
{
fout1<<ch;
flag = 0;
r++;
}
else
{
fout2<<"Char not been able to be read from source file\n";
nr++;
}
}
cout<<"Number of characters able to be read: "<<r;
cout<<endl<<"Number of characters not been able to be read: "<<nr;
return 0;
}
如果字符可读,则打印字符;如果不可读,则不打印字符,但我观察到两个文件的输出都是空白的,因此我可以得出结论,即该文件“acle5v1_op.txt”不可读且不可编辑。你能帮我解决这个问题吗?
为了告诉您一点关于原始输入文件acle5v1.txt的统计信息,该文件中大约有3441行,其中大约有300万个字符。
记住,您编辑的文件中的字符数可能/可能无法打开该文件。我可以打开费多拉10的Gedit文件,我目前正在使用。这只是为了通知您,用特定的编辑器打开实际上并不是一个问题,至少在我的情况下……
如果可以,我可以使用诸如python和perl这样的脚本语言来处理这个问题吗?如何处理?请具体说明一下,因为我是Perl和Python的新手。或者你能告诉我如何用C++本身解决这个问题吗?谢谢你……:)我真的很期待关于如何解决这个问题的一些帮助或指导。