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.

335 lines
9.8 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. if (state.current.project_type === 'DocumentClassification') {
  182. json.examples = [
  183. '{"id": 1, "text": "Terrible customer service.", "annotations": [{"id": 1, "label": 1, "user": 1}]}\n',
  184. '{"id": 2, "text": "Really great transaction.", "annotations": [{"id": 2, "label": 2, "user": 1}]}\n',
  185. '{"id": 3, "text": "Great price.", "annotations": [{"id": 3, "label": 2, "user": 1}]}'
  186. ]
  187. csv.examples = [
  188. 'id,text,label,user\n',
  189. '1,"Terrible customer service.",1,1\n',
  190. '2,"Really great transaction.",2,1\n',
  191. '3,"Great price.",2,1'
  192. ]
  193. return [
  194. csv,
  195. json
  196. ]
  197. } else if (state.current.project_type === 'SequenceLabeling') {
  198. json.examples = [
  199. '{"id": 1, "text": "EU rejects ...", "annotations": [{"id": 1, "label": 2, "start_offset": 0, "end_offset": 2, "user": 1}]}\n',
  200. '{"id": 2, "text": "Peter Blackburn", "annotations": [{"id": 2, "label": 1, "start_offset": 0, "end_offset": 15, "user": 1}]}\n',
  201. '{"id": 3, "text": "President Obama", "annotations": [{"id": 3, "label": 1, "start_offset": 10, "end_offset": 15, "user": 1}]}'
  202. ]
  203. jsonl.examples = [
  204. '{"id": 1, "text": "EU rejects ...", "labels": [[0,2,"ORG"], [11,17, "MISC"], [34,41,"ORG"]]}\n',
  205. '{"id": 2, "text": "Peter Blackburn", "labels": [[0, 15, "PERSON"]]}\n',
  206. '{"id": 3, "text": "President Obama", "labels": [[10, 15, "PERSON"]]}\n'
  207. ]
  208. return [
  209. json,
  210. jsonl
  211. ]
  212. } else if (state.current.project_type === 'Seq2seq') {
  213. json.examples = [
  214. '{"id": 1, "text": "Hello!", "annotations": [{"id": 1, "label": "こんにちは!", "user": 1}]}\n',
  215. '{"id": 2, "text": "Good morning.", "annotations": [{"id": 2, "label": "おはようございます。", "user": 1}]}\n',
  216. '{"id": 3, "text": "See you.", "annotations": [{"id": 3, "label": "さようなら。", "user": 1}]}'
  217. ]
  218. csv.examples = [
  219. 'id,text,label,user\n',
  220. '1,"Hello!","こんにちは!",1\n',
  221. '2,"Good morning.","おはようございます。",1\n',
  222. '3,"See you.","さようなら。",1'
  223. ]
  224. return [
  225. csv,
  226. json
  227. ]
  228. } else {
  229. return []
  230. }
  231. },
  232. loadSearchOptions(state) {
  233. const checkpoint = JSON.parse(localStorage.getItem('checkpoint')) || {}
  234. return checkpoint[state.current.id] ? checkpoint[state.current.id] : { page: 1 }
  235. }
  236. }
  237. export const mutations = {
  238. setProjectList(state, payload) {
  239. state.projects = payload
  240. },
  241. createProject(state, project) {
  242. state.projects.unshift(project)
  243. },
  244. updateProject(state, project) {
  245. const item = state.projects.find(item => item.id === project.id)
  246. Object.assign(item, project)
  247. },
  248. deleteProject(state, projectId) {
  249. state.projects = state.projects.filter(item => item.id !== projectId)
  250. },
  251. updateSelected(state, selected) {
  252. state.selected = selected
  253. },
  254. resetSelected(state) {
  255. state.selected = []
  256. },
  257. setLoading(state, payload) {
  258. state.loading = payload
  259. },
  260. setCurrent(state, payload) {
  261. state.current = payload
  262. },
  263. saveSearchOptions(state, options) {
  264. const checkpoint = JSON.parse(localStorage.getItem('checkpoint')) || {}
  265. checkpoint[state.current.id] = options
  266. localStorage.setItem('checkpoint', JSON.stringify(checkpoint))
  267. }
  268. }
  269. export const actions = {
  270. getProjectList({ commit }, config) {
  271. commit('setLoading', true)
  272. ProjectService.getProjectList()
  273. .then((response) => {
  274. commit('setProjectList', response.data)
  275. })
  276. .catch((error) => {
  277. alert(error)
  278. })
  279. .finally(() => {
  280. commit('setLoading', false)
  281. })
  282. },
  283. createProject({ commit }, project) {
  284. ProjectService.createProject(project)
  285. .then((response) => {
  286. commit('createProject', response.data)
  287. })
  288. .catch((error) => {
  289. alert(error)
  290. })
  291. },
  292. updateProject({ commit }, data) {
  293. ProjectService.updateProject(data.projectId, data)
  294. .then((response) => {
  295. commit('updateProject', response.data)
  296. })
  297. .catch((error) => {
  298. alert(error)
  299. })
  300. },
  301. deleteProject({ commit, state }, config) {
  302. for (const project of state.selected) {
  303. ProjectService.deleteProject(project.id)
  304. .then((response) => {
  305. commit('deleteProject', project.id)
  306. })
  307. .catch((error) => {
  308. alert(error)
  309. })
  310. }
  311. commit('resetSelected')
  312. },
  313. setCurrentProject({ commit }, projectId) {
  314. return ProjectService.fetchProjectById(projectId)
  315. .then((response) => {
  316. commit('setCurrent', response.data)
  317. })
  318. .catch((error) => {
  319. alert(error)
  320. })
  321. },
  322. updateCurrentProject({ commit }, data) {
  323. ProjectService.updateProject(data.projectId, data)
  324. .then((response) => {
  325. commit('setCurrent', response.data)
  326. })
  327. .catch((error) => {
  328. alert(error)
  329. })
  330. }
  331. }