You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

345 lines
10 KiB

  1. import ProjectService from '@/services/project.service'
  2. export const state = () => ({
  3. projects: [],
  4. selected: [],
  5. current: {},
  6. loading: false
  7. })
  8. export const getters = {
  9. isDeletable(state) {
  10. const isProjectAdministrator = project => project.current_users_role.is_project_admin
  11. return state.selected.length > 0 && state.selected.every(isProjectAdministrator)
  12. },
  13. isEmpty(state) {
  14. return Object.keys(state.current).length === 0 && state.current.constructor === Object
  15. },
  16. currentProject(state) {
  17. return state.current
  18. },
  19. getCurrentUserRole(state) {
  20. return state.current.current_users_role || {}
  21. },
  22. canViewApproveButton(state) {
  23. const role = state.current.current_users_role
  24. return role && !role.is_annotator
  25. },
  26. getFilterOption(state) {
  27. if (state.current.project_type === 'DocumentClassification') {
  28. return 'doc_annotations__isnull'
  29. } else if (state.current.project_type === 'SequenceLabeling') {
  30. return 'seq_annotations__isnull'
  31. } else if (state.current.project_type === 'Seq2seq') {
  32. return 'seq2seq_annotations__isnull'
  33. } else {
  34. return ''
  35. }
  36. },
  37. getLink(state) {
  38. if (state.current.project_type === 'DocumentClassification') {
  39. return 'text-classification'
  40. } else if (state.current.project_type === 'SequenceLabeling') {
  41. return 'sequence-labeling'
  42. } else if (state.current.project_type === 'Seq2seq') {
  43. return 'sequence-to-sequence'
  44. } else {
  45. return ''
  46. }
  47. },
  48. getImportFormat(state) {
  49. const plain = {
  50. type: 'plain',
  51. text: 'Plain text',
  52. accept: '.txt',
  53. examples: [
  54. 'EU rejects German call to boycott British lamb.\n',
  55. 'Peter Blackburn\n',
  56. 'President Obama'
  57. ]
  58. }
  59. const csv = {
  60. type: 'csv',
  61. text: 'CSV',
  62. accept: '.csv'
  63. }
  64. const json = {
  65. type: 'json',
  66. text: 'JSONL',
  67. accept: '.json,.jsonl'
  68. }
  69. const conll = {
  70. type: 'conll',
  71. text: 'CoNLL',
  72. accept: '.conll'
  73. }
  74. const excel = {
  75. type: 'excel',
  76. text: 'Excel',
  77. accept: '.xlsx'
  78. }
  79. const fastText = {
  80. type: 'fastText',
  81. text: 'FastText',
  82. accept: '.txt'
  83. }
  84. if (state.current.project_type === 'DocumentClassification') {
  85. json.examples = [
  86. '{"text": "Terrible customer service.", "labels": ["negative"]}\n',
  87. '{"text": "Really great transaction.", "labels": ["positive"]}\n',
  88. '{"text": "Great price.", "labels": ["positive"]}'
  89. ]
  90. csv.examples = [
  91. 'text,label\n',
  92. '"Terrible customer service.","negative"\n',
  93. '"Really great transaction.","positive"\n',
  94. '"Great price.","positive"'
  95. ]
  96. excel.examples = [
  97. 'text,label\n',
  98. '"Terrible customer service.","negative"\n',
  99. '"Really great transaction.","positive"\n',
  100. '"Great price.","positive"'
  101. ]
  102. fastText.examples = [
  103. '__label__[label name] text \n',
  104. '__label_president Obama Trump'
  105. ]
  106. return [
  107. plain,
  108. csv,
  109. json,
  110. excel,
  111. fastText
  112. ]
  113. } else if (state.current.project_type === 'SequenceLabeling') {
  114. json.examples = [
  115. '{"text": "EU rejects German call to boycott British lamb.", "labels": [ [0, 2, "ORG"], [11, 17, "MISC"], ... ]}\n',
  116. '{"text": "Peter Blackburn", "labels": [ [0, 15, "PERSON"] ]}\n',
  117. '{"text": "President Obama", "labels": [ [10, 15, "PERSON"] ]}'
  118. ]
  119. conll.examples = [
  120. 'EU\tB-ORG\n',
  121. 'rejects\tO\n',
  122. 'German\tB-MISC\n',
  123. 'call\tO\n',
  124. 'to\tO\n',
  125. 'boycott\tO\n',
  126. 'British\tB-MISC\n',
  127. 'lamb\tO\n',
  128. '.\tO\n\n',
  129. 'Peter\tB-PER\n',
  130. 'Blackburn\tI-PER'
  131. ]
  132. return [
  133. plain,
  134. json,
  135. conll
  136. ]
  137. } else if (state.current.project_type === 'Seq2seq') {
  138. json.examples = [
  139. '{"text": "Hello!", "labels": ["こんにちは!"]}\n',
  140. '{"text": "Good morning.", "labels": ["おはようございます。"]}\n',
  141. '{"text": "See you.", "labels": ["さようなら。"]}'
  142. ]
  143. csv.examples = [
  144. 'text,label\n',
  145. '"Hello!","こんにちは!"\n',
  146. '"Good morning.","おはようございます。"\n',
  147. '"See you.","さようなら。"'
  148. ]
  149. excel.examples = [
  150. 'text,label\n',
  151. '"Hello!","こんにちは!"\n',
  152. '"Good morning.","おはようございます。"\n',
  153. '"See you.","さようなら。"'
  154. ]
  155. return [
  156. plain,
  157. csv,
  158. json,
  159. excel
  160. ]
  161. } else {
  162. return []
  163. }
  164. },
  165. getExportFormat(state) {
  166. const csv = {
  167. type: 'csv',
  168. text: 'CSV',
  169. suffix: 'csv'
  170. }
  171. const json = {
  172. type: 'json',
  173. text: 'JSONL',
  174. suffix: 'jsonl'
  175. }
  176. const jsonl = {
  177. type: 'jsonl',
  178. text: 'JSONL(Text label)',
  179. suffix: 'jsonl'
  180. }
  181. const fastText = {
  182. type: 'txt',
  183. text: 'FastText',
  184. suffix: 'txt'
  185. }
  186. if (state.current.project_type === 'DocumentClassification') {
  187. json.examples = [
  188. '{"id": 1, "text": "Terrible customer service.", "annotations": [{"id": 1, "label": 1, "user": 1}]}\n',
  189. '{"id": 2, "text": "Really great transaction.", "annotations": [{"id": 2, "label": 2, "user": 1}]}\n',
  190. '{"id": 3, "text": "Great price.", "annotations": [{"id": 3, "label": 2, "user": 1}]}'
  191. ]
  192. csv.examples = [
  193. 'id,text,label,user\n',
  194. '1,"Terrible customer service.",1,1\n',
  195. '2,"Really great transaction.",2,1\n',
  196. '3,"Great price.",2,1'
  197. ]
  198. fastText.examples = [
  199. '__label__pet dog cat \n',
  200. '__label__car VW BMW'
  201. ]
  202. return [
  203. csv,
  204. json,
  205. fastText
  206. ]
  207. } else if (state.current.project_type === 'SequenceLabeling') {
  208. json.examples = [
  209. '{"id": 1, "text": "EU rejects ...", "annotations": [{"id": 1, "label": 2, "start_offset": 0, "end_offset": 2, "user": 1}]}\n',
  210. '{"id": 2, "text": "Peter Blackburn", "annotations": [{"id": 2, "label": 1, "start_offset": 0, "end_offset": 15, "user": 1}]}\n',
  211. '{"id": 3, "text": "President Obama", "annotations": [{"id": 3, "label": 1, "start_offset": 10, "end_offset": 15, "user": 1}]}'
  212. ]
  213. jsonl.examples = [
  214. '{"id": 1, "text": "EU rejects ...", "labels": [[0,2,"ORG"], [11,17, "MISC"], [34,41,"ORG"]]}\n',
  215. '{"id": 2, "text": "Peter Blackburn", "labels": [[0, 15, "PERSON"]]}\n',
  216. '{"id": 3, "text": "President Obama", "labels": [[10, 15, "PERSON"]]}\n'
  217. ]
  218. return [
  219. json,
  220. jsonl
  221. ]
  222. } else if (state.current.project_type === 'Seq2seq') {
  223. json.examples = [
  224. '{"id": 1, "text": "Hello!", "annotations": [{"id": 1, "label": "こんにちは!", "user": 1}]}\n',
  225. '{"id": 2, "text": "Good morning.", "annotations": [{"id": 2, "label": "おはようございます。", "user": 1}]}\n',
  226. '{"id": 3, "text": "See you.", "annotations": [{"id": 3, "label": "さようなら。", "user": 1}]}'
  227. ]
  228. csv.examples = [
  229. 'id,text,label,user\n',
  230. '1,"Hello!","こんにちは!",1\n',
  231. '2,"Good morning.","おはようございます。",1\n',
  232. '3,"See you.","さようなら。",1'
  233. ]
  234. return [
  235. csv,
  236. json
  237. ]
  238. } else {
  239. return []
  240. }
  241. },
  242. loadSearchOptions(state) {
  243. const checkpoint = JSON.parse(localStorage.getItem('checkpoint')) || {}
  244. return checkpoint[state.current.id] ? checkpoint[state.current.id] : { page: 1 }
  245. }
  246. }
  247. export const mutations = {
  248. setProjectList(state, payload) {
  249. state.projects = payload
  250. },
  251. createProject(state, project) {
  252. state.projects.unshift(project)
  253. },
  254. updateProject(state, project) {
  255. const item = state.projects.find(item => item.id === project.id)
  256. Object.assign(item, project)
  257. },
  258. deleteProject(state, projectId) {
  259. state.projects = state.projects.filter(item => item.id !== projectId)
  260. },
  261. updateSelected(state, selected) {
  262. state.selected = selected
  263. },
  264. resetSelected(state) {
  265. state.selected = []
  266. },
  267. setLoading(state, payload) {
  268. state.loading = payload
  269. },
  270. setCurrent(state, payload) {
  271. state.current = payload
  272. },
  273. saveSearchOptions(state, options) {
  274. const checkpoint = JSON.parse(localStorage.getItem('checkpoint')) || {}
  275. checkpoint[state.current.id] = options
  276. localStorage.setItem('checkpoint', JSON.stringify(checkpoint))
  277. }
  278. }
  279. export const actions = {
  280. getProjectList({ commit }, config) {
  281. commit('setLoading', true)
  282. ProjectService.getProjectList()
  283. .then((response) => {
  284. commit('setProjectList', response.data)
  285. })
  286. .catch((error) => {
  287. alert(error)
  288. })
  289. .finally(() => {
  290. commit('setLoading', false)
  291. })
  292. },
  293. createProject({ commit }, project) {
  294. ProjectService.createProject(project)
  295. .then((response) => {
  296. commit('createProject', response.data)
  297. })
  298. .catch((error) => {
  299. alert(error)
  300. })
  301. },
  302. updateProject({ commit }, data) {
  303. ProjectService.updateProject(data.projectId, data)
  304. .then((response) => {
  305. commit('updateProject', response.data)
  306. })
  307. .catch((error) => {
  308. alert(error)
  309. })
  310. },
  311. deleteProject({ commit, state }, config) {
  312. for (const project of state.selected) {
  313. ProjectService.deleteProject(project.id)
  314. .then((response) => {
  315. commit('deleteProject', project.id)
  316. })
  317. .catch((error) => {
  318. alert(error)
  319. })
  320. }
  321. commit('resetSelected')
  322. },
  323. setCurrentProject({ commit }, projectId) {
  324. return ProjectService.fetchProjectById(projectId)
  325. .then((response) => {
  326. commit('setCurrent', response.data)
  327. })
  328. .catch((error) => {
  329. alert(error)
  330. })
  331. },
  332. updateCurrentProject({ commit }, data) {
  333. ProjectService.updateProject(data.projectId, data)
  334. .then((response) => {
  335. commit('setCurrent', response.data)
  336. })
  337. .catch((error) => {
  338. alert(error)
  339. })
  340. }
  341. }