输入数据:
我一直在使用Android SDK,关于保持一个应用的状态我有一点不明白。所以给出了重新修改的“hello,android”示例:
package com.android.hello;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class HelloAndroid extends Activity {
/** 当activity第一次创建的时候进行调用 */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mTextView = new TextView(this);
if (savedInstanceState == null) {
mTextView.setText("Welcome to HelloAndroid!");
} else {
mTextView.setText("Welcome back.");
}
setContentView(mTextView);
}
private TextView mTextView = null;
}
我认为对于一个小的项目来说这是一个小需求,但是它总是给我第一条信息,无论我多么想要离开这个应用。我确定它就像重写onPause或者是类似的事情一样简单,但是我已经在这个文档中看了三十来分钟,却并没有发现什么明显的问题,所以希望寻求一些帮助。
输出数据:
package com.android.hello;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class HelloAndroid extends Activity {
/** 当activity第一次创建的时候进行调用 */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mTextView = new TextView(this);
if (savedInstanceState == null) {
mTextView.setText("Welcome to HelloAndroid!");
} else {
mTextView.setText("Welcome back.");
}
setContentView(mTextView);
}
private TextView mTextView = null;
}
需求场景
针对ask.csdn.net网站上的用户提问,抽取出问题中的代码块
开发过程
分析问题
可以看出,如果是代码块,除了部分注释是中文外,其他的基本上都是英文字符,而其他地方是对问题的中文描述,因此,我们可以将数据按行切分开,计算每行的英文字符数和中文字符数,比较两者之间的大小,若该行英文字符数大于中文字符数,我们则认为该行为代码块,并保留下来,反之,则认为该行都是中文问题描述,需要丢弃,最终将每行的结果拼接起来,得到输出。
待解决的问题
很明显,该方法只适合中文问题描述,若是英文的问题描述,该方法便失效,因此,从英文中抽取代码块还有待解决。
Code
def getnum_of_cn(inputdata):
chi=re.findall(r'[\u4E00-\u9FFF]',inputdata)#汉字的Unicode编码范围
return len(chi)
def getnum_of_en(inputdata):
char = re.findall(r'[a-zA-Z]',inputdata)#以列表类型返回全部能匹配的子串
blank = re.findall(r' ',inputdata)
#other = len(inputdata) - len(char) - len(blank)
return len(char) + len(blank)
def extract_code(datapath):
with open(datapath, 'r') as load_f:
load_dict = json.load(load_f)
codedata = load_dict['question_content']
codedata_split = codedata.split('\n')
re_line = []
for line in codedata_split:
cn_num = getnum_of_cn(line)
en_num = getnum_of_en(line)
if cn_num > en_num:
if line.startswith('<p>') and line.endswith('</p>'):
re_line.append(line + '\n')
else:
re_line.append('')
else:
if cn_num > 20:
if line.startswith('<p>') and line.endswith('</p>'):
re_line.append(line + '\n')
else:
re_line.append('')
else:
if '图片说明' in line:
re_line.append('')
elif 'error' in line.lower():
re_line.append('')
elif line.endswith('png)') and line.startswith('(https'):
re_line.append('')
else:
re_line.append(line + '\n')
# print('*'*10)
# print(len(re_line))
# if len(re_line) <= 2:
# re_line = ' '
return ''.join(re_line)
# %%
import re
import json
#数据路径,我这里是使用json格式的数据,question_content字段就是输入数据,可以去ask.csdn.net感受一下
datapath = 'xxx.json'
with open(datapath, 'r') as load_f:
orig_code = json.load(load_f)
orig_code = orig_code['question_content']
print(orig_code)
print('\n')
print('#'*20 + '\n')
result = extract_code(datapath)
print(result)
总结
question_content字段全是英文的情况还有待解决,如果大家有更好的解决方式,欢迎随时私信我,感谢