如何在matlab中提取带有条件的文本中的单词?
How to extract word in a text with condition in matlab?
我有多个文本文件,只想提取一个有条件的值,
文件看起来像这样
157.76941498460488, u'id': 1056080, u'image_id': 354282, u'bbox': [188.68243243243242, 229.17468354430378, 16.21621621621621, 9.729113924050637], u'legibility': u'illegible', u'class': u'machine printed'}, {u'language': u'na', u'area': 157.76941498460522, u'id': 1056081, u'image_id': 354282, u'bbox': [176.79054054054052, 241.06582278481014, 16.216216216216246, 9.729113924050637], u'legibility': u'illegible', u'class': u'machine printed'}, {u'language': u'na', u'area': 130.89018132056108, u'id': 1056082, u'image_id': 354282, u'bbox': [60.03378378378378, 224.8506329113924, 15.13513513513514, 8.648101265822783], u'legibility': u'illegible', u'class': u'machine printed'}, {u'language': u'english', u'area': 229.08553456429397, u'class': u'machine printed', u'utf8_string': u'7206', u'image_id': 354282, u'bbox': [447.84940154212785, 338.8799273943157, 15.489338584815993, 14.78988488177692], u'legibility': u'legible', u'id': 1232932}, {u'language': u'english', u'area': 125.41629858832702, u'class': u'machine printed', u'utf8_string': u'HSS', u'image_id': 354282, u'bbox': [465.63345695432395, 333.1362827800334, 10.039386119788142, 12.492427036063997], u'legibility': u'legible', u'id': 1232933}]
如果它 utf8_string 我想提取所有 bbox,输出存储像这样
bbox = [188.68243243243242, 229.17468354430378, 16.21621621621621, 9.729113924050637]
bbox1 = [60.03378378378378, 224.8506329113924, 15.13513513513514, 8.648101265822783]
..etc bbox3 and box4 all the bboxs if its 'utf8_string'and legible
我的代码
i=imread('image.JPEG');
fid = fopen('text1.txt','r');
C = textscan(fid, '%s','Delimiter','');
fclose(fid);
C = C{:};
box = ~cellfun(@isempty, strfind(C,'bbox'));
output = [C{find(box)}]
我得到了整行,而不仅仅是 bbox。
这是一个使用正则表达式进行提取的代码示例。它可能不是世界上最快的,也不是最强大的,但是如果您的文本文件很小,它就可以完成工作。它让您了解如何在这种情况下继续进行,并修改代码,以便在必要时完全适用于您的数据。
我想你在 [filepathList]
中有一个文件列表
Cdata = cell(numel(filepathList),1);
for i=1:numel(filepathList)
fid = fopen(filepathList{i});
tline = fgetl(fid);
% find the bbox blocks
C = regexp(tline,'u''bbox'': +\[([0-9\. ,]+)\]','tokens');
C = cellfun(@(x) x{1},C,'UniformOutput',false)';
% in each block, find the numbers
C2 = cellfun(@(x) textscan(x,'%f','Whitespace',', '),C,'UniformOutput',false);
Cdata{i} = cell2mat(cellfun(@(x) x', cat(1,C2{:}),'UniformOutput',false));
fclose(fid);
end
希望对您有所帮助!
我有多个文本文件,只想提取一个有条件的值,
文件看起来像这样
157.76941498460488, u'id': 1056080, u'image_id': 354282, u'bbox': [188.68243243243242, 229.17468354430378, 16.21621621621621, 9.729113924050637], u'legibility': u'illegible', u'class': u'machine printed'}, {u'language': u'na', u'area': 157.76941498460522, u'id': 1056081, u'image_id': 354282, u'bbox': [176.79054054054052, 241.06582278481014, 16.216216216216246, 9.729113924050637], u'legibility': u'illegible', u'class': u'machine printed'}, {u'language': u'na', u'area': 130.89018132056108, u'id': 1056082, u'image_id': 354282, u'bbox': [60.03378378378378, 224.8506329113924, 15.13513513513514, 8.648101265822783], u'legibility': u'illegible', u'class': u'machine printed'}, {u'language': u'english', u'area': 229.08553456429397, u'class': u'machine printed', u'utf8_string': u'7206', u'image_id': 354282, u'bbox': [447.84940154212785, 338.8799273943157, 15.489338584815993, 14.78988488177692], u'legibility': u'legible', u'id': 1232932}, {u'language': u'english', u'area': 125.41629858832702, u'class': u'machine printed', u'utf8_string': u'HSS', u'image_id': 354282, u'bbox': [465.63345695432395, 333.1362827800334, 10.039386119788142, 12.492427036063997], u'legibility': u'legible', u'id': 1232933}]
如果它 utf8_string 我想提取所有 bbox,输出存储像这样
bbox = [188.68243243243242, 229.17468354430378, 16.21621621621621, 9.729113924050637]
bbox1 = [60.03378378378378, 224.8506329113924, 15.13513513513514, 8.648101265822783]
..etc bbox3 and box4 all the bboxs if its 'utf8_string'and legible
我的代码
i=imread('image.JPEG');
fid = fopen('text1.txt','r');
C = textscan(fid, '%s','Delimiter','');
fclose(fid);
C = C{:};
box = ~cellfun(@isempty, strfind(C,'bbox'));
output = [C{find(box)}]
我得到了整行,而不仅仅是 bbox。
这是一个使用正则表达式进行提取的代码示例。它可能不是世界上最快的,也不是最强大的,但是如果您的文本文件很小,它就可以完成工作。它让您了解如何在这种情况下继续进行,并修改代码,以便在必要时完全适用于您的数据。
我想你在 [filepathList]
Cdata = cell(numel(filepathList),1);
for i=1:numel(filepathList)
fid = fopen(filepathList{i});
tline = fgetl(fid);
% find the bbox blocks
C = regexp(tline,'u''bbox'': +\[([0-9\. ,]+)\]','tokens');
C = cellfun(@(x) x{1},C,'UniformOutput',false)';
% in each block, find the numbers
C2 = cellfun(@(x) textscan(x,'%f','Whitespace',', '),C,'UniformOutput',false);
Cdata{i} = cell2mat(cellfun(@(x) x', cat(1,C2{:}),'UniformOutput',false));
fclose(fid);
end
希望对您有所帮助!