您好我正在尝试根据段落拆分文本文件。我已经将它们分成了一个列表。但是我的目标是只提取那些只包含一个句子的段落。有没有办法做到这一点?
public class TestWidget extends AppWidgetProvider {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction() == null) {
int appWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
if (appWidgetId != -1) {
updateWidgetText(context, appWidgetId, Math.random() + "");
}
}
else {
super.onReceive(context, intent);
}
}
static void updateWidgetText(Context context, int appWidgetId, String newText) {
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.test_widget);
views.setTextViewText(R.id.appwidget_text, newText);
AppWidgetManager.getInstance(context).updateAppWidget(appWidgetId, views);
}
static void updateAppWidget(Context context, final AppWidgetManager appWidgetManager,
final int appWidgetId) {
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.test_widget);
Intent intent = new Intent(context, TestWidget.class);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
appWidgetId,
intent,
PendingIntent.FLAG_UPDATE_CURRENT);
views.setOnClickPendingIntent(R.id.appwidget_button, pendingIntent);
appWidgetManager.updateAppWidget(appWidgetId, views);
}
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
for (int appWidgetId : appWidgetIds) {
updateAppWidget(context, appWidgetManager, appWidgetId);
}
}
}
我只需要1和4,因为他们有单句。我已经在下面做了:
if (env.IsDevelopment()){
// app.UseWebpackDevMiddleware(new WebpackDevMiddlewareOptions
// {
// HotModuleReplacement = true
// });
}
将文档拆分为段落列表。但是我如何只提取一个句子。有什么想法吗?
答案 0 :(得分:0)
一种方法可能是在段落中搜索.
。 (请注意space
之后的.
)。这可能表明该段落有多个句子。
所以你可以这样说:
if '. ' in paragraph:
# don't print
else:
# print
只是一个想法...
答案 1 :(得分:0)
{ "_id" : ObjectId("5a9797b480591678e0771190"), "totalClockIns" : [ { "_id" : ObjectId("5a9797b480591678e077118f"), "count" : 12 } ] }
生成器之前应用您需要的任何条件。文件yield
的内容为:
My_text_file.txt
我的代码如下:
paragraph1:
sentence
paragraph2:
sentence. sentence. sentence.
paragraph3:
sentence. sentence.
paragraph4:
sentence
输出为:
import re def gen_read_lines(file_name): with open(file_name) as fp: for line in fp: yield line.strip() def is_para_starting(line): return True if re.match("^paragraph\d+:$", line) else False def gen_read_para(file_name): gen = gen_read_lines(file_name) para = [] for line in gen: if is_para_starting(line): if para: #returning the previous para yield para para = [] if ". " in line: #resetting para. para = [] continue para.append(line) #returning last para. yield para if __name__ == "__main__": para_gen = gen_read_para("My_text_file.txt") for para in para_gen: print "\n".join(para) print "~"*10
编辑条件已添加到支票"。 "在段落中